clean up zone iteration logic
[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 fail_cb (void *cls)
160 {
161   GNUNET_assert (0);
162 }
163
164
165 static void
166 zone_proc (void *cls,
167            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
168            const char *label,
169            unsigned int rd_count,
170            const struct GNUNET_GNSRECORD_Data *rd)
171 {
172   int failed = GNUNET_NO;
173
174   GNUNET_assert (NULL != zone);
175   if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
176   {
177     if (0 == strcmp (label, s_name_1))
178     {
179       if (rd_count == 1)
180       {
181         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
182         {
183           failed = GNUNET_YES;
184           GNUNET_break (0);
185         }
186       }
187       else
188       {
189         failed = GNUNET_YES;
190         GNUNET_break (0);
191       }
192     }
193     else if (0 == strcmp (label, s_name_2))
194     {
195       if (rd_count == 1)
196       {
197         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
198         {
199           failed = GNUNET_YES;
200           GNUNET_break (0);
201         }
202       }
203       else
204       {
205         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206                     "Received invalid record count\n");
207         failed = GNUNET_YES;
208         GNUNET_break (0);
209       }
210     }
211     else
212     {
213       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214                   "Comparing result failed: got name `%s' for first zone\n", label);
215       failed = GNUNET_YES;
216       GNUNET_break (0);
217     }
218   }
219   else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
220   {
221     if (0 == strcmp (label, s_name_3))
222     {
223       if (rd_count == 1)
224       {
225         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
226         {
227           failed = GNUNET_YES;
228           GNUNET_break (0);
229         }
230       }
231       else
232       {
233         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234                     "Received invalid record count\n");
235         failed = GNUNET_YES;
236         GNUNET_break (0);
237       }
238     }
239     else
240     {
241       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242                   "Comparing result failed: got name `%s' for first zone\n", label);
243       failed = GNUNET_YES;
244       GNUNET_break (0);
245     }
246   }
247   else
248   {
249     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250                 "Received invalid zone\n");
251     failed = GNUNET_YES;
252     GNUNET_break (0);
253   }
254   if (failed == GNUNET_NO)
255   {
256     if (1 == returned_records)
257     {
258       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259                 "Telling namestore to stop zone iteration\n");
260                 GNUNET_NAMESTORE_zone_iteration_stop (zi);
261                 zi = NULL;
262       if (NULL != endbadly_task)
263       {
264         GNUNET_SCHEDULER_cancel (endbadly_task);
265         endbadly_task = NULL;
266       }
267       res = 0;
268       GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
269       return;
270     }
271     returned_records ++;
272     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273                 "Telling namestore to send the next result\n");
274     GNUNET_NAMESTORE_zone_iterator_next (zi,
275                                          1);
276   }
277   else
278   {
279     GNUNET_break (0);
280     GNUNET_SCHEDULER_add_now (&end, NULL);
281   }
282 }
283
284
285 static void
286 zone_proc_end (void *cls)
287 {
288   GNUNET_break (1 <= returned_records);
289   if (1 >= returned_records)
290     res = 1; /* Last iteraterator callback, we are done */
291   else
292     res = 0;
293   zi = NULL;
294   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295               "Received last result, iteration done after receing %u results\n",
296               returned_records);
297   GNUNET_SCHEDULER_add_now (&end, NULL);
298 }
299
300
301 static void
302 put_cont (void *cls, int32_t success, const char *emsg)
303 {
304   static int c = 0;
305
306   if (success == GNUNET_OK)
307   {
308     c++;
309     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
310   }
311   else
312   {
313     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
314                 emsg);
315     GNUNET_break (0);
316     if (NULL != endbadly_task)
317         GNUNET_SCHEDULER_cancel (endbadly_task);
318     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
319     return;
320   }
321
322   if (c == 3)
323   {
324     res = 1;
325     returned_records = 0;
326     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327                 "All records created, starting iteration over all zones \n");
328     zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
329                                                 NULL,
330                                                 &fail_cb,
331                                                 NULL,
332                                                 &zone_proc,
333                                                 NULL,
334                                                 &zone_proc_end,
335                                                 NULL);
336     if (zi == NULL)
337     {
338       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
339                   "Failed to create zone iterator\n");
340       GNUNET_break (0);
341       if (NULL != endbadly_task)
342         GNUNET_SCHEDULER_cancel (endbadly_task);
343       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
344       return;
345     }
346   }
347 }
348
349
350 static struct GNUNET_GNSRECORD_Data *
351 create_record (unsigned int count)
352 {
353   unsigned int c;
354   struct GNUNET_GNSRECORD_Data * rd;
355
356   rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
357   for (c = 0; c < count; c++)
358   {
359     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
360     rd[c].record_type = 1111;
361     rd[c].data_size = 50;
362     rd[c].data = GNUNET_malloc(50);
363     rd[c].flags = 0;
364     memset ((char *) rd[c].data, 'a', 50);
365   }
366   return rd;
367 }
368
369
370 /**
371  * Callback called from the zone iterator when we iterate over
372  * the empty zone.  Check that we got no records and then
373  * start the actual tests by filling the zone.
374  */
375 static void
376 empty_zone_proc (void *cls,
377                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
378                  const char *label,
379                  unsigned int rd_count,
380                  const struct GNUNET_GNSRECORD_Data *rd)
381 {
382
383   GNUNET_assert (nsh == cls);
384   if (NULL != zone)
385   {
386     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
387                 _("Expected empty zone but received zone private key\n"));
388     GNUNET_break (0);
389     if (endbadly_task != NULL)
390       GNUNET_SCHEDULER_cancel (endbadly_task);
391     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
392     return;
393   }
394   if ((NULL != label) || (NULL != rd) || (0 != rd_count))
395   {
396     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
397                 _("Expected no zone content but received data\n"));
398     GNUNET_break (0);
399     if (endbadly_task != NULL)
400       GNUNET_SCHEDULER_cancel (endbadly_task);
401     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
402     return;
403   }
404   GNUNET_assert (0);
405 }
406
407
408 static void
409 empty_zone_proc_end (void *cls)
410 {
411   char *hostkey_file;
412
413   GNUNET_assert (nsh == cls);
414   zi = NULL;
415   GNUNET_asprintf(&hostkey_file,
416                   "zonefiles%s%s",
417                   DIR_SEPARATOR_STR,
418                   "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
419   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420               "Using zonekey file `%s' \n",
421               hostkey_file);
422   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
423   GNUNET_free (hostkey_file);
424   GNUNET_assert (privkey != NULL);
425
426   GNUNET_asprintf (&hostkey_file,
427                    "zonefiles%s%s",
428                    DIR_SEPARATOR_STR,
429                    "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
430   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431               "Using zonekey file `%s'\n",
432               hostkey_file);
433   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
434   GNUNET_free (hostkey_file);
435   GNUNET_assert (privkey2 != NULL);
436
437   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
438               "Created record 1\n");
439
440   GNUNET_asprintf(&s_name_1,
441                   "dummy1");
442   s_rd_1 = create_record(1);
443   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
444                 1, s_rd_1, &put_cont, NULL);
445
446   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447               "Created record 2 \n");
448   GNUNET_asprintf(&s_name_2,
449                   "dummy2");
450   s_rd_2 = create_record(1);
451   GNUNET_NAMESTORE_records_store (nsh,
452                                   privkey,
453                                   s_name_2,
454                                   1,
455                                   s_rd_2,
456                                   &put_cont, NULL);
457
458   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
459               "Created record 3\n");
460
461   /* name in different zone */
462   GNUNET_asprintf(&s_name_3, "dummy3");
463   s_rd_3 = create_record(1);
464   GNUNET_NAMESTORE_records_store(nsh,
465                                  privkey2,
466                                  s_name_3,
467                                  1,
468                                  s_rd_3,
469                                  &put_cont, NULL);
470 }
471
472
473 static void
474 run (void *cls,
475      const struct GNUNET_CONFIGURATION_Handle *cfg,
476      struct GNUNET_TESTING_Peer *peer)
477 {
478   GNUNET_assert (GNUNET_OK ==
479                  GNUNET_CONFIGURATION_get_value_string (cfg,
480                                                         "PATHS",
481                                                         "GNUNET_TEST_HOME",
482                                                         &directory));
483   GNUNET_DISK_directory_remove (directory);
484
485   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
486   nsh = GNUNET_NAMESTORE_connect (cfg);
487   GNUNET_break (NULL != nsh);
488   /* first, iterate over empty namestore */
489   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
490                                              NULL,
491                                              &fail_cb,
492                                              NULL,
493                                              &empty_zone_proc,
494                                              nsh,
495                                              &empty_zone_proc_end,
496                                              nsh);
497   if (NULL == zi)
498   {
499     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
500                 "Failed to create zone iterator\n");
501     GNUNET_break (0);
502     GNUNET_SCHEDULER_cancel (endbadly_task);
503     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
504   }
505 }
506
507
508 int
509 main (int argc, char *argv[])
510 {
511   res = 1;
512   if (0 !=
513       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
514                                "test_namestore_api.conf",
515                                &run,
516                                NULL))
517   {
518     res = 1;
519   }
520   if (NULL != directory)
521   {
522       GNUNET_DISK_directory_remove (directory);
523       GNUNET_free (directory);
524   }
525   return res;
526 }
527
528 /* end of test_namestore_api_zone_iteration_stop.c */