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