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