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_nz ((void *) s_rd_1->data);
102 GNUNET_free_nz ((void *) s_rd_2->data);
103 GNUNET_free (s_rd_2);
107 GNUNET_free_nz ((void *) s_rd_3->data);
108 GNUNET_free (s_rd_3);
114 * Re-establish the connection to the service.
116 * @param cls handle to use to re-connect.
135 zone_proc (void *cls,
136 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
138 unsigned int rd_count,
139 const struct GNUNET_GNSRECORD_Data *rd)
141 static int returned_records;
142 static int fail = GNUNET_NO;
144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
145 "Comparing results name %s\n",
147 if (0 != GNUNET_memcmp (zone_key,
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Monitoring returned wrong zone key\n");
153 GNUNET_SCHEDULER_cancel (endbadly_task);
154 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
158 if (0 == strcmp (name, s_name_1))
160 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_1))
166 else if (0 == strcmp (name, s_name_2))
168 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp (rd, s_rd_2))
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
182 GNUNET_NAMESTORE_zone_monitor_next (zm,
184 if (2 == ++returned_records)
186 if (endbadly_task != NULL)
188 GNUNET_SCHEDULER_cancel (endbadly_task);
189 endbadly_task = NULL;
191 if (GNUNET_YES == fail)
192 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
194 GNUNET_SCHEDULER_add_now (&end, NULL);
207 if (0 == strcmp (label, s_name_1))
209 else if (0 == strcmp (label, s_name_2))
211 else if (0 == strcmp (label, s_name_3))
214 if (success == GNUNET_OK)
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218 "Created record %u: `%s'\n",
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
225 "Failed to create record `%s'\n",
228 GNUNET_SCHEDULER_cancel (endbadly_task);
229 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
235 static struct GNUNET_GNSRECORD_Data *
236 create_record (unsigned int count)
238 struct GNUNET_GNSRECORD_Data *rd;
240 rd = GNUNET_new_array (count,
241 struct GNUNET_GNSRECORD_Data);
242 for (unsigned int c = 0; c < count; c++)
244 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (
245 GNUNET_TIME_UNIT_HOURS).abs_value_us;
246 rd[c].record_type = TEST_RECORD_TYPE;
247 rd[c].data_size = 50;
248 rd[c].data = GNUNET_malloc (50);
250 memset ((char *) rd[c].data, 'a', 50);
272 const struct GNUNET_CONFIGURATION_Handle *cfg,
273 struct GNUNET_TESTING_Peer *peer)
276 GNUNET_CRYPTO_ecdsa_key_create (&privkey);
277 /* Start monitoring */
278 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290 "Failed to create zone monitor\n");
292 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
296 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
297 /* Connect to namestore */
298 nsh = GNUNET_NAMESTORE_connect (cfg);
301 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
303 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
307 GNUNET_CRYPTO_ecdsa_key_create (&privkey2);
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Created record 3\n");
311 /* name in different zone */
312 GNUNET_asprintf (&s_name_3, "dummy3");
313 s_rd_3 = create_record (1);
314 GNUNET_assert (NULL != (ns_ops[2] =
315 GNUNET_NAMESTORE_records_store (nsh,
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "Created record 1\n");
324 GNUNET_asprintf (&s_name_1, "dummy1");
325 s_rd_1 = create_record (1);
326 GNUNET_assert (NULL != (ns_ops[0] =
327 GNUNET_NAMESTORE_records_store (nsh,
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
337 GNUNET_asprintf (&s_name_2, "dummy2");
338 s_rd_2 = create_record (1);
339 GNUNET_assert (NULL != (ns_ops[1] =
340 GNUNET_NAMESTORE_records_store (nsh,
350 #include "test_common.c"
357 const char *plugin_name;
360 SETUP_CFG (plugin_name, cfg_name);
363 GNUNET_TESTING_peer_run ("test-namestore-api-monitoring",
370 GNUNET_DISK_purge_cfg_dir (cfg_name,
372 GNUNET_free (cfg_name);
377 /* end of test_namestore_api_monitoring.c */