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