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