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