reduce loop counters to more practical levels
[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
61 /**
62  * Handle timeout.
63  *
64  * @param cls handle to use to re-connect.
65  */
66 static void
67 endbadly (void *cls)
68 {
69   endbadly_task = NULL;
70   GNUNET_SCHEDULER_shutdown ();
71   res = 1;
72 }
73
74
75 static void
76 end (void *cls)
77 {
78   if (NULL != zi)
79   {
80     GNUNET_NAMESTORE_zone_iteration_stop (zi);
81     zi = NULL;
82   }
83   if (NULL != endbadly_task)
84   {
85     GNUNET_SCHEDULER_cancel (endbadly_task);
86     endbadly_task = NULL;
87   }
88   if (NULL != privkey)
89   {
90     GNUNET_free (privkey);
91     privkey = NULL;
92   }
93   if (NULL != privkey2)
94   {
95     GNUNET_free (privkey2);
96     privkey2 = NULL;
97   }
98   GNUNET_free (s_name_1);
99   GNUNET_free (s_name_2);
100   GNUNET_free (s_name_3);
101   if (s_rd_1 != NULL)
102   {
103     GNUNET_free ((void *)s_rd_1->data);
104     GNUNET_free (s_rd_1);
105   }
106   if (s_rd_2 != NULL)
107   {
108     GNUNET_free ((void *)s_rd_2->data);
109     GNUNET_free (s_rd_2);
110   }
111   if (s_rd_3 != NULL)
112   {
113     GNUNET_free ((void *)s_rd_3->data);
114     GNUNET_free (s_rd_3);
115   }
116   if (nsh != NULL)
117   {
118     GNUNET_NAMESTORE_disconnect (nsh);
119     nsh = NULL;
120   }
121 }
122
123
124 static void
125 fail_cb (void *cls)
126 {
127   GNUNET_assert (0);
128   zi = NULL;
129 }
130
131
132 static void
133 zone_proc (void *cls,
134            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
135            const char *label,
136            unsigned int rd_count,
137            const struct GNUNET_GNSRECORD_Data *rd)
138 {
139   int failed = GNUNET_NO;
140
141   GNUNET_assert (NULL != zone);
142   if (0 == memcmp (zone,
143                    privkey,
144                    sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
145   {
146     if (0 == strcmp (label, s_name_1))
147     {
148       if (rd_count == 1)
149       {
150         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
151         {
152           failed = GNUNET_YES;
153           GNUNET_break (0);
154         }
155       }
156       else
157       {
158         failed = GNUNET_YES;
159         GNUNET_break (0);
160       }
161     }
162     else if (0 == strcmp (label, s_name_2))
163     {
164       if (rd_count == 1)
165       {
166         if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
167         {
168           failed = GNUNET_YES;
169           GNUNET_break (0);
170         }
171       }
172       else
173       {
174         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175                     "Received invalid record count\n");
176         failed = GNUNET_YES;
177         GNUNET_break (0);
178       }
179     }
180     else
181     {
182       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
183                   "Comparing result failed: got name `%s' for first zone\n",
184                   label);
185       failed = GNUNET_YES;
186       GNUNET_break (0);
187     }
188   }
189   else if (0 == memcmp (zone, privkey2, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
190   {
191     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192                 "Received data for not requested zone\n");
193     failed = GNUNET_YES;
194     GNUNET_break (0);
195   }
196   else
197   {
198     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
199                 "Received invalid zone\n");
200     failed = GNUNET_YES;
201     GNUNET_break (0);
202   }
203   if (failed == GNUNET_NO)
204   {
205     returned_records ++;
206     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
207                 "Telling namestore to send the next result\n");
208     GNUNET_NAMESTORE_zone_iterator_next (zi,
209                                          1);
210   }
211   else
212   {
213     GNUNET_break (0);
214     res = 2;
215     GNUNET_SCHEDULER_shutdown ();
216   }
217 }
218
219
220 static void
221 zone_proc_end (void *cls)
222 {
223   zi = NULL;
224   GNUNET_break (2 == returned_records);
225   if (2 == returned_records)
226   {
227     res = 0; /* Last iteraterator callback, we are done */
228   }
229   else
230   {
231     res = 1;
232   }
233
234   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235               "Received last result, iteration done after receing %u results\n",
236               returned_records);
237   GNUNET_SCHEDULER_shutdown ();
238 }
239
240
241 static void
242 put_cont (void *cls,
243           int32_t success,
244           const char *emsg)
245 {
246   static int c = 0;
247
248   if (success == GNUNET_OK)
249   {
250     c++;
251     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252                 "Created record %u \n", c);
253   }
254   else
255   {
256     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
257                 "Failed to created records: `%s'\n",
258                 emsg);
259     GNUNET_break (0);
260     res = 2;
261     GNUNET_SCHEDULER_shutdown ();
262     return;
263   }
264
265   if (c == 3)
266   {
267     res = 1;
268     returned_records = 0;
269     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
270                 "All records created, starting iteration over all zones \n");
271     zi = GNUNET_NAMESTORE_zone_iteration_start(nsh,
272                                                privkey,
273                                                &fail_cb,
274                                                NULL,
275                                                &zone_proc,
276                                                NULL,
277                                                &zone_proc_end,
278                                                NULL);
279     if (zi == NULL)
280     {
281       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282                   "Failed to create zone iterator\n");
283       GNUNET_break (0);
284       res = 2;
285       GNUNET_SCHEDULER_shutdown ();
286       return;
287     }
288   }
289 }
290
291
292 static struct GNUNET_GNSRECORD_Data *
293 create_record (unsigned int count)
294 {
295   struct GNUNET_GNSRECORD_Data *rd;
296
297   rd = GNUNET_new_array (count,
298                          struct GNUNET_GNSRECORD_Data);
299   for (unsigned int c = 0; c < count; c++)
300   {
301     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
302     rd[c].record_type = 1111;
303     rd[c].data_size = 50;
304     rd[c].data = GNUNET_malloc(50);
305     rd[c].flags = 0;
306     memset ((char *) rd[c].data, 'a', 50);
307   }
308   return rd;
309 }
310
311
312 /**
313  * Callback called from the zone iterator when we iterate over
314  * the empty zone.  Check that we got no records and then
315  * start the actual tests by filling the zone.
316  */
317 static void
318 empty_zone_proc (void *cls,
319                  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
320                  const char *label,
321                  unsigned int rd_count,
322                  const struct GNUNET_GNSRECORD_Data *rd)
323 {
324   GNUNET_assert (nsh == cls);
325   if (NULL != zone)
326   {
327     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
328                 _("Expected empty zone but received zone private key\n"));
329     GNUNET_break (0);
330     res = 2;
331     GNUNET_SCHEDULER_shutdown ();
332     return;
333   }
334   if ((NULL != label) || (NULL != rd) || (0 != rd_count))
335   {
336     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
337                 _("Expected no zone content but received data\n"));
338     GNUNET_break (0);
339     res = 2;
340     GNUNET_SCHEDULER_shutdown ();
341     return;
342   }
343   GNUNET_assert (0);
344 }
345
346
347 static void
348 empty_zone_proc_end (void *cls)
349 {
350   zi = NULL;
351   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
352   GNUNET_assert (privkey != NULL);
353   privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
354   GNUNET_assert (privkey2 != NULL);
355
356   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
357               "Created record 1\n");
358   GNUNET_asprintf (&s_name_1,
359                    "dummy1");
360   s_rd_1 = create_record (1);
361   GNUNET_NAMESTORE_records_store (nsh,
362                                   privkey,
363                                   s_name_1,
364                                   1,
365                                   s_rd_1,
366                                   &put_cont,
367                                   NULL);
368
369   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
370               "Created record 2 \n");
371   GNUNET_asprintf (&s_name_2,
372                    "dummy2");
373   s_rd_2 = create_record (1);
374   GNUNET_NAMESTORE_records_store (nsh,
375                                   privkey,
376                                   s_name_2,
377                                   1,
378                                   s_rd_2,
379                                   &put_cont,
380                                   NULL);
381
382   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
383               "Created record 3\n");
384
385   /* name in different zone */
386   GNUNET_asprintf (&s_name_3,
387                    "dummy3");
388   s_rd_3 = create_record (1);
389   GNUNET_NAMESTORE_records_store (nsh,
390                                   privkey2,
391                                   s_name_3,
392                                   1, s_rd_3,
393                                   &put_cont,
394                                   NULL);
395 }
396
397
398 static void
399 run (void *cls,
400      const struct GNUNET_CONFIGURATION_Handle *cfg,
401      struct GNUNET_TESTING_Peer *peer)
402 {
403   GNUNET_SCHEDULER_add_shutdown (&end,
404                                  NULL);
405   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
406                                                 &endbadly,
407                                                 NULL);
408   nsh = GNUNET_NAMESTORE_connect (cfg);
409   GNUNET_break (NULL != nsh);
410   /* first, iterate over empty namestore */
411   zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
412                                               NULL,
413                                               &fail_cb,
414                                               NULL,
415                                               &empty_zone_proc,
416                                               nsh,
417                                               &empty_zone_proc_end,
418                                               nsh);
419   if (NULL == zi)
420   {
421     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
422                 "Failed to create zone iterator\n");
423     GNUNET_break (0);
424     GNUNET_SCHEDULER_shutdown ();
425   }
426 }
427
428
429 int
430 main (int argc, char *argv[])
431 {
432   const char *plugin_name;
433   char *cfg_name;
434
435   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
436   GNUNET_asprintf (&cfg_name,
437                    "test_namestore_api_%s.conf",
438                    plugin_name);
439   res = 1;
440   GNUNET_DISK_purge_cfg_dir (cfg_name,
441                              "GNUNET_TEST_HOME");
442   if (0 !=
443       GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration-specific-zone",
444                                cfg_name,
445                                &run,
446                                NULL))
447   {
448     res = 1;
449   }
450   GNUNET_DISK_purge_cfg_dir (cfg_name,
451                              "GNUNET_TEST_HOME");
452   GNUNET_free (cfg_name);
453   return res;
454 }
455
456 /* end of test_namestore_api_zone_iteration_specific_zone.c */