-check NULL, check RV
[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 static char *directory;
64
65 /**
66  * Re-establish the connection to the service.
67  *
68  * @param cls handle to use to re-connect.
69  * @param tc scheduler context
70  */
71 static void
72 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 {
74   if (NULL != zi)
75   {
76     GNUNET_NAMESTORE_zone_iteration_stop (zi);
77     zi = NULL;
78   }
79   if (nsh != NULL)
80   {
81     GNUNET_NAMESTORE_disconnect (nsh);
82     nsh = NULL;
83   }
84   GNUNET_free_non_null(s_name_1);
85   GNUNET_free_non_null(s_name_2);
86   GNUNET_free_non_null(s_name_3);
87
88   if (s_rd_1 != NULL)
89   {
90     GNUNET_free ((void *)s_rd_1->data);
91     GNUNET_free (s_rd_1);
92   }
93   if (s_rd_2 != NULL)
94   {
95     GNUNET_free ((void *)s_rd_2->data);
96     GNUNET_free (s_rd_2);
97   }
98   if (s_rd_3 != NULL)
99   {
100     GNUNET_free ((void *)s_rd_3->data);
101     GNUNET_free (s_rd_3);
102   }
103
104   if (privkey != NULL)
105     GNUNET_free (privkey);
106   privkey = NULL;
107
108   if (privkey2 != NULL)
109     GNUNET_free (privkey2);
110   privkey2 = NULL;
111   res = 1;
112 }
113
114
115 static void
116 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118   if (NULL != zi)
119   {
120     GNUNET_NAMESTORE_zone_iteration_stop (zi);
121     zi = NULL;
122   }
123   if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
124   {
125     GNUNET_SCHEDULER_cancel (endbadly_task);
126     endbadly_task = GNUNET_SCHEDULER_NO_TASK;
127   }
128
129   if (privkey != NULL)
130     GNUNET_free (privkey);
131   privkey = NULL;
132
133   if (privkey2 != NULL)
134     GNUNET_free (privkey2);
135   privkey2 = NULL;
136
137   GNUNET_free (s_name_1);
138   GNUNET_free (s_name_2);
139   GNUNET_free (s_name_3);
140   if (s_rd_1 != NULL)
141   {
142     GNUNET_free ((void *)s_rd_1->data);
143     GNUNET_free (s_rd_1);
144   }
145   if (s_rd_2 != NULL)
146   {
147     GNUNET_free ((void *)s_rd_2->data);
148     GNUNET_free (s_rd_2);
149   }
150   if (s_rd_3 != NULL)
151   {
152     GNUNET_free ((void *)s_rd_3->data);
153     GNUNET_free (s_rd_3);
154   }
155   if (nsh != NULL)
156     GNUNET_NAMESTORE_disconnect (nsh);
157   nsh = NULL;
158 }
159
160
161 static int
162 check_zone_1 (const char *label, unsigned int rd_count,
163     const struct GNUNET_GNSRECORD_Data *rd)
164 {
165   int failed = GNUNET_NO;
166   int c;
167   for (c = 0; c< rd_count ; c++)
168   {
169     if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
170         if (0 != strcmp (rd[c].data, ZONE_NICK_1))
171         {
172           GNUNET_break (0);
173           return GNUNET_YES;
174         }
175   }
176   return failed;
177 }
178
179 static int
180 check_zone_2 (const char *label,
181            unsigned int rd_count,
182            const struct GNUNET_GNSRECORD_Data *rd)
183 {
184   int failed = GNUNET_NO;
185   int c;
186
187   for (c = 0; c< rd_count ; c++)
188   {
189     if (rd[c].record_type == GNUNET_GNSRECORD_TYPE_NICK)
190         if (0 != strcmp (rd[c].data, ZONE_NICK_2))
191         {
192           GNUNET_break (0);
193           return GNUNET_YES;
194         }
195   }
196
197   return failed;
198 }
199
200
201 static void
202 zone_proc (void *cls,
203            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
204            const char *label,
205            unsigned int rd_count,
206            const struct GNUNET_GNSRECORD_Data *rd)
207 {
208   int failed = GNUNET_NO;
209   if ((zone == NULL) && (label == NULL))
210   {
211     zi = NULL;
212     res = 0;
213     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214                 "Received last result, iteration done after receing %u results\n",
215                 returned_records);
216     GNUNET_SCHEDULER_add_now (&end, NULL);
217     return;
218   }
219   GNUNET_assert (NULL != zone);
220   if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
221   {
222     failed = check_zone_1 (label, rd_count, rd);
223     if (GNUNET_YES == failed)
224       GNUNET_break (0);
225   }
226   else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
227   {
228     failed = check_zone_2 (label, rd_count, rd);
229     if (GNUNET_YES == failed)
230       GNUNET_break (0);
231   }
232   else
233   {
234     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235                 "Received invalid zone\n");
236     failed = GNUNET_YES;
237     GNUNET_break (0);
238   }
239
240   if (failed == GNUNET_NO)
241   {
242     returned_records ++;
243     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244                 "Telling namestore to send the next result\n");
245     GNUNET_NAMESTORE_zone_iterator_next (zi);
246   }
247   else
248   {
249     GNUNET_break (0);
250     res = 1;
251     GNUNET_SCHEDULER_add_now (&end, NULL);
252   }
253 }
254
255
256 static void
257 put_cont (void *cls, int32_t success, const char *emsg)
258 {
259   static int c = 0;
260
261   if (success == GNUNET_OK)
262   {
263     c++;
264     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
265   }
266   else
267   {
268     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
269                 emsg);
270     GNUNET_break (0);
271     if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
272         GNUNET_SCHEDULER_cancel (endbadly_task);
273     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
274     return;
275   }
276
277   if (c == 3)
278   {
279     res = 1;
280     returned_records = 0;
281     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
282     zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
283                                                 NULL,
284                                                 &zone_proc,
285                                                 NULL);
286     if (zi == NULL)
287     {
288       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
289       GNUNET_break (0);
290       if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
291         GNUNET_SCHEDULER_cancel (endbadly_task);
292       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
293       return;
294     }
295   }
296 }
297
298
299 static struct GNUNET_GNSRECORD_Data *
300 create_record (unsigned int count)
301 {
302   unsigned int c;
303   struct GNUNET_GNSRECORD_Data * rd;
304
305   rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
306   for (c = 0; c < count; c++)
307   {
308     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
309     rd[c].record_type = 1111;
310     rd[c].data_size = 50;
311     rd[c].data = GNUNET_malloc(50);
312     rd[c].flags = 0;
313     memset ((char *) rd[c].data, 'a', 50);
314   }
315   return rd;
316 }
317
318
319 static void
320 nick_2_cont (void *cls, int32_t success, const char *emsg)
321 {
322   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323               "Nick added : %s\n",
324               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
325
326   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
327
328   GNUNET_asprintf(&s_name_1, "dummy1");
329   s_rd_1 = create_record(1);
330   GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_1,
331                                   1, s_rd_1,
332                                   &put_cont, NULL);
333
334   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335               "Created record 2 \n");
336   GNUNET_asprintf(&s_name_2, "dummy2");
337   s_rd_2 = create_record(1);
338   GNUNET_NAMESTORE_records_store (nsh, privkey, s_name_2,
339                                   1, s_rd_2, &put_cont, NULL);
340
341   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342               "Created record 3\n");
343
344   /* name in different zone */
345   GNUNET_asprintf(&s_name_3, "dummy3");
346   s_rd_3 = create_record(1);
347   GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
348                                   1, s_rd_3,
349                                   &put_cont, NULL);
350 }
351
352
353 static void
354 nick_1_cont (void *cls, int32_t success, const char *emsg)
355 {
356   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357               "Nick 1 added : %s\n",
358               (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
359
360   nsqe = GNUNET_NAMESTORE_set_nick (nsh, privkey2, ZONE_NICK_2, &nick_2_cont, &privkey2);
361   if (NULL == nsqe)
362   {
363     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
364               _("Namestore cannot store no block\n"));
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   directory = NULL;
435   GNUNET_assert (GNUNET_OK ==
436                  GNUNET_CONFIGURATION_get_value_string (cfg,
437                                                         "PATHS",
438                                                         "GNUNET_TEST_HOME",
439                                                         &directory));
440   GNUNET_DISK_directory_remove (directory);
441
442   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
443   nsh = GNUNET_NAMESTORE_connect (cfg);
444   GNUNET_break (NULL != nsh);
445
446   /* first, iterate over empty namestore */
447   zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
448                                              NULL, &empty_zone_proc, nsh);
449   if (NULL == zi)
450   {
451     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
452                 "Failed to create zone iterator\n");
453     GNUNET_break (0);
454     GNUNET_SCHEDULER_cancel (endbadly_task);
455     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
456   }
457 }
458
459
460 int
461 main (int argc, char *argv[])
462 {
463   res = 1;
464   if (0 !=
465       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
466                                "test_namestore_api.conf",
467                                &run,
468                                NULL))
469   {
470     res = 1;
471   }
472   if (NULL != directory)
473   {
474       GNUNET_DISK_directory_remove (directory);
475       GNUNET_free (directory);
476   }
477   return res;
478 }
479
480
481 /* end of test_namestore_api_zone_iteration.c */