-remove debug message
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2009, 2016 GNUnet e.V.
4
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.
9
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.
14
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/>.
17
18      SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 /**
21  * @file hostlist/test_gnunet_daemon_hostlist.c
22  * @brief test for gnunet_daemon_hostslist.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet_transport_hello_service.h"
30
31
32 /**
33  * How long until we give up on transmitting the message?
34  */
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
36
37 static int ok;
38
39 static struct GNUNET_SCHEDULER_Task *timeout_task;
40
41 struct PeerContext
42 {
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;
48 };
49
50 static struct PeerContext p1;
51
52 static struct PeerContext p2;
53
54
55 static void
56 clean_up (void *cls)
57 {
58   if (NULL != p1.th)
59   {
60     if (NULL != p1.ghh)
61     {
62       GNUNET_TRANSPORT_hello_get_cancel (p1.ghh);
63       p1.ghh = NULL;
64     }
65     GNUNET_TRANSPORT_core_disconnect (p1.th);
66     p1.th = NULL;
67   }
68   if (NULL != p2.th)
69   {
70     if (NULL != p2.ghh)
71     {
72       GNUNET_TRANSPORT_hello_get_cancel (p2.ghh);
73       p2.ghh = NULL;
74     }
75     GNUNET_TRANSPORT_core_disconnect (p2.th);
76     p2.th = NULL;
77   }
78   GNUNET_SCHEDULER_shutdown ();
79 }
80
81
82 /**
83  * Timeout, give up.
84  */
85 static void
86 timeout_error (void *cls)
87 {
88   timeout_task = NULL;
89   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
90               "Timeout trying to connect peers, test failed.\n");
91   clean_up (NULL);
92 }
93
94
95 /**
96  * Function called to notify transport users that another
97  * peer connected to us.
98  *
99  * @param cls closure
100  * @param peer the peer that connected
101  * @param mq message queue to send messages to the peer
102  */
103 static void *
104 notify_connect (void *cls,
105                 const struct GNUNET_PeerIdentity *peer,
106                 struct GNUNET_MQ_Handle *mq)
107 {
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected, shutting down.\n");
109   ok = 0;
110   if (NULL != timeout_task)
111   {
112     GNUNET_SCHEDULER_cancel (timeout_task);
113     timeout_task = NULL;
114   }
115   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
116   return NULL;
117 }
118
119
120 static void
121 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
122 {
123   struct PeerContext *p = cls;
124
125   GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
126   p->ghh = NULL;
127   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128               "Received HELLO, starting hostlist service.\n");
129 }
130
131
132 static void
133 setup_peer (struct PeerContext *p, const char *cfgname)
134 {
135   char *binary;
136
137   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
138   p->cfg = GNUNET_CONFIGURATION_create ();
139   p->arm_proc = GNUNET_OS_start_process (GNUNET_YES,
140                                          GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
141                                          NULL,
142                                          NULL,
143                                          NULL,
144                                          binary,
145                                          "gnunet-service-arm",
146                                          "-c",
147                                          cfgname,
148                                          NULL);
149   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
150   p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
151                                          NULL,
152                                          NULL,
153                                          p,
154                                          &notify_connect,
155                                          NULL,
156                                          NULL);
157   GNUNET_assert (NULL != p->th);
158   p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
159                                        GNUNET_TRANSPORT_AC_ANY,
160                                        &process_hello,
161                                        p);
162   GNUNET_free (binary);
163 }
164
165
166 static void
167 waitpid_task (void *cls)
168 {
169   struct PeerContext *p = cls;
170
171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
172   if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
173     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
174   if (GNUNET_OK != GNUNET_OS_process_wait (p->arm_proc))
175     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
176   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177               "ARM process %u stopped\n",
178               GNUNET_OS_process_get_pid (p->arm_proc));
179   GNUNET_OS_process_destroy (p->arm_proc);
180   p->arm_proc = NULL;
181   GNUNET_CONFIGURATION_destroy (p->cfg);
182 }
183
184
185 static void
186 stop_arm (struct PeerContext *p)
187 {
188   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking ARM to stop core service\n");
189   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &waitpid_task, p);
190 }
191
192
193 /**
194  * Try again to connect to transport service.
195  */
196 static void
197 shutdown_task (void *cls)
198 {
199   stop_arm (&p1);
200   stop_arm (&p2);
201 }
202
203
204 static void
205 run (void *cls,
206      char *const *args,
207      const char *cfgfile,
208      const struct GNUNET_CONFIGURATION_Handle *cfg)
209 {
210   GNUNET_assert (ok == 1);
211   ok++;
212   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
213   GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL);
214   setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
215   setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
216 }
217
218
219 static int
220 check ()
221 {
222   char *const argv[] = { "test-gnunet-daemon-hostlist",
223                          "-c",
224                          "test_gnunet_daemon_hostlist_data.conf",
225                          NULL };
226   struct GNUNET_GETOPT_CommandLineOption options[] =
227   { GNUNET_GETOPT_OPTION_END };
228
229   ok = 1;
230   GNUNET_PROGRAM_run ((sizeof(argv) / sizeof(char *)) - 1,
231                       argv,
232                       "test-gnunet-daemon-hostlist",
233                       "nohelp",
234                       options,
235                       &run,
236                       &ok);
237   return ok;
238 }
239
240
241 int
242 main (int argc, char *argv[])
243 {
244   int ret;
245
246   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer1.conf",
247                              "GNUNET_TEST_HOME");
248   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer2.conf",
249                              "GNUNET_TEST_HOME");
250   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_data.conf",
251                              "GNUNET_TEST_HOME");
252   GNUNET_log_setup ("test-gnunet-daemon-hostlist", "WARNING", NULL);
253   ret = check ();
254   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer1.conf",
255                              "GNUNET_TEST_HOME");
256   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer2.conf",
257                              "GNUNET_TEST_HOME");
258   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_data.conf",
259                              "GNUNET_TEST_HOME");
260   return ret;
261 }
262
263
264 /* end of test_gnunet_daemon_hostlist.c */