-never store NICKs anywhere but in '+', do not display nicks in gnunet-namestore
[oweals/gnunet.git] / src / namestore / test_namestore_api_zone_iteration_specific_zone.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_specific_zone.c
22  * @brief testcase for zone iteration functionality: iterate over a specific zone
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 static char *directory;
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_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   else
184   {
185         if (0 == memcmp (zone, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
186         {
187       if (0 == strcmp (label, s_name_1))
188       {
189         if (rd_count == 1)
190         {
191           if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
192           {
193             failed = GNUNET_YES;
194             GNUNET_break (0);
195           }
196         }
197         else
198         {
199           failed = GNUNET_YES;
200           GNUNET_break (0);
201         }
202       }
203       else if (0 == strcmp (label, s_name_2))
204       {
205         if (rd_count == 1)
206         {
207           if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
208           {
209             failed = GNUNET_YES;
210             GNUNET_break (0);
211           }
212         }
213         else
214         {
215           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
216                         "Received invalid record count\n");
217           failed = GNUNET_YES;
218           GNUNET_break (0);
219         }
220       }
221       else
222       {
223         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224                         "Comparing result failed: got name `%s' for first zone\n", label);
225         failed = GNUNET_YES;
226         GNUNET_break (0);
227       }
228         }
229         else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
230         {
231       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received data for not requested zone\n");
232       failed = GNUNET_YES;
233       GNUNET_break (0);
234         }
235         else
236         {
237       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Received invalid zone\n");
238       failed = GNUNET_YES;
239       GNUNET_break (0);
240         }
241   }
242
243   if (failed == GNUNET_NO)
244   {
245     returned_records ++;
246     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247                 "Telling namestore to send the next result\n");
248     GNUNET_NAMESTORE_zone_iterator_next (zi);
249   }
250   else
251   {
252     GNUNET_break (0);
253     GNUNET_SCHEDULER_add_now (&end, NULL);
254   }
255 }
256
257
258 static void
259 put_cont (void *cls, int32_t success, const char *emsg)
260 {
261   static int c = 0;
262
263   if (success == GNUNET_OK)
264   {
265     c++;
266     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u \n", c);
267   }
268   else
269   {
270     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records: `%s'\n",
271                 emsg);
272     GNUNET_break (0);
273     if (GNUNET_SCHEDULER_NO_TASK != endbadly_task)
274         GNUNET_SCHEDULER_cancel (endbadly_task);
275     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
276     return;
277   }
278
279   if (c == 3)
280   {
281     res = 1;
282     returned_records = 0;
283     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All records created, starting iteration over all zones \n");
284     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
285                                                privkey,
286                                                &zone_proc,
287                                                NULL);
288     if (zi == NULL)
289     {
290       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
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 }
299
300
301 static struct GNUNET_GNSRECORD_Data *
302 create_record (unsigned int count)
303 {
304   unsigned int c;
305   struct GNUNET_GNSRECORD_Data * rd;
306
307   rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
308   for (c = 0; c < count; c++)
309   {
310     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
311     rd[c].record_type = 1111;
312     rd[c].data_size = 50;
313     rd[c].data = GNUNET_malloc(50);
314     rd[c].flags = 0;
315     memset ((char *) rd[c].data, 'a', 50);
316   }
317   return rd;
318 }
319
320
321 /**
322  * Callback called from the zone iterator when we iterate over
323  * the empty zone.  Check that we got no records and then
324  * start the actual tests by filling the zone.
325  */
326 static void
327 empty_zone_proc (void *cls,
328                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
329                  const char *label,
330                  unsigned int rd_count,
331                  const struct GNUNET_GNSRECORD_Data *rd)
332 {
333   char *hostkey_file;
334
335   GNUNET_assert (nsh == cls);
336   if (NULL != zone)
337     {
338       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
339                   _("Expected empty zone but received zone private key\n"));
340       GNUNET_break (0);
341       if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
342         GNUNET_SCHEDULER_cancel (endbadly_task);
343       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
344       return;
345     }
346   if ((NULL != label) || (NULL != rd) || (0 != rd_count))
347     {
348       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
349                   _("Expected no zone content but received data\n"));
350       GNUNET_break (0);
351       if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
352         GNUNET_SCHEDULER_cancel (endbadly_task);
353       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
354       return;
355     }
356
357
358   zi = NULL;
359   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
360       "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
361   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
362   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
363   GNUNET_free (hostkey_file);
364   GNUNET_assert (privkey != NULL);
365
366   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",DIR_SEPARATOR_STR,
367       "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
368   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
369   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
370   GNUNET_free (hostkey_file);
371   GNUNET_assert (privkey2 != NULL);
372
373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
374
375   GNUNET_asprintf(&s_name_1, "dummy1");
376   s_rd_1 = create_record(1);
377   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
378                 1, s_rd_1, &put_cont, NULL);
379
380   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
381   GNUNET_asprintf(&s_name_2, "dummy2");
382   s_rd_2 = create_record(1);
383   GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
384                 1, s_rd_2, &put_cont, NULL);
385
386   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
387
388   /* name in different zone */
389   GNUNET_asprintf(&s_name_3, "dummy3");
390   s_rd_3 = create_record(1);
391   GNUNET_NAMESTORE_records_store(nsh, privkey2, s_name_3,
392                 1, s_rd_3, &put_cont, NULL);
393 }
394
395
396 static void
397 run (void *cls,
398      const struct GNUNET_CONFIGURATION_Handle *cfg,
399      struct GNUNET_TESTING_Peer *peer)
400 {
401   directory = NULL;
402   GNUNET_CONFIGURATION_get_value_string(cfg, "PATHS", "GNUNET_TEST_HOME", &directory);
403   GNUNET_DISK_directory_remove (directory);
404
405   endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, &endbadly, NULL);
406   nsh = GNUNET_NAMESTORE_connect (cfg);
407   GNUNET_break (NULL != nsh);
408   /* first, iterate over empty namestore */
409   zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
410                                               NULL,
411                                               &empty_zone_proc, nsh);
412   if (NULL == zi)
413   {
414     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone iterator\n");
415     GNUNET_break (0);
416     GNUNET_SCHEDULER_cancel (endbadly_task);
417     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
418   }
419 }
420
421
422 int
423 main (int argc, char *argv[])
424 {
425   res = 1;
426   if (0 !=
427       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
428                                "test_namestore_api.conf",
429                                &run,
430                                NULL))
431   {
432     res = 1;
433   }
434   if (NULL != directory)
435   {
436       GNUNET_DISK_directory_remove (directory);
437       GNUNET_free (directory);
438   }
439   return res;
440 }
441
442 /* end of test_namestore_api_zone_iteration_specific_zone.c */