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