2 This file is part of GNUnet
3 Copyright (C) 2010, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file hostlist/test_gnunet_daemon_hostlist_reconnect.c
22 * @brief test for gnunet-daemon-hostslist.c; tries to re-start the peers
23 * and connect a second time
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_transport_core_service.h"
30 #include "gnunet_transport_hello_service.h"
33 * How long until we give up on transmitting the message?
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
39 static struct GNUNET_SCHEDULER_Task *timeout_task;
43 struct GNUNET_CONFIGURATION_Handle *cfg;
44 struct GNUNET_TRANSPORT_CoreHandle *th;
45 struct GNUNET_MessageHeader *hello;
46 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
47 struct GNUNET_OS_Process *arm_proc;
50 static struct PeerContext p1;
52 static struct PeerContext p2;
59 timeout_error (void *cls)
62 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
63 "Timeout trying to connect peers, test failed.\n");
64 GNUNET_SCHEDULER_shutdown ();
69 * Function called to notify transport users that another
70 * peer connected to us.
73 * @param peer the peer that connected
74 * @param mq message queue to send to @a peer
78 notify_connect (void *cls,
79 const struct GNUNET_PeerIdentity *peer,
80 struct GNUNET_MQ_Handle *mq)
82 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
83 "Peers connected, shutting down.\n");
85 GNUNET_SCHEDULER_shutdown ();
91 process_hello (void *cls,
92 const struct GNUNET_MessageHeader *message)
94 struct PeerContext *p = cls;
96 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
98 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99 "Received HELLO, starting hostlist service.\n");
104 setup_peer (struct PeerContext *p,
109 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
110 p->cfg = GNUNET_CONFIGURATION_create ();
112 GNUNET_OS_start_process (GNUNET_YES,
113 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
118 "gnunet-service-arm",
122 GNUNET_assert (GNUNET_OK ==
123 GNUNET_CONFIGURATION_load (p->cfg,
125 p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
132 GNUNET_assert (NULL != p->th);
133 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
134 GNUNET_TRANSPORT_AC_ANY,
137 GNUNET_free (binary);
142 waitpid_task (void *cls)
144 struct PeerContext *p = cls;
146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147 "Killing ARM process.\n");
148 if (0 != GNUNET_OS_process_kill (p->arm_proc,
150 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
153 GNUNET_OS_process_wait (p->arm_proc))
154 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "ARM process %u stopped\n",
158 GNUNET_OS_process_get_pid (p->arm_proc));
159 GNUNET_OS_process_destroy (p->arm_proc);
161 GNUNET_CONFIGURATION_destroy (p->cfg);
166 stop_arm (struct PeerContext *p)
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Asking ARM to stop core service\n");
170 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
177 * Try again to connect to transport service.
180 shutdown_task (void *cls)
182 if (NULL != timeout_task)
184 GNUNET_SCHEDULER_cancel (timeout_task);
189 GNUNET_TRANSPORT_hello_get_cancel (p1.ghh);
194 GNUNET_TRANSPORT_core_disconnect (p1.th);
199 GNUNET_TRANSPORT_hello_get_cancel (p2.ghh);
204 GNUNET_TRANSPORT_core_disconnect (p2.th);
216 const struct GNUNET_CONFIGURATION_Handle *cfg)
218 GNUNET_assert (ok == 1);
220 timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
223 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
226 "test_gnunet_daemon_hostlist_peer1.conf");
228 "test_gnunet_daemon_hostlist_peer2.conf");
236 static char *const argv[] = {
237 "test-gnunet-daemon-hostlist",
238 "-c", "test_gnunet_daemon_hostlist_data.conf",
241 static struct GNUNET_GETOPT_CommandLineOption options[] = {
242 GNUNET_GETOPT_OPTION_END
245 GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer1.conf",
247 GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer2.conf",
249 GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_data.conf",
251 GNUNET_log_setup ("test-gnunet-daemon-hostlist",
255 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
257 "test-gnunet-daemon-hostlist",
264 FPRINTF (stderr, "%s", ".");
265 /* now do it again */
267 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
269 "test-gnunet-daemon-hostlist",
278 GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer1.conf",
280 GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer2.conf",
282 GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_data.conf",
287 /* end of test_gnunet_daemon_hostlist_reconnect.c */