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