note about performance issue, indentation fix
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration.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
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 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  * @param tc scheduler context
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   if (nsh != NULL)
77   {
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   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 zone_end (void *cls)
160 {
161   GNUNET_break (3 == returned_records);
162   if (3 == returned_records)
163   {
164     res = 0; /* Last iteraterator callback, we are done */
165     zi = NULL;
166   }
167   else
168     res = 1;
169
170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171               "Received last result, iteration done after receing %u results\n",
172               returned_records);
173   GNUNET_SCHEDULER_add_now (&end, NULL);
174 }
175
176
177 static void
178 fail_cb (void *cls)
179 {
180   GNUNET_assert (0);
181 }
182
183
184 static void
185 zone_proc (void *cls,
186            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
187            const char *label,
188            unsigned int rd_count,
189            const struct GNUNET_GNSRECORD_Data *rd)
190 {
191   int failed = GNUNET_NO;
192
193   GNUNET_assert (NULL != zone);
194   if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
195   {
196     if (0 == strcmp (label, s_name_1))
197     {
198       if (rd_count == 1)
199       {
200         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
201         {
202           failed = GNUNET_YES;
203           GNUNET_break (0);
204         }
205       }
206       else
207       {
208         failed = GNUNET_YES;
209         GNUNET_break (0);
210       }
211     }
212     else if (0 == strcmp (label, s_name_2))
213     {
214       if (rd_count == 1)
215       {
216         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
217         {
218           failed = GNUNET_YES;
219           GNUNET_break (0);
220         }
221       }
222       else
223       {
224         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225                     "Received invalid record count\n");
226         failed = GNUNET_YES;
227         GNUNET_break (0);
228       }
229     }
230     else
231     {
232       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233                   "Comparing result failed: got name `%s' for first zone\n", label);
234       failed = GNUNET_YES;
235       GNUNET_break (0);
236     }
237   }
238   else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
239   {
240     if (0 == strcmp (label, s_name_3))
241     {
242       if (rd_count == 1)
243       {
244         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
245         {
246           failed = GNUNET_YES;
247           GNUNET_break (0);
248         }
249       }
250       else
251       {
252         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253                     "Received invalid record count\n");
254         failed = GNUNET_YES;
255         GNUNET_break (0);
256       }
257     }
258     else
259     {
260       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261                   "Comparing result failed: got name `%s' for first zone\n", label);
262       failed = GNUNET_YES;
263       GNUNET_break (0);
264     }
265   }
266   else
267   {
268     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
269                 "Received invalid zone\n");
270     failed = GNUNET_YES;
271     GNUNET_break (0);
272   }
273
274   if (failed == GNUNET_NO)
275   {
276     returned_records ++;
277     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278                 "Telling namestore to send the next result\n");
279     GNUNET_NAMESTORE_zone_iterator_next (zi);
280   }
281   else
282   {
283     GNUNET_break (0);
284     GNUNET_SCHEDULER_add_now (&end, NULL);
285   }
286 }
287
288
289 static void
290 put_cont (void *cls, int32_t success, const char *emsg)
291 {
292   static int c = 0;
293
294   if (success == GNUNET_OK)
295   {
296     c++;
297     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
298   }
299   else
300   {
301     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
302                 emsg);
303     GNUNET_break (0);
304     if (NULL != endbadly_task)
305         GNUNET_SCHEDULER_cancel (endbadly_task);
306     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
307     return;
308   }
309
310   if (c == 3)
311   {
312     res = 1;
313     returned_records = 0;
314     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
315     zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
316                                                 NULL,
317                                                 &fail_cb,
318                                                 NULL,
319                                                 &zone_proc,
320                                                 NULL,
321                                                 &zone_end,
322                                                 NULL);
323     if (zi == NULL)
324     {
325       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
326       GNUNET_break (0);
327       if (NULL != endbadly_task)
328         GNUNET_SCHEDULER_cancel (endbadly_task);
329       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
330       return;
331     }
332   }
333 }
334
335
336 static struct GNUNET_GNSRECORD_Data *
337 create_record (unsigned int count)
338 {
339   unsigned int c;
340   struct GNUNET_GNSRECORD_Data * rd;
341
342   rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
343   for (c = 0; c < count; c++)
344   {
345     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
346     rd[c].record_type = 1111;
347     rd[c].data_size = 50;
348     rd[c].data = GNUNET_malloc(50);
349     rd[c].flags = 0;
350     memset ((char *) rd[c].data, 'a', 50);
351   }
352   return rd;
353 }
354
355
356 /**
357  * Callback called from the zone iterator when we iterate over
358  * the empty zone.  Check that we got no records and then
359  * start the actual tests by filling the zone.
360  */
361 static void
362 empty_zone_proc (void *cls,
363                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
364                  const char *label,
365                  unsigned int rd_count,
366                  const struct GNUNET_GNSRECORD_Data *rd)
367 {
368   GNUNET_assert (nsh == cls);
369   if (NULL != zone)
370   {
371     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
372                 _("Expected empty zone but received zone private key\n"));
373     GNUNET_break (0);
374     if (endbadly_task != NULL)
375       GNUNET_SCHEDULER_cancel (endbadly_task);
376     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
377     return;
378   }
379   if ((NULL != label) || (NULL != rd) || (0 != rd_count))
380   {
381     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
382                 _("Expected no zone content but received data\n"));
383     GNUNET_break (0);
384     if (endbadly_task != NULL)
385       GNUNET_SCHEDULER_cancel (endbadly_task);
386     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
387     return;
388   }
389   GNUNET_assert (0);
390 }
391
392
393 static void
394 empty_zone_end (void *cls)
395 {
396   char *hostkey_file;
397
398   zi = NULL;
399   GNUNET_asprintf (&hostkey_file,
400                    "zonefiles%s%s",
401                    DIR_SEPARATOR_STR,
402                    "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
404               "Using zonekey file `%s' \n",
405               hostkey_file);
406   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
407   GNUNET_free (hostkey_file);
408   GNUNET_assert (privkey != NULL);
409
410   GNUNET_asprintf (&hostkey_file,
411                    "zonefiles%s%s",
412                    DIR_SEPARATOR_STR,
413                    "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
414   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
415   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
416   GNUNET_free (hostkey_file);
417   GNUNET_assert (privkey2 != NULL);
418
419   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
420
421   GNUNET_asprintf(&s_name_1, "dummy1");
422   s_rd_1 = create_record(1);
423   GNUNET_NAMESTORE_records_store (nsh,
424                                   privkey,
425                                   s_name_1,
426                                   1, s_rd_1,
427                                   &put_cont, NULL);
428
429   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
430               "Created record 2 \n");
431   GNUNET_asprintf(&s_name_2, "dummy2");
432   s_rd_2 = create_record(1);
433   GNUNET_NAMESTORE_records_store (nsh,
434                                   privkey,
435                                   s_name_2,
436                                   1, s_rd_2,
437                                   &put_cont,
438                                   NULL);
439
440   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
441               "Created record 3\n");
442
443   /* name in different zone */
444   GNUNET_asprintf(&s_name_3, "dummy3");
445   s_rd_3 = create_record(1);
446   GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
447                                   1, s_rd_3,
448                                   &put_cont, NULL);
449 }
450
451
452 static void
453 run (void *cls,
454      const struct GNUNET_CONFIGURATION_Handle *cfg,
455      struct GNUNET_TESTING_Peer *peer)
456 {
457   directory = NULL;
458   GNUNET_assert (GNUNET_OK ==
459                  GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS",
460                                                        "GNUNET_TEST_HOME",
461                                                        &directory));
462   GNUNET_DISK_directory_remove (directory);
463
464   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
465   nsh = GNUNET_NAMESTORE_connect (cfg);
466   GNUNET_break (NULL != nsh);
467   /* first, iterate over empty namestore */
468   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
469                                              NULL,
470                                              &fail_cb,
471                                              NULL,
472                                              &empty_zone_proc,
473                                              nsh,
474                                              &empty_zone_end,
475                                              NULL);
476   if (NULL == zi)
477   {
478     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
479     GNUNET_break (0);
480     GNUNET_SCHEDULER_cancel (endbadly_task);
481     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
482   }
483 }
484
485
486 int
487 main (int argc, char *argv[])
488 {
489   res = 1;
490   if (0 !=
491       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
492                                "test_namestore_api.conf",
493                                &run,
494                                NULL))
495   {
496     res = 1;
497   }
498   if (NULL != directory)
499   {
500       GNUNET_DISK_directory_remove (directory);
501       GNUNET_free (directory);
502   }
503   return res;
504 }
505
506
507 /* end of test_namestore_api_zone_iteration.c */