2 This file is part of GNUnet
3 Copyright (C) 2009, 2010, 2011, 2012 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 hostlist/test_gnunet_daemon_hostlist_learning.c
22 * @brief test for gnunet_daemon_hostslist.c
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_core_service.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_resolver_service.h"
31 #include "gnunet_statistics_service.h"
33 #define MAX_URL_LEN 1000
36 * How long until wait until testcases fails
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 180)
40 #define CHECK_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
45 struct GNUNET_CONFIGURATION_Handle *cfg;
46 struct GNUNET_TRANSPORT_Handle *th;
47 struct GNUNET_MessageHeader *hello;
48 struct GNUNET_CORE_Handle *core;
49 struct GNUNET_STATISTICS_Handle *stats;
50 struct GNUNET_OS_Process *arm_proc;
57 static int adv_arrived;
59 static int learned_hostlist_saved;
61 static int learned_hostlist_downloaded;
63 static char *current_adv_uri;
65 static const struct GNUNET_CONFIGURATION_Handle *cfg;
67 static struct GNUNET_SCHEDULER_Task * timeout_task;
69 static struct GNUNET_SCHEDULER_Task * check_task;
71 static struct PeerContext adv_peer;
73 static struct PeerContext learn_peer;
75 static struct GNUNET_STATISTICS_GetHandle *download_stats;
77 static struct GNUNET_STATISTICS_GetHandle *urisrecv_stat;
79 static struct GNUNET_STATISTICS_GetHandle *advsent_stat;
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown testcase....\n");
86 if (timeout_task != NULL)
88 GNUNET_SCHEDULER_cancel (timeout_task);
91 if (NULL != download_stats)
93 GNUNET_STATISTICS_get_cancel (download_stats);
94 download_stats = NULL;
96 if (NULL != urisrecv_stat)
98 GNUNET_STATISTICS_get_cancel (urisrecv_stat);
101 if (NULL != advsent_stat)
103 GNUNET_STATISTICS_get_cancel (advsent_stat);
106 if (NULL != adv_peer.stats)
108 GNUNET_STATISTICS_destroy (adv_peer.stats, GNUNET_NO);
109 adv_peer.stats = NULL;
111 if (NULL != learn_peer.stats)
113 GNUNET_STATISTICS_destroy (learn_peer.stats, GNUNET_NO);
114 learn_peer.stats = NULL;
116 if (check_task != NULL)
118 GNUNET_SCHEDULER_cancel (check_task);
121 if (NULL != current_adv_uri)
123 GNUNET_free (current_adv_uri);
124 current_adv_uri = NULL;
126 if (adv_peer.th != NULL)
128 GNUNET_TRANSPORT_disconnect (adv_peer.th);
131 if (learn_peer.th != NULL)
133 GNUNET_TRANSPORT_disconnect (learn_peer.th);
134 learn_peer.th = NULL;
136 if (adv_peer.core != NULL)
138 GNUNET_CORE_disconnect (adv_peer.core);
139 adv_peer.core = NULL;
141 if (learn_peer.core != NULL)
143 GNUNET_CORE_disconnect (learn_peer.core);
144 learn_peer.core = NULL;
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147 "Killing hostlist server ARM process.\n");
148 if (0 != GNUNET_OS_process_kill (adv_peer.arm_proc, GNUNET_TERM_SIG))
149 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
150 if (GNUNET_OS_process_wait (adv_peer.arm_proc) != GNUNET_OK)
151 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
152 GNUNET_OS_process_destroy (adv_peer.arm_proc);
153 adv_peer.arm_proc = NULL;
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Killing hostlist client ARM process.\n");
156 if (0 != GNUNET_OS_process_kill (learn_peer.arm_proc, GNUNET_TERM_SIG))
157 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
158 if (GNUNET_OS_process_wait (learn_peer.arm_proc) != GNUNET_OK)
159 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
160 GNUNET_OS_process_destroy (learn_peer.arm_proc);
161 learn_peer.arm_proc = NULL;
162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutdown complete....\n");
169 timeout_error (void *cls)
172 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
173 "Timeout while executing testcase, test failed.\n");
174 timeout = GNUNET_YES;
175 shutdown_testcase ();
180 process_downloads_done (void *cls, int success)
182 download_stats = NULL;
187 do_shutdown (void *cls)
189 shutdown_testcase ();
194 process_downloads (void *cls, const char *subsystem, const char *name,
195 uint64_t value, int is_persistent)
197 if ((value >= 2) && (learned_hostlist_downloaded == GNUNET_NO))
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
200 "Peer has successfully downloaded advertised URI\n");
201 learned_hostlist_downloaded = GNUNET_YES;
202 if ((learned_hostlist_saved == GNUNET_YES) && (adv_sent == GNUNET_YES))
204 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
212 process_uris_recv_done (void *cls, int success)
214 urisrecv_stat = NULL;
219 process_uris_recv (void *cls, const char *subsystem, const char *name,
220 uint64_t value, int is_persistent)
222 if (((struct PeerContext *) cls == &learn_peer) && (value == 1) &&
223 (learned_hostlist_saved == GNUNET_NO))
225 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
226 "Peer has successfully saved advertised URI\n");
227 learned_hostlist_saved = GNUNET_YES;
228 if ((learned_hostlist_downloaded == GNUNET_YES) && (adv_sent == GNUNET_YES))
230 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
238 process_adv_sent_done (void *cls, int success)
245 process_adv_sent (void *cls,
246 const char *subsystem,
248 uint64_t value, int is_persistent)
250 if ((value >= 1) && (adv_sent == GNUNET_NO))
252 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
253 "Server has successfully sent advertisement\n");
254 adv_sent = GNUNET_YES;
255 if ((learned_hostlist_downloaded == GNUNET_YES) &&
256 (learned_hostlist_saved == GNUNET_YES))
258 GNUNET_SCHEDULER_add_now (&do_shutdown, NULL);
266 * Check the server statistics regularly
269 check_statistics (void *cls)
274 GNUNET_asprintf (&stat,
275 gettext_noop ("# advertised URI `%s' downloaded"),
277 if (NULL != learn_peer.stats)
279 if (NULL != download_stats)
280 GNUNET_STATISTICS_get_cancel (download_stats);
282 GNUNET_STATISTICS_get (learn_peer.stats, "hostlist", stat,
283 &process_downloads_done, &process_downloads,
285 if (NULL != urisrecv_stat)
286 GNUNET_STATISTICS_get_cancel (urisrecv_stat);
288 GNUNET_STATISTICS_get (learn_peer.stats, "hostlist",
289 gettext_noop ("# advertised hostlist URIs"),
290 &process_uris_recv_done, &process_uris_recv,
294 if (NULL != adv_peer.stats)
296 if (NULL != advsent_stat)
297 GNUNET_STATISTICS_get_cancel (advsent_stat);
299 GNUNET_STATISTICS_get (adv_peer.stats, "hostlist",
300 gettext_noop ("# hostlist advertisements send"),
301 &process_adv_sent_done,
302 &process_adv_sent, NULL);
305 GNUNET_SCHEDULER_add_delayed (CHECK_INTERVAL, &check_statistics, NULL);
310 * Core handler for p2p hostlist advertisements
313 ad_arrive_handler (void *cls,
314 const struct GNUNET_PeerIdentity *peer,
315 const struct GNUNET_MessageHeader *message)
319 unsigned long long port;
320 const struct GNUNET_MessageHeader *incoming;
324 GNUNET_CONFIGURATION_get_value_number (adv_peer.cfg, "HOSTLIST",
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
328 "Could not read advertising server's configuration\n");
329 return GNUNET_SYSERR;
333 GNUNET_CONFIGURATION_get_value_string (adv_peer.cfg, "HOSTLIST",
334 "EXTERNAL_DNS_NAME", &hostname))
335 hostname = GNUNET_RESOLVER_local_fqdn_get ();
336 GNUNET_asprintf (&expected_uri, "http://%s:%u/",
337 hostname != NULL ? hostname : "localhost",
338 (unsigned int) port);
339 incoming = (const struct GNUNET_MessageHeader *) message;
340 end = (const char *) &incoming[1];
342 end[ntohs (message->size) - sizeof (struct GNUNET_MessageHeader) - 1])
345 GNUNET_free (expected_uri);
346 GNUNET_free_non_null (hostname);
347 return GNUNET_SYSERR;
349 current_adv_uri = GNUNET_strdup (end);
350 if (0 == strcmp (expected_uri, current_adv_uri))
352 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
353 "Received hostlist advertisement with URI `%s' as expected\n",
355 adv_arrived = GNUNET_YES;
356 adv_sent = GNUNET_YES;
359 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 "Expected URI `%s' and received URI `%s' differ\n",
361 expected_uri, current_adv_uri);
362 GNUNET_free (expected_uri);
363 GNUNET_free_non_null (hostname);
369 * List of handlers if we are learning.
371 static struct GNUNET_CORE_MessageHandler learn_handlers[] = {
372 {&ad_arrive_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
378 setup_learn_peer (struct PeerContext *p, const char *cfgname)
384 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
385 p->cfg = GNUNET_CONFIGURATION_create ();
387 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
390 "gnunet-service-arm",
391 "-c", cfgname, NULL);
392 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
394 GNUNET_CONFIGURATION_get_value_string (p->cfg, "HOSTLIST", "HOSTLISTFILE",
397 if (GNUNET_YES == GNUNET_DISK_file_test (filename))
399 result = UNLINK (filename);
401 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
402 _("Hostlist file `%s' was removed\n"), filename);
404 GNUNET_free (filename);
407 GNUNET_CORE_connect (p->cfg, NULL, NULL, NULL, NULL, NULL, GNUNET_NO,
408 NULL, GNUNET_NO, learn_handlers);
409 GNUNET_assert (NULL != p->core);
410 p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg);
411 GNUNET_assert (NULL != p->stats);
412 GNUNET_free (binary);
417 setup_adv_peer (struct PeerContext *p, const char *cfgname)
421 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
422 p->cfg = GNUNET_CONFIGURATION_create ();
424 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
427 "gnunet-service-arm",
428 "-c", cfgname, NULL);
429 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
430 p->stats = GNUNET_STATISTICS_create ("hostlist", p->cfg);
431 GNUNET_assert (NULL != p->stats);
432 GNUNET_free (binary);
437 run (void *cls, char *const *args, const char *cfgfile,
438 const struct GNUNET_CONFIGURATION_Handle *c)
441 adv_sent = GNUNET_NO;
444 learned_hostlist_saved = GNUNET_NO;
445 learned_hostlist_downloaded = GNUNET_NO;
449 setup_adv_peer (&adv_peer, "test_learning_adv_peer.conf");
450 setup_learn_peer (&learn_peer, "test_learning_learn_peer.conf");
451 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
454 GNUNET_SCHEDULER_add_delayed (CHECK_INTERVAL, &check_statistics, NULL);
463 char *const argv[] = {
464 "test-gnunet-daemon-hostlist-learning",
465 "-c", "learning_data.conf",
468 struct GNUNET_GETOPT_CommandLineOption options[] = {
469 GNUNET_GETOPT_OPTION_END
472 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
473 "test-gnunet-daemon-hostlist-learning", "nohelp", options,
476 if (timeout == GNUNET_YES)
478 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Testcase timeout\n");
481 if (adv_arrived != GNUNET_YES)
483 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
484 "Learning peer did not receive advertisement from server\n");
487 if (learned_hostlist_saved == GNUNET_NO)
489 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
490 "Advertised hostlist was not saved in datastore\n");
493 if (learned_hostlist_downloaded == GNUNET_NO)
495 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
496 "Advertised hostlist could not be downloaded from server\n");
499 if (adv_sent == GNUNET_NO)
501 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
502 "Advertised was not sent from server to client\n");
505 if (GNUNET_YES == failed)
512 main (int argc, char *argv[])
516 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
517 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
518 GNUNET_log_setup ("test-gnunet-daemon-hostlist",
522 system ("gnunet-peerinfo -s -c test_learning_adv_peer.conf > /dev/null");
523 system ("gnunet-peerinfo -s -c test_learning_learn_peer.conf > /dev/null");
525 system ("gnunet-peerinfo -s -c test_learning_adv_peer.conf > NUL");
526 system ("gnunet-peerinfo -s -c test_learning_learn_peer.conf > NUL");
529 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
530 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
531 if (GNUNET_YES == GNUNET_DISK_file_test ("hostlists_learn_peer.file"))
533 if (0 == UNLINK ("hostlists_learn_peer.file"))
534 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
535 "Hostlist file hostlists_learn_peer.file was removed\n");
540 /* end of test_gnunet_daemon_hostlist_learning.c */