-skeletons for transport-ng
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration_stop.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file namestore/test_namestore_api_zone_iteration.c
22  * @brief testcase for zone iteration functionality: stop iterating of zones
23  */
24 #include "platform.h"
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_testing_lib.h"
27 #include "namestore.h"
28
29
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
31 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
32
33 static struct GNUNET_NAMESTORE_Handle * nsh;
34
35 static struct GNUNET_SCHEDULER_Task * endbadly_task;
36
37 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
38
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
40
41 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
42
43 static int res;
44
45 static int returned_records;
46
47 static char * s_name_1;
48
49 static struct GNUNET_GNSRECORD_Data *s_rd_1;
50
51 static char * s_name_2;
52
53 static struct GNUNET_GNSRECORD_Data *s_rd_2;
54
55 static char * s_name_3;
56
57 static struct GNUNET_GNSRECORD_Data *s_rd_3;
58
59 static char *directory;
60
61
62 /**
63  * Re-establish the connection to the service.
64  *
65  * @param cls handle to use to re-connect.
66  */
67 static void
68 endbadly (void *cls)
69 {
70   if (NULL != zi)
71   {
72     GNUNET_NAMESTORE_zone_iteration_stop (zi);
73     zi = NULL;
74   }
75
76   if (nsh != NULL)
77     GNUNET_NAMESTORE_disconnect (nsh);
78   nsh = NULL;
79
80   GNUNET_free_non_null(s_name_1);
81   GNUNET_free_non_null(s_name_2);
82   GNUNET_free_non_null(s_name_3);
83
84   if (s_rd_1 != NULL)
85   {
86     GNUNET_free ((void *)s_rd_1->data);
87     GNUNET_free (s_rd_1);
88   }
89   if (s_rd_2 != NULL)
90   {
91     GNUNET_free ((void *)s_rd_2->data);
92     GNUNET_free (s_rd_2);
93   }
94   if (s_rd_3 != NULL)
95   {
96     GNUNET_free ((void *)s_rd_3->data);
97     GNUNET_free (s_rd_3);
98   }
99
100   if (privkey != NULL)
101     GNUNET_free (privkey);
102   privkey = NULL;
103
104   if (privkey2 != NULL)
105     GNUNET_free (privkey2);
106   privkey2 = NULL;
107   res = 1;
108 }
109
110
111 static void
112 end (void *cls)
113 {
114   if (NULL != zi)
115   {
116     GNUNET_NAMESTORE_zone_iteration_stop (zi);
117     zi = NULL;
118   }
119
120   if (endbadly_task != NULL)
121   {
122     GNUNET_SCHEDULER_cancel (endbadly_task);
123     endbadly_task = NULL;
124   }
125
126   if (privkey != NULL)
127     GNUNET_free (privkey);
128   privkey = NULL;
129
130   if (privkey2 != NULL)
131     GNUNET_free (privkey2);
132   privkey2 = NULL;
133
134   GNUNET_free (s_name_1);
135   GNUNET_free (s_name_2);
136   GNUNET_free (s_name_3);
137   if (s_rd_1 != NULL)
138   {
139     GNUNET_free ((void *)s_rd_1->data);
140     GNUNET_free (s_rd_1);
141   }
142   if (s_rd_2 != NULL)
143   {
144     GNUNET_free ((void *)s_rd_2->data);
145     GNUNET_free (s_rd_2);
146   }
147   if (s_rd_3 != NULL)
148   {
149     GNUNET_free ((void *)s_rd_3->data);
150     GNUNET_free (s_rd_3);
151   }
152   if (nsh != NULL)
153     GNUNET_NAMESTORE_disconnect (nsh);
154   nsh = NULL;
155 }
156
157
158 static void
159 zone_proc (void *cls,
160            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
161            const char *label,
162            unsigned int rd_count,
163            const struct GNUNET_GNSRECORD_Data *rd)
164 {
165   int failed = GNUNET_NO;
166   if ((zone == NULL) && (label == NULL))
167   {
168     GNUNET_break (1 <= returned_records);
169     if (1 >= returned_records)
170         res = 1; /* Last iteraterator callback, we are done */
171     else
172       res = 0;
173     zi = NULL;
174     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175                 "Received last result, iteration done after receing %u results\n",
176                 returned_records );
177     GNUNET_SCHEDULER_add_now (&end, NULL);
178     return;
179   }
180   GNUNET_assert (NULL != zone);
181   if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
182   {
183     if (0 == strcmp (label, s_name_1))
184     {
185       if (rd_count == 1)
186       {
187         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
188         {
189           failed = GNUNET_YES;
190           GNUNET_break (0);
191         }
192       }
193       else
194       {
195         failed = GNUNET_YES;
196         GNUNET_break (0);
197       }
198     }
199     else if (0 == strcmp (label, s_name_2))
200     {
201       if (rd_count == 1)
202       {
203         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
204         {
205           failed = GNUNET_YES;
206           GNUNET_break (0);
207         }
208       }
209       else
210       {
211         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212                     "Received invalid record count\n");
213         failed = GNUNET_YES;
214         GNUNET_break (0);
215       }
216     }
217     else
218     {
219       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220                   "Comparing result failed: got name `%s' for first zone\n", label);
221       failed = GNUNET_YES;
222       GNUNET_break (0);
223     }
224   }
225   else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
226   {
227     if (0 == strcmp (label, s_name_3))
228     {
229       if (rd_count == 1)
230       {
231         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
232         {
233           failed = GNUNET_YES;
234           GNUNET_break (0);
235         }
236       }
237       else
238       {
239         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240                     "Received invalid record count\n");
241         failed = GNUNET_YES;
242         GNUNET_break (0);
243       }
244     }
245     else
246     {
247       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248                   "Comparing result failed: got name `%s' for first zone\n", label);
249       failed = GNUNET_YES;
250       GNUNET_break (0);
251     }
252   }
253   else
254   {
255     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256                 "Received invalid zone\n");
257     failed = GNUNET_YES;
258     GNUNET_break (0);
259   }
260   if (failed == GNUNET_NO)
261   {
262     if (1 == returned_records)
263     {
264       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265                 "Telling namestore to stop zone iteration\n");
266                 GNUNET_NAMESTORE_zone_iteration_stop (zi);
267                 zi = NULL;
268       if (NULL != endbadly_task)
269       {
270         GNUNET_SCHEDULER_cancel (endbadly_task);
271         endbadly_task = NULL;
272       }
273       res = 0;
274       GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
275       return;
276     }
277     returned_records ++;
278     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
279                 "Telling namestore to send the next result\n");
280     GNUNET_NAMESTORE_zone_iterator_next (zi);
281   }
282   else
283   {
284     GNUNET_break (0);
285     GNUNET_SCHEDULER_add_now (&end, NULL);
286   }
287 }
288
289
290 static void
291 put_cont (void *cls, int32_t success, const char *emsg)
292 {
293   static int c = 0;
294
295   if (success == GNUNET_OK)
296   {
297     c++;
298     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
299   }
300   else
301   {
302     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
303                 emsg);
304     GNUNET_break (0);
305     if (NULL != endbadly_task)
306         GNUNET_SCHEDULER_cancel (endbadly_task);
307     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
308     return;
309   }
310
311   if (c == 3)
312   {
313     res = 1;
314     returned_records = 0;
315     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
316     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
317                                                NULL,
318                                                &zone_proc,
319                                                NULL);
320     if (zi == NULL)
321     {
322       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
323       GNUNET_break (0);
324       if (NULL != endbadly_task)
325         GNUNET_SCHEDULER_cancel (endbadly_task);
326       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
327       return;
328     }
329   }
330 }
331
332
333 static struct GNUNET_GNSRECORD_Data *
334 create_record (unsigned int count)
335 {
336   unsigned int c;
337   struct GNUNET_GNSRECORD_Data * rd;
338
339   rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
340   for (c = 0; c < count; c++)
341   {
342     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
343     rd[c].record_type = 1111;
344     rd[c].data_size = 50;
345     rd[c].data = GNUNET_malloc(50);
346     rd[c].flags = 0;
347     memset ((char *) rd[c].data, 'a', 50);
348   }
349   return rd;
350 }
351
352
353 /**
354  * Callback called from the zone iterator when we iterate over
355  * the empty zone.  Check that we got no records and then
356  * start the actual tests by filling the zone.
357  */
358 static void
359 empty_zone_proc (void *cls,
360                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
361                  const char *label,
362                  unsigned int rd_count,
363                  const struct GNUNET_GNSRECORD_Data *rd)
364 {
365   char *hostkey_file;
366
367   GNUNET_assert (nsh == cls);
368   if (NULL != zone)
369   {
370     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371                 _("Expected empty zone but received zone private key\n"));
372     GNUNET_break (0);
373     if (endbadly_task != NULL)
374       GNUNET_SCHEDULER_cancel (endbadly_task);
375     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
376     return;
377   }
378   if ((NULL != label) || (NULL != rd) || (0 != rd_count))
379   {
380     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381                 _("Expected no zone content but received data\n"));
382     GNUNET_break (0);
383     if (endbadly_task != NULL)
384       GNUNET_SCHEDULER_cancel (endbadly_task);
385     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
386     return;
387   }
388
389   zi = NULL;
390   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
391       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
392   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
393   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
394   GNUNET_free (hostkey_file);
395   GNUNET_assert (privkey != NULL);
396
397   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
398       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
399   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
400   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
401   GNUNET_free (hostkey_file);
402   GNUNET_assert (privkey2 != NULL);
403
404   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
405
406   GNUNET_asprintf(&s_name_1, "dummy1");
407   s_rd_1 = create_record(1);
408   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
409                 1, s_rd_1, &put_cont, NULL);
410
411   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
412   GNUNET_asprintf(&s_name_2, "dummy2");
413   s_rd_2 = create_record(1);
414   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
415                 1, s_rd_2, &put_cont, NULL);
416
417   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
418
419   /* name in different zone */
420   GNUNET_asprintf(&s_name_3, "dummy3");
421   s_rd_3 = create_record(1);
422   GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
423                 1, s_rd_3, &put_cont, NULL);
424 }
425
426
427 static void
428 run (void *cls,
429      const struct GNUNET_CONFIGURATION_Handle *cfg,
430      struct GNUNET_TESTING_Peer *peer)
431 {
432   GNUNET_assert (GNUNET_OK ==
433                  GNUNET_CONFIGURATION_get_value_string (cfg,
434                                                         "PATHS",
435                                                         "GNUNET_TEST_HOME",
436                                                         &directory));
437   GNUNET_DISK_directory_remove (directory);
438
439   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
440   nsh = GNUNET_NAMESTORE_connect (cfg);
441   GNUNET_break (NULL != nsh);
442   /* first, iterate over empty namestore */
443   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
444                                              NULL, &empty_zone_proc, nsh);
445   if (NULL == zi)
446   {
447     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
448                 "Failed to create zone iterator\n");
449     GNUNET_break (0);
450     GNUNET_SCHEDULER_cancel (endbadly_task);
451     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
452   }
453 }
454
455
456 int
457 main (int argc, char *argv[])
458 {
459   res = 1;
460   if (0 !=
461       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
462                                "test_namestore_api.conf",
463                                &run,
464                                NULL))
465   {
466     res = 1;
467   }
468   if (NULL != directory)
469   {
470       GNUNET_DISK_directory_remove (directory);
471       GNUNET_free (directory);
472   }
473   return res;
474 }
475
476 /* end of test_namestore_api_zone_iteration_stop.c */