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_transport_service.h"
30 #define VERBOSE GNUNET_YES
32 #define START_ARM GNUNET_YES
36 * How long until we give up on transmitting the message?
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
42 static struct GNUNET_SCHEDULER_Handle *sched;
44 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
48 struct GNUNET_CONFIGURATION_Handle *cfg;
49 struct GNUNET_TRANSPORT_Handle *th;
50 struct GNUNET_MessageHeader *hello;
51 struct GNUNET_ARM_Handle *arm;
57 static struct PeerContext p1;
59 static struct PeerContext p2;
63 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
67 GNUNET_TRANSPORT_disconnect (p1.th);
72 GNUNET_TRANSPORT_disconnect (p2.th);
75 GNUNET_SCHEDULER_shutdown (sched);
82 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 timeout_task = GNUNET_SCHEDULER_NO_TASK;
85 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
86 "Timeout trying to connect peers, test failed.\n");
92 * Function called to notify transport users that another
93 * peer connected to us.
96 * @param peer the peer that connected
97 * @param latency current latency of the connection
100 notify_connect (void *cls,
101 const struct GNUNET_PeerIdentity * peer,
102 struct GNUNET_TIME_Relative latency)
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107 "Peers connected, shutting down.\n");
109 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
111 GNUNET_SCHEDULER_cancel (sched,
113 timeout_task = GNUNET_SCHEDULER_NO_TASK;
115 GNUNET_SCHEDULER_add_delayed (sched,
116 GNUNET_TIME_UNIT_MINUTES,
122 process_hello (void *cls,
123 struct GNUNET_TIME_Relative latency,
124 const struct GNUNET_PeerIdentity *peer,
125 const struct GNUNET_MessageHeader *message)
127 struct PeerContext *p = cls;
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "Received HELLO, starting hostlist service.\n");
133 GNUNET_ARM_start_services (p->cfg, sched, "hostlist", NULL);
138 setup_peer (struct PeerContext *p, const char *cfgname)
140 p->cfg = GNUNET_CONFIGURATION_create ();
142 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
143 "gnunet-service-arm",
147 "-c", cfgname, NULL);
149 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
150 GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
151 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL,
152 ¬ify_connect, NULL);
153 GNUNET_assert (p->th != NULL);
154 GNUNET_TRANSPORT_get_hello (p->th, TIMEOUT, &process_hello, p);
159 waitpid_task (void *cls,
160 const struct GNUNET_SCHEDULER_TaskContext *tc)
162 struct PeerContext *p = cls;
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Killing ARM process.\n");
167 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
168 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
169 if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
170 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "ARM process %u stopped\n", p->arm_pid);
174 GNUNET_CONFIGURATION_destroy (p->cfg);
182 struct PeerContext *p = cls;
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 ? "ARM stopped core service\n"
187 : "ARM failed to stop core service\n");
188 GNUNET_ARM_disconnect (p->arm);
190 /* make sure this runs after all other tasks are done */
191 GNUNET_SCHEDULER_add_delayed (sched,
192 GNUNET_TIME_UNIT_SECONDS,
198 stop_arm (struct PeerContext *p)
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "Asking ARM to stop core service\n");
202 p->arm = GNUNET_ARM_connect (p->cfg, sched, NULL);
203 GNUNET_ARM_stop_service (p->arm, "core", GNUNET_TIME_UNIT_SECONDS,
209 * Try again to connect to transport service.
212 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
221 struct GNUNET_SCHEDULER_Handle *s,
224 const struct GNUNET_CONFIGURATION_Handle *cfg)
226 GNUNET_assert (ok == 1);
229 timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
230 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
234 GNUNET_SCHEDULER_add_delayed (sched,
235 GNUNET_TIME_UNIT_FOREVER_REL,
238 setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
239 setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
246 char *const argv[] = { "test-gnunet-daemon-hostlist",
247 "-c", "test_gnunet_daemon_hostlist_data.conf",
253 struct GNUNET_GETOPT_CommandLineOption options[] = {
254 GNUNET_GETOPT_OPTION_END
257 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
258 argv, "test-gnunet-daemon-hostlist",
259 "nohelp", options, &run, &ok);
265 main (int argc, char *argv[])
270 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
271 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
272 GNUNET_log_setup ("test-gnunet-daemon-hostlist",
280 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
281 GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
285 /* end of test_gnunet_daemon_hostlist.c */