-fix leaks
[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
60 /**
61  * Re-establish the connection to the service.
62  *
63  * @param cls handle to use to re-connect.
64  * @param tc scheduler context
65  */
66 static void
67 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
68 {
69         if (NULL != zi)
70         {
71                 GNUNET_NAMESTORE_zone_iteration_stop (zi);
72                 zi = NULL;
73         }
74
75   if (nsh != NULL)
76     GNUNET_NAMESTORE_disconnect (nsh);
77   nsh = NULL;
78
79   GNUNET_free_non_null(s_name_1);
80   GNUNET_free_non_null(s_name_2);
81   GNUNET_free_non_null(s_name_3);
82
83   if (s_rd_1 != NULL)
84   {
85     GNUNET_free ((void *)s_rd_1->data);
86     GNUNET_free (s_rd_1);
87   }
88   if (s_rd_2 != NULL)
89   {
90     GNUNET_free ((void *)s_rd_2->data);
91     GNUNET_free (s_rd_2);
92   }
93   if (s_rd_3 != NULL)
94   {
95     GNUNET_free ((void *)s_rd_3->data);
96     GNUNET_free (s_rd_3);
97   }
98
99   if (privkey != NULL)
100     GNUNET_free (privkey);
101   privkey = NULL;
102
103   if (privkey2 != NULL)
104     GNUNET_free (privkey2);
105   privkey2 = NULL;
106   res = 1;
107 }
108
109
110 static void
111 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 {
113         if (NULL != zi)
114         {
115                 GNUNET_NAMESTORE_zone_iteration_stop (zi);
116                 zi = NULL;
117         }
118
119   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
120   {
121     GNUNET_SCHEDULER_cancel (endbadly_task);
122     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
123   }
124
125   if (privkey != NULL)
126     GNUNET_free (privkey);
127   privkey = NULL;
128
129   if (privkey2 != NULL)
130     GNUNET_free (privkey2);
131   privkey2 = NULL;
132
133   GNUNET_free (s_name_1);
134   GNUNET_free (s_name_2);
135   GNUNET_free (s_name_3);
136   if (s_rd_1 != NULL)
137   {
138     GNUNET_free ((void *)s_rd_1->data);
139     GNUNET_free (s_rd_1);
140   }
141   if (s_rd_2 != NULL)
142   {
143     GNUNET_free ((void *)s_rd_2->data);
144     GNUNET_free (s_rd_2);
145   }
146   if (s_rd_3 != NULL)
147   {
148     GNUNET_free ((void *)s_rd_3->data);
149     GNUNET_free (s_rd_3);
150   }
151   if (nsh != NULL)
152     GNUNET_NAMESTORE_disconnect (nsh);
153   nsh = NULL;
154 }
155
156
157 static void
158 zone_proc (void *cls,
159                                          const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
160                                          const char *label,
161                                          unsigned int rd_count,
162                                          const struct GNUNET_GNSRECORD_Data *rd)
163 {
164   int failed = GNUNET_NO;
165   if ((zone == NULL) && (label == NULL))
166   {
167     GNUNET_break (1 <= returned_records);
168     if (1 >= returned_records)
169         res = 1; /* Last iteraterator callback, we are done */
170     else
171       res = 0;
172     zi = NULL;
173     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
174                 "Received last result, iteration done after receing %u results\n",
175                 returned_records );
176     GNUNET_SCHEDULER_add_now (&end, NULL);
177     return;
178   }
179   else
180   {
181         if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
182         {
183       if (0 == strcmp (label, s_name_1))
184       {
185         if (rd_count == 1)
186         {
187           if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
188           {
189             failed = GNUNET_YES;
190             GNUNET_break (0);
191           }
192         }
193         else
194         {
195           failed = GNUNET_YES;
196           GNUNET_break (0);
197         }
198       }
199       else if (0 == strcmp (label, s_name_2))
200       {
201         if (rd_count == 1)
202         {
203           if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
204           {
205             failed = GNUNET_YES;
206             GNUNET_break (0);
207           }
208         }
209         else
210         {
211           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
212                         "Received invalid record count\n");
213           failed = GNUNET_YES;
214           GNUNET_break (0);
215         }
216       }
217       else
218       {
219         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220                         "Comparing result failed: got name `%s' for first zone\n", label);
221         failed = GNUNET_YES;
222         GNUNET_break (0);
223       }
224         }
225         else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
226         {
227       if (0 == strcmp (label, s_name_3))
228       {
229         if (rd_count == 1)
230         {
231           if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_3))
232           {
233             failed = GNUNET_YES;
234             GNUNET_break (0);
235           }
236         }
237         else
238         {
239           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240                         "Received invalid record count\n");
241           failed = GNUNET_YES;
242           GNUNET_break (0);
243         }
244       }
245       else
246       {
247         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248                         "Comparing result failed: got name `%s' for first zone\n", label);
249         failed = GNUNET_YES;
250         GNUNET_break (0);
251       }
252         }
253         else
254         {
255       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received invalid zone\n");
256       failed = GNUNET_YES;
257       GNUNET_break (0);
258         }
259   }
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     memset ((char *) rd[c].data, 'a', 50);
348   }
349   return rd;
350 }
351
352
353 /**
354  * Callback called from the zone iterator when we iterate over
355  * the empty zone.  Check that we got no records and then
356  * start the actual tests by filling the zone.
357  */
358 static void
359 empty_zone_proc (void *cls,
360                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
361                  const char *label,
362                  unsigned int rd_count,
363                  const struct GNUNET_GNSRECORD_Data *rd)
364 {
365   char *hostkey_file;
366
367   GNUNET_assert (nsh == cls);
368         if (NULL != zone)
369         {
370           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371                         _("Expected empty zone but received zone private key\n"));
372     GNUNET_break (0);
373     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
374         GNUNET_SCHEDULER_cancel (endbadly_task);
375     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
376     return;
377         }
378         if ((NULL != label) || (NULL != rd) || (0 != rd))
379         {
380           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381                         _("Expected no zone content but received data\n"));
382     GNUNET_break (0);
383     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
384         GNUNET_SCHEDULER_cancel (endbadly_task);
385     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
386     return;
387         }
388
389
390   zi = NULL;
391   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
392       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
393   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
394   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
395   GNUNET_free (hostkey_file);
396   GNUNET_assert (privkey != NULL);
397
398   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
399       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
400   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
401   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
402   GNUNET_free (hostkey_file);
403   GNUNET_assert (privkey2 != NULL);
404
405   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
406
407   GNUNET_asprintf(&s_name_1, "dummy1");
408   s_rd_1 = create_record(1);
409   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
410                 1, s_rd_1, &put_cont, NULL);
411
412   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
413   GNUNET_asprintf(&s_name_2, "dummy2");
414   s_rd_2 = create_record(1);
415   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
416                 1, s_rd_2, &put_cont, NULL);
417
418   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
419
420   /* name in different zone */
421   GNUNET_asprintf(&s_name_3, "dummy3");
422   s_rd_3 = create_record(1);
423   GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
424                 1, s_rd_3, &put_cont, NULL);
425 }
426
427
428 static void
429 run (void *cls,
430      const struct GNUNET_CONFIGURATION_Handle *cfg,
431      struct GNUNET_TESTING_Peer *peer)
432 {
433   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
434   nsh = GNUNET_NAMESTORE_connect (cfg);
435   GNUNET_break (NULL != nsh);
436   /* first, iterate over empty namestore */
437   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
438                                              NULL, &empty_zone_proc, nsh);
439   if (NULL == zi)
440   {
441     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
442     GNUNET_break (0);
443     GNUNET_SCHEDULER_cancel (endbadly_task);
444     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
445   }
446 }
447
448
449 int
450 main (int argc, char *argv[])
451 {
452   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namestore/");
453   res = 1;
454   if (0 !=
455       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-stop",
456                                "test_namestore_api.conf",
457                                &run,
458                                NULL))
459     return 1;
460   return res;
461 }
462
463 /* end of test_namestore_api_zone_iteration_stop.c */