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