rps: store valid peer ids in file
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2009 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, 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
31 /**
32  * How long until we give up on transmitting the message?
33  */
34 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 150)
35
36 static int ok;
37
38 static struct GNUNET_SCHEDULER_Task *timeout_task;
39
40 struct PeerContext
41 {
42   struct GNUNET_CONFIGURATION_Handle *cfg;
43   struct GNUNET_TRANSPORT_Handle *th;
44   struct GNUNET_MessageHeader *hello;
45   struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
46   struct GNUNET_OS_Process *arm_proc;
47 };
48
49 static struct PeerContext p1;
50
51 static struct PeerContext p2;
52
53
54 static void
55 clean_up (void *cls)
56 {
57   if (p1.th != NULL)
58   {
59     if (p1.ghh != NULL)
60     {
61       GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
62       p1.ghh = NULL;
63     }
64     GNUNET_TRANSPORT_disconnect (p1.th);
65     p1.th = NULL;
66   }
67   if (p2.th != NULL)
68   {
69     if (p2.ghh != NULL)
70     {
71       GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
72       p2.ghh = NULL;
73     }
74     GNUNET_TRANSPORT_disconnect (p2.th);
75     p2.th = NULL;
76   }
77   GNUNET_SCHEDULER_shutdown ();
78 }
79
80 /**
81  * Timeout, give up.
82  */
83 static void
84 timeout_error (void *cls)
85 {
86   timeout_task = NULL;
87   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
88               "Timeout trying to connect peers, test failed.\n");
89   clean_up (NULL);
90 }
91
92
93 /**
94  * Function called to notify transport users that another
95  * peer connected to us.
96  *
97  * @param cls closure
98  * @param peer the peer that connected
99  * @param latency current latency of the connection
100  * @param distance in overlay hops, as given by transport plugin
101  */
102 static void
103 notify_connect (void *cls,
104                 const struct GNUNET_PeerIdentity *peer)
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 != NULL)
112   {
113     GNUNET_SCHEDULER_cancel (timeout_task);
114     timeout_task = NULL;
115   }
116   GNUNET_SCHEDULER_add_now (&clean_up, NULL);
117 }
118
119
120 static void
121 process_hello (void *cls,
122                const struct GNUNET_MessageHeader *message)
123 {
124   struct PeerContext *p = cls;
125
126   GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
127   p->ghh = NULL;
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   char *binary;
137
138   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
139   p->cfg = GNUNET_CONFIGURATION_create ();
140   p->arm_proc =
141     GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
142                              NULL, NULL, NULL,
143                              binary,
144                              "gnunet-service-arm",
145                              "-c", cfgname, NULL);
146   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
147   p->th =
148       GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, &notify_connect, NULL);
149   GNUNET_assert (p->th != NULL);
150   p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
151   GNUNET_free (binary);
152 }
153
154
155 static void
156 waitpid_task (void *cls)
157 {
158   struct PeerContext *p = cls;
159
160   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Killing ARM process.\n");
161   if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
162     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
163   if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
164     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
165   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
166               GNUNET_OS_process_get_pid (p->arm_proc));
167   GNUNET_OS_process_destroy (p->arm_proc);
168   p->arm_proc = NULL;
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 (GNUNET_TIME_UNIT_SECONDS,
179                                 &waitpid_task, p);
180 }
181
182
183 /**
184  * Try again to connect to transport service.
185  */
186 static void
187 shutdown_task (void *cls)
188 {
189   stop_arm (&p1);
190   stop_arm (&p2);
191 }
192
193
194 static void
195 run (void *cls,
196      char *const *args,
197      const char *cfgfile,
198      const struct GNUNET_CONFIGURATION_Handle *cfg)
199 {
200   GNUNET_assert (ok == 1);
201   ok++;
202   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
203                                                &timeout_error,
204                                                NULL);
205   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
206                                  NULL);
207   setup_peer (&p1, "test_gnunet_daemon_hostlist_peer1.conf");
208   setup_peer (&p2, "test_gnunet_daemon_hostlist_peer2.conf");
209 }
210
211
212 static int
213 check ()
214 {
215   char *const argv[] = { "test-gnunet-daemon-hostlist",
216     "-c", "test_gnunet_daemon_hostlist_data.conf",
217     NULL
218   };
219   struct GNUNET_GETOPT_CommandLineOption options[] = {
220     GNUNET_GETOPT_OPTION_END
221   };
222   ok = 1;
223   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
224                       "test-gnunet-daemon-hostlist", "nohelp", options, &run,
225                       &ok);
226   return ok;
227 }
228
229
230 int
231 main (int argc, char *argv[])
232 {
233   int ret;
234
235   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
236   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
237   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist");
238   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
239                     "WARNING",
240                     NULL);
241   ret = check ();
242   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-1");
243   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist-peer-2");
244   GNUNET_DISK_directory_remove ("/tmp/test-gnunet-hostlist");
245   return ret;
246 }
247
248 /* end of test_gnunet_daemon_hostlist.c */