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