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