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.c
22 * @brief testcase for zone monitoring functionality: monitor first, then add records
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, 100)
36 static struct GNUNET_NAMESTORE_Handle *nsh;
38 static struct GNUNET_SCHEDULER_Task *endbadly_task;
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
42 static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey2;
44 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
48 static char *s_name_1;
50 static struct GNUNET_GNSRECORD_Data *s_rd_1;
52 static char *s_name_2;
54 static struct GNUNET_GNSRECORD_Data *s_rd_2;
56 static char *s_name_3;
58 static struct GNUNET_GNSRECORD_Data *s_rd_3;
60 struct GNUNET_NAMESTORE_QueueEntry *ns_ops[3];
68 GNUNET_NAMESTORE_zone_monitor_stop (zm);
71 if (NULL != ns_ops[0])
73 GNUNET_NAMESTORE_cancel (ns_ops[0]);
76 if (NULL != ns_ops[1])
78 GNUNET_NAMESTORE_cancel (ns_ops[1]);
81 if (NULL != ns_ops[2])
83 GNUNET_NAMESTORE_cancel (ns_ops[2]);
88 GNUNET_NAMESTORE_disconnect (nsh);
91 GNUNET_free_non_null (s_name_1);
92 GNUNET_free_non_null (s_name_2);
93 GNUNET_free_non_null (s_name_3);
97 GNUNET_free ((void *) s_rd_1->data);
102 GNUNET_free ((void *) s_rd_2->data);
103 GNUNET_free (s_rd_2);
107 GNUNET_free ((void *) s_rd_3->data);
108 GNUNET_free (s_rd_3);
113 GNUNET_free (privkey);
116 if (NULL != privkey2)
118 GNUNET_free (privkey2);
125 * Re-establish the connection to the service.
127 * @param cls handle to use to re-connect.
146 zone_proc (void *cls,
147 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
149 unsigned int rd_count,
150 const struct GNUNET_GNSRECORD_Data *rd)
152 static int returned_records;
153 static int fail = GNUNET_NO;
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "Comparing results name %s\n",
158 if (0 != GNUNET_memcmp (zone_key,
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
162 "Monitoring returned wrong zone key\n");
164 GNUNET_SCHEDULER_cancel (endbadly_task);
165 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
169 if (0 == strcmp (name, s_name_1))
171 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
177 else if (0 == strcmp (name, s_name_2))
179 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_2))
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
193 GNUNET_NAMESTORE_zone_monitor_next (zm,
195 if (2 == ++returned_records)
197 if (endbadly_task != NULL)
199 GNUNET_SCHEDULER_cancel (endbadly_task);
200 endbadly_task = NULL;
202 if (GNUNET_YES == fail)
203 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
205 GNUNET_SCHEDULER_add_now (&end, NULL);
218 if (0 == strcmp (label, s_name_1))
220 else if (0 == strcmp (label, s_name_2))
222 else if (0 == strcmp (label, s_name_3))
225 if (success == GNUNET_OK)
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Created record %u: `%s'\n",
235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236 "Failed to create record `%s'\n",
239 GNUNET_SCHEDULER_cancel (endbadly_task);
240 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
246 static struct GNUNET_GNSRECORD_Data *
247 create_record (unsigned int count)
249 struct GNUNET_GNSRECORD_Data *rd;
251 rd = GNUNET_new_array (count,
252 struct GNUNET_GNSRECORD_Data);
253 for (unsigned int c = 0; c < count; c++)
255 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
256 GNUNET_TIME_UNIT_HOURS).abs_value_us;
257 rd[c].record_type = TEST_RECORD_TYPE;
258 rd[c].data_size = 50;
259 rd[c].data = GNUNET_malloc (50);
261 memset ((char *) rd[c].data, 'a', 50);
283 const struct GNUNET_CONFIGURATION_Handle *cfg,
284 struct GNUNET_TESTING_Peer *peer)
287 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
288 GNUNET_assert (privkey != NULL);
290 /* Start monitoring */
291 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
302 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
303 "Failed to create zone monitor\n");
305 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
309 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
310 /* Connect to namestore */
311 nsh = GNUNET_NAMESTORE_connect (cfg);
314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
316 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
320 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
321 GNUNET_assert (privkey2 != NULL);
324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
325 "Created record 3\n");
326 /* name in different zone */
327 GNUNET_asprintf (&s_name_3, "dummy3");
328 s_rd_3 = create_record (1);
329 GNUNET_assert (NULL != (ns_ops[2] =
330 GNUNET_NAMESTORE_records_store (nsh,
337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
338 "Created record 1\n");
339 GNUNET_asprintf (&s_name_1, "dummy1");
340 s_rd_1 = create_record (1);
341 GNUNET_assert (NULL != (ns_ops[0] =
342 GNUNET_NAMESTORE_records_store (nsh,
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
352 GNUNET_asprintf (&s_name_2, "dummy2");
353 s_rd_2 = create_record (1);
354 GNUNET_assert (NULL != (ns_ops[1] =
355 GNUNET_NAMESTORE_records_store (nsh,
365 #include "test_common.c"
372 const char *plugin_name;
375 SETUP_CFG (plugin_name, cfg_name);
378 GNUNET_TESTING_peer_run ("test-namestore-api-monitoring",
385 GNUNET_DISK_purge_cfg_dir (cfg_name,
387 GNUNET_free (cfg_name);
392 /* end of test_namestore_api_monitoring.c */