Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / namestore / test_namestore_api_monitoring_existing.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_monitoring.c
22  * @brief testcase for zone monitoring functionality: add records first, then monitor
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 static const struct GNUNET_CONFIGURATION_Handle *cfg;
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_ZoneMonitor *zm;
43
44 static int res;
45
46 static char * s_name_1;
47
48 static struct GNUNET_GNSRECORD_Data *s_rd_1;
49
50 static char * s_name_2;
51
52 static struct GNUNET_GNSRECORD_Data *s_rd_2;
53
54 static char * s_name_3;
55
56 static struct GNUNET_GNSRECORD_Data *s_rd_3;
57
58 struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3];
59
60 static char *directory;
61
62
63 static void
64 do_shutdown ()
65 {
66   if (NULL != zm)
67   {
68     GNUNET_NAMESTORE_zone_monitor_stop (zm);
69     zm = NULL;
70   }
71
72   if (NULL != ns_ops[0])
73   {
74     GNUNET_NAMESTORE_cancel(ns_ops[0]);
75     ns_ops[0] = NULL;
76   }
77   if (NULL != ns_ops[1])
78   {
79     GNUNET_NAMESTORE_cancel(ns_ops[1]);
80     ns_ops[1] = NULL;
81   }
82   if (NULL != ns_ops[2])
83   {
84     GNUNET_NAMESTORE_cancel(ns_ops[2]);
85     ns_ops[2] = NULL;
86   }
87
88   if (NULL != nsh)
89   {
90     GNUNET_NAMESTORE_disconnect (nsh);
91     nsh = NULL;
92   }
93
94   GNUNET_free_non_null(s_name_1);
95   GNUNET_free_non_null(s_name_2);
96   GNUNET_free_non_null(s_name_3);
97
98   if (s_rd_1 != NULL)
99   {
100     GNUNET_free ((void *)s_rd_1->data);
101     GNUNET_free (s_rd_1);
102   }
103   if (s_rd_2 != NULL)
104   {
105     GNUNET_free ((void *)s_rd_2->data);
106     GNUNET_free (s_rd_2);
107   }
108   if (s_rd_3 != NULL)
109   {
110     GNUNET_free ((void *)s_rd_3->data);
111     GNUNET_free (s_rd_3);
112   }
113
114   if (NULL != privkey)
115   {
116     GNUNET_free (privkey);
117     privkey = NULL;
118   }
119   if (NULL != privkey2)
120   {
121     GNUNET_free (privkey2);
122     privkey2 = NULL;
123   }
124 }
125
126
127 /**
128  * Re-establish the connection to the service.
129  *
130  * @param cls handle to use to re-connect.
131  */
132 static void
133 endbadly (void *cls)
134 {
135   do_shutdown ();
136   res = 1;
137 }
138
139
140 static void
141 end (void *cls)
142 {
143   do_shutdown ();
144   res = 0;
145 }
146
147
148 static void
149 zone_proc (void *cls,
150            const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
151            const char *name,
152            unsigned int rd_count,
153            const struct GNUNET_GNSRECORD_Data *rd)
154 {
155   static int returned_records;
156   static int fail = GNUNET_NO;
157
158   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159               "Comparing results name %s\n",
160               name);
161
162   if (0 != memcmp (zone_key,
163                    privkey,
164                    sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
165   {
166     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167               "Monitoring returned wrong zone key\n");
168     GNUNET_break (0);
169     GNUNET_SCHEDULER_cancel (endbadly_task);
170     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
171                                               NULL);
172     return;
173   }
174
175   if (0 == strcmp (name, s_name_1))
176   {
177     if (GNUNET_YES !=
178         GNUNET_GNSRECORD_records_cmp (rd,
179                                       s_rd_1))
180     {
181       GNUNET_break (0);
182         fail = GNUNET_YES;
183     }
184   }
185   else if (0 == strcmp (name, s_name_2))
186   {
187     if (GNUNET_YES !=
188         GNUNET_GNSRECORD_records_cmp (rd,
189                                       s_rd_2))
190     {
191       GNUNET_break (0);
192         fail = GNUNET_YES;
193     }
194   }
195   else
196   {
197     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
198               "Invalid name %s\n",  name);
199         GNUNET_break (0);
200     fail = GNUNET_YES;
201   }
202   GNUNET_NAMESTORE_zone_monitor_next (zm,
203                                       1);
204   if (2 == ++returned_records)
205   {
206     if (endbadly_task != NULL)
207     {
208       GNUNET_SCHEDULER_cancel (endbadly_task);
209       endbadly_task = NULL;
210     }
211     if (GNUNET_YES == fail)
212       GNUNET_SCHEDULER_add_now (&endbadly,
213                                 NULL);
214     else
215       GNUNET_SCHEDULER_add_now (&end,
216                                 NULL);
217   }
218 }
219
220
221 static void
222 fail_cb (void *cls)
223 {
224   GNUNET_assert (0);
225 }
226
227
228 static void
229 sync_cb (void *cls)
230 {
231   /* do nothing */
232 }
233
234
235 static void
236 put_cont (void *cls,
237           int32_t success,
238           const char *emsg)
239 {
240   static int c = 0;
241   char *label = cls;
242
243   if (0 == strcmp (label, s_name_1))
244         ns_ops[0] = NULL;
245   else if (0 == strcmp (label, s_name_2))
246         ns_ops[1] = NULL;
247   else if (0 == strcmp (label, s_name_3))
248         ns_ops[2] = NULL;
249
250   if (success == GNUNET_OK)
251   {
252     c++;
253     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254                 "Created record %u: `%s'\n",
255                 c,
256                 label);
257   }
258   else
259   {
260     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
261                 "Failed to created records\n");
262     GNUNET_break (0);
263     GNUNET_SCHEDULER_cancel (endbadly_task);
264     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
265   }
266
267   if (3 == c)
268   {
269     /* Start monitoring */
270     zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
271                                               privkey,
272                                               GNUNET_YES,
273                                               &fail_cb,
274                                               NULL,
275                                               &zone_proc,
276                                               NULL,
277                                               &sync_cb,
278                                               NULL);
279     if (NULL == zm)
280     {
281       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
282                   "Failed to create zone monitor\n");
283       GNUNET_break (0);
284       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
285                                                 NULL);
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,
307             'a',
308             50);
309   }
310   return rd;
311 }
312
313
314 static void
315 run (void *cls,
316      const struct GNUNET_CONFIGURATION_Handle *mycfg,
317      struct GNUNET_TESTING_Peer *peer)
318 {
319   char *hostkey_file;
320
321   directory = NULL;
322   GNUNET_assert (GNUNET_OK ==
323                  GNUNET_CONFIGURATION_get_value_string (mycfg,
324                                                         "PATHS",
325                                                         "GNUNET_TEST_HOME",
326                                                         &directory));
327   GNUNET_DISK_directory_remove (directory);
328
329   res = 1;
330
331   GNUNET_asprintf(&hostkey_file,
332                   "zonefiles%s%s",
333                   DIR_SEPARATOR_STR,
334                   "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
335   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336               "Using zonekey file `%s' \n", hostkey_file);
337   privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
338   GNUNET_free (hostkey_file);
339   GNUNET_assert (privkey != NULL);
340
341   cfg = mycfg;
342   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
343   /* Connect to namestore */
344   nsh = GNUNET_NAMESTORE_connect (cfg);
345   if (NULL == nsh)
346   {
347     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
348     GNUNET_break (0);
349     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
350     return;
351   }
352
353   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",
354                   DIR_SEPARATOR_STR,
355                   "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
356   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
357   privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
358   GNUNET_free (hostkey_file);
359   GNUNET_assert (privkey2 != NULL);
360
361
362   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
363   /* name in different zone */
364   GNUNET_asprintf(&s_name_3, "dummy3");
365   s_rd_3 = create_record(1);
366   GNUNET_assert (NULL != (ns_ops[2] = GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
367                 1, s_rd_3, &put_cont, s_name_3)));
368
369   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
370   GNUNET_asprintf(&s_name_1, "dummy1");
371   s_rd_1 = create_record(1);
372   GNUNET_assert (NULL != (ns_ops[0] = GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
373                 1, s_rd_1, &put_cont, s_name_1)));
374
375   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
376   GNUNET_asprintf(&s_name_2, "dummy2");
377   s_rd_2 = create_record(1);
378   GNUNET_assert (NULL != (ns_ops[1] =
379                           GNUNET_NAMESTORE_records_store (nsh,
380                                                           privkey,
381                                                           s_name_2,
382                                                           1,
383                                                           s_rd_2,
384                                                           &put_cont,
385                                                           s_name_2)));
386 }
387
388
389 int
390 main (int argc, char *argv[])
391 {
392   const char *plugin_name;
393   char *cfg_name;
394
395   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
396   GNUNET_asprintf (&cfg_name,
397                    "test_namestore_api_%s.conf",
398                    plugin_name);
399   res = 1;
400   if (0 !=
401       GNUNET_TESTING_peer_run ("test-namestore-api-monitoring-existing",
402                                cfg_name,
403                                &run,
404                                NULL))
405   {
406     res = 1;
407   }
408   GNUNET_free (cfg_name);
409   if (NULL != directory)
410   {
411       GNUNET_DISK_directory_remove (directory);
412       GNUNET_free (directory);
413   }
414   return res;
415 }
416
417
418 /* end of test_namestore_api_monitoring_existing.c */