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"
32 #define VERBOSE GNUNET_YES
34 #define START_ARM GNUNET_YES
35 #define MAX_URL_LEN 1000
38 * How long until wait until testcases fails
40 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
41 #define CHECK_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
44 static int adv_arrived;
46 static struct GNUNET_SCHEDULER_Handle *sched;
48 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
49 static GNUNET_SCHEDULER_TaskIdentifier check_task;
53 struct GNUNET_CONFIGURATION_Handle *cfg;
54 struct GNUNET_TRANSPORT_Handle *th;
55 struct GNUNET_MessageHeader *hello;
56 struct GNUNET_ARM_Handle *arm;
57 struct GNUNET_CORE_Handle *core;
63 static struct PeerContext adv_peer;
65 static struct PeerContext learn_peer;
68 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
70 if (adv_peer.th != NULL)
72 GNUNET_TRANSPORT_disconnect (adv_peer.th);
75 if (learn_peer.th != NULL)
77 GNUNET_TRANSPORT_disconnect (learn_peer.th);
80 if (adv_peer.core != NULL)
82 GNUNET_CORE_disconnect (adv_peer.core);
85 if (learn_peer.core != NULL)
87 GNUNET_CORE_disconnect (learn_peer.core);
88 learn_peer.core = NULL;
90 GNUNET_SCHEDULER_shutdown (sched);
93 static void shutdown_testcase()
95 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
97 GNUNET_SCHEDULER_cancel (sched,
99 timeout_task = GNUNET_SCHEDULER_NO_TASK;
101 if (check_task != GNUNET_SCHEDULER_NO_TASK)
103 GNUNET_SCHEDULER_cancel (sched,
105 check_task = GNUNET_SCHEDULER_NO_TASK;
107 GNUNET_SCHEDULER_add_now (sched,
115 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
117 timeout_task = GNUNET_SCHEDULER_NO_TASK;
118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
119 "Timeout while executing testcase, test failed.\n");
120 timeout = GNUNET_YES;
125 * Check the server statistics regularly
128 check_statistics (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Checking server stats...\n");
132 check_task = GNUNET_SCHEDULER_add_delayed (sched,
139 * Core handler for p2p hostlist advertisements
141 static int ad_arrive_handler (void *cls,
142 const struct GNUNET_PeerIdentity * peer,
143 const struct GNUNET_MessageHeader * message,
144 struct GNUNET_TIME_Relative latency,
148 char *expected_uri = GNUNET_malloc (MAX_URL_LEN);
151 unsigned long long port;
153 const struct GNUNET_MessageHeader * incoming;
155 if (-1 == GNUNET_CONFIGURATION_get_value_number (adv_peer.cfg,
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
161 "Could not read advertising server's configuration\n" );
162 return GNUNET_SYSERR;
164 hostname = GNUNET_RESOLVER_local_hostname_get ();
165 if (NULL != hostname)
167 size = strlen (hostname);
168 if (size + 15 > MAX_URL_LEN)
174 GNUNET_asprintf (&expected_uri,
177 (unsigned int) port);
181 incoming = (const struct GNUNET_MessageHeader *) message;
182 recv_uri = (char*) &incoming[1];
183 if ( 0 == strcmp( expected_uri, recv_uri ) )
185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
186 "Recieved hostlist advertisement with URI `%s'as expected\n", recv_uri);
187 adv_arrived = GNUNET_YES;
190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
191 "Expected URI `%s' and recieved URI `%s' differ\n", expected_uri, recv_uri);
192 GNUNET_free ( expected_uri );
193 GNUNET_free ( hostname );
199 * List of handlers if we are learning.
201 static struct GNUNET_CORE_MessageHandler learn_handlers[] = {
202 { &ad_arrive_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
207 setup_learn_peer (struct PeerContext *p, const char *cfgname)
211 p->cfg = GNUNET_CONFIGURATION_create ();
213 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
214 "gnunet-service-arm",
218 "-c", cfgname, NULL);
220 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
221 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (p->cfg,
226 if ( GNUNET_YES == GNUNET_DISK_file_test (filename) )
228 result = remove (filename);
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
231 _("Hostlist hostlist file `%s' was removed\n"),filename);
234 GNUNET_free ( filename );
235 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
237 p->core = GNUNET_CORE_connect (sched, p->cfg,
238 GNUNET_TIME_UNIT_FOREVER_REL,
245 GNUNET_assert ( NULL != p->core );
250 setup_adv_peer (struct PeerContext *p, const char *cfgname)
252 p->cfg = GNUNET_CONFIGURATION_create ();
254 p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
255 "gnunet-service-arm",
259 "-c", cfgname, NULL);
261 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
262 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
268 waitpid_task (void *cls,
269 const struct GNUNET_SCHEDULER_TaskContext *tc)
271 struct PeerContext *p = cls;
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Killing ARM process.\n");
276 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
277 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
278 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
279 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "ARM process %u stopped\n", p->arm_pid);
283 GNUNET_CONFIGURATION_destroy (p->cfg);
291 struct PeerContext *p = cls;
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
295 ? "ARM stopped core service\n"
296 : "ARM failed to stop core service\n");
297 GNUNET_ARM_disconnect (p->arm);
299 /* make sure this runs after all other tasks are done */
300 GNUNET_SCHEDULER_add_delayed (sched,
301 GNUNET_TIME_UNIT_SECONDS,
307 stop_arm (struct PeerContext *p)
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
310 "Asking ARM to stop core service\n");
311 p->arm = GNUNET_ARM_connect (p->cfg, sched, NULL);
312 GNUNET_ARM_stop_service (p->arm, "core", GNUNET_TIME_UNIT_SECONDS,
318 * Try again to connect to transport service.
321 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
323 stop_arm (&adv_peer);
324 stop_arm (&learn_peer);
330 struct GNUNET_SCHEDULER_Handle *s,
333 const struct GNUNET_CONFIGURATION_Handle *cfg)
336 adv_arrived = GNUNET_NO;
338 timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
342 check_task = GNUNET_SCHEDULER_add_delayed (sched,
346 GNUNET_SCHEDULER_add_delayed (sched,
347 GNUNET_TIME_UNIT_FOREVER_REL,
350 setup_adv_peer (&adv_peer, "test_learning_adv_peer.conf");
351 setup_learn_peer (&learn_peer, "test_learning_learn_peer.conf");
358 char *const argv[] = { "test-gnunet-daemon-hostlist",
359 "-c", "learning_data.conf",
365 struct GNUNET_GETOPT_CommandLineOption options[] = {
366 GNUNET_GETOPT_OPTION_END
369 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
370 argv, "test-gnunet-daemon-hostlist",
371 "nohelp", options, &run, NULL);
373 if ( (timeout == GNUNET_YES) || (adv_arrived == GNUNET_NO))
380 main (int argc, char *argv[])
385 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
386 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
387 GNUNET_log_setup ("test-gnunet-daemon-hostlist",
395 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
396 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
400 /* end of test_gnunet_daemon_hostlist.c */