error handling
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration_nick.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 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: 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 #include "gnunet_dnsparser_lib.h"
29
30 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
31
32 #define ZONE_NICK_1 "nick1"
33 #define ZONE_NICK_2 "nick2"
34
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
36
37
38 static struct GNUNET_NAMESTORE_Handle *nsh;
39
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
41
42 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey2;
43
44 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
45
46 static int res;
47
48 static int returned_records;
49
50 static char *s_name_1;
51
52 static struct GNUNET_GNSRECORD_Data *s_rd_1;
53
54 static char *s_name_2;
55
56 static struct GNUNET_GNSRECORD_Data *s_rd_2;
57
58 static char *s_name_3;
59
60 static struct GNUNET_GNSRECORD_Data *s_rd_3;
61
62 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
63
64
65 /**
66  * Re-establish the connection to the service.
67  *
68  * @param cls handle to use to re-connect.
69  * @param tc scheduler context
70  */
71 static void
72 end (void *cls)
73 {
74   if (NULL != zi)
75   {
76     GNUNET_NAMESTORE_zone_iteration_stop (zi);
77     zi = NULL;
78   }
79   if (nsh != NULL)
80   {
81     GNUNET_NAMESTORE_disconnect (nsh);
82     nsh = NULL;
83   }
84   GNUNET_free_non_null (s_name_1);
85   GNUNET_free_non_null (s_name_2);
86   GNUNET_free_non_null (s_name_3);
87
88   if (s_rd_1 != NULL)
89   {
90     GNUNET_free ((void *) s_rd_1->data);
91     GNUNET_free (s_rd_1);
92   }
93   if (s_rd_2 != NULL)
94   {
95     GNUNET_free ((void *) s_rd_2->data);
96     GNUNET_free (s_rd_2);
97   }
98   if (s_rd_3 != NULL)
99   {
100     GNUNET_free ((void *) s_rd_3->data);
101     GNUNET_free (s_rd_3);
102   }
103
104   if (privkey != NULL)
105   {
106     GNUNET_free (privkey);
107     privkey = NULL;
108   }
109   if (privkey2 != NULL)
110   {
111     GNUNET_free (privkey2);
112     privkey2 = NULL;
113   }
114 }
115
116
117 static int
118 check_zone_1 (const char *label, unsigned int rd_count,
119               const struct GNUNET_GNSRECORD_Data *rd)
120 {
121   for (unsigned int c = 0; c < rd_count; c++)
122   {
123     if ((rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK) &&
124         (0 != strcmp (rd[c].data, ZONE_NICK_1)))
125     {
126       GNUNET_break (0);
127       return GNUNET_YES;
128     }
129   }
130   return GNUNET_NO;
131 }
132
133
134 static int
135 check_zone_2 (const char *label,
136               unsigned int rd_count,
137               const struct GNUNET_GNSRECORD_Data *rd)
138 {
139   for (unsigned int c = 0; c < rd_count; c++)
140   {
141     if ((rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK) &&
142         (0 != strcmp (rd[c].data, ZONE_NICK_2)))
143     {
144       GNUNET_break (0);
145       return GNUNET_YES;
146     }
147   }
148   return GNUNET_NO;
149 }
150
151
152 static void
153 zone_proc_end (void *cls)
154 {
155   zi = NULL;
156   res = 0;
157   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158               "Received last result, iteration done after receing %u results\n",
159               returned_records);
160   GNUNET_SCHEDULER_shutdown ();
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 == GNUNET_memcmp (zone, privkey))
175   {
176     failed = check_zone_1 (label, rd_count, rd);
177     if (GNUNET_YES == failed)
178       GNUNET_break (0);
179   }
180   else if (0 == GNUNET_memcmp (zone, privkey2))
181   {
182     failed = check_zone_2 (label, rd_count, rd);
183     if (GNUNET_YES == failed)
184       GNUNET_break (0);
185   }
186   else
187   {
188     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189                 "Received invalid zone\n");
190     failed = GNUNET_YES;
191     GNUNET_break (0);
192   }
193
194   if (failed == GNUNET_NO)
195   {
196     returned_records++;
197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198                 "Telling namestore to send the next result\n");
199     GNUNET_NAMESTORE_zone_iterator_next (zi,
200                                          1);
201   }
202   else
203   {
204     GNUNET_break (0);
205     res = 1;
206     GNUNET_SCHEDULER_shutdown ();
207   }
208 }
209
210
211 static void
212 fail_cb (void *cls)
213 {
214   GNUNET_assert (0);
215 }
216
217
218 static void
219 put_cont (void *cls,
220           int32_t success,
221           const char *emsg)
222 {
223   static int c = 0;
224
225   if (success == GNUNET_OK)
226   {
227     c++;
228     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
229   }
230   else
231   {
232     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
233                 emsg);
234     GNUNET_break (0);
235     GNUNET_SCHEDULER_shutdown ();
236     return;
237   }
238
239   if (c == 3)
240   {
241     res = 1;
242     returned_records = 0;
243     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244                 "All records created, starting iteration over all zones \n");
245     zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
246                                                 NULL,
247                                                 &fail_cb,
248                                                 NULL,
249                                                 &zone_proc,
250                                                 NULL,
251                                                 &zone_proc_end,
252                                                 NULL);
253     if (zi == NULL)
254     {
255       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
256       GNUNET_break (0);
257       GNUNET_SCHEDULER_shutdown ();
258       return;
259     }
260   }
261 }
262
263
264 static struct GNUNET_GNSRECORD_Data *
265 create_record (unsigned int count)
266 {
267   struct GNUNET_GNSRECORD_Data *rd;
268
269   rd = GNUNET_new_array (count,
270                          struct GNUNET_GNSRECORD_Data);
271   for (unsigned int c = 0; c < count; c++)
272   {
273     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
274       GNUNET_TIME_UNIT_HOURS).abs_value_us;
275     rd[c].record_type = TEST_RECORD_TYPE;
276     rd[c].data_size = 50;
277     rd[c].data = GNUNET_malloc (50);
278     rd[c].flags = 0;
279     memset ((char *) rd[c].data, 'a', 50);
280   }
281   return rd;
282 }
283
284
285 static void
286 nick_2_cont (void *cls,
287              int32_t success,
288              const char *emsg)
289 {
290   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291               "Nick added : %s\n",
292               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
293
294   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
295
296   GNUNET_asprintf (&s_name_1, "dummy1");
297   s_rd_1 = create_record (1);
298   GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
299                                   1, s_rd_1,
300                                   &put_cont, NULL);
301
302   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303               "Created record 2 \n");
304   GNUNET_asprintf (&s_name_2, "dummy2");
305   s_rd_2 = create_record (1);
306   GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2,
307                                   1, s_rd_2, &put_cont, NULL);
308
309   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310               "Created record 3\n");
311
312   /* name in different zone */
313   GNUNET_asprintf (&s_name_3, "dummy3");
314   s_rd_3 = create_record (1);
315   GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
316                                   1, s_rd_3,
317                                   &put_cont, NULL);
318 }
319
320
321 static void
322 nick_1_cont (void *cls, int32_t success, const char *emsg)
323 {
324   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325               "Nick 1 added : %s\n",
326               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
327
328   nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey2, ZONE_NICK_2, &nick_2_cont,
329                                     &privkey2);
330   if (NULL == nsqe)
331   {
332     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
333                 _ ("Namestore cannot store no block\n"));
334   }
335 }
336
337
338 /**
339  * Callback called from the zone iterator when we iterate over
340  * the empty zone.  Check that we got no records and then
341  * start the actual tests by filling the zone.
342  */
343 static void
344 empty_zone_proc (void *cls,
345                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
346                  const char *label,
347                  unsigned int rd_count,
348                  const struct GNUNET_GNSRECORD_Data *rd)
349 {
350   GNUNET_assert (nsh == cls);
351
352   if (NULL != zone)
353   {
354     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355                 _ ("Expected empty zone but received zone private key\n"));
356     GNUNET_break (0);
357     GNUNET_SCHEDULER_shutdown ();
358     return;
359   }
360   if ((NULL != label) || (NULL != rd) || (0 != rd_count))
361   {
362     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
363                 _ ("Expected no zone content but received data\n"));
364     GNUNET_break (0);
365     GNUNET_SCHEDULER_shutdown ();
366     return;
367   }
368   GNUNET_assert (0);
369 }
370
371
372 static void
373 empty_zone_end (void *cls)
374 {
375   GNUNET_assert (nsh == cls);
376   zi = NULL;
377   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
378   GNUNET_assert (privkey != NULL);
379   privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
380   GNUNET_assert (privkey2 != NULL);
381
382   nsqe = GNUNET_NAMESTORE_set_nick (nsh,
383                                     privkey,
384                                     ZONE_NICK_1,
385                                     &nick_1_cont,
386                                     &privkey);
387   if (NULL == nsqe)
388   {
389     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
390                 _ ("Namestore cannot store no block\n"));
391   }
392 }
393
394
395 static void
396 run (void *cls,
397      const struct GNUNET_CONFIGURATION_Handle *cfg,
398      struct GNUNET_TESTING_Peer *peer)
399 {
400   nsh = GNUNET_NAMESTORE_connect (cfg);
401   GNUNET_break (NULL != nsh);
402   GNUNET_SCHEDULER_add_shutdown (&end,
403                                  NULL);
404   /* first, iterate over empty namestore */
405   zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
406                                               NULL,
407                                               &fail_cb,
408                                               NULL,
409                                               &empty_zone_proc,
410                                               nsh,
411                                               &empty_zone_end,
412                                               nsh);
413   if (NULL == zi)
414   {
415     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
416                 "Failed to create zone iterator\n");
417     GNUNET_break (0);
418     GNUNET_SCHEDULER_shutdown ();
419   }
420 }
421
422
423 #include "test_common.c"
424
425
426 int
427 main (int argc, char *argv[])
428 {
429   const char *plugin_name;
430   char *cfg_name;
431
432   SETUP_CFG (plugin_name, cfg_name);
433   res = 1;
434   if (0 !=
435       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-nick",
436                                cfg_name,
437                                &run,
438                                NULL))
439   {
440     res = 1;
441   }
442   GNUNET_DISK_purge_cfg_dir (cfg_name,
443                              "GNUNET_TEST_HOME");
444   GNUNET_free (cfg_name);
445   return res;
446 }
447
448
449 /* end of test_namestore_api_zone_iteration.c */