fixed crash: wrong cls in api
[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 iteration functionality: iterate of a specific zone
23  */
24 #include "platform.h"
25 #include "gnunet_common.h"
26 #include "gnunet_namestore_service.h"
27 #include "gnunet_testing_lib.h"
28 #include "namestore.h"
29
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
32
33
34 static struct GNUNET_NAMESTORE_Handle * nsh;
35
36 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
37
38 static GNUNET_SCHEDULER_TaskIdentifier stopiteration_task;
39
40 static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
41
42 static struct GNUNET_CRYPTO_EccPublicKey pubkey;
43
44 static struct GNUNET_HashCode zone;
45
46 static struct GNUNET_CRYPTO_EccPrivateKey * privkey2;
47
48 static struct GNUNET_CRYPTO_EccPublicKey pubkey2;
49
50 static struct GNUNET_HashCode zone2;
51
52 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
53
54 static int res;
55
56 static int returned_records;
57
58 static struct GNUNET_CRYPTO_EccSignature *sig_1;
59
60 static char * s_name_1;
61
62 static struct GNUNET_NAMESTORE_RecordData *s_rd_1;
63
64 static struct GNUNET_CRYPTO_EccSignature *sig_2;
65
66 static char * s_name_2;
67
68 static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
69
70 static struct GNUNET_CRYPTO_EccSignature *sig_3;
71
72 static char * s_name_3;
73
74 static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
75
76
77 static void
78 do_shutdown ()
79 {
80   if (stopiteration_task != GNUNET_SCHEDULER_NO_TASK)
81   {
82     GNUNET_SCHEDULER_cancel (stopiteration_task);
83     stopiteration_task = GNUNET_SCHEDULER_NO_TASK;
84   }
85   if (NULL != zm)
86   {
87     GNUNET_NAMESTORE_zone_monitor_stop (zm);
88     zm = NULL;
89   }
90   if (NULL != nsh)
91   {
92     GNUNET_NAMESTORE_disconnect (nsh);
93     nsh = NULL;
94   }
95   GNUNET_free_non_null(sig_1);
96   GNUNET_free_non_null(sig_2);
97   GNUNET_free_non_null(sig_3);
98   GNUNET_free_non_null(s_name_1);
99   GNUNET_free_non_null(s_name_2);
100   GNUNET_free_non_null(s_name_3);
101
102   if (s_rd_1 != NULL)
103   {
104     GNUNET_free ((void *)s_rd_1->data);
105     GNUNET_free (s_rd_1);
106   }
107   if (s_rd_2 != NULL)
108   {
109     GNUNET_free ((void *)s_rd_2->data);
110     GNUNET_free (s_rd_2);
111   }
112   if (s_rd_3 != NULL)
113   {
114     GNUNET_free ((void *)s_rd_3->data);
115     GNUNET_free (s_rd_3);
116   }
117
118   if (NULL != privkey)
119   {
120     GNUNET_CRYPTO_ecc_key_free (privkey);
121     privkey = NULL;
122   }
123   if (NULL != privkey2)
124   {
125     GNUNET_CRYPTO_ecc_key_free (privkey2);
126     privkey2 = NULL;
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_EccPublicKey *zone_key,
156            struct GNUNET_TIME_Absolute expire,
157            const char *name,
158            unsigned int rd_count,
159            const struct GNUNET_NAMESTORE_RecordData *rd,
160            const struct GNUNET_CRYPTO_EccSignature *signature)
161 {
162   GNUNET_break (NULL == signature);
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164               "Comparing results name %s\n", 
165               name);
166   if (0 == strcmp (name, s_name_1))
167   {
168     if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
169       GNUNET_break (0);
170   }
171   if (0 == strcmp (name, s_name_2))
172   {
173     if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
174       GNUNET_break (0);
175   }
176   if (0 == strcmp (name, s_name_3))
177   {
178     if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_3))
179       GNUNET_break (0);
180   }
181   if (3 == ++returned_records)
182   {  
183     if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
184     {
185       GNUNET_SCHEDULER_cancel (endbadly_task);
186       endbadly_task = GNUNET_SCHEDULER_NO_TASK;
187     }    
188     GNUNET_SCHEDULER_add_now (&end, NULL);
189   }
190 }
191
192
193 static void
194 put_cont (void *cls, int32_t success, const char *emsg)
195 {
196   static int c = 0;
197
198   if (success == GNUNET_OK)
199   {
200     c++;
201     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u\n", c);
202   }
203   else
204   {
205     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
206     GNUNET_break (0);
207     GNUNET_SCHEDULER_cancel (endbadly_task);
208     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
209   }
210 }
211
212
213 static struct GNUNET_NAMESTORE_RecordData *
214 create_record (unsigned int count)
215 {
216   unsigned int c;
217   struct GNUNET_NAMESTORE_RecordData * rd;
218
219   rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
220   for (c = 0; c < count; c++)
221   {
222     rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
223     rd[c].record_type = 1111;
224     rd[c].data_size = 50;
225     rd[c].data = GNUNET_malloc(50);
226     memset ((char *) rd[c].data, 'a', 50);
227   }
228   return rd;
229 }
230
231
232 static void
233 run (void *cls, 
234      const struct GNUNET_CONFIGURATION_Handle *cfg,
235      struct GNUNET_TESTING_Peer *peer)
236 {
237   char *hostkey_file;
238   struct GNUNET_TIME_Absolute et;
239
240   res = 1;
241   returned_records = 0;
242   zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
243                                             NULL,
244                                             &zone_proc,
245                                             NULL,
246                                             NULL);
247   if (NULL == zm)
248   {
249     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone monitor\n");
250     GNUNET_break (0);
251     endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
252     return;
253   }
254   endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
255   nsh = GNUNET_NAMESTORE_connect (cfg);
256   GNUNET_break (NULL != nsh);
257
258   GNUNET_asprintf(&hostkey_file,
259                   "zonefiles%s%s",
260                   DIR_SEPARATOR_STR,
261                   "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
262   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
263               "Using zonekey file `%s' \n", hostkey_file);
264   privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
265   GNUNET_free (hostkey_file);
266   GNUNET_assert (privkey != NULL);
267   GNUNET_CRYPTO_ecc_key_get_public(privkey, &pubkey);
268   GNUNET_CRYPTO_hash(&pubkey, sizeof (pubkey), &zone);
269
270   GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",
271                   DIR_SEPARATOR_STR,
272                   "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
273   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
274   privkey2 = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
275   GNUNET_free (hostkey_file);
276
277   GNUNET_assert (privkey2 != NULL);
278   GNUNET_CRYPTO_ecc_key_get_public(privkey2, &pubkey2);
279   GNUNET_CRYPTO_hash(&pubkey2, sizeof (pubkey), &zone2);
280
281   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
282
283   GNUNET_asprintf(&s_name_1, "dummy1");
284   s_rd_1 = create_record(1);
285   et.abs_value_us = s_rd_1->expiration_time;
286   sig_1 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_1, s_rd_1, 1);
287   GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, s_name_1, 
288                                             1, s_rd_1, 
289                                             &put_cont, NULL);
290
291
292   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
293   GNUNET_asprintf(&s_name_2, "dummy2");
294   s_rd_2 = create_record(1);
295
296   et.abs_value_us = s_rd_2->expiration_time;
297   sig_2 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_2, s_rd_2, 1);
298   GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, s_name_2,
299                                             1, s_rd_2, 
300                                             &put_cont, NULL);
301
302   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
303   /* name in different zone */
304   GNUNET_asprintf(&s_name_3, "dummy3");
305   s_rd_3 = create_record(1);
306   et.abs_value_us = s_rd_3->expiration_time;
307   sig_3 = GNUNET_NAMESTORE_create_signature(privkey2, et, s_name_3, s_rd_3, 1);
308   GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3, 
309                                GNUNET_TIME_UNIT_FOREVER_ABS, 1, s_rd_3, sig_3, 
310                                &put_cont, NULL);
311 }
312
313
314 int
315 main (int argc, char *argv[])
316 {
317   res = 1;
318   if (0 != 
319       GNUNET_TESTING_service_run ("test-namestore-api-monitoring",
320                                   "namestore",
321                                   "test_namestore_api.conf",
322                                   &run,
323                                   NULL))
324     return 1;
325   return res;
326 }
327
328
329 /* end of test_namestore_api_monitoring.c */