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.
16 * @file namestore/test_namestore_api_monitoring.c
17 * @brief testcase for zone monitoring functionality: monitor first, then add records
20 #include "gnunet_namestore_service.h"
21 #include "gnunet_testing_lib.h"
22 #include "namestore.h"
23 #include "gnunet_dnsparser_lib.h"
25 #define TEST_RECORD_TYPE GNUNET_DNSPARSER_TYPE_TXT
29 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
32 static struct GNUNET_NAMESTORE_Handle * nsh;
34 static struct GNUNET_SCHEDULER_Task * endbadly_task;
36 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
38 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
40 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
44 static char * s_name_1;
46 static struct GNUNET_GNSRECORD_Data *s_rd_1;
48 static char * s_name_2;
50 static struct GNUNET_GNSRECORD_Data *s_rd_2;
52 static char * s_name_3;
54 static struct GNUNET_GNSRECORD_Data *s_rd_3;
56 struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3];
64 GNUNET_NAMESTORE_zone_monitor_stop (zm);
67 if (NULL != ns_ops[0])
69 GNUNET_NAMESTORE_cancel(ns_ops[0]);
72 if (NULL != ns_ops[1])
74 GNUNET_NAMESTORE_cancel(ns_ops[1]);
77 if (NULL != ns_ops[2])
79 GNUNET_NAMESTORE_cancel(ns_ops[2]);
84 GNUNET_NAMESTORE_disconnect (nsh);
87 GNUNET_free_non_null(s_name_1);
88 GNUNET_free_non_null(s_name_2);
89 GNUNET_free_non_null(s_name_3);
93 GNUNET_free ((void *)s_rd_1->data);
98 GNUNET_free ((void *)s_rd_2->data);
103 GNUNET_free ((void *)s_rd_3->data);
104 GNUNET_free (s_rd_3);
109 GNUNET_free (privkey);
112 if (NULL != privkey2)
114 GNUNET_free (privkey2);
121 * Re-establish the connection to the service.
123 * @param cls handle to use to re-connect.
142 zone_proc (void *cls,
143 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
145 unsigned int rd_count,
146 const struct GNUNET_GNSRECORD_Data *rd)
148 static int returned_records;
149 static int fail = GNUNET_NO;
151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
152 "Comparing results name %s\n",
154 if (0 != memcmp (zone_key,
156 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Monitoring returned wrong zone key\n");
161 GNUNET_SCHEDULER_cancel (endbadly_task);
162 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
166 if (0 == strcmp (name, s_name_1))
168 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
174 else if (0 == strcmp (name, s_name_2))
176 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
190 GNUNET_NAMESTORE_zone_monitor_next (zm,
192 if (2 == ++returned_records)
194 if (endbadly_task != NULL)
196 GNUNET_SCHEDULER_cancel (endbadly_task);
197 endbadly_task = NULL;
199 if (GNUNET_YES == fail)
200 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
202 GNUNET_SCHEDULER_add_now (&end, NULL);
215 if (0 == strcmp (label, s_name_1))
217 else if (0 == strcmp (label, s_name_2))
219 else if (0 == strcmp (label, s_name_3))
222 if (success == GNUNET_OK)
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Created record %u: `%s'\n",
232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
233 "Failed to create record `%s'\n",
236 GNUNET_SCHEDULER_cancel (endbadly_task);
237 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly,
243 static struct GNUNET_GNSRECORD_Data *
244 create_record (unsigned int count)
246 struct GNUNET_GNSRECORD_Data *rd;
248 rd = GNUNET_new_array (count,
249 struct GNUNET_GNSRECORD_Data);
250 for (unsigned int c = 0; c < count; c++)
252 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
253 rd[c].record_type = TEST_RECORD_TYPE;
254 rd[c].data_size = 50;
255 rd[c].data = GNUNET_malloc(50);
257 memset ((char *) rd[c].data, 'a', 50);
279 const struct GNUNET_CONFIGURATION_Handle *cfg,
280 struct GNUNET_TESTING_Peer *peer)
283 privkey = GNUNET_CRYPTO_ecdsa_key_create ();
284 GNUNET_assert (privkey != NULL);
286 /* Start monitoring */
287 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
299 "Failed to create zone monitor\n");
301 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
305 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
306 /* Connect to namestore */
307 nsh = GNUNET_NAMESTORE_connect (cfg);
310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
312 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
316 privkey2 = GNUNET_CRYPTO_ecdsa_key_create ();
317 GNUNET_assert (privkey2 != NULL);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Created record 3\n");
322 /* name in different zone */
323 GNUNET_asprintf(&s_name_3, "dummy3");
324 s_rd_3 = create_record(1);
325 GNUNET_assert (NULL != (ns_ops[2] =
326 GNUNET_NAMESTORE_records_store (nsh,
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "Created record 1\n");
335 GNUNET_asprintf(&s_name_1, "dummy1");
336 s_rd_1 = create_record(1);
337 GNUNET_assert (NULL != (ns_ops[0] =
338 GNUNET_NAMESTORE_records_store (nsh,
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
348 GNUNET_asprintf(&s_name_2, "dummy2");
349 s_rd_2 = create_record(1);
350 GNUNET_assert (NULL != (ns_ops[1] =
351 GNUNET_NAMESTORE_records_store (nsh,
365 const char *plugin_name;
368 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
369 GNUNET_asprintf (&cfg_name,
370 "test_namestore_api_%s.conf",
372 GNUNET_DISK_purge_cfg_dir (cfg_name,
376 GNUNET_TESTING_peer_run ("test-namestore-api-monitoring",
383 GNUNET_DISK_purge_cfg_dir (cfg_name,
385 GNUNET_free (cfg_name);
390 /* end of test_namestore_api_monitoring.c */