aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMatthias Wachs <wachs@net.in.tum.de>2013-09-11 09:58:13 +0000
committerMatthias Wachs <wachs@net.in.tum.de>2013-09-11 09:58:13 +0000
commitd4db63e2864bf82a7765d6ad9631550bfed1a274 (patch)
tree616ac5da4a8f9dde8315a5e2062f580002c20b7d /src
parente76ee72ca139e341a5126b08de0c80f4ad514bfc (diff)
downloadgnunet-d4db63e2864bf82a7765d6ad9631550bfed1a274.tar.gz
gnunet-d4db63e2864bf82a7765d6ad9631550bfed1a274.zip
additional test for monitoring: check if existing records are returned
Diffstat (limited to 'src')
-rw-r--r--src/namestore/Makefile.am12
-rw-r--r--src/namestore/test_namestore_api_monitoring_existing.c354
2 files changed, 364 insertions, 2 deletions
diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am
index 6bad4b092..402128983 100644
--- a/src/namestore/Makefile.am
+++ b/src/namestore/Makefile.am
@@ -46,7 +46,8 @@ TESTING_TESTS = \
46 test_namestore_api_zone_iteration \ 46 test_namestore_api_zone_iteration \
47 test_namestore_api_zone_iteration_specific_zone \ 47 test_namestore_api_zone_iteration_specific_zone \
48 test_namestore_api_zone_iteration_stop \ 48 test_namestore_api_zone_iteration_stop \
49 test_namestore_api_monitoring 49 test_namestore_api_monitoring \
50 test_namestore_api_monitoring_existing
50# test_namestore_api_lookup 51# test_namestore_api_lookup
51#test_namestore_api_lookup_specific_type 52#test_namestore_api_lookup_specific_type
52#test_namestore_api_create 53#test_namestore_api_create
@@ -251,7 +252,14 @@ test_namestore_api_monitoring_LDADD = \
251 $(top_builddir)/src/testing/libgnunettesting.la \ 252 $(top_builddir)/src/testing/libgnunettesting.la \
252 $(top_builddir)/src/namestore/libgnunetnamestore.la \ 253 $(top_builddir)/src/namestore/libgnunetnamestore.la \
253 $(top_builddir)/src/util/libgnunetutil.la 254 $(top_builddir)/src/util/libgnunetutil.la
254 255
256test_namestore_api_monitoring_existing_SOURCES = \
257 test_namestore_api_monitoring_existing.c
258test_namestore_api_monitoring_existing_LDADD = \
259 $(top_builddir)/src/testing/libgnunettesting.la \
260 $(top_builddir)/src/namestore/libgnunetnamestore.la \
261 $(top_builddir)/src/util/libgnunetutil.la
262
255test_namestore_api_zone_iteration_SOURCES = \ 263test_namestore_api_zone_iteration_SOURCES = \
256 test_namestore_api_zone_iteration.c 264 test_namestore_api_zone_iteration.c
257test_namestore_api_zone_iteration_LDADD = \ 265test_namestore_api_zone_iteration_LDADD = \
diff --git a/src/namestore/test_namestore_api_monitoring_existing.c b/src/namestore/test_namestore_api_monitoring_existing.c
new file mode 100644
index 000000000..31f71d664
--- /dev/null
+++ b/src/namestore/test_namestore_api_monitoring_existing.c
@@ -0,0 +1,354 @@
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_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
33static const struct GNUNET_CONFIGURATION_Handle *cfg;
34
35static struct GNUNET_NAMESTORE_Handle * nsh;
36
37static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
38
39static struct GNUNET_CRYPTO_EccPrivateKey * privkey;
40
41static struct GNUNET_CRYPTO_EccPrivateKey * privkey2;
42
43static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
44
45static int res;
46
47static char * s_name_1;
48
49static struct GNUNET_NAMESTORE_RecordData *s_rd_1;
50
51static char * s_name_2;
52
53static struct GNUNET_NAMESTORE_RecordData *s_rd_2;
54
55static char * s_name_3;
56
57static struct GNUNET_NAMESTORE_RecordData *s_rd_3;
58
59struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3];
60
61
62static void
63do_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}
124
125
126/**
127 * Re-establish the connection to the service.
128 *
129 * @param cls handle to use to re-connect.
130 * @param tc scheduler context
131 */
132static void
133endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
134{
135 do_shutdown ();
136 res = 1;
137}
138
139
140static void
141end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
142{
143 do_shutdown ();
144 res = 0;
145}
146
147
148static void
149zone_proc (void *cls,
150 const struct GNUNET_CRYPTO_EccPrivateKey *zone_key,
151 const char *name,
152 unsigned int rd_count,
153 const struct GNUNET_NAMESTORE_RecordData *rd)
154{
155 static int returned_records;
156 static int fail = GNUNET_NO;
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Comparing results name %s\n",
159 name);
160
161 if (0 != memcmp (zone_key, privkey, sizeof (struct GNUNET_CRYPTO_EccPrivateKey)))
162 {
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "Monitoring returned wrong zone key\n");
165 GNUNET_break (0);
166 GNUNET_SCHEDULER_cancel (endbadly_task);
167 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
168 return;
169 }
170
171 if (0 == strcmp (name, s_name_1))
172 {
173 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_1))
174 {
175 GNUNET_break (0);
176 fail = GNUNET_YES;
177 }
178 }
179 else if (0 == strcmp (name, s_name_2))
180 {
181 if (GNUNET_YES != GNUNET_NAMESTORE_records_cmp(rd, s_rd_2))
182 {
183 GNUNET_break (0);
184 fail = GNUNET_YES;
185 }
186 }
187 else
188 {
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 "Invalid name %s\n", name);
191 GNUNET_break (0);
192 fail = GNUNET_YES;
193 }
194
195 if (2 == ++returned_records)
196 {
197 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
198 {
199 GNUNET_SCHEDULER_cancel (endbadly_task);
200 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
201 }
202 if (GNUNET_YES == fail)
203 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
204 else
205 GNUNET_SCHEDULER_add_now (&end, NULL);
206 }
207
208}
209
210
211static void
212put_cont (void *cls, int32_t success, const char *emsg)
213{
214 static int c = 0;
215 char *label = cls;
216
217 if (0 == strcmp (label, s_name_1))
218 ns_ops[0] = NULL;
219 else if (0 == strcmp (label, s_name_2))
220 ns_ops[1] = NULL;
221 else if (0 == strcmp (label, s_name_3))
222 ns_ops[2] = NULL;
223
224 if (success == GNUNET_OK)
225 {
226 c++;
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u: `%s'\n", c, label);
228 }
229 else
230 {
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
232 GNUNET_break (0);
233 GNUNET_SCHEDULER_cancel (endbadly_task);
234 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
235 }
236
237 if (3 == c)
238 {
239 /* Start monitoring */
240 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
241 privkey,
242 &zone_proc,
243 NULL,
244 NULL);
245 if (NULL == zm)
246 {
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone monitor\n");
248 GNUNET_break (0);
249 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
250 return;
251 }
252 }
253}
254
255
256static struct GNUNET_NAMESTORE_RecordData *
257create_record (unsigned int count)
258{
259 unsigned int c;
260 struct GNUNET_NAMESTORE_RecordData * rd;
261
262 rd = GNUNET_malloc (count * sizeof (struct GNUNET_NAMESTORE_RecordData));
263 for (c = 0; c < count; c++)
264 {
265 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
266 rd[c].record_type = 1111;
267 rd[c].data_size = 50;
268 rd[c].data = GNUNET_malloc(50);
269 memset ((char *) rd[c].data, 'a', 50);
270 }
271 return rd;
272}
273
274
275static void
276run (void *cls,
277 const struct GNUNET_CONFIGURATION_Handle *mycfg,
278 struct GNUNET_TESTING_Peer *peer)
279{
280 char *hostkey_file;
281
282 res = 1;
283
284 GNUNET_asprintf(&hostkey_file,
285 "zonefiles%s%s",
286 DIR_SEPARATOR_STR,
287 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
289 "Using zonekey file `%s' \n", hostkey_file);
290 privkey = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
291 GNUNET_free (hostkey_file);
292 GNUNET_assert (privkey != NULL);
293
294 cfg = mycfg;
295 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
296 /* Connect to namestore */
297 nsh = GNUNET_NAMESTORE_connect (cfg);
298 if (NULL == nsh)
299 {
300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
301 GNUNET_break (0);
302 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
303 return;
304 }
305
306 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",
307 DIR_SEPARATOR_STR,
308 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
310 privkey2 = GNUNET_CRYPTO_ecc_key_create_from_file(hostkey_file);
311 GNUNET_free (hostkey_file);
312 GNUNET_assert (privkey2 != NULL);
313
314
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
316 /* name in different zone */
317 GNUNET_asprintf(&s_name_3, "dummy3");
318 s_rd_3 = create_record(1);
319 GNUNET_assert (NULL != (ns_ops[2] = GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
320 1, s_rd_3, &put_cont, s_name_3)));
321
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
323 GNUNET_asprintf(&s_name_1, "dummy1");
324 s_rd_1 = create_record(1);
325 GNUNET_assert (NULL != (ns_ops[0] = GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
326 1, s_rd_1, &put_cont, s_name_1)));
327
328
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
330 GNUNET_asprintf(&s_name_2, "dummy2");
331 s_rd_2 = create_record(1);
332 GNUNET_assert (NULL != (ns_ops[1] = GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
333 1, s_rd_2, &put_cont, s_name_2)));
334
335
336}
337
338
339int
340main (int argc, char *argv[])
341{
342 res = 1;
343 if (0 !=
344 GNUNET_TESTING_service_run ("test-namestore-api-monitoring",
345 "namestore",
346 "test_namestore_api.conf",
347 &run,
348 NULL))
349 return 1;
350 return res;
351}
352
353
354/* end of test_namestore_api_monitoring.c */