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