NAMESTORE/JSON: fix parsing exp and flags
[oweals/gnunet.git] / src / namestore / test_namestore_api_monitoring.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2013, 2018 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
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 #include "gnunet_dnsparser_lib.h"
29
30 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
31
32
33
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
35
36
37 static struct GNUNET_NAMESTORE_Handle * nsh;
38
39 static struct GNUNET_SCHEDULER_Task * endbadly_task;
40
41 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
42
43 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
44
45 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
46
47 static int res;
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 struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3];
62
63
64 static void
65 do_shutdown ()
66 {
67   if (NULL != zm)
68   {
69     GNUNET_NAMESTORE_zone_monitor_stop (zm);
70     zm = NULL;
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   if (NULL != nsh)
88   {
89     GNUNET_NAMESTORE_disconnect (nsh);
90     nsh = NULL;
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   GNUNET_NAMESTORE_zone_monitor_next (zm,
196                                       1);
197   if (2 == ++returned_records)
198   {
199     if (endbadly_task != NULL)
200     {
201       GNUNET_SCHEDULER_cancel (endbadly_task);
202       endbadly_task = NULL;
203     }
204     if (GNUNET_YES == fail)
205       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
206     else
207         GNUNET_SCHEDULER_add_now (&end, NULL);
208   }
209 }
210
211
212 static void
213 put_cont (void *cls,
214           int32_t success,
215           const char *emsg)
216 {
217   static int c = 0;
218   char *label = cls;
219
220   if (0 == strcmp (label, s_name_1))
221     ns_ops[0] = NULL;
222   else if (0 == strcmp (label, s_name_2))
223     ns_ops[1] = NULL;
224   else if (0 == strcmp (label, s_name_3))
225     ns_ops[2] = NULL;
226
227   if (success == GNUNET_OK)
228   {
229     c++;
230     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
231                 "Created record %u: `%s'\n",
232                 c,
233                 label);
234   }
235   else
236   {
237     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
238                 "Failed to create record `%s'\n",
239                 label);
240     GNUNET_break (0);
241     GNUNET_SCHEDULER_cancel (endbadly_task);
242     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
243                                               NULL);
244   }
245 }
246
247
248 static struct GNUNET_GNSRECORD_Data *
249 create_record (unsigned int count)
250 {
251   struct GNUNET_GNSRECORD_Data *rd;
252
253   rd = GNUNET_new_array (count,
254                          struct GNUNET_GNSRECORD_Data);
255   for (unsigned int c = 0; c < count; c++)
256   {
257     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
258     rd[c].record_type = TEST_RECORD_TYPE;
259     rd[c].data_size = 50;
260     rd[c].data = GNUNET_malloc(50);
261     rd[c].flags = 0;
262     memset ((char *) rd[c].data, 'a', 50);
263   }
264   return rd;
265 }
266
267
268 static void
269 fail_cb (void *cls)
270 {
271   GNUNET_assert (0);
272 }
273
274
275 static void
276 sync_cb (void *cls)
277 {
278   /* do nothing */
279 }
280
281
282 static void
283 run (void *cls,
284      const struct GNUNET_CONFIGURATION_Handle *cfg,
285      struct GNUNET_TESTING_Peer *peer)
286 {
287   res = 1;
288   privkey = GNUNET_CRYPTO_ecdsa_key_create ();
289   GNUNET_assert (privkey != NULL);
290
291   /* Start monitoring */
292   zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
293                                             privkey,
294                                             GNUNET_YES,
295                                             &fail_cb,
296                                             NULL,
297                                             &zone_proc,
298                                             NULL,
299                                             &sync_cb,
300                                             NULL);
301   if (NULL == zm)
302   {
303     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
304                 "Failed to create zone monitor\n");
305     GNUNET_break (0);
306     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
307     return;
308   }
309
310   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
311   /* Connect to namestore */
312   nsh = GNUNET_NAMESTORE_connect (cfg);
313   if (NULL == nsh)
314   {
315     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
316     GNUNET_break (0);
317     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
318     return;
319   }
320
321   privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
322   GNUNET_assert (privkey2 != NULL);
323
324
325   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326               "Created record 3\n");
327   /* name in different zone */
328   GNUNET_asprintf(&s_name_3, "dummy3");
329   s_rd_3 = create_record(1);
330   GNUNET_assert (NULL != (ns_ops[2] =
331                           GNUNET_NAMESTORE_records_store (nsh,
332                                                           privkey2,
333                                                           s_name_3,
334                                                           1,
335                                                           s_rd_3,
336                                                           &put_cont,
337                                                           s_name_3)));
338   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339               "Created record 1\n");
340   GNUNET_asprintf(&s_name_1, "dummy1");
341   s_rd_1 = create_record(1);
342   GNUNET_assert (NULL != (ns_ops[0] =
343                           GNUNET_NAMESTORE_records_store (nsh,
344                                                           privkey,
345                                                           s_name_1,
346                                                           1,
347                                                           s_rd_1,
348                                                           &put_cont,
349                                                           s_name_1)));
350
351
352   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
353   GNUNET_asprintf(&s_name_2, "dummy2");
354   s_rd_2 = create_record(1);
355   GNUNET_assert (NULL != (ns_ops[1] =
356                           GNUNET_NAMESTORE_records_store (nsh,
357                                                           privkey,
358                                                           s_name_2,
359                                                           1,
360                                                           s_rd_2,
361                                                           &put_cont,
362                                                           s_name_2)));
363 }
364
365
366 #include "test_common.c"
367
368
369 int
370 main (int argc,
371       char *argv[])
372 {
373   const char *plugin_name;
374   char *cfg_name;
375
376   SETUP_CFG (plugin_name, cfg_name);
377   res = 1;
378   if (0 !=
379       GNUNET_TESTING_peer_run ("test-namestore-api-monitoring",
380                                cfg_name,
381                                &run,
382                                NULL))
383   {
384     res = 1;
385   }
386   GNUNET_DISK_purge_cfg_dir (cfg_name,
387                              "GNUNET_TEST_HOME");
388   GNUNET_free (cfg_name);
389   return res;
390 }
391
392
393 /* end of test_namestore_api_monitoring.c */