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