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