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 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 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_EXTRA_LOGGING
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 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
46 struct GNUNET_CONFIGURATION_Handle *cfg;
47 struct GNUNET_TRANSPORT_Handle *th;
48 struct GNUNET_MessageHeader *hello;
49 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
51 struct GNUNET_OS_Process *arm_proc;
55 static struct PeerContext p1;
57 static struct PeerContext p2;
61 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
65 GNUNET_TRANSPORT_disconnect (p1.th);
70 GNUNET_TRANSPORT_disconnect (p2.th);
73 GNUNET_SCHEDULER_shutdown ();
80 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
82 timeout_task = GNUNET_SCHEDULER_NO_TASK;
83 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
84 "Timeout trying to connect peers, test failed.\n");
90 * Function called to notify transport users that another
91 * peer connected to us.
94 * @param peer the peer that connected
95 * @param latency current latency of the connection
96 * @param distance in overlay hops, as given by transport plugin
99 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
100 const struct GNUNET_ATS_Information *ats,
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected, shutting down.\n");
107 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
109 GNUNET_SCHEDULER_cancel (timeout_task);
110 timeout_task = GNUNET_SCHEDULER_NO_TASK;
112 GNUNET_SCHEDULER_add_now (&clean_up, NULL);
117 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
119 struct PeerContext *p = cls;
121 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
123 "Received HELLO, starting hostlist service.\n");
128 setup_peer (struct PeerContext *p, const char *cfgname)
130 p->cfg = GNUNET_CONFIGURATION_create ();
133 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
134 "gnunet-service-arm",
138 "-c", cfgname, NULL);
140 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
142 GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, ¬ify_connect, NULL);
143 GNUNET_assert (p->th != NULL);
144 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
149 waitpid_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
151 struct PeerContext *p = cls;
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
155 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
156 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
157 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
158 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
160 GNUNET_OS_process_get_pid (p->arm_proc));
161 GNUNET_OS_process_close (p->arm_proc);
164 GNUNET_CONFIGURATION_destroy (p->cfg);
169 stop_arm (struct PeerContext *p)
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking ARM to stop core service\n");
172 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &waitpid_task, p);
177 * Try again to connect to transport service.
180 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
188 run (void *cls, char *const *args, const char *cfgfile,
189 const struct GNUNET_CONFIGURATION_Handle *cfg)
191 GNUNET_assert (ok == 1);
193 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
194 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
196 setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
197 setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
204 char *const argv[] = { "test-gnunet-daemon-hostlist",
205 "-c", "test_gnunet_daemon_hostlist_data.conf",
211 struct GNUNET_GETOPT_CommandLineOption options[] = {
212 GNUNET_GETOPT_OPTION_END
215 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
216 "test-gnunet-daemon-hostlist", "nohelp", options, &run,
223 main (int argc, char *argv[])
228 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
229 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
230 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist");
231 GNUNET_log_setup ("test-gnunet-daemon-hostlist",
239 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
240 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
241 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist");
245 /* end of test_gnunet_daemon_hostlist.c */