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