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