aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/test_namestore_api_monitoring.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-07-09 14:04:00 +0000
committerChristian Grothoff <christian@grothoff.org>2013-07-09 14:04:00 +0000
commit6b0a149769a7196d687d91020eb0c83c7abd14c1 (patch)
treef0627ef99ca62d19374cf67fe4c64cb14ee2bbb4 /src/namestore/test_namestore_api_monitoring.c
parent3102825b3ba6e097f1a6f370a6ab4a6291cc9fcf (diff)
downloadgnunet-6b0a149769a7196d687d91020eb0c83c7abd14c1.tar.gz
gnunet-6b0a149769a7196d687d91020eb0c83c7abd14c1.zip
-testcase for monitoring, fails right now, as a good test should...
Diffstat (limited to 'src/namestore/test_namestore_api_monitoring.c')
-rw-r--r--src/namestore/test_namestore_api_monitoring.c350
1 files changed, 350 insertions, 0 deletions
diff --git a/src/namestore/test_namestore_api_monitoring.c b/src/namestore/test_namestore_api_monitoring.c
new file mode 100644
index 000000000..a55e8b525
--- /dev/null
+++ b/src/namestore/test_namestore_api_monitoring.c
@@ -0,0 +1,350 @@
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
34static struct GNUNET_NAMESTORE_Handle * nsh;
35
36static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
37
38static GNUNET_SCHEDULER_TaskIdentifier stopiteration_task;
39
40static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
41
42static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pubkey;
43
44static struct GNUNET_HashCode zone;
45
46static struct GNUNET_CRYPTO_EccPrivateKey * privkey2;
47
48static struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded pubkey2;
49
50static struct GNUNET_HashCode zone2;
51
52static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
53
54static int res;
55
56static int returned_records;
57
58static struct GNUNET_CRYPTO_EccSignature *sig_1;
59
60static char * s_name_1;
61
62static struct GNUNET_NAMESTORE_RecordData *s_rd_1;
63
64static struct GNUNET_CRYPTO_EccSignature *sig_2;
65
66static char * s_name_2;
67
68static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
69
70static struct GNUNET_CRYPTO_EccSignature *sig_3;
71
72static char * s_name_3;
73
74static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
75
76
77static void
78do_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 */
137static void
138endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139{
140 do_shutdown ();
141 res = 1;
142}
143
144
145static void
146end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147{
148 do_shutdown ();
149 res = 0;
150}
151
152
153static void
154zone_proc (void *cls,
155 const struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded *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 fprintf (stderr,
163 "Monitor cb for %s\n",
164 name);
165 if (GNUNET_OK != GNUNET_NAMESTORE_verify_signature (zone_key,
166 expire, name,
167 rd_count, rd,
168 signature))
169 {
170 struct GNUNET_HashCode zone_key_hash;
171
172 GNUNET_CRYPTO_hash (zone_key,
173 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
174 &zone_key_hash);
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
176 "Verifying signature for `%s' in zone `%s' with %u records and expiration %llu failed\n",
177 name,
178 GNUNET_h2s(&zone_key_hash),
179 rd_count,
180 expire.abs_value);
181 GNUNET_break (0);
182 }
183
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
185 "Comparing results name %s\n",
186 name);
187 if (0 == strcmp (name, s_name_1))
188 {
189 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
190 GNUNET_break (0);
191 }
192 if (0 == strcmp (name, s_name_2))
193 {
194 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
195 GNUNET_break (0);
196 }
197 if (0 == strcmp (name, s_name_3))
198 {
199 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_3))
200 GNUNET_break (0);
201 }
202 if (3 == ++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 GNUNET_SCHEDULER_add_now (&end, NULL);
210 }
211}
212
213
214static void
215put_cont (void *cls, int32_t success, const char *emsg)
216{
217 static int c = 0;
218
219 if (success == GNUNET_OK)
220 {
221 c++;
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u\n", c);
223 }
224 else
225 {
226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
227 GNUNET_break (0);
228 GNUNET_SCHEDULER_cancel (endbadly_task);
229 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
230 }
231}
232
233
234static struct GNUNET_NAMESTORE_RecordData *
235create_record (unsigned int count)
236{
237 unsigned int c;
238 struct GNUNET_NAMESTORE_RecordData * rd;
239
240 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
241 for (c = 0; c < count; c++)
242 {
243 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value;
244 rd[c].record_type = 1111;
245 rd[c].data_size = 50;
246 rd[c].data = GNUNET_malloc(50);
247 memset ((char *) rd[c].data, 'a', 50);
248 }
249 return rd;
250}
251
252
253static void
254run (void *cls,
255 const struct GNUNET_CONFIGURATION_Handle *cfg,
256 struct GNUNET_TESTING_Peer *peer)
257{
258 char *hostkey_file;
259 struct GNUNET_TIME_Absolute et;
260
261 res = 1;
262 returned_records = 0;
263 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
264 NULL,
265 &zone_proc,
266 NULL,
267 NULL);
268 if (NULL == zm)
269 {
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone monitor\n");
271 GNUNET_break (0);
272 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
273 return;
274 }
275 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
276 nsh = GNUNET_NAMESTORE_connect (cfg);
277 GNUNET_break (NULL != nsh);
278
279 GNUNET_asprintf(&hostkey_file,
280 "zonefiles%s%s",
281 DIR_SEPARATOR_STR,
282 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Using zonekey file `%s' \n", hostkey_file);
285 privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
286 GNUNET_free (hostkey_file);
287 GNUNET_assert (privkey != NULL);
288 GNUNET_CRYPTO_ecc_key_get_public(privkey, &pubkey);
289 GNUNET_CRYPTO_hash(&pubkey, sizeof (pubkey), &zone);
290
291 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",
292 DIR_SEPARATOR_STR,
293 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
294 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
295 privkey2 = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
296 GNUNET_free (hostkey_file);
297
298 GNUNET_assert (privkey2 != NULL);
299 GNUNET_CRYPTO_ecc_key_get_public(privkey2, &pubkey2);
300 GNUNET_CRYPTO_hash(&pubkey2, sizeof (pubkey), &zone2);
301
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
303
304 GNUNET_asprintf(&s_name_1, "dummy1");
305 s_rd_1 = create_record(1);
306 et.abs_value = s_rd_1->expiration_time;
307 sig_1 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_1, s_rd_1, 1);
308 GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, s_name_1,
309 1, s_rd_1,
310 &put_cont, NULL);
311
312
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
314 GNUNET_asprintf(&s_name_2, "dummy2");
315 s_rd_2 = create_record(1);
316
317 et.abs_value = s_rd_2->expiration_time;
318 sig_2 = GNUNET_NAMESTORE_create_signature(privkey, et, s_name_2, s_rd_2, 1);
319 GNUNET_NAMESTORE_record_put_by_authority (nsh, privkey, s_name_2,
320 1, s_rd_2,
321 &put_cont, NULL);
322
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
324 /* name in different zone */
325 GNUNET_asprintf(&s_name_3, "dummy3");
326 s_rd_3 = create_record(1);
327 et.abs_value = s_rd_3->expiration_time;
328 sig_3 = GNUNET_NAMESTORE_create_signature(privkey2, et, s_name_3, s_rd_3, 1);
329 GNUNET_NAMESTORE_record_put (nsh, &pubkey2, s_name_3,
330 GNUNET_TIME_UNIT_FOREVER_ABS, 1, s_rd_3, sig_3,
331 &put_cont, NULL);
332}
333
334
335int
336main (int argc, char *argv[])
337{
338 res = 1;
339 if (0 !=
340 GNUNET_TESTING_service_run ("test-namestore-api-monitoring",
341 "namestore",
342 "test_namestore_api.conf",
343 &run,
344 NULL))
345 return 1;
346 return res;
347}
348
349
350/* end of test_namestore_api_monitoring.c */