allow empty/NULL context message
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration_specific_zone.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file namestore/test_namestore_api_zone_iteration_specific_zone.c
22  * @brief testcase for zone iteration functionality: iterate over a specific zone
23  * @author Matthias Wachs
24  */
25 #include "platform.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
33
34 static struct GNUNET_NAMESTORE_Handle * nsh;
35
36 static struct GNUNET_SCHEDULER_Task * endbadly_task;
37
38 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
39
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey * 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_GNSRECORD_Data *s_rd_1;
51
52 static char * s_name_2;
53
54 static struct GNUNET_GNSRECORD_Data *s_rd_2;
55
56 static char * s_name_3;
57
58 static struct GNUNET_GNSRECORD_Data *s_rd_3;
59
60 static char *directory;
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)
70 {
71   if (NULL != zi)
72   {
73     GNUNET_NAMESTORE_zone_iteration_stop (zi);
74     zi = NULL;
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)
113 {
114   if (NULL != zi)
115   {
116     GNUNET_NAMESTORE_zone_iteration_stop (zi);
117     zi = NULL;
118   }
119
120   if (endbadly_task != NULL)
121   {
122     GNUNET_SCHEDULER_cancel (endbadly_task);
123     endbadly_task = NULL;
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_EcdsaPrivateKey *zone,
161                                          const char *label,
162                                          unsigned int rd_count,
163                                          const struct GNUNET_GNSRECORD_Data *rd)
164 {
165   int failed = GNUNET_NO;
166   if ((zone == NULL) && (label == NULL))
167   {
168     GNUNET_break (2 == returned_records);
169     if (2 == returned_records)
170     {
171       res = 0; /* Last iteraterator callback, we are done */
172       zi = NULL;
173     }
174     else
175       res = 1;
176
177     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178                 "Received last result, iteration done after receing %u results\n",
179                 returned_records );
180     GNUNET_SCHEDULER_add_now (&end, NULL);
181     return;
182   }
183   GNUNET_assert (NULL != zone);
184   if (0 == memcmp (zone,
185                    privkey,
186                    sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
187   {
188     if (0 == strcmp (label, s_name_1))
189     {
190       if (rd_count == 1)
191       {
192         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
193         {
194           failed = GNUNET_YES;
195           GNUNET_break (0);
196         }
197       }
198       else
199       {
200         failed = GNUNET_YES;
201         GNUNET_break (0);
202       }
203     }
204     else if (0 == strcmp (label, s_name_2))
205     {
206       if (rd_count == 1)
207       {
208         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
209         {
210           failed = GNUNET_YES;
211           GNUNET_break (0);
212         }
213       }
214       else
215       {
216         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
217                     "Received invalid record count\n");
218         failed = GNUNET_YES;
219         GNUNET_break (0);
220       }
221     }
222     else
223     {
224       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225                   "Comparing result failed: got name `%s' for first zone\n",
226                   label);
227       failed = GNUNET_YES;
228       GNUNET_break (0);
229     }
230   }
231   else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
232   {
233     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
234                 "Received data for not requested zone\n");
235     failed = GNUNET_YES;
236     GNUNET_break (0);
237   }
238   else
239   {
240     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
241                 "Received invalid zone\n");
242     failed = GNUNET_YES;
243     GNUNET_break (0);
244   }
245   if (failed == GNUNET_NO)
246   {
247     returned_records ++;
248     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249                 "Telling namestore to send the next result\n");
250     GNUNET_NAMESTORE_zone_iterator_next (zi);
251   }
252   else
253   {
254     GNUNET_break (0);
255     GNUNET_SCHEDULER_add_now (&end, NULL);
256   }
257 }
258
259
260 static void
261 put_cont (void *cls, int32_t success, const char *emsg)
262 {
263   static int c = 0;
264
265   if (success == GNUNET_OK)
266   {
267     c++;
268     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
269                 "Created record %u \n", c);
270   }
271   else
272   {
273     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
274                 "Failed to created records: `%s'\n",
275                 emsg);
276     GNUNET_break (0);
277     if (NULL != endbadly_task)
278         GNUNET_SCHEDULER_cancel (endbadly_task);
279     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
280     return;
281   }
282
283   if (c == 3)
284   {
285     res = 1;
286     returned_records = 0;
287     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288                 "All records created, starting iteration over all zones \n");
289     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
290                                                privkey,
291                                                &zone_proc,
292                                                NULL);
293     if (zi == NULL)
294     {
295       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
296                   "Failed to create zone iterator\n");
297       GNUNET_break (0);
298       if (NULL != endbadly_task)
299         GNUNET_SCHEDULER_cancel (endbadly_task);
300       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
301       return;
302     }
303   }
304 }
305
306
307 static struct GNUNET_GNSRECORD_Data *
308 create_record (unsigned int count)
309 {
310   unsigned int c;
311   struct GNUNET_GNSRECORD_Data * rd;
312
313   rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
314   for (c = 0; c < count; c++)
315   {
316     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
317     rd[c].record_type = 1111;
318     rd[c].data_size = 50;
319     rd[c].data = GNUNET_malloc(50);
320     rd[c].flags = 0;
321     memset ((char *) rd[c].data, 'a', 50);
322   }
323   return rd;
324 }
325
326
327 /**
328  * Callback called from the zone iterator when we iterate over
329  * the empty zone.  Check that we got no records and then
330  * start the actual tests by filling the zone.
331  */
332 static void
333 empty_zone_proc (void *cls,
334                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
335                  const char *label,
336                  unsigned int rd_count,
337                  const struct GNUNET_GNSRECORD_Data *rd)
338 {
339   char *hostkey_file;
340
341   GNUNET_assert (nsh == cls);
342   if (NULL != zone)
343     {
344       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
345                   _("Expected empty zone but received zone private key\n"));
346       GNUNET_break (0);
347       if (endbadly_task != NULL)
348         GNUNET_SCHEDULER_cancel (endbadly_task);
349       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
350       return;
351     }
352   if ((NULL != label) || (NULL != rd) || (0 != rd_count))
353     {
354       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
355                   _("Expected no zone content but received data\n"));
356       GNUNET_break (0);
357       if (endbadly_task != NULL)
358         GNUNET_SCHEDULER_cancel (endbadly_task);
359       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
360       return;
361     }
362
363
364   zi = NULL;
365   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
366       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
367   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
368   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
369   GNUNET_free (hostkey_file);
370   GNUNET_assert (privkey != NULL);
371
372   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
373       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
374   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
375   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
376   GNUNET_free (hostkey_file);
377   GNUNET_assert (privkey2 != NULL);
378
379   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
380               "Created record 1\n");
381
382   GNUNET_asprintf(&s_name_1, "dummy1");
383   s_rd_1 = create_record(1);
384   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
385                 1, s_rd_1, &put_cont, NULL);
386
387   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
388               "Created record 2 \n");
389   GNUNET_asprintf(&s_name_2, "dummy2");
390   s_rd_2 = create_record(1);
391   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
392                 1, s_rd_2, &put_cont, NULL);
393
394   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395               "Created record 3\n");
396
397   /* name in different zone */
398   GNUNET_asprintf(&s_name_3, "dummy3");
399   s_rd_3 = create_record(1);
400   GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
401                 1, s_rd_3, &put_cont, NULL);
402 }
403
404
405 static void
406 run (void *cls,
407      const struct GNUNET_CONFIGURATION_Handle *cfg,
408      struct GNUNET_TESTING_Peer *peer)
409 {
410   directory = NULL;
411   GNUNET_assert (GNUNET_OK ==
412                  GNUNET_CONFIGURATION_get_value_string (cfg,
413                                                         "PATHS",
414                                                         "GNUNET_TEST_HOME",
415                                                         &directory));
416   GNUNET_DISK_directory_remove (directory);
417
418   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
419                                                 &endbadly,
420                                                 NULL);
421   nsh = GNUNET_NAMESTORE_connect (cfg);
422   GNUNET_break (NULL != nsh);
423   /* first, iterate over empty namestore */
424   zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
425                                               NULL,
426                                               &empty_zone_proc, nsh);
427   if (NULL == zi)
428   {
429     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
430                 "Failed to create zone iterator\n");
431     GNUNET_break (0);
432     GNUNET_SCHEDULER_cancel (endbadly_task);
433     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
434   }
435 }
436
437
438 int
439 main (int argc, char *argv[])
440 {
441   res = 1;
442   if (0 !=
443       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
444                                "test_namestore_api.conf",
445                                &run,
446                                NULL))
447   {
448     res = 1;
449   }
450   if (NULL != directory)
451   {
452       GNUNET_DISK_directory_remove (directory);
453       GNUNET_free (directory);
454   }
455   return res;
456 }
457
458 /* end of test_namestore_api_zone_iteration_specific_zone.c */