e347673d8a49c23659f6fcf98d7b07a5b245836d
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist_reconnect.c
1 /*
2      This file is part of GNUnet
3      (C) 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, 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_service.h"
30
31 #define VERBOSE GNUNET_NO
32
33 #define START_ARM GNUNET_YES
34
35
36 /**
37  * How long until we give up on transmitting the message?
38  */
39 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
40
41 static int ok;
42
43 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
44
45 struct PeerContext
46 {
47   struct GNUNET_CONFIGURATION_Handle *cfg;
48   struct GNUNET_TRANSPORT_Handle *th;
49   struct GNUNET_MessageHeader *hello;
50   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
51 #if START_ARM
52   struct GNUNET_OS_Process *arm_proc;
53 #endif
54 };
55
56 static struct PeerContext p1;
57
58 static struct PeerContext p2;
59
60
61 static void
62 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
63 {
64   if (NULL != p1.ghh)
65   {
66     GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
67     p1.ghh = NULL;
68   }
69   if (p1.th != NULL)
70   {
71     GNUNET_TRANSPORT_disconnect (p1.th);
72     p1.th = NULL;
73   }
74   if (NULL != p2.ghh)
75   {
76     GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
77     p2.ghh = NULL;
78   }
79   if (p2.th != NULL)
80   {
81     GNUNET_TRANSPORT_disconnect (p2.th);
82     p2.th = NULL;
83   }
84   GNUNET_SCHEDULER_shutdown ();
85 }
86
87 /**
88  * Timeout, give up.
89  */
90 static void
91 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
92 {
93   timeout_task = GNUNET_SCHEDULER_NO_TASK;
94   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
95               "Timeout trying to connect peers, test failed.\n");
96   clean_up (NULL, tc);
97 }
98
99
100 /**
101  * Function called to notify transport users that another
102  * peer connected to us.
103  *
104  * @param cls closure
105  * @param peer the peer that connected
106  * @param latency current latency of the connection
107  * @param distance in overlay hops, as given by transport plugin
108  */
109 static void
110 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
111                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
112 {
113   if (peer == NULL)
114     return;
115 #if VERBOSE
116   FPRINTF (stderr, "Peer %s connected\n", GNUNET_i2s (peer));
117 #endif
118   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected, shutting down.\n");
119   ok = 0;
120   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
121   {
122     GNUNET_SCHEDULER_cancel (timeout_task);
123     timeout_task = GNUNET_SCHEDULER_NO_TASK;
124   }
125   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
126 }
127
128
129 static void
130 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
131 {
132   struct PeerContext *p = cls;
133
134   GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
135   p->ghh = NULL;
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137               "Received HELLO, starting hostlist service.\n");
138 }
139
140
141 static void
142 setup_peer (struct PeerContext *p, const char *cfgname)
143 {
144   p->cfg = GNUNET_CONFIGURATION_create ();
145 #if START_ARM
146   p->arm_proc =
147       GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
148                                "gnunet-service-arm",
149 #if VERBOSE
150                                "-L", "DEBUG",
151 #endif
152                                "-c", cfgname, NULL);
153 #endif
154   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
155   p->th =
156       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, &notify_connect, NULL);
157   GNUNET_assert (p->th != NULL);
158   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
159 }
160
161
162 static void
163 waitpid_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 {
165   struct PeerContext *p = cls;
166
167 #if START_ARM
168   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
169   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
170     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
171   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
172     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
173   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
174               GNUNET_OS_process_get_pid (p->arm_proc));
175   GNUNET_OS_process_close (p->arm_proc);
176   p->arm_proc = NULL;
177 #endif
178   GNUNET_CONFIGURATION_destroy (p->cfg);
179 }
180
181
182 static void
183 stop_arm (struct PeerContext *p)
184 {
185   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking ARM to stop core service\n");
186   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &waitpid_task, p);
187 }
188
189
190 /**
191  * Try again to connect to transport service.
192  */
193 static void
194 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
195 {
196   stop_arm (&p1);
197   stop_arm (&p2);
198 }
199
200
201 static void
202 run (void *cls, char *const *args, const char *cfgfile,
203      const struct GNUNET_CONFIGURATION_Handle *cfg)
204 {
205   GNUNET_assert (ok == 1);
206   ok++;
207   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
208   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
209                                 NULL);
210   setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
211   setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
212 }
213
214
215 static int
216 check ()
217 {
218   char *const argv[] = { "test-gnunet-daemon-hostlist",
219     "-c", "test_gnunet_daemon_hostlist_data.conf",
220 #if VERBOSE
221     "-L", "DEBUG",
222 #endif
223     NULL
224   };
225   struct GNUNET_GETOPT_CommandLineOption options[] = {
226     GNUNET_GETOPT_OPTION_END
227   };
228   ok = 1;
229   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
230                       "test-gnunet-daemon-hostlist", "nohelp", options, &run,
231                       &ok);
232   return ok;
233 }
234
235
236 int
237 main (int argc, char *argv[])
238 {
239
240   int ret;
241
242   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
243   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
244   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
245   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
246 #if VERBOSE
247                     "DEBUG",
248 #else
249                     "WARNING",
250 #endif
251                     NULL);
252   ret = check ();
253   if (ret == 0)
254   {
255     FPRINTF (stderr, "%s",  ".");
256     /* now do it again */
257     ret = check ();
258     FPRINTF (stderr, "%s",  ".\n");
259   }
260   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
261   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
262   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
263   return ret;
264 }
265
266 /* end of test_gnunet_daemon_hostlist_reconnect.c */