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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file namestore/test_namestore_api_monitoring_existing.c
22 * @brief testcase for zone monitoring functionality: add records first, then monitor
25 #include "gnunet_namestore_service.h"
26 #include "gnunet_testing_lib.h"
27 #include "namestore.h"
28 #include "gnunet_dnsparser_lib.h"
30 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
35 static const struct GNUNET_CONFIGURATION_Handle *cfg;
37 static struct GNUNET_NAMESTORE_Handle *nsh;
39 static struct GNUNET_SCHEDULER_Task *endbadly_task;
41 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
43 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey2;
45 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
49 static const char *s_name_1;
51 static struct GNUNET_GNSRECORD_Data *s_rd_1;
53 static const char *s_name_2;
55 static struct GNUNET_GNSRECORD_Data *s_rd_2;
57 static const char *s_name_3;
59 static struct GNUNET_GNSRECORD_Data *s_rd_3;
61 struct GNUNET_NAMESTORE_QueueEntry *ns_ops[3];
65 * Re-establish the connection to the service.
67 * @param cls handle to use to re-connect.
74 GNUNET_SCHEDULER_shutdown ();
84 GNUNET_NAMESTORE_zone_monitor_stop (zm);
87 if (NULL != ns_ops[0])
89 GNUNET_NAMESTORE_cancel (ns_ops[0]);
92 if (NULL != ns_ops[1])
94 GNUNET_NAMESTORE_cancel (ns_ops[1]);
97 if (NULL != ns_ops[2])
99 GNUNET_NAMESTORE_cancel (ns_ops[2]);
102 if (NULL != endbadly_task)
104 GNUNET_SCHEDULER_cancel (endbadly_task);
105 endbadly_task = NULL;
109 GNUNET_NAMESTORE_disconnect (nsh);
114 GNUNET_free ((void *) s_rd_1->data);
115 GNUNET_free (s_rd_1);
119 GNUNET_free ((void *) s_rd_2->data);
120 GNUNET_free (s_rd_2);
124 GNUNET_free ((void *) s_rd_3->data);
125 GNUNET_free (s_rd_3);
129 GNUNET_free (privkey);
132 if (NULL != privkey2)
134 GNUNET_free (privkey2);
141 zone_proc (void *cls,
142 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
144 unsigned int rd_count,
145 const struct GNUNET_GNSRECORD_Data *rd)
147 static int returned_records;
148 static int fail = GNUNET_NO;
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Comparing results name %s\n",
153 if (0 != GNUNET_memcmp (zone_key,
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "Monitoring returned wrong zone key\n");
159 GNUNET_SCHEDULER_shutdown ();
163 if (0 == strcmp (name,
167 GNUNET_GNSRECORD_records_cmp (rd,
174 else if (0 == strcmp (name,
178 GNUNET_GNSRECORD_records_cmp (rd,
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
193 GNUNET_NAMESTORE_zone_monitor_next (zm,
195 if (2 == ++returned_records)
197 GNUNET_SCHEDULER_shutdown ();
198 if (GNUNET_YES == fail)
231 const char *label = cls;
233 if (0 == strcmp (label,
236 else if (0 == strcmp (label,
239 else if (0 == strcmp (label,
243 if (success == GNUNET_OK)
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Created record %u: `%s'\n",
253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
254 "Failed to created records\n");
257 GNUNET_SCHEDULER_shutdown ();
263 /* Start monitoring */
264 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
275 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
276 "Failed to create zone monitor\n");
279 GNUNET_SCHEDULER_shutdown ();
286 static struct GNUNET_GNSRECORD_Data *
287 create_record (unsigned int count)
289 struct GNUNET_GNSRECORD_Data *rd;
291 rd = GNUNET_new_array (count,
292 struct GNUNET_GNSRECORD_Data);
293 for (unsigned int c = 0; c < count; c++)
295 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
296 GNUNET_TIME_UNIT_HOURS).abs_value_us;
297 rd[c].record_type = TEST_RECORD_TYPE;
298 rd[c].data_size = 50;
299 rd[c].data = GNUNET_malloc (50);
301 memset ((char *) rd[c].data,
311 const struct GNUNET_CONFIGURATION_Handle *mycfg,
312 struct GNUNET_TESTING_Peer *peer)
315 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
316 GNUNET_assert (NULL != privkey);
317 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
318 GNUNET_assert (NULL != privkey2);
321 GNUNET_SCHEDULER_add_shutdown (&end,
323 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
326 /* Connect to namestore */
327 nsh = GNUNET_NAMESTORE_connect (cfg);
330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
331 "Connect to namestore failed\n");
333 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Created record 3\n");
340 /* name in different zone */
342 s_rd_3 = create_record (1);
343 GNUNET_assert (NULL != (ns_ops[2] =
344 GNUNET_NAMESTORE_records_store (nsh,
350 (void *) s_name_3)));
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Created record 1\n");
355 s_rd_1 = create_record (1);
356 GNUNET_assert (NULL != (ns_ops[0] =
357 GNUNET_NAMESTORE_records_store (nsh,
363 (void *) s_name_1)));
364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
365 "Created record 2 \n");
367 s_rd_2 = create_record (1);
368 GNUNET_assert (NULL != (ns_ops[1] =
369 GNUNET_NAMESTORE_records_store (nsh,
375 (void *) s_name_2)));
379 #include "test_common.c"
386 const char *plugin_name;
389 SETUP_CFG (plugin_name, 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 */