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