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