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