2 This file is part of GNUnet.
3 Copyright (C) 2013, 2018 GNUnet e.V.
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.
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.
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/>.
19 * @file namestore/test_namestore_api_monitoring_existing.c
20 * @brief testcase for zone monitoring functionality: add records first, then monitor
23 #include "gnunet_namestore_service.h"
24 #include "gnunet_testing_lib.h"
25 #include "namestore.h"
26 #include "gnunet_dnsparser_lib.h"
28 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33 static const struct GNUNET_CONFIGURATION_Handle *cfg;
35 static struct GNUNET_NAMESTORE_Handle * nsh;
37 static struct GNUNET_SCHEDULER_Task * endbadly_task;
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
41 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
43 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
47 static const char * s_name_1;
49 static struct GNUNET_GNSRECORD_Data *s_rd_1;
51 static const char * s_name_2;
53 static struct GNUNET_GNSRECORD_Data *s_rd_2;
55 static const char * s_name_3;
57 static struct GNUNET_GNSRECORD_Data *s_rd_3;
59 struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3];
63 * Re-establish the connection to the service.
65 * @param cls handle to use to re-connect.
72 GNUNET_SCHEDULER_shutdown ();
82 GNUNET_NAMESTORE_zone_monitor_stop (zm);
85 if (NULL != ns_ops[0])
87 GNUNET_NAMESTORE_cancel(ns_ops[0]);
90 if (NULL != ns_ops[1])
92 GNUNET_NAMESTORE_cancel(ns_ops[1]);
95 if (NULL != ns_ops[2])
97 GNUNET_NAMESTORE_cancel(ns_ops[2]);
100 if (NULL != endbadly_task)
102 GNUNET_SCHEDULER_cancel (endbadly_task);
103 endbadly_task = NULL;
107 GNUNET_NAMESTORE_disconnect (nsh);
112 GNUNET_free ((void *)s_rd_1->data);
113 GNUNET_free (s_rd_1);
117 GNUNET_free ((void *)s_rd_2->data);
118 GNUNET_free (s_rd_2);
122 GNUNET_free ((void *)s_rd_3->data);
123 GNUNET_free (s_rd_3);
127 GNUNET_free (privkey);
130 if (NULL != privkey2)
132 GNUNET_free (privkey2);
139 zone_proc (void *cls,
140 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
142 unsigned int rd_count,
143 const struct GNUNET_GNSRECORD_Data *rd)
145 static int returned_records;
146 static int fail = GNUNET_NO;
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Comparing results name %s\n",
151 if (0 != memcmp (zone_key,
153 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "Monitoring returned wrong zone key\n");
158 GNUNET_SCHEDULER_shutdown ();
162 if (0 == strcmp (name,
166 GNUNET_GNSRECORD_records_cmp (rd,
173 else if (0 == strcmp (name,
177 GNUNET_GNSRECORD_records_cmp (rd,
186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
192 GNUNET_NAMESTORE_zone_monitor_next (zm,
194 if (2 == ++returned_records)
196 GNUNET_SCHEDULER_shutdown ();
197 if (GNUNET_YES == fail)
230 const char *label = cls;
232 if (0 == strcmp (label,
235 else if (0 == strcmp (label,
238 else if (0 == strcmp (label,
242 if (success == GNUNET_OK)
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "Created record %u: `%s'\n",
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
253 "Failed to created records\n");
256 GNUNET_SCHEDULER_shutdown ();
262 /* Start monitoring */
263 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
274 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
275 "Failed to create zone monitor\n");
278 GNUNET_SCHEDULER_shutdown ();
285 static struct GNUNET_GNSRECORD_Data *
286 create_record (unsigned int count)
288 struct GNUNET_GNSRECORD_Data *rd;
290 rd = GNUNET_new_array (count,
291 struct GNUNET_GNSRECORD_Data);
292 for (unsigned int c = 0; c < count; c++)
294 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (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);
299 memset ((char *) rd[c].data,
309 const struct GNUNET_CONFIGURATION_Handle *mycfg,
310 struct GNUNET_TESTING_Peer *peer)
313 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
314 GNUNET_assert (NULL != privkey);
315 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
316 GNUNET_assert (NULL != privkey2);
319 GNUNET_SCHEDULER_add_shutdown (&end,
321 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
324 /* Connect to namestore */
325 nsh = GNUNET_NAMESTORE_connect (cfg);
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
329 "Connect to namestore failed\n");
331 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Created record 3\n");
338 /* name in different zone */
340 s_rd_3 = create_record(1);
341 GNUNET_assert (NULL != (ns_ops[2] =
342 GNUNET_NAMESTORE_records_store (nsh,
348 (void *) s_name_3)));
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Created record 1\n");
353 s_rd_1 = create_record (1);
354 GNUNET_assert (NULL != (ns_ops[0] =
355 GNUNET_NAMESTORE_records_store (nsh,
361 (void *) s_name_1)));
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Created record 2 \n");
365 s_rd_2 = create_record (1);
366 GNUNET_assert (NULL != (ns_ops[1] =
367 GNUNET_NAMESTORE_records_store (nsh,
373 (void *) s_name_2)));
381 const char *plugin_name;
384 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
385 GNUNET_asprintf (&cfg_name,
386 "test_namestore_api_%s.conf",
389 GNUNET_DISK_purge_cfg_dir (cfg_name,
392 GNUNET_TESTING_peer_run ("test-namestore-api-monitoring-existing",
400 GNUNET_DISK_purge_cfg_dir (cfg_name,
402 GNUNET_free (cfg_name);
407 /* end of test_namestore_api_monitoring_existing.c */