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