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