2 This file is part of GNUnet.
3 Copyright (C) 2013 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file namestore/test_namestore_api_monitoring.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"
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
32 static const struct GNUNET_CONFIGURATION_Handle *cfg;
34 static struct GNUNET_NAMESTORE_Handle * nsh;
36 static struct GNUNET_SCHEDULER_Task * endbadly_task;
38 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
40 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
42 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
46 static char * s_name_1;
48 static struct GNUNET_GNSRECORD_Data *s_rd_1;
50 static char * s_name_2;
52 static struct GNUNET_GNSRECORD_Data *s_rd_2;
54 static char * s_name_3;
56 static struct GNUNET_GNSRECORD_Data *s_rd_3;
58 struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3];
60 static char *directory;
68 GNUNET_NAMESTORE_zone_monitor_stop (zm);
72 if (NULL != ns_ops[0])
74 GNUNET_NAMESTORE_cancel(ns_ops[0]);
77 if (NULL != ns_ops[1])
79 GNUNET_NAMESTORE_cancel(ns_ops[1]);
82 if (NULL != ns_ops[2])
84 GNUNET_NAMESTORE_cancel(ns_ops[2]);
90 GNUNET_NAMESTORE_disconnect (nsh);
94 GNUNET_free_non_null(s_name_1);
95 GNUNET_free_non_null(s_name_2);
96 GNUNET_free_non_null(s_name_3);
100 GNUNET_free ((void *)s_rd_1->data);
101 GNUNET_free (s_rd_1);
105 GNUNET_free ((void *)s_rd_2->data);
106 GNUNET_free (s_rd_2);
110 GNUNET_free ((void *)s_rd_3->data);
111 GNUNET_free (s_rd_3);
116 GNUNET_free (privkey);
119 if (NULL != privkey2)
121 GNUNET_free (privkey2);
128 * Re-establish the connection to the service.
130 * @param cls handle to use to re-connect.
149 zone_proc (void *cls,
150 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
152 unsigned int rd_count,
153 const struct GNUNET_GNSRECORD_Data *rd)
155 static int returned_records;
156 static int fail = GNUNET_NO;
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Comparing results name %s\n",
162 if (0 != memcmp (zone_key, privkey, sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Monitoring returned wrong zone key\n");
167 GNUNET_SCHEDULER_cancel (endbadly_task);
168 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
172 if (0 == strcmp (name, s_name_1))
174 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
180 else if (0 == strcmp (name, s_name_2))
182 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 "Invalid name %s\n", name);
196 if (2 == ++returned_records)
198 if (endbadly_task != NULL)
200 GNUNET_SCHEDULER_cancel (endbadly_task);
201 endbadly_task = NULL;
203 if (GNUNET_YES == fail)
204 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
206 GNUNET_SCHEDULER_add_now (&end, NULL);
226 put_cont (void *cls, int32_t success, const char *emsg)
231 if (0 == strcmp (label, s_name_1))
233 else if (0 == strcmp (label, s_name_2))
235 else if (0 == strcmp (label, s_name_3))
238 if (success == GNUNET_OK)
241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record %u: `%s'\n", c, label);
245 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to created records\n");
247 GNUNET_SCHEDULER_cancel (endbadly_task);
248 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
253 /* Start monitoring */
254 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to create zone monitor\n");
267 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
274 static struct GNUNET_GNSRECORD_Data *
275 create_record (unsigned int count)
278 struct GNUNET_GNSRECORD_Data * rd;
280 rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
281 for (c = 0; c < count; c++)
283 rd[c].expiration_time = GNUNET_TIME_relative_to_absolute (GNUNET_TIME_UNIT_HOURS).abs_value_us;
284 rd[c].record_type = 1111;
285 rd[c].data_size = 50;
286 rd[c].data = GNUNET_malloc(50);
288 memset ((char *) rd[c].data, 'a', 50);
296 const struct GNUNET_CONFIGURATION_Handle *mycfg,
297 struct GNUNET_TESTING_Peer *peer)
302 GNUNET_assert (GNUNET_OK ==
303 GNUNET_CONFIGURATION_get_value_string(mycfg, "PATHS", "GNUNET_TEST_HOME", &directory));
304 GNUNET_DISK_directory_remove (directory);
308 GNUNET_asprintf(&hostkey_file,
311 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Using zonekey file `%s' \n", hostkey_file);
314 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
315 GNUNET_free (hostkey_file);
316 GNUNET_assert (privkey != NULL);
319 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
320 /* Connect to namestore */
321 nsh = GNUNET_NAMESTORE_connect (cfg);
324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
326 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
330 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",
332 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
334 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
335 GNUNET_free (hostkey_file);
336 GNUNET_assert (privkey2 != NULL);
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
340 /* name in different zone */
341 GNUNET_asprintf(&s_name_3, "dummy3");
342 s_rd_3 = create_record(1);
343 GNUNET_assert (NULL != (ns_ops[2] = GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
344 1, s_rd_3, &put_cont, s_name_3)));
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
347 GNUNET_asprintf(&s_name_1, "dummy1");
348 s_rd_1 = create_record(1);
349 GNUNET_assert (NULL != (ns_ops[0] = GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
350 1, s_rd_1, &put_cont, s_name_1)));
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
354 GNUNET_asprintf(&s_name_2, "dummy2");
355 s_rd_2 = create_record(1);
356 GNUNET_assert (NULL != (ns_ops[1] = GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
357 1, s_rd_2, &put_cont, s_name_2)));
364 main (int argc, char *argv[])
368 GNUNET_TESTING_peer_run ("test-namestore-api-monitoring",
369 "test_namestore_api.conf",
375 if (NULL != directory)
377 GNUNET_DISK_directory_remove (directory);
378 GNUNET_free (directory);
384 /* end of test_namestore_api_monitoring.c */