-wip token endpoint fix
[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
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.
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      General Public License for more details.
14
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
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_core_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,
83               "Peers connected, shutting down.\n");
84   ok = 0;
85   GNUNET_SCHEDULER_shutdown ();
86   return NULL;
87 }
88
89
90 static void
91 process_hello (void *cls,
92                const struct GNUNET_MessageHeader *message)
93 {
94   struct PeerContext *p = cls;
95
96   GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
97   p->ghh = NULL;
98   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
99               "Received HELLO, starting hostlist service.\n");
100 }
101
102
103 static void
104 setup_peer (struct PeerContext *p,
105             const char *cfgname)
106 {
107   char *binary;
108
109   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
110   p->cfg = GNUNET_CONFIGURATION_create ();
111   p->arm_proc =
112     GNUNET_OS_start_process (GNUNET_YES,
113                              GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
114                              NULL,
115                              NULL,
116                              NULL,
117                              binary,
118                              "gnunet-service-arm",
119                              "-c",
120                              cfgname,
121                              NULL);
122   GNUNET_assert (GNUNET_OK ==
123                  GNUNET_CONFIGURATION_load (p->cfg,
124                                             cfgname));
125   p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
126                                          NULL,
127                                          NULL,
128                                          p,
129                                          &notify_connect,
130                                          NULL,
131                                          NULL);
132   GNUNET_assert (NULL != p->th);
133   p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
134                                        GNUNET_TRANSPORT_AC_ANY,
135                                        &process_hello,
136                                        p);
137   GNUNET_free (binary);
138 }
139
140
141 static void
142 waitpid_task (void *cls)
143 {
144   struct PeerContext *p = cls;
145
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147               "Killing ARM process.\n");
148   if (0 != GNUNET_OS_process_kill (p->arm_proc,
149                                    GNUNET_TERM_SIG))
150     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
151                          "kill");
152   if (GNUNET_OK !=
153       GNUNET_OS_process_wait (p->arm_proc))
154     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
155                          "waitpid");
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);
160   p->arm_proc = NULL;
161   GNUNET_CONFIGURATION_destroy (p->cfg);
162 }
163
164
165 static void
166 stop_arm (struct PeerContext *p)
167 {
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169               "Asking ARM to stop core service\n");
170   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
171                                 &waitpid_task,
172                                 p);
173 }
174
175
176 /**
177  * Try again to connect to transport service.
178  */
179 static void
180 shutdown_task (void *cls)
181 {
182   if (NULL != timeout_task)
183   {
184     GNUNET_SCHEDULER_cancel (timeout_task);
185     timeout_task = NULL;
186   }
187   if (NULL != p1.ghh)
188   {
189     GNUNET_TRANSPORT_hello_get_cancel (p1.ghh);
190     p1.ghh = NULL;
191   }
192   if (NULL != p1.th)
193   {
194     GNUNET_TRANSPORT_core_disconnect (p1.th);
195     p1.th = NULL;
196   }
197   if (NULL != p2.ghh)
198   {
199     GNUNET_TRANSPORT_hello_get_cancel (p2.ghh);
200     p2.ghh = NULL;
201   }
202   if (NULL != p2.th)
203   {
204     GNUNET_TRANSPORT_core_disconnect (p2.th);
205     p2.th = NULL;
206   }
207   stop_arm (&p1);
208   stop_arm (&p2);
209 }
210
211
212 static void
213 run (void *cls,
214      char *const *args,
215      const char *cfgfile,
216      const struct GNUNET_CONFIGURATION_Handle *cfg)
217 {
218   GNUNET_assert (ok == 1);
219   ok++;
220   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
221                                                &timeout_error,
222                                                NULL);
223   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
224                                  NULL);
225   setup_peer (&p1,
226               "test_gnunet_daemon_hostlist_peer1.conf");
227   setup_peer (&p2,
228               "test_gnunet_daemon_hostlist_peer2.conf");
229 }
230
231
232 int
233 main (int argcx,
234       char *argvx[])
235 {
236   static char *const argv[] = {
237     "test-gnunet-daemon-hostlist",
238     "-c", "test_gnunet_daemon_hostlist_data.conf",
239     NULL
240   };
241   static struct GNUNET_GETOPT_CommandLineOption options[] = {
242     GNUNET_GETOPT_OPTION_END
243   };
244
245   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
246   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
247   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
248   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
249                     "WARNING",
250                     NULL);
251   ok = 1;
252   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
253                       argv,
254                       "test-gnunet-daemon-hostlist",
255                       "nohelp",
256                       options,
257                       &run,
258                       &ok);
259   if (0 == ok)
260   {
261     FPRINTF (stderr, "%s",  ".");
262     /* now do it again */
263     ok = 1;
264     GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
265                         argv,
266                         "test-gnunet-daemon-hostlist",
267                         "nohelp",
268                         options,
269                         &run,
270                         &ok);
271     FPRINTF (stderr,
272              "%s",
273              ".\n");
274   }
275   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
276   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
277   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
278   return ok;
279 }
280
281 /* end of test_gnunet_daemon_hostlist_reconnect.c */