(no commit message)
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist_learning.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_core_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, 10)
40
41 static int timeout;
42 static int adv_arrived;
43
44 static struct GNUNET_SCHEDULER_Handle *sched;
45
46 static GNUNET_SCHEDULER_TaskIdentifier timeout_task;
47     
48 struct PeerContext
49 {
50   struct GNUNET_CONFIGURATION_Handle *cfg;
51   struct GNUNET_TRANSPORT_Handle *th;
52   struct GNUNET_MessageHeader *hello;
53   struct GNUNET_ARM_Handle *arm;
54   struct GNUNET_CORE_Handle *core;
55 #if START_ARM
56   pid_t arm_pid;
57 #endif
58 };
59
60 static struct PeerContext adv_peer;
61
62 static struct PeerContext learn_peer;
63
64 static void
65 clean_up (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
66 {
67   if (adv_peer.th != NULL)
68     {
69       GNUNET_TRANSPORT_disconnect (adv_peer.th);
70       adv_peer.th = NULL;
71     }
72   if (learn_peer.th != NULL)
73     {
74       GNUNET_TRANSPORT_disconnect (learn_peer.th);
75       learn_peer.th = NULL;
76     }
77   if (adv_peer.core != NULL)
78     {
79       GNUNET_CORE_disconnect (adv_peer.core);
80       adv_peer.core = NULL;
81     }
82   if (learn_peer.core != NULL)
83     {
84       GNUNET_CORE_disconnect (learn_peer.core);
85       learn_peer.core = NULL;
86     }
87   GNUNET_SCHEDULER_shutdown (sched);
88 }
89
90 /**
91  * Timeout, give up.
92  */
93 static void
94 timeout_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
95 {
96   timeout_task = GNUNET_SCHEDULER_NO_TASK;
97   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
98               "Timeout while executing testcase, test failed.\n");
99   timeout = GNUNET_YES;
100   clean_up (NULL, tc);
101 }
102
103 /**
104  * Core handler for p2p hostlist advertisements
105  */
106 static void ad_arrive_handler (void *cls,
107                              const struct GNUNET_PeerIdentity * peer,
108                              const struct GNUNET_MessageHeader * message,
109                              struct GNUNET_TIME_Relative latency,
110                              uint32_t distance)
111 {
112   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
113               "A ADV message, notifying client and server\n");
114   if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
115     {
116       GNUNET_SCHEDULER_cancel (sched,
117                                timeout_task);
118       timeout_task = GNUNET_SCHEDULER_NO_TASK;
119     }
120   adv_arrived = GNUNET_YES;
121   GNUNET_SCHEDULER_add_now (sched,
122                             &clean_up, NULL);
123 }
124
125 static void
126 connect_handler (void *cls,
127                  const struct
128                  GNUNET_PeerIdentity * peer,
129                  struct GNUNET_TIME_Relative latency,
130                  uint32_t distance)
131 {
132   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133               "A new peer connected, notifying client and server\n");
134 }
135
136 /**
137  * List of handlers if we are learning.
138  */
139 static struct GNUNET_CORE_MessageHandler learn_handlers[] = {
140   { &ad_arrive_handler, GNUNET_MESSAGE_TYPE_HOSTLIST_ADVERTISEMENT, 0},
141   { NULL, 0, 0 }
142 };
143
144 static void
145 setup_learn_peer (struct PeerContext *p, const char *cfgname)
146 {
147   p->cfg = GNUNET_CONFIGURATION_create ();
148 #if START_ARM
149   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
150                                         "gnunet-service-arm",
151 #if VERBOSE
152                                         "-L", "DEBUG",
153 #endif
154                                         "-c", cfgname, NULL);
155 #endif
156   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
157   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
158
159   p->core = GNUNET_CORE_connect (sched, p->cfg,
160                               GNUNET_TIME_UNIT_FOREVER_REL,
161                               NULL,
162                               NULL,
163                               &connect_handler, NULL,
164                               NULL, GNUNET_NO,
165                               NULL, GNUNET_NO,
166                               learn_handlers );
167   GNUNET_assert ( NULL != p->core );
168
169
170 }
171
172
173 static void
174 setup_adv_peer (struct PeerContext *p, const char *cfgname)
175 {
176   p->cfg = GNUNET_CONFIGURATION_create ();
177 #if START_ARM
178   p->arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
179                                         "gnunet-service-arm",
180 #if VERBOSE
181                                         "-L", "DEBUG",
182 #endif
183                                         "-c", cfgname, NULL);
184 #endif
185   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
186   GNUNET_ARM_start_services (p->cfg, sched, "core", NULL);
187 }
188
189
190
191 static void
192 waitpid_task (void *cls, 
193               const struct GNUNET_SCHEDULER_TaskContext *tc)
194 {
195   struct PeerContext *p = cls;
196
197 #if START_ARM 
198   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
199               "Killing ARM process.\n");
200   if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
201     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
202   if (GNUNET_OS_process_wait(p->arm_pid) != GNUNET_OK)
203     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
204   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205               "ARM process %u stopped\n", p->arm_pid);
206 #endif
207   GNUNET_CONFIGURATION_destroy (p->cfg);
208 }
209
210
211 static void
212 stop_cb (void *cls, 
213          int success)
214 {
215   struct PeerContext *p = cls;
216
217   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218               success
219               ? "ARM stopped core service\n"
220               : "ARM failed to stop core service\n");
221   GNUNET_ARM_disconnect (p->arm);
222   p->arm = NULL;
223   /* make sure this runs after all other tasks are done */
224   GNUNET_SCHEDULER_add_delayed (sched,
225                                 GNUNET_TIME_UNIT_SECONDS,
226                                 &waitpid_task, p);
227 }
228
229
230 static void
231 stop_arm (struct PeerContext *p)
232 {
233   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234               "Asking ARM to stop core service\n");
235   p->arm = GNUNET_ARM_connect (p->cfg, sched, NULL);
236   GNUNET_ARM_stop_service (p->arm, "core", GNUNET_TIME_UNIT_SECONDS,
237                            &stop_cb, p);
238 }
239
240
241 /**
242  * Try again to connect to transport service.
243  */
244 static void
245 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
246 {
247   stop_arm (&adv_peer);
248   stop_arm (&learn_peer);
249 }
250
251
252 static void
253 run (void *cls,
254      struct GNUNET_SCHEDULER_Handle *s,
255      char *const *args,
256      const char *cfgfile, 
257      const struct GNUNET_CONFIGURATION_Handle *cfg)
258 {
259   timeout = GNUNET_NO;
260   adv_arrived = GNUNET_NO;
261   sched = s;
262   timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
263                                                TIMEOUT,
264                                                &timeout_error,
265                                                NULL);
266   GNUNET_SCHEDULER_add_delayed (sched,
267                                 GNUNET_TIME_UNIT_FOREVER_REL,
268                                 &shutdown_task,
269                                 NULL);
270   setup_adv_peer (&adv_peer, "test_learning_adv_peer.conf");
271   setup_learn_peer (&learn_peer, "test_learning_learn_peer.conf");
272 }
273
274
275 static int
276 check ()
277 {
278   char *const argv[] = { "test-gnunet-daemon-hostlist",
279     "-c", "learning_data.conf",
280 #if VERBOSE
281     "-L", "DEBUG",
282 #endif
283     NULL
284   };
285   struct GNUNET_GETOPT_CommandLineOption options[] = {
286     GNUNET_GETOPT_OPTION_END
287   };
288
289   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
290                       argv, "test-gnunet-daemon-hostlist",
291                       "nohelp", options, &run, NULL);
292
293   if ( (timeout == GNUNET_YES) || (adv_arrived == GNUNET_NO))
294     return GNUNET_YES;
295   else
296     return GNUNET_NO;
297 }
298
299 int
300 main (int argc, char *argv[])
301 {
302   
303   int ret;
304
305   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
306   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
307   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
308 #if VERBOSE
309                     "DEBUG",
310 #else
311                     "WARNING",
312 #endif
313                     NULL);
314   ret = check ();
315   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-1");
316   GNUNET_DISK_directory_remove ("/tmp/test-gnunetd-hostlist-peer-2");
317   return ret; 
318 }
319
320 /* end of test_gnunet_daemon_hostlist.c */