2 This file is part of GNUnet.
3 (C) 2013 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
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"
30 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
33 static struct GNUNET_NAMESTORE_Handle * nsh;
35 static GNUNET_SCHEDULER_TaskIdentifier endbadly_task;
37 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey;
39 static struct GNUNET_CRYPTO_EcdsaPrivateKey * privkey2;
41 static struct GNUNET_NAMESTORE_ZoneMonitor *zm;
45 static char * s_name_1;
47 static struct GNUNET_GNSRECORD_Data *s_rd_1;
49 static char * s_name_2;
51 static struct GNUNET_GNSRECORD_Data *s_rd_2;
53 static char * s_name_3;
55 static struct GNUNET_GNSRECORD_Data *s_rd_3;
57 struct GNUNET_NAMESTORE_QueueEntry * ns_ops[3];
65 GNUNET_NAMESTORE_zone_monitor_stop (zm);
69 if (NULL != ns_ops[0])
71 GNUNET_NAMESTORE_cancel(ns_ops[0]);
74 if (NULL != ns_ops[1])
76 GNUNET_NAMESTORE_cancel(ns_ops[1]);
79 if (NULL != ns_ops[2])
81 GNUNET_NAMESTORE_cancel(ns_ops[2]);
87 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.
128 * @param tc scheduler context
131 endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
139 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
147 zone_proc (void *cls,
148 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone_key,
150 unsigned int rd_count,
151 const struct GNUNET_GNSRECORD_Data *rd)
153 static int returned_records;
154 static int fail = GNUNET_NO;
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "Comparing results name %s\n",
159 if (0 != memcmp (zone_key,
161 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "Monitoring returned wrong zone key\n");
166 GNUNET_SCHEDULER_cancel (endbadly_task);
167 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
171 if (0 == strcmp (name, s_name_1))
173 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_1))
179 else if (0 == strcmp (name, s_name_2))
181 if (GNUNET_YES != GNUNET_GNSRECORD_records_cmp(rd, s_rd_2))
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
196 if (2 == ++returned_records)
198 if (endbadly_task != GNUNET_SCHEDULER_NO_TASK)
200 GNUNET_SCHEDULER_cancel (endbadly_task);
201 endbadly_task = GNUNET_SCHEDULER_NO_TASK;
203 if (GNUNET_YES == fail)
204 GNUNET_SCHEDULER_add_now (&endbadly, NULL);
206 GNUNET_SCHEDULER_add_now (&end, NULL);
212 put_cont (void *cls, int32_t success, const char *emsg)
217 if (0 == strcmp (label, s_name_1))
219 else if (0 == strcmp (label, s_name_2))
221 else if (0 == strcmp (label, s_name_3))
224 if (success == GNUNET_OK)
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Created record %u: `%s'\n",
234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
235 "Failed to created records\n");
237 GNUNET_SCHEDULER_cancel (endbadly_task);
238 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
243 static struct GNUNET_GNSRECORD_Data *
244 create_record (unsigned int count)
247 struct GNUNET_GNSRECORD_Data * rd;
249 rd = GNUNET_malloc (count * sizeof (struct GNUNET_GNSRECORD_Data));
250 for (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 = 1111;
254 rd[c].data_size = 50;
255 rd[c].data = GNUNET_malloc(50);
257 memset ((char *) rd[c].data, 'a', 50);
265 const struct GNUNET_CONFIGURATION_Handle *cfg,
266 struct GNUNET_TESTING_Peer *peer)
272 GNUNET_asprintf(&hostkey_file,
275 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277 "Using zonekey file `%s' \n", hostkey_file);
278 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
279 GNUNET_free (hostkey_file);
280 GNUNET_assert (privkey != NULL);
282 /* Start monitoring */
283 zm = GNUNET_NAMESTORE_zone_monitor_start (cfg,
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 "Failed to create zone monitor\n");
294 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
298 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &endbadly, NULL);
299 /* Connect to namestore */
300 nsh = GNUNET_NAMESTORE_connect (cfg);
303 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Connect to namestore\n");
305 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
309 GNUNET_asprintf(&hostkey_file,"zonefiles%s%s",
311 "HGU0A0VCU334DN7F2I9UIUMVQMM7JMSD142LIMNUGTTV9R0CF4EG.zkey");
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file);
313 privkey2 = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file);
314 GNUNET_free (hostkey_file);
315 GNUNET_assert (privkey2 != NULL);
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 3\n");
319 /* name in different zone */
320 GNUNET_asprintf(&s_name_3, "dummy3");
321 s_rd_3 = create_record(1);
322 GNUNET_assert (NULL != (ns_ops[2] = GNUNET_NAMESTORE_records_store (nsh, privkey2, s_name_3,
323 1, s_rd_3, &put_cont, s_name_3)));
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 1\n");
326 GNUNET_asprintf(&s_name_1, "dummy1");
327 s_rd_1 = create_record(1);
328 GNUNET_assert (NULL != (ns_ops[0] = GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_1,
329 1, s_rd_1, &put_cont, s_name_1)));
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created record 2 \n");
333 GNUNET_asprintf(&s_name_2, "dummy2");
334 s_rd_2 = create_record(1);
335 GNUNET_assert (NULL != (ns_ops[1] = GNUNET_NAMESTORE_records_store(nsh, privkey, s_name_2,
336 1, s_rd_2, &put_cont, s_name_2)));
343 main (int argc, char *argv[])
347 GNUNET_TESTING_peer_run ("test-namestore-api-monitoring",
348 "test_namestore_api.conf",
356 /* end of test_namestore_api_monitoring.c */