longer timeout to make test work on slower machines, but the problem is still that...
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist.c
1 /*
2      This file is part of GNUnet
3      (C) 2009 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 2, 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.c
22  * @brief test for gnunet_daemon_hostslist.c
23  * @author Christian Grothoff
24  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_transport_service.h"
29
30 #define VERBOSE GNUNET_YES
31
32 #define START_ARM GNUNET_YES
33
34
35 /**
36  * How long until we give up on transmitting the message?
37  */
38 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
39
40 static int ok;
41
42 static struct GNUNET_SCHEDULER_Handle *sched;
43
44 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
45     
46 struct PeerContext
47 {
48   struct GNUNET_CONFIGURATION_Handle *cfg;
49   struct GNUNET_TRANSPORT_Handle *th;
50   struct GNUNET_MessageHeader *hello;
51   struct GNUNET_ARM_Handle *arm;
52 #if START_ARM
53   pid_t arm_pid;
54 #endif
55 };
56
57 static struct PeerContext p1;
58
59 static struct PeerContext p2;
60
61
62 static void
63 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
64 {
65   if (p1.th != NULL)
66     {
67       GNUNET_TRANSPORT_disconnect (p1.th);
68       p1.th = NULL;
69     }
70   if (p2.th != NULL)
71     {
72       GNUNET_TRANSPORT_disconnect (p2.th);
73       p2.th = NULL;
74     }
75   GNUNET_SCHEDULER_shutdown (sched);
76 }
77
78 /**
79  * Timeout, give up.
80  */
81 static void
82 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 {
84   timeout_task = GNUNET_SCHEDULER_NO_TASK;
85   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
86               "Timeout trying to connect peers, test failed.\n");
87   clean_up (NULL, tc);
88 }
89
90
91 /**
92  * Function called to notify transport users that another
93  * peer connected to us.
94  *
95  * @param cls closure
96  * @param peer the peer that connected
97  * @param latency current latency of the connection
98  * @param distance in overlay hops, as given by transport plugin
99  */
100 static void
101 notify_connect (void *cls,
102                 const struct GNUNET_PeerIdentity * peer,
103                 struct GNUNET_TIME_Relative latency,
104                 unsigned int distance)
105 {
106   if (peer == NULL)
107     return;
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 (sched,
114                                timeout_task);
115       timeout_task = GNUNET_SCHEDULER_NO_TASK;
116     }
117   GNUNET_SCHEDULER_add_delayed (sched,
118                                 GNUNET_TIME_UNIT_MINUTES,
119                                 &clean_up, NULL);
120 }
121
122
123 static void
124 process_hello (void *cls,
125                const struct GNUNET_MessageHeader *message)
126 {
127   struct PeerContext *p = cls;
128
129   GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
130   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131               "Received HELLO, starting hostlist service.\n");
132   GNUNET_ARM_start_services (p->cfg, sched, "hostlist", NULL);
133 }
134
135
136 static void
137 setup_peer (struct PeerContext *p, const char *cfgname)
138 {
139   p->cfg = GNUNET_CONFIGURATION_create ();
140 #if START_ARM
141   p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
142                                         "gnunet-service-arm",
143 #if VERBOSE
144                                         "-L", "DEBUG",
145 #endif
146                                         "-c", cfgname, NULL);
147 #endif
148   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
149   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
150   p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, 
151                                     &notify_connect, NULL);
152   GNUNET_assert (p->th != NULL);
153   GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
154 }
155
156
157 static void
158 waitpid_task (void *cls, 
159               const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   struct PeerContext *p = cls;
162
163 #if START_ARM 
164   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165               "Killing ARM process.\n");
166   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
167     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
168   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
169     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
170   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171               "ARM process %u stopped\n", p->arm_pid);
172 #endif
173   GNUNET_CONFIGURATION_destroy (p->cfg);
174 }
175
176
177 static void
178 stop_cb (void *cls, 
179          int success)
180 {
181   struct PeerContext *p = cls;
182
183   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
184               success 
185               ? "ARM stopped core service\n" 
186               : "ARM failed to stop core service\n");
187   GNUNET_ARM_disconnect (p->arm);
188   p->arm = NULL;
189   /* make sure this runs after all other tasks are done */
190   GNUNET_SCHEDULER_add_delayed (sched,
191                                 GNUNET_TIME_UNIT_SECONDS,
192                                 &waitpid_task, p);
193 }
194
195
196 static void
197 stop_arm (struct PeerContext *p)
198 {
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200               "Asking ARM to stop core service\n");
201   p->arm = GNUNET_ARM_connect (p->cfg, sched, NULL);
202   GNUNET_ARM_stop_service (p->arm, "core", GNUNET_TIME_UNIT_SECONDS,
203                            &stop_cb, p);
204 }
205
206
207 /**
208  * Try again to connect to transport service.
209  */
210 static void
211 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
212 {
213   stop_arm (&p1);
214   stop_arm (&p2);
215 }
216
217
218 static void
219 run (void *cls,
220      struct GNUNET_SCHEDULER_Handle *s,
221      char *const *args,
222      const char *cfgfile, 
223      const struct GNUNET_CONFIGURATION_Handle *cfg)
224 {
225   GNUNET_assert (ok == 1);
226   ok++;
227   sched = s;
228   timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
229                                                GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS,
230                                                                               15),
231                                                &timeout_error,
232                                                NULL);
233   GNUNET_SCHEDULER_add_delayed (sched,
234                                 GNUNET_TIME_UNIT_FOREVER_REL,
235                                 &shutdown_task,
236                                 NULL);
237   setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
238   setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
239 }
240
241
242 static int
243 check ()
244 {
245   char *const argv[] = { "test-gnunet-daemon-hostlist",
246     "-c", "test_gnunet_daemon_hostlist_data.conf",
247 #if VERBOSE
248     "-L", "DEBUG",
249 #endif
250     NULL
251   };
252   struct GNUNET_GETOPT_CommandLineOption options[] = {
253     GNUNET_GETOPT_OPTION_END
254   };
255   ok = 1;
256   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
257                       argv, "test-gnunet-daemon-hostlist",
258                       "nohelp", options, &run, &ok);
259   return ok;
260 }
261
262
263 int
264 main (int argc, char *argv[])
265 {
266   
267   int ret;
268
269   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
270   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
271   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
272 #if VERBOSE
273                     "DEBUG",
274 #else
275                     "WARNING",
276 #endif
277                     NULL);
278   ret = check ();
279   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
280   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
281   return ret; 
282 }
283
284 /* end of test_gnunet_daemon_hostlist.c */