fix for size
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration_nick.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 #define ZONE_NICK_1 "nick1"
30 #define ZONE_NICK_2 "nick2"
31
32 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
33
34
35 static struct GNUNET_NAMESTORE_Handle * nsh;
36
37 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
38
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
40
41 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
42
43 static struct GNUNET_NAMESTORE_ZoneIterator *zi;
44
45 static int res;
46
47 static int returned_records;
48
49 static char * s_name_1;
50
51 static struct GNUNET_GNSRECORD_Data *s_rd_1;
52
53 static char * s_name_2;
54
55 static struct GNUNET_GNSRECORD_Data *s_rd_2;
56
57 static char * s_name_3;
58
59 static struct GNUNET_GNSRECORD_Data *s_rd_3;
60
61 static struct GNUNET_NAMESTORE_QueueEntry *nsqe;
62
63 /**
64  * Re-establish the connection to the service.
65  *
66  * @param cls handle to use to re-connect.
67  * @param tc scheduler context
68  */
69 static void
70 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
71 {
72   if (NULL != zi)
73   {
74     GNUNET_NAMESTORE_zone_iteration_stop (zi);
75     zi = NULL;
76   }
77   if (nsh != NULL)
78   {
79     GNUNET_NAMESTORE_disconnect (nsh);
80     nsh = NULL;
81   }
82   GNUNET_free_non_null(s_name_1);
83   GNUNET_free_non_null(s_name_2);
84   GNUNET_free_non_null(s_name_3);
85
86   if (s_rd_1 != NULL)
87   {
88     GNUNET_free ((void *)s_rd_1->data);
89     GNUNET_free (s_rd_1);
90   }
91   if (s_rd_2 != NULL)
92   {
93     GNUNET_free ((void *)s_rd_2->data);
94     GNUNET_free (s_rd_2);
95   }
96   if (s_rd_3 != NULL)
97   {
98     GNUNET_free ((void *)s_rd_3->data);
99     GNUNET_free (s_rd_3);
100   }
101
102   if (privkey != NULL)
103     GNUNET_free (privkey);
104   privkey = NULL;
105
106   if (privkey2 != NULL)
107     GNUNET_free (privkey2);
108   privkey2 = NULL;
109   res = 1;
110 }
111
112
113 static void
114 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 {
116   if (NULL != zi)
117   {
118     GNUNET_NAMESTORE_zone_iteration_stop (zi);
119     zi = NULL;
120   }
121   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
122   {
123     GNUNET_SCHEDULER_cancel (endbadly_task);
124     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
125   }
126
127   if (privkey != NULL)
128     GNUNET_free (privkey);
129   privkey = NULL;
130
131   if (privkey2 != NULL)
132     GNUNET_free (privkey2);
133   privkey2 = NULL;
134
135   GNUNET_free (s_name_1);
136   GNUNET_free (s_name_2);
137   GNUNET_free (s_name_3);
138   if (s_rd_1 != NULL)
139   {
140     GNUNET_free ((void *)s_rd_1->data);
141     GNUNET_free (s_rd_1);
142   }
143   if (s_rd_2 != NULL)
144   {
145     GNUNET_free ((void *)s_rd_2->data);
146     GNUNET_free (s_rd_2);
147   }
148   if (s_rd_3 != NULL)
149   {
150     GNUNET_free ((void *)s_rd_3->data);
151     GNUNET_free (s_rd_3);
152   }
153   if (nsh != NULL)
154     GNUNET_NAMESTORE_disconnect (nsh);
155   nsh = NULL;
156 }
157
158 static int
159 check_zone_1 (const char *label, unsigned int rd_count,
160     const struct GNUNET_GNSRECORD_Data *rd)
161 {
162   int failed = GNUNET_NO;
163   int c;
164   for (c = 0; c< rd_count ; c++)
165   {
166     if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
167         if (0 != strcmp (rd[c].data, ZONE_NICK_1))
168         {
169           GNUNET_break (0);
170           return GNUNET_YES;
171         }
172   }
173   return failed;
174 }
175
176 static int
177 check_zone_2 (const char *label,
178            unsigned int rd_count,
179            const struct GNUNET_GNSRECORD_Data *rd)
180 {
181   int failed = GNUNET_NO;
182   int c;
183
184   for (c = 0; c< rd_count ; c++)
185   {
186     if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
187         if (0 != strcmp (rd[c].data, ZONE_NICK_2))
188         {
189           GNUNET_break (0);
190           return GNUNET_YES;
191         }
192   }
193
194   return failed;
195 }
196
197
198 static void
199 zone_proc (void *cls,
200            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
201            const char *label,
202            unsigned int rd_count,
203            const struct GNUNET_GNSRECORD_Data *rd)
204 {
205   int failed = GNUNET_NO;
206   if ((zone == NULL) && (label == NULL))
207   {
208     zi = NULL;
209     res = 0;
210     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211                 "Received last result, iteration done after receing %u results\n",
212                 returned_records);
213     GNUNET_SCHEDULER_add_now (&end, NULL);
214     return;
215   }
216
217   if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
218   {
219     failed = check_zone_1 (label, rd_count, rd);
220     if (GNUNET_YES == failed)
221       GNUNET_break (0);
222   }
223   else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
224   {
225     failed = check_zone_2 (label, rd_count, rd);
226     if (GNUNET_YES == failed)
227       GNUNET_break (0);
228   }
229   else
230   {
231     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232                 "Received invalid zone\n");
233     failed = GNUNET_YES;
234     GNUNET_break (0);
235   }
236
237   if (failed == GNUNET_NO)
238   {
239     returned_records ++;
240     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241                 "Telling namestore to send the next result\n");
242     GNUNET_NAMESTORE_zone_iterator_next (zi);
243   }
244   else
245   {
246     GNUNET_break (0);
247     res = 1;
248     GNUNET_SCHEDULER_add_now (&end, NULL);
249   }
250 }
251
252
253 static void
254 put_cont (void *cls, int32_t success, const char *emsg)
255 {
256   static int c = 0;
257
258   if (success == GNUNET_OK)
259   {
260     c++;
261     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
262   }
263   else
264   {
265     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
266                 emsg);
267     GNUNET_break (0);
268     if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
269         GNUNET_SCHEDULER_cancel (endbadly_task);
270     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
271     return;
272   }
273
274   if (c == 3)
275   {
276     res = 1;
277     returned_records = 0;
278     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
279     zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
280                                                 NULL,
281                                                 &zone_proc,
282                                                 NULL);
283     if (zi == NULL)
284     {
285       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
286       GNUNET_break (0);
287       if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
288         GNUNET_SCHEDULER_cancel (endbadly_task);
289       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
290       return;
291     }
292   }
293 }
294
295
296 static struct GNUNET_GNSRECORD_Data *
297 create_record (unsigned int count)
298 {
299   unsigned int c;
300   struct GNUNET_GNSRECORD_Data * rd;
301
302   rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
303   for (c = 0; c < count; c++)
304   {
305     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
306     rd[c].record_type = 1111;
307     rd[c].data_size = 50;
308     rd[c].data = GNUNET_malloc(50);
309     rd[c].flags = 0;
310     memset ((char *) rd[c].data, 'a', 50);
311   }
312   return rd;
313 }
314
315 static void
316 nick_2_cont (void *cls, int32_t success, const char *emsg)
317 {
318   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319               "Nick added : %s\n",
320               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
321
322   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
323
324   GNUNET_asprintf(&s_name_1, "dummy1");
325   s_rd_1 = create_record(1);
326   GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
327                                   1, s_rd_1,
328                                   &put_cont, NULL);
329
330   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
331               "Created record 2 \n");
332   GNUNET_asprintf(&s_name_2, "dummy2");
333   s_rd_2 = create_record(1);
334   GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2,
335                                   1, s_rd_2, &put_cont, NULL);
336
337   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338               "Created record 3\n");
339
340   /* name in different zone */
341   GNUNET_asprintf(&s_name_3, "dummy3");
342   s_rd_3 = create_record(1);
343   GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
344                                   1, s_rd_3,
345                                   &put_cont, NULL);
346 }
347
348
349 static void
350 nick_1_cont (void *cls, int32_t success, const char *emsg)
351 {
352   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353               "Nick 1 added : %s\n",
354               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
355
356   nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey2, ZONE_NICK_2, &nick_2_cont, &privkey2);
357   if (NULL == nsqe)
358   {
359     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360               _("Namestore cannot store no block\n"));
361   }
362
363
364
365 }
366
367
368
369 /**
370  * Callback called from the zone iterator when we iterate over
371  * the empty zone.  Check that we got no records and then
372  * start the actual tests by filling the zone.
373  */
374 static void
375 empty_zone_proc (void *cls,
376                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
377                  const char *label,
378                  unsigned int rd_count,
379                  const struct GNUNET_GNSRECORD_Data *rd)
380 {
381   char *hostkey_file;
382   GNUNET_assert (nsh == cls);
383   if (NULL != zone)
384   {
385     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
386                 _("Expected empty zone but received zone private key\n"));
387     GNUNET_break (0);
388     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
389       GNUNET_SCHEDULER_cancel (endbadly_task);
390     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
391     return;
392   }
393   if ((NULL != label) || (NULL != rd) || (0 != rd_count))
394   {
395     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
396                 _("Expected no zone content but received data\n"));
397     GNUNET_break (0);
398     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
399       GNUNET_SCHEDULER_cancel (endbadly_task);
400     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
401     return;
402   }
403
404   zi = NULL;
405   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
406       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
407   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
408   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
409   GNUNET_free (hostkey_file);
410   GNUNET_assert (privkey != NULL);
411
412   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
413       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
414   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
415   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
416   GNUNET_free (hostkey_file);
417   GNUNET_assert (privkey2 != NULL);
418
419   nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey, ZONE_NICK_1, &nick_1_cont, &privkey);
420   if (NULL == nsqe)
421   {
422     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
423               _("Namestore cannot store no block\n"));
424   }
425
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
438   /* first, iterate over empty namestore */
439   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
440                                              NULL, &empty_zone_proc, nsh);
441   if (NULL == zi)
442   {
443     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
444     GNUNET_break (0);
445     GNUNET_SCHEDULER_cancel (endbadly_task);
446     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
447   }
448 }
449
450
451 int
452 main (int argc, char *argv[])
453 {
454   res = 1;
455   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namestore");
456   if (0 !=
457       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
458                                "test_namestore_api.conf",
459                                &run,
460                                NULL))
461     return 1;
462   return res;
463 }
464
465
466 /* end of test_namestore_api_zone_iteration.c */