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