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