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