aboutsummaryrefslogtreecommitdiff
path: root/src/namestore/test_namestore_api_zone_iteration.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/namestore/test_namestore_api_zone_iteration.c')
-rw-r--r--src/namestore/test_namestore_api_zone_iteration.c465
1 files changed, 0 insertions, 465 deletions
diff --git a/src/namestore/test_namestore_api_zone_iteration.c b/src/namestore/test_namestore_api_zone_iteration.c
deleted file mode 100644
index 55ca901e2..000000000
--- a/src/namestore/test_namestore_api_zone_iteration.c
+++ /dev/null
@@ -1,465 +0,0 @@
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_zone_iteration.c
22 * @brief testcase for zone iteration functionality: iterate all zones
23 */
24#include "platform.h"
25#include "gnunet_namestore_service.h"
26#include "gnunet_testing_lib.h"
27#include "namestore.h"
28#include "gnunet_dnsparser_lib.h"
29
30#define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
31
32
33#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
34
35
36static struct GNUNET_NAMESTORE_Handle *nsh;
37
38static struct GNUNET_SCHEDULER_Task *endbadly_task;
39
40static struct GNUNET_IDENTITY_PrivateKey privkey;
41
42static struct GNUNET_IDENTITY_PrivateKey privkey2;
43
44static struct GNUNET_NAMESTORE_ZoneIterator *zi;
45
46static int res;
47
48static int returned_records;
49
50static char *s_name_1;
51
52static struct GNUNET_GNSRECORD_Data *s_rd_1;
53
54static char *s_name_2;
55
56static struct GNUNET_GNSRECORD_Data *s_rd_2;
57
58static char *s_name_3;
59
60static struct GNUNET_GNSRECORD_Data *s_rd_3;
61
62
63/**
64 * Re-establish the connection to the service.
65 *
66 * @param cls handle to use to re-connect.
67 * @param tc scheduler context
68 */
69static void
70endbadly (void *cls)
71{
72 endbadly_task = NULL;
73 GNUNET_SCHEDULER_shutdown ();
74 res = 1;
75}
76
77
78static void
79end (void *cls)
80{
81 if (NULL != zi)
82 {
83 GNUNET_NAMESTORE_zone_iteration_stop (zi);
84 zi = NULL;
85 }
86 if (NULL != endbadly_task)
87 {
88 GNUNET_SCHEDULER_cancel (endbadly_task);
89 endbadly_task = NULL;
90 }
91 GNUNET_free (s_name_1);
92 GNUNET_free (s_name_2);
93 GNUNET_free (s_name_3);
94 if (NULL != s_rd_1)
95 {
96 GNUNET_free_nz ((void *) s_rd_1->data);
97 GNUNET_free (s_rd_1);
98 }
99 if (NULL != s_rd_2)
100 {
101 GNUNET_free_nz ((void *) s_rd_2->data);
102 GNUNET_free (s_rd_2);
103 }
104 if (NULL != s_rd_3)
105 {
106 GNUNET_free_nz ((void *) s_rd_3->data);
107 GNUNET_free (s_rd_3);
108 }
109 if (NULL != nsh)
110 {
111 GNUNET_NAMESTORE_disconnect (nsh);
112 nsh = NULL;
113 }
114}
115
116
117static void
118zone_end (void *cls)
119{
120 GNUNET_break (3 == returned_records);
121 if (3 == returned_records)
122 {
123 res = 0; /* Last iteraterator callback, we are done */
124 zi = NULL;
125 }
126 else
127 res = 1;
128
129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130 "Received last result, iteration done after receing %u results\n",
131 returned_records);
132 GNUNET_SCHEDULER_shutdown ();
133}
134
135
136static void
137fail_cb (void *cls)
138{
139 GNUNET_assert (0);
140}
141
142
143static void
144zone_proc (void *cls,
145 const struct GNUNET_IDENTITY_PrivateKey *zone,
146 const char *label,
147 unsigned int rd_count,
148 const struct GNUNET_GNSRECORD_Data *rd)
149{
150 int failed = GNUNET_NO;
151
152 GNUNET_assert (NULL != zone);
153 if (0 == GNUNET_memcmp (zone,
154 &privkey))
155 {
156 if (0 == strcmp (label, s_name_1))
157 {
158 if (rd_count == 1)
159 {
160 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
161 {
162 failed = GNUNET_YES;
163 GNUNET_break (0);
164 }
165 }
166 else
167 {
168 failed = GNUNET_YES;
169 GNUNET_break (0);
170 }
171 }
172 else if (0 == strcmp (label, s_name_2))
173 {
174 if (rd_count == 1)
175 {
176 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_2))
177 {
178 failed = GNUNET_YES;
179 GNUNET_break (0);
180 }
181 }
182 else
183 {
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
185 "Received invalid record count\n");
186 failed = GNUNET_YES;
187 GNUNET_break (0);
188 }
189 }
190 else
191 {
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
193 "Comparing result failed: got name `%s' for first zone\n",
194 label);
195 failed = GNUNET_YES;
196 GNUNET_break (0);
197 }
198 }
199 else if (0 == GNUNET_memcmp (zone,
200 &privkey2))
201 {
202 if (0 == strcmp (label, s_name_3))
203 {
204 if (rd_count == 1)
205 {
206 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_3))
207 {
208 failed = GNUNET_YES;
209 GNUNET_break (0);
210 }
211 }
212 else
213 {
214 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
215 "Received invalid record count\n");
216 failed = GNUNET_YES;
217 GNUNET_break (0);
218 }
219 }
220 else
221 {
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Comparing result failed: got name `%s' for first zone\n",
224 label);
225 failed = GNUNET_YES;
226 GNUNET_break (0);
227 }
228 }
229 else
230 {
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
232 "Received invalid zone\n");
233 failed = GNUNET_YES;
234 GNUNET_break (0);
235 }
236
237 if (failed == GNUNET_NO)
238 {
239 returned_records++;
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Telling namestore to send the next result\n");
242 GNUNET_NAMESTORE_zone_iterator_next (zi,
243 1);
244 }
245 else
246 {
247 GNUNET_break (0);
248 GNUNET_SCHEDULER_shutdown ();
249 res = 1;
250 }
251}
252
253
254static void
255put_cont (void *cls,
256 int32_t success,
257 const char *emsg)
258{
259 static int c = 0;
260
261 if (success == GNUNET_OK)
262 {
263 c++;
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Created record %u \n",
266 c);
267 }
268 else
269 {
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271 "Failed to created records: `%s'\n",
272 emsg);
273 GNUNET_break (0);
274 GNUNET_SCHEDULER_shutdown ();
275 res = 1;
276 return;
277 }
278
279 if (c == 3)
280 {
281 res = 1;
282 returned_records = 0;
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "All records created, starting iteration over all zones \n");
285 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
286 NULL,
287 &fail_cb,
288 NULL,
289 &zone_proc,
290 NULL,
291 &zone_end,
292 NULL);
293 if (zi == NULL)
294 {
295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
296 "Failed to create zone iterator\n");
297 GNUNET_break (0);
298 GNUNET_SCHEDULER_shutdown ();
299 res = 1;
300 return;
301 }
302 }
303}
304
305
306static struct GNUNET_GNSRECORD_Data *
307create_record (unsigned int count)
308{
309 struct GNUNET_GNSRECORD_Data *rd;
310
311 rd = GNUNET_new_array (count,
312 struct GNUNET_GNSRECORD_Data);
313 for (unsigned int c = 0; c < count; c++)
314 {
315 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
316 GNUNET_TIME_UNIT_HOURS).abs_value_us;
317 rd[c].record_type = TEST_RECORD_TYPE;
318 rd[c].data_size = 50;
319 rd[c].data = GNUNET_malloc (50);
320 rd[c].flags = 0;
321 memset ((char *) rd[c].data, 'a', 50);
322 }
323 return rd;
324}
325
326
327/**
328 * Callback called from the zone iterator when we iterate over
329 * the empty zone. Check that we got no records and then
330 * start the actual tests by filling the zone.
331 */
332static void
333empty_zone_proc (void *cls,
334 const struct GNUNET_IDENTITY_PrivateKey *zone,
335 const char *label,
336 unsigned int rd_count,
337 const struct GNUNET_GNSRECORD_Data *rd)
338{
339 GNUNET_assert (nsh == cls);
340 if (NULL != zone)
341 {
342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
343 _ ("Expected empty zone but received zone private key\n"));
344 GNUNET_break (0);
345 GNUNET_SCHEDULER_shutdown ();
346 res = 1;
347 return;
348 }
349 if ((NULL != label) || (NULL != rd) || (0 != rd_count))
350 {
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 _ ("Expected no zone content but received data\n"));
353 GNUNET_break (0);
354 GNUNET_SCHEDULER_shutdown ();
355 res = 1;
356 return;
357 }
358 GNUNET_assert (0);
359}
360
361
362static void
363empty_zone_end (void *cls)
364{
365 zi = NULL;
366 privkey.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
367 privkey2.type = htonl (GNUNET_GNSRECORD_TYPE_PKEY);
368 GNUNET_CRYPTO_ecdsa_key_create (&privkey.ecdsa_key);
369 GNUNET_CRYPTO_ecdsa_key_create (&privkey2.ecdsa_key);
370
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
372
373 GNUNET_asprintf (&s_name_1, "dummy1");
374 s_rd_1 = create_record (1);
375 GNUNET_NAMESTORE_records_store (nsh,
376 &privkey,
377 s_name_1,
378 1, s_rd_1,
379 &put_cont,
380 NULL);
381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Created record 2 \n");
383 GNUNET_asprintf (&s_name_2, "dummy2");
384 s_rd_2 = create_record (1);
385 GNUNET_NAMESTORE_records_store (nsh,
386 &privkey,
387 s_name_2,
388 1, s_rd_2,
389 &put_cont,
390 NULL);
391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
392 "Created record 3\n");
393 /* name in different zone */
394 GNUNET_asprintf (&s_name_3, "dummy3");
395 s_rd_3 = create_record (1);
396 GNUNET_NAMESTORE_records_store (nsh,
397 &privkey2,
398 s_name_3,
399 1,
400 s_rd_3,
401 &put_cont,
402 NULL);
403}
404
405
406static void
407run (void *cls,
408 const struct GNUNET_CONFIGURATION_Handle *cfg,
409 struct GNUNET_TESTING_Peer *peer)
410{
411 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
412 &endbadly,
413 NULL);
414 GNUNET_SCHEDULER_add_shutdown (&end,
415 NULL);
416
417 nsh = GNUNET_NAMESTORE_connect (cfg);
418 GNUNET_break (NULL != nsh);
419 /* first, iterate over empty namestore */
420 zi = GNUNET_NAMESTORE_zone_iteration_start (nsh,
421 NULL,
422 &fail_cb,
423 NULL,
424 &empty_zone_proc,
425 nsh,
426 &empty_zone_end,
427 NULL);
428 if (NULL == zi)
429 {
430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
431 "Failed to create zone iterator\n");
432 GNUNET_break (0);
433 res = 1;
434 GNUNET_SCHEDULER_shutdown ();
435 }
436}
437
438
439#include "test_common.c"
440
441
442int
443main (int argc, char *argv[])
444{
445 const char *plugin_name;
446 char *cfg_name;
447
448 SETUP_CFG (plugin_name, cfg_name);
449 res = 1;
450 if (0 !=
451 GNUNET_TESTING_peer_run ("test-namestore-api-zone-iteration",
452 cfg_name,
453 &run,
454 NULL))
455 {
456 res = 1;
457 }
458 GNUNET_DISK_purge_cfg_dir (cfg_name,
459 "GNUNET_TEST_HOME");
460 GNUNET_free (cfg_name);
461 return res;
462}
463
464
465/* end of test_namestore_api_zone_iteration.c */