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