fix type
[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_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "namestore.h"
29
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
32 #define WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
33
34 static struct GNUNET_NAMESTORE_Handle * nsh;
35
36 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
37
38 static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
39
40 static struct GNUNET_CRYPTO_EccPrivateKey * privkey2;
41
42 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
43
44 static int res;
45
46 static int returned_records;
47
48 static char * s_name_1;
49
50 static struct GNUNET_NAMESTORE_RecordData *s_rd_1;
51
52 static char * s_name_2;
53
54 static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
55
56 static char * s_name_3;
57
58 static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
59
60
61 /**
62  * Re-establish the connection to the service.
63  *
64  * @param cls handle to use to re-connect.
65  * @param tc scheduler context
66  */
67 static void
68 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
69 {
70         if (NULL != zi)
71         {
72                 GNUNET_NAMESTORE_zone_iteration_stop (zi);
73                 zi = NULL;
74         }
75
76   if (nsh != NULL)
77     GNUNET_NAMESTORE_disconnect (nsh);
78   nsh = NULL;
79
80   GNUNET_free_non_null(s_name_1);
81   GNUNET_free_non_null(s_name_2);
82   GNUNET_free_non_null(s_name_3);
83
84   if (s_rd_1 != NULL)
85   {
86     GNUNET_free ((void *)s_rd_1->data);
87     GNUNET_free (s_rd_1);
88   }
89   if (s_rd_2 != NULL)
90   {
91     GNUNET_free ((void *)s_rd_2->data);
92     GNUNET_free (s_rd_2);
93   }
94   if (s_rd_3 != NULL)
95   {
96     GNUNET_free ((void *)s_rd_3->data);
97     GNUNET_free (s_rd_3);
98   }
99
100   if (privkey != NULL)
101     GNUNET_free (privkey);
102   privkey = NULL;
103
104   if (privkey2 != NULL)
105     GNUNET_free (privkey2);
106   privkey2 = NULL;
107   res = 1;
108 }
109
110
111 static void
112 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
113 {
114         if (NULL != zi)
115         {
116                 GNUNET_NAMESTORE_zone_iteration_stop (zi);
117                 zi = NULL;
118         }
119
120   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
121   {
122     GNUNET_SCHEDULER_cancel (endbadly_task);
123     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
124   }
125
126   if (privkey != NULL)
127     GNUNET_free (privkey);
128   privkey = NULL;
129
130   if (privkey2 != NULL)
131     GNUNET_free (privkey2);
132   privkey2 = NULL;
133
134   GNUNET_free (s_name_1);
135   GNUNET_free (s_name_2);
136   GNUNET_free (s_name_3);
137   if (s_rd_1 != NULL)
138   {
139     GNUNET_free ((void *)s_rd_1->data);
140     GNUNET_free (s_rd_1);
141   }
142   if (s_rd_2 != NULL)
143   {
144     GNUNET_free ((void *)s_rd_2->data);
145     GNUNET_free (s_rd_2);
146   }
147   if (s_rd_3 != NULL)
148   {
149     GNUNET_free ((void *)s_rd_3->data);
150     GNUNET_free (s_rd_3);
151   }
152   if (nsh != NULL)
153     GNUNET_NAMESTORE_disconnect (nsh);
154   nsh = NULL;
155 }
156
157
158 static void
159 zone_proc (void *cls,
160                                          const struct GNUNET_CRYPTO_EccPrivateKey *zone,
161                                          const char *label,
162                                          unsigned int rd_count,
163                                          const struct GNUNET_NAMESTORE_RecordData *rd)
164 {
165   int failed = GNUNET_NO;
166   if ((zone == NULL) && (label == NULL))
167   {
168     GNUNET_break (1 <= returned_records);
169     if (1 >= returned_records)
170         res = 1; /* Last iteraterator callback, we are done */
171     else
172       res = 0;
173     zi = NULL;
174     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175                 "Received last result, iteration done after receing %u results\n",
176                 returned_records );
177     GNUNET_SCHEDULER_add_now (&end, NULL);
178     return;
179   }
180   else
181   {
182         if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
183         {
184       if (0 == strcmp (label, s_name_1))
185       {
186         if (rd_count == 1)
187         {
188           if (GNUNET_YES != GNUNET_NAMESTORE_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_NAMESTORE_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_EccPrivateKey)))
227         {
228       if (0 == strcmp (label, s_name_3))
229       {
230         if (rd_count == 1)
231         {
232           if (GNUNET_YES != GNUNET_NAMESTORE_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, "Received invalid zone\n");
257       failed = GNUNET_YES;
258       GNUNET_break (0);
259         }
260   }
261
262   if (failed == GNUNET_NO)
263   {
264         if (1 == returned_records)
265         {
266       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
267                 "Telling namestore to stop zone iteration\n");
268                 GNUNET_NAMESTORE_zone_iteration_stop (zi);
269                 zi = NULL;
270       if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
271       {
272         GNUNET_SCHEDULER_cancel (endbadly_task);
273         endbadly_task = GNUNET_SCHEDULER_NO_TASK;
274       }
275       res = 0;
276       GNUNET_SCHEDULER_add_delayed (WAIT, &end, NULL);
277                 return;
278         }
279     returned_records ++;
280     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281                 "Telling namestore to send the next result\n");
282     GNUNET_NAMESTORE_zone_iterator_next (zi);
283   }
284   else
285   {
286     GNUNET_break (0);
287     GNUNET_SCHEDULER_add_now (&end, NULL);
288   }
289 }
290
291
292 static void
293 put_cont (void *cls, int32_t success, const char *emsg)
294 {
295   static int c = 0;
296
297   if (success == GNUNET_OK)
298   {
299     c++;
300     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
301   }
302   else
303   {
304     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
305                 emsg);
306     GNUNET_break (0);
307     if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
308         GNUNET_SCHEDULER_cancel (endbadly_task);
309     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
310     return;
311   }
312
313   if (c == 3)
314   {
315     res = 1;
316     returned_records = 0;
317     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
318     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
319                                                NULL,
320                                                &zone_proc,
321                                                NULL);
322     if (zi == NULL)
323     {
324       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
325       GNUNET_break (0);
326       if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
327         GNUNET_SCHEDULER_cancel (endbadly_task);
328       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
329       return;
330     }
331   }
332 }
333
334
335 static struct GNUNET_NAMESTORE_RecordData *
336 create_record (unsigned int count)
337 {
338   unsigned int c;
339   struct GNUNET_NAMESTORE_RecordData * rd;
340
341   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
342   for (c = 0; c < count; c++)
343   {
344     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
345     rd[c].record_type = 1111;
346     rd[c].data_size = 50;
347     rd[c].data = GNUNET_malloc(50);
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_EccPrivateKey *zone,
362                  const char *label,
363                  unsigned int rd_count,
364                  const struct GNUNET_NAMESTORE_RecordData *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_ecc_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_ecc_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   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
435   nsh = GNUNET_NAMESTORE_connect (cfg);
436   GNUNET_break (NULL != nsh);
437   /* first, iterate over empty namestore */
438   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
439                                              NULL, &empty_zone_proc, nsh);
440   if (NULL == zi)
441   {
442     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
443     GNUNET_break (0);
444     GNUNET_SCHEDULER_cancel (endbadly_task);
445     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
446   }
447 }
448
449
450 int
451 main (int argc, char *argv[])
452 {
453   res = 1;
454   if (0 != 
455       GNUNET_TESTING_service_run ("test-namestore-api-zone-iteration-stop",
456                                   "namestore",
457                                   "test_namestore_api.conf",
458                                   &run,
459                                   NULL))
460     return 1;
461   return res;
462 }
463
464 /* end of test_namestore_api_zone_iteration_stop.c */