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