note about performance issue, indentation fix
[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 static char *directory;
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 endbadly (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     GNUNET_free (privkey);
106   privkey = NULL;
107
108   if (privkey2 != NULL)
109     GNUNET_free (privkey2);
110   privkey2 = NULL;
111   res = 1;
112 }
113
114
115 static void
116 end (void *cls)
117 {
118   if (NULL != zi)
119   {
120     GNUNET_NAMESTORE_zone_iteration_stop (zi);
121     zi = NULL;
122   }
123   if (endbadly_task != NULL)
124   {
125     GNUNET_SCHEDULER_cancel (endbadly_task);
126     endbadly_task = NULL;
127   }
128
129   if (privkey != NULL)
130     GNUNET_free (privkey);
131   privkey = NULL;
132
133   if (privkey2 != NULL)
134     GNUNET_free (privkey2);
135   privkey2 = NULL;
136
137   GNUNET_free (s_name_1);
138   GNUNET_free (s_name_2);
139   GNUNET_free (s_name_3);
140   if (s_rd_1 != NULL)
141   {
142     GNUNET_free ((void *)s_rd_1->data);
143     GNUNET_free (s_rd_1);
144   }
145   if (s_rd_2 != NULL)
146   {
147     GNUNET_free ((void *)s_rd_2->data);
148     GNUNET_free (s_rd_2);
149   }
150   if (s_rd_3 != NULL)
151   {
152     GNUNET_free ((void *)s_rd_3->data);
153     GNUNET_free (s_rd_3);
154   }
155   if (nsh != NULL)
156     GNUNET_NAMESTORE_disconnect (nsh);
157   nsh = NULL;
158 }
159
160
161 static int
162 check_zone_1 (const char *label, unsigned int rd_count,
163     const struct GNUNET_GNSRECORD_Data *rd)
164 {
165   int failed = GNUNET_NO;
166   int c;
167   for (c = 0; c< rd_count ; c++)
168   {
169     if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
170         if (0 != strcmp (rd[c].data, ZONE_NICK_1))
171         {
172           GNUNET_break (0);
173           return GNUNET_YES;
174         }
175   }
176   return failed;
177 }
178
179 static int
180 check_zone_2 (const char *label,
181            unsigned int rd_count,
182            const struct GNUNET_GNSRECORD_Data *rd)
183 {
184   int failed = GNUNET_NO;
185   int c;
186
187   for (c = 0; c< rd_count ; c++)
188   {
189     if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
190         if (0 != strcmp (rd[c].data, ZONE_NICK_2))
191         {
192           GNUNET_break (0);
193           return GNUNET_YES;
194         }
195   }
196
197   return failed;
198 }
199
200
201 static void
202 zone_proc_end (void *cls)
203 {
204   zi = NULL;
205   res = 0;
206   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207               "Received last result, iteration done after receing %u results\n",
208               returned_records);
209   GNUNET_SCHEDULER_add_now (&end, NULL);
210 }
211
212
213 static void
214 zone_proc (void *cls,
215            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
216            const char *label,
217            unsigned int rd_count,
218            const struct GNUNET_GNSRECORD_Data *rd)
219 {
220   int failed = GNUNET_NO;
221
222   GNUNET_assert (NULL != zone);
223   if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
224   {
225     failed = check_zone_1 (label, rd_count, rd);
226     if (GNUNET_YES == failed)
227       GNUNET_break (0);
228   }
229   else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
230   {
231     failed = check_zone_2 (label, rd_count, rd);
232     if (GNUNET_YES == failed)
233       GNUNET_break (0);
234   }
235   else
236   {
237     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238                 "Received invalid zone\n");
239     failed = GNUNET_YES;
240     GNUNET_break (0);
241   }
242
243   if (failed == GNUNET_NO)
244   {
245     returned_records ++;
246     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247                 "Telling namestore to send the next result\n");
248     GNUNET_NAMESTORE_zone_iterator_next (zi);
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, int32_t success, const char *emsg)
268 {
269   static int c = 0;
270
271   if (success == GNUNET_OK)
272   {
273     c++;
274     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
275   }
276   else
277   {
278     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
279                 emsg);
280     GNUNET_break (0);
281     if (NULL != endbadly_task)
282         GNUNET_SCHEDULER_cancel (endbadly_task);
283     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
284     return;
285   }
286
287   if (c == 3)
288   {
289     res = 1;
290     returned_records = 0;
291     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
292     zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
293                                                 NULL,
294                                                 &fail_cb,
295                                                 NULL,
296                                                 &zone_proc,
297                                                 NULL,
298                                                 &zone_proc_end,
299                                                 NULL);
300     if (zi == NULL)
301     {
302       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
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 }
311
312
313 static struct GNUNET_GNSRECORD_Data *
314 create_record (unsigned int count)
315 {
316   unsigned int c;
317   struct GNUNET_GNSRECORD_Data * rd;
318
319   rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
320   for (c = 0; c < count; c++)
321   {
322     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
323     rd[c].record_type = 1111;
324     rd[c].data_size = 50;
325     rd[c].data = GNUNET_malloc(50);
326     rd[c].flags = 0;
327     memset ((char *) rd[c].data, 'a', 50);
328   }
329   return rd;
330 }
331
332
333 static void
334 nick_2_cont (void *cls, int32_t success, const char *emsg)
335 {
336   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337               "Nick added : %s\n",
338               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
339
340   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
341
342   GNUNET_asprintf(&s_name_1, "dummy1");
343   s_rd_1 = create_record(1);
344   GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
345                                   1, s_rd_1,
346                                   &put_cont, NULL);
347
348   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349               "Created record 2 \n");
350   GNUNET_asprintf(&s_name_2, "dummy2");
351   s_rd_2 = create_record(1);
352   GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2,
353                                   1, s_rd_2, &put_cont, NULL);
354
355   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
356               "Created record 3\n");
357
358   /* name in different zone */
359   GNUNET_asprintf(&s_name_3, "dummy3");
360   s_rd_3 = create_record(1);
361   GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
362                                   1, s_rd_3,
363                                   &put_cont, NULL);
364 }
365
366
367 static void
368 nick_1_cont (void *cls, int32_t success, const char *emsg)
369 {
370   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371               "Nick 1 added : %s\n",
372               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
373
374   nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey2, ZONE_NICK_2, &nick_2_cont, &privkey2);
375   if (NULL == nsqe)
376   {
377     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
378               _("Namestore cannot store no block\n"));
379   }
380 }
381
382
383 /**
384  * Callback called from the zone iterator when we iterate over
385  * the empty zone.  Check that we got no records and then
386  * start the actual tests by filling the zone.
387  */
388 static void
389 empty_zone_proc (void *cls,
390                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
391                  const char *label,
392                  unsigned int rd_count,
393                  const struct GNUNET_GNSRECORD_Data *rd)
394 {
395   GNUNET_assert (nsh == cls);
396
397   if (NULL != zone)
398   {
399     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
400                 _("Expected empty zone but received zone private key\n"));
401     GNUNET_break (0);
402     if (endbadly_task != NULL)
403       GNUNET_SCHEDULER_cancel (endbadly_task);
404     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
405     return;
406   }
407   if ((NULL != label) || (NULL != rd) || (0 != rd_count))
408   {
409     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410                 _("Expected no zone content but received data\n"));
411     GNUNET_break (0);
412     if (endbadly_task != NULL)
413       GNUNET_SCHEDULER_cancel (endbadly_task);
414     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
415     return;
416   }
417   GNUNET_assert (0);
418 }
419
420
421 static void
422 empty_zone_end (void *cls)
423 {
424   char *hostkey_file;
425   GNUNET_assert (nsh == cls);
426
427   zi = NULL;
428   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
429       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
430   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
431   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
432   GNUNET_free (hostkey_file);
433   GNUNET_assert (privkey != NULL);
434
435   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
436       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
437   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
438   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
439   GNUNET_free (hostkey_file);
440   GNUNET_assert (privkey2 != NULL);
441
442   nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey, ZONE_NICK_1, &nick_1_cont, &privkey);
443   if (NULL == nsqe)
444   {
445     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
446               _("Namestore cannot store no block\n"));
447   }
448
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,
460                                                         "PATHS",
461                                                         "GNUNET_TEST_HOME",
462                                                         &directory));
463   GNUNET_DISK_directory_remove (directory);
464
465   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
466   nsh = GNUNET_NAMESTORE_connect (cfg);
467   GNUNET_break (NULL != nsh);
468
469   /* first, iterate over empty namestore */
470   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
471                                              NULL,
472                                              &fail_cb,
473                                              NULL,
474                                              &empty_zone_proc,
475                                              nsh,
476                                              &empty_zone_end,
477                                              nsh);
478   if (NULL == zi)
479   {
480     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
481                 "Failed to create zone iterator\n");
482     GNUNET_break (0);
483     GNUNET_SCHEDULER_cancel (endbadly_task);
484     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
485   }
486 }
487
488
489 int
490 main (int argc, char *argv[])
491 {
492   res = 1;
493   if (0 !=
494       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
495                                "test_namestore_api.conf",
496                                &run,
497                                NULL))
498   {
499     res = 1;
500   }
501   if (NULL != directory)
502   {
503       GNUNET_DISK_directory_remove (directory);
504       GNUNET_free (directory);
505   }
506   return res;
507 }
508
509
510 /* end of test_namestore_api_zone_iteration.c */