2 This file is part of GNUnet
3 (C) 2009 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 2, 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 hostlist/test_gnunet_daemon_hostlist.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 VERBOSE GNUNET_YES
35 #define START_ARM GNUNET_YES
36 #define MAX_URL_LEN 1000
39 * How long until wait until testcases fails
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
42 #define CHECK_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
45 static int adv_arrived;
46 static int learned_hostlist_downloaded;
48 static struct GNUNET_SCHEDULER_Handle *sched;
50 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
51 static GNUNET_SCHEDULER_TaskIdentifier check_task;
55 struct GNUNET_CONFIGURATION_Handle *cfg;
56 struct GNUNET_TRANSPORT_Handle *th;
57 struct GNUNET_MessageHeader *hello;
58 struct GNUNET_ARM_Handle *arm;
59 struct GNUNET_CORE_Handle *core;
60 struct GNUNET_STATISTICS_Handle *stats;
66 static struct PeerContext adv_peer;
68 static struct PeerContext learn_peer;
71 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
73 if (adv_peer.th != NULL)
75 GNUNET_TRANSPORT_disconnect (adv_peer.th);
78 if (learn_peer.th != NULL)
80 GNUNET_TRANSPORT_disconnect (learn_peer.th);
83 if (adv_peer.core != NULL)
85 GNUNET_CORE_disconnect (adv_peer.core);
88 if (learn_peer.core != NULL)
90 GNUNET_CORE_disconnect (learn_peer.core);
91 learn_peer.core = NULL;
93 GNUNET_SCHEDULER_shutdown (sched);
96 static void shutdown_testcase()
98 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
100 GNUNET_SCHEDULER_cancel (sched,
102 timeout_task = GNUNET_SCHEDULER_NO_TASK;
104 if (check_task != GNUNET_SCHEDULER_NO_TASK)
106 GNUNET_SCHEDULER_cancel (sched,
108 check_task = GNUNET_SCHEDULER_NO_TASK;
110 GNUNET_SCHEDULER_add_now (sched,
118 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
120 timeout_task = GNUNET_SCHEDULER_NO_TASK;
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
122 "Timeout while executing testcase, test failed.\n");
123 timeout = GNUNET_YES;
128 process_stat (void *cls,
129 const char *subsystem,
135 if ( GNUNET_YES == GNUNET_YES)
137 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
138 _("Client has successfully downloaded advertised URI \n"));
139 learned_hostlist_downloaded = GNUNET_YES;
141 if ( GNUNET_NO != learned_hostlist_downloaded )
147 * Check the server statistics regularly
150 check_statistics (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
152 GNUNET_STATISTICS_get (learn_peer.stats,
154 gettext_noop("Learned URI downloaded"),
155 GNUNET_TIME_UNIT_MINUTES,
159 check_task = GNUNET_SCHEDULER_add_delayed (sched,
166 * Core handler for p2p hostlist advertisements
168 static int ad_arrive_handler (void *cls,
169 const struct GNUNET_PeerIdentity * peer,
170 const struct GNUNET_MessageHeader * message,
171 struct GNUNET_TIME_Relative latency,
175 char *expected_uri = GNUNET_malloc (MAX_URL_LEN);
178 unsigned long long port;
180 const struct GNUNET_MessageHeader * incoming;
182 if (-1 == GNUNET_CONFIGURATION_get_value_number (adv_peer.cfg,
187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
188 "Could not read advertising server's configuration\n" );
189 return GNUNET_SYSERR;
191 hostname = GNUNET_RESOLVER_local_hostname_get ();
192 if (NULL != hostname)
194 size = strlen (hostname);
195 if (size + 15 > MAX_URL_LEN)
201 GNUNET_asprintf (&expected_uri,
204 (unsigned int) port);
208 incoming = (const struct GNUNET_MessageHeader *) message;
209 recv_uri = (char*) &incoming[1];
210 if ( 0 == strcmp( expected_uri, recv_uri ) )
212 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
213 "Recieved hostlist advertisement with URI `%s'as expected\n", recv_uri);
214 adv_arrived = GNUNET_YES;
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
218 "Expected URI `%s' and recieved URI `%s' differ\n", expected_uri, recv_uri);
219 GNUNET_free ( expected_uri );
220 GNUNET_free ( hostname );
225 * List of handlers if we are learning.
227 static struct GNUNET_CORE_MessageHandler learn_handlers[] = {
228 { &ad_arrive_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
233 setup_learn_peer (struct PeerContext *p, const char *cfgname)
237 p->cfg = GNUNET_CONFIGURATION_create ();
239 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
240 "gnunet-service-arm",
244 "-c", cfgname, NULL);
246 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
247 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (p->cfg,
252 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
254 result = remove (filename);
256 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
257 _("Hostlist file `%s' was removed\n"),filename);
260 if ( NULL != filename) GNUNET_free ( filename );
262 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
264 p->core = GNUNET_CORE_connect (sched, p->cfg,
265 GNUNET_TIME_UNIT_FOREVER_REL,
272 GNUNET_assert ( NULL != p->core );
273 p->stats = GNUNET_STATISTICS_create (sched, "hostlist", p->cfg);
274 GNUNET_assert ( NULL != p->stats );
279 setup_adv_peer (struct PeerContext *p, const char *cfgname)
281 p->cfg = GNUNET_CONFIGURATION_create ();
283 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
284 "gnunet-service-arm",
288 "-c", cfgname, NULL);
290 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
291 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
297 waitpid_task (void *cls,
298 const struct GNUNET_SCHEDULER_TaskContext *tc)
300 struct PeerContext *p = cls;
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Killing ARM process.\n");
305 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
306 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
307 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
308 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "ARM process %u stopped\n", p->arm_pid);
312 GNUNET_CONFIGURATION_destroy (p->cfg);
320 struct PeerContext *p = cls;
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 ? "ARM stopped core service\n"
325 : "ARM failed to stop core service\n");
326 GNUNET_ARM_disconnect (p->arm);
328 /* make sure this runs after all other tasks are done */
329 GNUNET_SCHEDULER_add_delayed (sched,
330 GNUNET_TIME_UNIT_SECONDS,
336 stop_arm (struct PeerContext *p)
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Asking ARM to stop core service\n");
340 p->arm = GNUNET_ARM_connect (p->cfg, sched, NULL);
341 GNUNET_ARM_stop_service (p->arm, "core", GNUNET_TIME_UNIT_SECONDS,
347 * Try again to connect to transport service.
350 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
352 stop_arm (&adv_peer);
353 stop_arm (&learn_peer);
359 struct GNUNET_SCHEDULER_Handle *s,
362 const struct GNUNET_CONFIGURATION_Handle *cfg)
365 adv_arrived = GNUNET_NO;
366 learned_hostlist_downloaded = GNUNET_NO;
368 timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
372 check_task = GNUNET_SCHEDULER_add_delayed (sched,
376 GNUNET_SCHEDULER_add_delayed (sched,
377 GNUNET_TIME_UNIT_FOREVER_REL,
380 setup_adv_peer (&adv_peer, "test_learning_adv_peer.conf");
381 setup_learn_peer (&learn_peer, "test_learning_learn_peer.conf");
388 char *const argv[] = { "test-gnunet-daemon-hostlist",
389 "-c", "learning_data.conf",
395 struct GNUNET_GETOPT_CommandLineOption options[] = {
396 GNUNET_GETOPT_OPTION_END
399 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
400 argv, "test-gnunet-daemon-hostlist",
401 "nohelp", options, &run, NULL);
403 if (timeout == GNUNET_YES)
405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
406 "Testcase could not set up two communicating peers, timeout\n");
409 if (adv_arrived == GNUNET_NO)
411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
412 "Learning peer did not recieve advertisement from server\n");
415 if (learned_hostlist_downloaded == GNUNET_NO)
417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
418 "Advertisement hostlist could not be downloaded from server\n");
425 main (int argc, char *argv[])
430 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
431 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
432 GNUNET_log_setup ("test-gnunet-daemon-hostlist",
440 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
441 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
445 /* end of test_gnunet_daemon_hostlist.c */