-remove trailing whitespace
[oweals/gnunet.git] / src / namestore / test_namestore_api_monitoring_existing.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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 GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
37
38 static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
39
40 static struct GNUNET_CRYPTO_EccPrivateKey * 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_NAMESTORE_RecordData *s_rd_1;
49
50 static char * s_name_2;
51
52 static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
53
54 static char * s_name_3;
55
56 static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
57
58 struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3];
59
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  * @param tc scheduler context
130  */
131 static void
132 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 {
134   do_shutdown ();
135   res = 1;
136 }
137
138
139 static void
140 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
141 {
142   do_shutdown ();
143   res = 0;
144 }
145
146
147 static void
148 zone_proc (void *cls,
149            const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
150            const char *name,
151            unsigned int rd_count,
152            const struct GNUNET_NAMESTORE_RecordData *rd)
153 {
154         static int returned_records;
155         static int fail = GNUNET_NO;
156   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157               "Comparing results name %s\n",
158               name);
159
160   if (0 != memcmp (zone_key, privkey, sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
161   {
162     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163               "Monitoring returned wrong zone key\n");
164     GNUNET_break (0);
165     GNUNET_SCHEDULER_cancel (endbadly_task);
166     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
167     return;
168   }
169
170   if (0 == strcmp (name, s_name_1))
171   {
172     if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
173     {
174       GNUNET_break (0);
175         fail = GNUNET_YES;
176     }
177   }
178   else if (0 == strcmp (name, s_name_2))
179   {
180     if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
181     {
182       GNUNET_break (0);
183         fail = GNUNET_YES;
184     }
185   }
186   else
187   {
188     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
189               "Invalid name %s\n",  name);
190         GNUNET_break (0);
191     fail = GNUNET_YES;
192   }
193
194   if (2 == ++returned_records)
195   {
196     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
197     {
198       GNUNET_SCHEDULER_cancel (endbadly_task);
199       endbadly_task = GNUNET_SCHEDULER_NO_TASK;
200     }
201     if (GNUNET_YES == fail)
202       GNUNET_SCHEDULER_add_now (&endbadly, NULL);
203     else
204         GNUNET_SCHEDULER_add_now (&end, NULL);
205   }
206
207 }
208
209
210 static void
211 put_cont (void *cls, int32_t success, 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, "Created record %u: `%s'\n", c, label);
227   }
228   else
229   {
230     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
231     GNUNET_break (0);
232     GNUNET_SCHEDULER_cancel (endbadly_task);
233     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
234   }
235
236   if (3 == c)
237   {
238     /* Start monitoring */
239     zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
240                                                 privkey,
241                                             &zone_proc,
242                                             NULL,
243                                             NULL);
244     if (NULL == zm)
245     {
246       GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone monitor\n");
247       GNUNET_break (0);
248       endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
249       return;
250     }
251   }
252 }
253
254
255 static struct GNUNET_NAMESTORE_RecordData *
256 create_record (unsigned int count)
257 {
258   unsigned int c;
259   struct GNUNET_NAMESTORE_RecordData * rd;
260
261   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
262   for (c = 0; c < count; c++)
263   {
264     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
265     rd[c].record_type = 1111;
266     rd[c].data_size = 50;
267     rd[c].data = GNUNET_malloc(50);
268     memset ((char *) rd[c].data, 'a', 50);
269   }
270   return rd;
271 }
272
273
274 static void
275 run (void *cls,
276      const struct GNUNET_CONFIGURATION_Handle *mycfg,
277      struct GNUNET_TESTING_Peer *peer)
278 {
279   char *hostkey_file;
280
281   res = 1;
282
283   GNUNET_asprintf(&hostkey_file,
284                   "zonefiles%s%s",
285                   DIR_SEPARATOR_STR,
286                   "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
287   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288               "Using zonekey file `%s' \n", hostkey_file);
289   privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
290   GNUNET_free (hostkey_file);
291   GNUNET_assert (privkey != NULL);
292
293   cfg = mycfg;
294   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
295   /* Connect to namestore */
296   nsh = GNUNET_NAMESTORE_connect (cfg);
297   if (NULL == nsh)
298   {
299     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
300     GNUNET_break (0);
301     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
302     return;
303   }
304
305   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",
306                   DIR_SEPARATOR_STR,
307                   "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
309   privkey2 = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
310   GNUNET_free (hostkey_file);
311   GNUNET_assert (privkey2 != NULL);
312
313
314   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
315   /* name in different zone */
316   GNUNET_asprintf(&s_name_3, "dummy3");
317   s_rd_3 = create_record(1);
318   GNUNET_assert (NULL != (ns_ops[2] = GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
319                 1, s_rd_3, &put_cont, s_name_3)));
320
321   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
322   GNUNET_asprintf(&s_name_1, "dummy1");
323   s_rd_1 = create_record(1);
324   GNUNET_assert (NULL != (ns_ops[0] = GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
325                 1, s_rd_1, &put_cont, s_name_1)));
326
327
328   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
329   GNUNET_asprintf(&s_name_2, "dummy2");
330   s_rd_2 = create_record(1);
331   GNUNET_assert (NULL != (ns_ops[1] = GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
332                 1, s_rd_2, &put_cont, s_name_2)));
333
334
335 }
336
337
338 int
339 main (int argc, char *argv[])
340 {
341   res = 1;
342   if (0 !=
343       GNUNET_TESTING_service_run ("test-namestore-api-monitoring",
344                                   "namestore",
345                                   "test_namestore_api.conf",
346                                   &run,
347                                   NULL))
348     return 1;
349   return res;
350 }
351
352
353 /* end of test_namestore_api_monitoring.c */