note about performance issue, indentation fix
[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 static char *directory;
60
61
62 /**
63  * Re-establish the connection to the service.
64  *
65  * @param cls handle to use to re-connect.
66  */
67 static void
68 endbadly (void *cls)
69 {
70   if (NULL != zi)
71   {
72     GNUNET_NAMESTORE_zone_iteration_stop (zi);
73     zi = NULL;
74   }
75
76   if (nsh != NULL)
77     GNUNET_NAMESTORE_disconnect (nsh);
78   nsh = NULL;
79
80   GNUNET_free_non_null(s_name_1);
81   GNUNET_free_non_null(s_name_2);
82   GNUNET_free_non_null(s_name_3);
83
84   if (s_rd_1 != NULL)
85   {
86     GNUNET_free ((void *)s_rd_1->data);
87     GNUNET_free (s_rd_1);
88   }
89   if (s_rd_2 != NULL)
90   {
91     GNUNET_free ((void *)s_rd_2->data);
92     GNUNET_free (s_rd_2);
93   }
94   if (s_rd_3 != NULL)
95   {
96     GNUNET_free ((void *)s_rd_3->data);
97     GNUNET_free (s_rd_3);
98   }
99
100   if (privkey != NULL)
101     GNUNET_free (privkey);
102   privkey = NULL;
103
104   if (privkey2 != NULL)
105     GNUNET_free (privkey2);
106   privkey2 = NULL;
107   res = 1;
108 }
109
110
111 static void
112 end (void *cls)
113 {
114   if (NULL != zi)
115   {
116     GNUNET_NAMESTORE_zone_iteration_stop (zi);
117     zi = NULL;
118   }
119
120   if (endbadly_task != NULL)
121   {
122     GNUNET_SCHEDULER_cancel (endbadly_task);
123     endbadly_task = NULL;
124   }
125
126   if (privkey != NULL)
127     GNUNET_free (privkey);
128   privkey = NULL;
129
130   if (privkey2 != NULL)
131     GNUNET_free (privkey2);
132   privkey2 = NULL;
133
134   GNUNET_free (s_name_1);
135   GNUNET_free (s_name_2);
136   GNUNET_free (s_name_3);
137   if (s_rd_1 != NULL)
138   {
139     GNUNET_free ((void *)s_rd_1->data);
140     GNUNET_free (s_rd_1);
141   }
142   if (s_rd_2 != NULL)
143   {
144     GNUNET_free ((void *)s_rd_2->data);
145     GNUNET_free (s_rd_2);
146   }
147   if (s_rd_3 != NULL)
148   {
149     GNUNET_free ((void *)s_rd_3->data);
150     GNUNET_free (s_rd_3);
151   }
152   if (nsh != NULL)
153     GNUNET_NAMESTORE_disconnect (nsh);
154   nsh = NULL;
155 }
156
157
158 static void
159 fail_cb (void *cls)
160 {
161   GNUNET_assert (0);
162 }
163
164
165 static void
166 zone_proc (void *cls,
167            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
168            const char *label,
169            unsigned int rd_count,
170            const struct GNUNET_GNSRECORD_Data *rd)
171 {
172   int failed = GNUNET_NO;
173
174   GNUNET_assert (NULL != zone);
175   if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
176   {
177     if (0 == strcmp (label, s_name_1))
178     {
179       if (rd_count == 1)
180       {
181         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
182         {
183           failed = GNUNET_YES;
184           GNUNET_break (0);
185         }
186       }
187       else
188       {
189         failed = GNUNET_YES;
190         GNUNET_break (0);
191       }
192     }
193     else if (0 == strcmp (label, s_name_2))
194     {
195       if (rd_count == 1)
196       {
197         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
198         {
199           failed = GNUNET_YES;
200           GNUNET_break (0);
201         }
202       }
203       else
204       {
205         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206                     "Received invalid record count\n");
207         failed = GNUNET_YES;
208         GNUNET_break (0);
209       }
210     }
211     else
212     {
213       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214                   "Comparing result failed: got name `%s' for first zone\n", label);
215       failed = GNUNET_YES;
216       GNUNET_break (0);
217     }
218   }
219   else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
220   {
221     if (0 == strcmp (label, s_name_3))
222     {
223       if (rd_count == 1)
224       {
225         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
226         {
227           failed = GNUNET_YES;
228           GNUNET_break (0);
229         }
230       }
231       else
232       {
233         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234                     "Received invalid record count\n");
235         failed = GNUNET_YES;
236         GNUNET_break (0);
237       }
238     }
239     else
240     {
241       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
242                   "Comparing result failed: got name `%s' for first zone\n", label);
243       failed = GNUNET_YES;
244       GNUNET_break (0);
245     }
246   }
247   else
248   {
249     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
250                 "Received invalid zone\n");
251     failed = GNUNET_YES;
252     GNUNET_break (0);
253   }
254   if (failed == GNUNET_NO)
255   {
256     if (1 == returned_records)
257     {
258       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259                 "Telling namestore to stop zone iteration\n");
260                 GNUNET_NAMESTORE_zone_iteration_stop (zi);
261                 zi = NULL;
262       if (NULL != endbadly_task)
263       {
264         GNUNET_SCHEDULER_cancel (endbadly_task);
265         endbadly_task = NULL;
266       }
267       res = 0;
268       GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
269       return;
270     }
271     returned_records ++;
272     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273                 "Telling namestore to send the next result\n");
274     GNUNET_NAMESTORE_zone_iterator_next (zi);
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   GNUNET_assert (GNUNET_OK ==
478                  GNUNET_CONFIGURATION_get_value_string (cfg,
479                                                         "PATHS",
480                                                         "GNUNET_TEST_HOME",
481                                                         &directory));
482   GNUNET_DISK_directory_remove (directory);
483
484   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
485   nsh = GNUNET_NAMESTORE_connect (cfg);
486   GNUNET_break (NULL != nsh);
487   /* first, iterate over empty namestore */
488   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
489                                              NULL,
490                                              &fail_cb,
491                                              NULL,
492                                              &empty_zone_proc,
493                                              nsh,
494                                              &empty_zone_proc_end,
495                                              nsh);
496   if (NULL == zi)
497   {
498     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
499                 "Failed to create zone iterator\n");
500     GNUNET_break (0);
501     GNUNET_SCHEDULER_cancel (endbadly_task);
502     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
503   }
504 }
505
506
507 int
508 main (int argc, char *argv[])
509 {
510   res = 1;
511   if (0 !=
512       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
513                                "test_namestore_api.conf",
514                                &run,
515                                NULL))
516   {
517     res = 1;
518   }
519   if (NULL != directory)
520   {
521       GNUNET_DISK_directory_remove (directory);
522       GNUNET_free (directory);
523   }
524   return res;
525 }
526
527 /* end of test_namestore_api_zone_iteration_stop.c */