-fix #3301
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration_stop.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009 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: 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 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
77   if (nsh != NULL)
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
121   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
122   {
123     GNUNET_SCHEDULER_cancel (endbadly_task);
124     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
125   }
126
127   if (privkey != NULL)
128     GNUNET_free (privkey);
129   privkey = NULL;
130
131   if (privkey2 != NULL)
132     GNUNET_free (privkey2);
133   privkey2 = NULL;
134
135   GNUNET_free (s_name_1);
136   GNUNET_free (s_name_2);
137   GNUNET_free (s_name_3);
138   if (s_rd_1 != NULL)
139   {
140     GNUNET_free ((void *)s_rd_1->data);
141     GNUNET_free (s_rd_1);
142   }
143   if (s_rd_2 != NULL)
144   {
145     GNUNET_free ((void *)s_rd_2->data);
146     GNUNET_free (s_rd_2);
147   }
148   if (s_rd_3 != NULL)
149   {
150     GNUNET_free ((void *)s_rd_3->data);
151     GNUNET_free (s_rd_3);
152   }
153   if (nsh != NULL)
154     GNUNET_NAMESTORE_disconnect (nsh);
155   nsh = NULL;
156 }
157
158
159 static void
160 zone_proc (void *cls,
161            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
162            const char *label,
163            unsigned int rd_count,
164            const struct GNUNET_GNSRECORD_Data *rd)
165 {
166   int failed = GNUNET_NO;
167   if ((zone == NULL) && (label == NULL))
168   {
169     GNUNET_break (1 <= returned_records);
170     if (1 >= returned_records)
171         res = 1; /* Last iteraterator callback, we are done */
172     else
173       res = 0;
174     zi = NULL;
175     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176                 "Received last result, iteration done after receing %u results\n",
177                 returned_records );
178     GNUNET_SCHEDULER_add_now (&end, NULL);
179     return;
180   }
181   else
182   {
183         if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
184         {
185       if (0 == strcmp (label, s_name_1))
186       {
187         if (rd_count == 1)
188         {
189           if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
190           {
191             failed = GNUNET_YES;
192             GNUNET_break (0);
193           }
194         }
195         else
196         {
197           failed = GNUNET_YES;
198           GNUNET_break (0);
199         }
200       }
201       else if (0 == strcmp (label, s_name_2))
202       {
203         if (rd_count == 1)
204         {
205           if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
206           {
207             failed = GNUNET_YES;
208             GNUNET_break (0);
209           }
210         }
211         else
212         {
213           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
214                         "Received invalid record count\n");
215           failed = GNUNET_YES;
216           GNUNET_break (0);
217         }
218       }
219       else
220       {
221         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222                         "Comparing result failed: got name `%s' for first zone\n", label);
223         failed = GNUNET_YES;
224         GNUNET_break (0);
225       }
226         }
227         else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
228         {
229       if (0 == strcmp (label, s_name_3))
230       {
231         if (rd_count == 1)
232         {
233           if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
234           {
235             failed = GNUNET_YES;
236             GNUNET_break (0);
237           }
238         }
239         else
240         {
241           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
242                         "Received invalid record count\n");
243           failed = GNUNET_YES;
244           GNUNET_break (0);
245         }
246       }
247       else
248       {
249         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
250                         "Comparing result failed: got name `%s' for first zone\n", label);
251         failed = GNUNET_YES;
252         GNUNET_break (0);
253       }
254         }
255         else
256         {
257       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received invalid zone\n");
258       failed = GNUNET_YES;
259       GNUNET_break (0);
260         }
261   }
262
263   if (failed == GNUNET_NO)
264   {
265         if (1 == returned_records)
266         {
267       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268                 "Telling namestore to stop zone iteration\n");
269                 GNUNET_NAMESTORE_zone_iteration_stop (zi);
270                 zi = NULL;
271       if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
272       {
273         GNUNET_SCHEDULER_cancel (endbadly_task);
274         endbadly_task = GNUNET_SCHEDULER_NO_TASK;
275       }
276       res = 0;
277       GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
278                 return;
279         }
280     returned_records ++;
281     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
282                 "Telling namestore to send the next result\n");
283     GNUNET_NAMESTORE_zone_iterator_next (zi);
284   }
285   else
286   {
287     GNUNET_break (0);
288     GNUNET_SCHEDULER_add_now (&end, NULL);
289   }
290 }
291
292
293 static void
294 put_cont (void *cls, int32_t success, const char *emsg)
295 {
296   static int c = 0;
297
298   if (success == GNUNET_OK)
299   {
300     c++;
301     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
302   }
303   else
304   {
305     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
306                 emsg);
307     GNUNET_break (0);
308     if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
309         GNUNET_SCHEDULER_cancel (endbadly_task);
310     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
311     return;
312   }
313
314   if (c == 3)
315   {
316     res = 1;
317     returned_records = 0;
318     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
319     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
320                                                NULL,
321                                                &zone_proc,
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 (GNUNET_SCHEDULER_NO_TASK != 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   char *hostkey_file;
369
370   GNUNET_assert (nsh == cls);
371         if (NULL != zone)
372         {
373           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
374                         _("Expected empty zone but received zone private key\n"));
375     GNUNET_break (0);
376     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
377         GNUNET_SCHEDULER_cancel (endbadly_task);
378     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
379     return;
380         }
381         if ((NULL != label) || (NULL != rd) || (0 != rd))
382         {
383           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
384                         _("Expected no zone content but received data\n"));
385     GNUNET_break (0);
386     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
387         GNUNET_SCHEDULER_cancel (endbadly_task);
388     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
389     return;
390         }
391
392
393   zi = NULL;
394   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
395       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
396   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
397   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
398   GNUNET_free (hostkey_file);
399   GNUNET_assert (privkey != NULL);
400
401   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
402       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
403   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
404   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
405   GNUNET_free (hostkey_file);
406   GNUNET_assert (privkey2 != NULL);
407
408   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
409
410   GNUNET_asprintf(&s_name_1, "dummy1");
411   s_rd_1 = create_record(1);
412   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
413                 1, s_rd_1, &put_cont, NULL);
414
415   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
416   GNUNET_asprintf(&s_name_2, "dummy2");
417   s_rd_2 = create_record(1);
418   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
419                 1, s_rd_2, &put_cont, NULL);
420
421   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
422
423   /* name in different zone */
424   GNUNET_asprintf(&s_name_3, "dummy3");
425   s_rd_3 = create_record(1);
426   GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
427                 1, s_rd_3, &put_cont, NULL);
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   /* first, iterate over empty namestore */
444   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
445                                              NULL, &empty_zone_proc, nsh);
446   if (NULL == zi)
447   {
448     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
449     GNUNET_break (0);
450     GNUNET_SCHEDULER_cancel (endbadly_task);
451     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
452   }
453 }
454
455
456 int
457 main (int argc, char *argv[])
458 {
459   res = 1;
460   if (0 !=
461       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
462                                "test_namestore_api.conf",
463                                &run,
464                                NULL))
465   {
466     res = 1;
467   }
468   if (NULL != directory)
469   {
470       GNUNET_DISK_directory_remove (directory);
471       GNUNET_free (directory);
472   }
473   return res;
474 }
475
476 /* end of test_namestore_api_zone_iteration_stop.c */