docu
[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 /**
32  * How long until we give up on transmitting the message?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
35
36 static int ok;
37
38 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
39
40 struct PeerContext
41 {
42   struct GNUNET_CONFIGURATION_Handle *cfg;
43   struct GNUNET_TRANSPORT_Handle *th;
44   struct GNUNET_MessageHeader *hello;
45   struct GNUNET_TRANSPORT_GetHelloHandle *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, const struct GNUNET_SCHEDULER_TaskContext *tc)
56 {
57   if (NULL != p1.ghh)
58   {
59     GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
60     p1.ghh = NULL;
61   }
62   if (p1.th != NULL)
63   {
64     GNUNET_TRANSPORT_disconnect (p1.th);
65     p1.th = NULL;
66   }
67   if (NULL != p2.ghh)
68   {
69     GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
70     p2.ghh = NULL;
71   }
72   if (p2.th != NULL)
73   {
74     GNUNET_TRANSPORT_disconnect (p2.th);
75     p2.th = NULL;
76   }
77   GNUNET_SCHEDULER_shutdown ();
78 }
79
80 /**
81  * Timeout, give up.
82  */
83 static void
84 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 {
86   timeout_task = GNUNET_SCHEDULER_NO_TASK;
87   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
88               "Timeout trying to connect peers, test failed.\n");
89   clean_up (NULL, tc);
90 }
91
92
93 /**
94  * Function called to notify transport users that another
95  * peer connected to us.
96  *
97  * @param cls closure
98  * @param peer the peer that connected
99  * @param latency current latency of the connection
100  * @param distance in overlay hops, as given by transport plugin
101  */
102 static void
103 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
104                 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
105 {
106   if (peer == NULL)
107     return;
108   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected, shutting down.\n");
109   ok = 0;
110   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
111   {
112     GNUNET_SCHEDULER_cancel (timeout_task);
113     timeout_task = GNUNET_SCHEDULER_NO_TASK;
114   }
115   GNUNET_SCHEDULER_add_now (&clean_up, 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_get_hello_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 =
139     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, binary,
140                                "gnunet-service-arm",
141                                "-c", cfgname, NULL);
142   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
143   p->th =
144       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, &notify_connect, NULL);
145   GNUNET_assert (p->th != NULL);
146   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
147   GNUNET_free (binary);
148 }
149
150
151 static void
152 waitpid_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
153 {
154   struct PeerContext *p = cls;
155
156   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
157   if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
158     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
159   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
160     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
161   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
162               GNUNET_OS_process_get_pid (p->arm_proc));
163   GNUNET_OS_process_destroy (p->arm_proc);
164   p->arm_proc = NULL;
165   GNUNET_CONFIGURATION_destroy (p->cfg);
166 }
167
168
169 static void
170 stop_arm (struct PeerContext *p)
171 {
172   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asking ARM to stop core service\n");
173   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &waitpid_task, p);
174 }
175
176
177 /**
178  * Try again to connect to transport service.
179  */
180 static void
181 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
182 {
183   stop_arm (&p1);
184   stop_arm (&p2);
185 }
186
187
188 static void
189 run (void *cls, char *const *args, const char *cfgfile,
190      const struct GNUNET_CONFIGURATION_Handle *cfg)
191 {
192   GNUNET_assert (ok == 1);
193   ok++;
194   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &timeout_error, NULL);
195   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &shutdown_task,
196                                 NULL);
197   setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
198   setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
199 }
200
201
202 static int
203 check ()
204 {
205   char *const argv[] = { "test-gnunet-daemon-hostlist",
206     "-c", "test_gnunet_daemon_hostlist_data.conf",
207     NULL
208   };
209   struct GNUNET_GETOPT_CommandLineOption options[] = {
210     GNUNET_GETOPT_OPTION_END
211   };
212   ok = 1;
213   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
214                       "test-gnunet-daemon-hostlist", "nohelp", options, &run,
215                       &ok);
216   return ok;
217 }
218
219
220 int
221 main (int argc, char *argv[])
222 {
223
224   int ret;
225
226   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
227   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
228   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
229   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
230                     "WARNING",
231                     NULL);
232   ret = check ();
233   if (ret == 0)
234   {
235     FPRINTF (stderr, "%s",  ".");
236     /* now do it again */
237     ret = check ();
238     FPRINTF (stderr, "%s",  ".\n");
239   }
240   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
241   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
242   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-3");
243   return ret;
244 }
245
246 /* end of test_gnunet_daemon_hostlist_reconnect.c */