aboutsummaryrefslogtreecommitdiff
path: root/src/service/namestore/test_namestore_api_monitoring_existing.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/service/namestore/test_namestore_api_monitoring_existing.c')
-rw-r--r--src/service/namestore/test_namestore_api_monitoring_existing.c392
1 files changed, 392 insertions, 0 deletions
diff --git a/src/service/namestore/test_namestore_api_monitoring_existing.c b/src/service/namestore/test_namestore_api_monitoring_existing.c
new file mode 100644
index 000000000..4c8b99738
--- /dev/null
+++ b/src/service/namestore/test_namestore_api_monitoring_existing.c
@@ -0,0 +1,392 @@
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2013, 2018 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
20/**
21 * @file namestore/test_namestore_api_monitoring_existing.c
22 * @brief testcase for zone monitoring functionality: add records first, then monitor
23 */
24#include "platform.h"
25#include "gnunet_namestore_service.h"
26#include "gnunet_testing_lib.h"
27
28#define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
29
30
31#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
32
33static const struct GNUNET_CONFIGURATION_Handle *cfg;
34
35static struct GNUNET_NAMESTORE_Handle *nsh;
36
37static struct GNUNET_SCHEDULER_Task *endbadly_task;
38
39static struct GNUNET_CRYPTO_PrivateKey privkey;
40
41static struct GNUNET_CRYPTO_PrivateKey privkey2;
42
43static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
44
45static int res;
46
47static const char *s_name_1;
48
49static struct GNUNET_GNSRECORD_Data *s_rd_1;
50
51static const char *s_name_2;
52
53static struct GNUNET_GNSRECORD_Data *s_rd_2;
54
55static const char *s_name_3;
56
57static struct GNUNET_GNSRECORD_Data *s_rd_3;
58
59struct GNUNET_NAMESTORE_QueueEntry *ns_ops[3];
60
61
62/**
63 * Re-establish the connection to the service.
64 *
65 * @param cls handle to use to re-connect.
66 */
67static void
68endbadly (void *cls)
69{
70 endbadly_task = NULL;
71 GNUNET_break (0);
72 GNUNET_SCHEDULER_shutdown ();
73 res = 1;
74}
75
76
77static void
78end (void *cls)
79{
80 if (NULL != zm)
81 {
82 GNUNET_NAMESTORE_zone_monitor_stop (zm);
83 zm = NULL;
84 }
85 if (NULL != ns_ops[0])
86 {
87 GNUNET_NAMESTORE_cancel (ns_ops[0]);
88 ns_ops[0] = NULL;
89 }
90 if (NULL != ns_ops[1])
91 {
92 GNUNET_NAMESTORE_cancel (ns_ops[1]);
93 ns_ops[1] = NULL;
94 }
95 if (NULL != ns_ops[2])
96 {
97 GNUNET_NAMESTORE_cancel (ns_ops[2]);
98 ns_ops[2] = NULL;
99 }
100 if (NULL != endbadly_task)
101 {
102 GNUNET_SCHEDULER_cancel (endbadly_task);
103 endbadly_task = NULL;
104 }
105 if (NULL != nsh)
106 {
107 GNUNET_NAMESTORE_disconnect (nsh);
108 nsh = NULL;
109 }
110 if (NULL != s_rd_1)
111 {
112 GNUNET_free_nz ((void *) s_rd_1->data);
113 GNUNET_free (s_rd_1);
114 }
115 if (NULL != s_rd_2)
116 {
117 GNUNET_free_nz ((void *) s_rd_2->data);
118 GNUNET_free (s_rd_2);
119 }
120 if (NULL != s_rd_3)
121 {
122 GNUNET_free_nz ((void *) s_rd_3->data);
123 GNUNET_free (s_rd_3);
124 }
125}
126
127
128static void
129zone_proc (void *cls,
130 const struct GNUNET_CRYPTO_PrivateKey *zone_key,
131 const char *name,
132 unsigned int rd_count,
133 const struct GNUNET_GNSRECORD_Data *rd)
134{
135 static int returned_records;
136 static int fail = GNUNET_NO;
137
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139 "Comparing results name %s\n",
140 name);
141 if (0 != GNUNET_memcmp (zone_key,
142 &privkey))
143 {
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "Monitoring returned wrong zone key\n");
146 GNUNET_break (0);
147 GNUNET_SCHEDULER_shutdown ();
148 return;
149 }
150
151 if (0 == strcmp (name,
152 s_name_1))
153 {
154 if (GNUNET_YES !=
155 GNUNET_GNSRECORD_records_cmp (rd,
156 s_rd_1))
157 {
158 GNUNET_break (0);
159 fail = GNUNET_YES;
160 }
161 }
162 else if (0 == strcmp (name,
163 s_name_2))
164 {
165 if (GNUNET_YES !=
166 GNUNET_GNSRECORD_records_cmp (rd,
167 s_rd_2))
168 {
169 GNUNET_break (0);
170 fail = GNUNET_YES;
171 }
172 }
173 else
174 {
175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
176 "Invalid name %s\n",
177 name);
178 GNUNET_break (0);
179 fail = GNUNET_YES;
180 }
181 GNUNET_NAMESTORE_zone_monitor_next (zm,
182 1);
183 if (2 == ++returned_records)
184 {
185 GNUNET_SCHEDULER_shutdown ();
186 if (GNUNET_YES == fail)
187 {
188 GNUNET_break (0);
189 res = 1;
190 }
191 else
192 {
193 res = 0;
194 }
195 }
196}
197
198
199static void
200fail_cb (void *cls)
201{
202 GNUNET_assert (0);
203}
204
205
206static void
207sync_cb (void *cls)
208{
209 /* do nothing */
210}
211
212
213static void
214put_cont (void *cls,
215 enum GNUNET_ErrorCode ec)
216{
217 static int c = 0;
218 const char *label = cls;
219
220 if (0 == strcmp (label,
221 s_name_1))
222 ns_ops[0] = NULL;
223 else if (0 == strcmp (label,
224 s_name_2))
225 ns_ops[1] = NULL;
226 else if (0 == strcmp (label,
227 s_name_3))
228 ns_ops[2] = NULL;
229
230 if (GNUNET_EC_NONE == ec)
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 res = 1;
244 GNUNET_SCHEDULER_shutdown ();
245 return;
246 }
247
248}
249
250
251static struct GNUNET_GNSRECORD_Data *
252create_record (unsigned int count)
253{
254 struct GNUNET_GNSRECORD_Data *rd;
255
256 rd = GNUNET_new_array (count,
257 struct GNUNET_GNSRECORD_Data);
258 for (unsigned int c = 0; c < count; c++)
259 {
260 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
261 GNUNET_TIME_UNIT_HOURS).abs_value_us;
262 rd[c].record_type = TEST_RECORD_TYPE;
263 rd[c].data_size = 50;
264 rd[c].data = GNUNET_malloc (50);
265 rd[c].flags = 0;
266 memset ((char *) rd[c].data,
267 'a',
268 50);
269 }
270 return rd;
271}
272
273
274static void
275run (void *cls,
276 const struct GNUNET_CONFIGURATION_Handle *mycfg,
277 struct GNUNET_TESTING_Peer *peer)
278{
279 res = 1;
280 privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
281 privkey2.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
282 GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key);
283 GNUNET_CRYPTO_ecdsa_key_create (&privkey2.ecdsa_key);
284
285 cfg = mycfg;
286 GNUNET_SCHEDULER_add_shutdown (&end,
287 NULL);
288 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
289 &endbadly,
290 NULL);
291 /* Connect to namestore */
292 nsh = GNUNET_NAMESTORE_connect (cfg);
293 if (NULL == nsh)
294 {
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
296 "Connect to namestore failed\n");
297 GNUNET_break (0);
298 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
299 NULL);
300 return;
301 }
302 /* Start monitoring */
303 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
304 &privkey,
305 GNUNET_YES,
306 &fail_cb,
307 NULL,
308 &zone_proc,
309 NULL,
310 &sync_cb,
311 NULL);
312 if (NULL == zm)
313 {
314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
315 "Failed to create zone monitor\n");
316 GNUNET_break (0);
317 res = 1;
318 GNUNET_SCHEDULER_shutdown ();
319 return;
320 }
321
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Created record 3\n");
324 /* name in different zone */
325 s_name_3 = "dummy3";
326 s_rd_3 = create_record (1);
327 GNUNET_assert (NULL != (ns_ops[2] =
328 GNUNET_NAMESTORE_record_set_store (nsh,
329 &privkey2,
330 s_name_3,
331 1,
332 s_rd_3,
333 &put_cont,
334 (void *) s_name_3)));
335
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Created record 1\n");
338 s_name_1 = "dummy1";
339 s_rd_1 = create_record (1);
340 GNUNET_assert (NULL != (ns_ops[0] =
341 GNUNET_NAMESTORE_record_set_store (nsh,
342 &privkey,
343 s_name_1,
344 1,
345 s_rd_1,
346 &put_cont,
347 (void *) s_name_1)));
348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
349 "Created record 2 \n");
350 s_name_2 = "dummy2";
351 s_rd_2 = create_record (1);
352 GNUNET_assert (NULL != (ns_ops[1] =
353 GNUNET_NAMESTORE_record_set_store (nsh,
354 &privkey,
355 s_name_2,
356 1,
357 s_rd_2,
358 &put_cont,
359 (void *) s_name_2)));
360}
361
362
363#include "test_common.c"
364
365
366int
367main (int argc,
368 char *argv[])
369{
370 char *plugin_name;
371 char *cfg_name;
372
373 SETUP_CFG (plugin_name, cfg_name);
374 res = 1;
375 if (0 !=
376 GNUNET_TESTING_peer_run ("test-namestore-api-monitoring-existing",
377 cfg_name,
378 &run,
379 NULL))
380 {
381 GNUNET_break (0);
382 res = 1;
383 }
384 GNUNET_DISK_purge_cfg_dir (cfg_name,
385 "GNUNET_TEST_HOME");
386 GNUNET_free (plugin_name);
387 GNUNET_free (cfg_name);
388 return res;
389}
390
391
392/* end of test_namestore_api_monitoring_existing.c */