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