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