c3292939fcc72f9f75bbb3c3fcfd0379d9032086
[oweals/gnunet.git] / src / hostlist / test_gnunet_daemon_hostlist.c
1 /*
2      This file is part of GNUnet
3      Copyright (C) 2009, 2016 GNUnet e.V.
4
5      GNUnet is free software: you can redistribute it and/or modify it
6      under the terms of the GNU Affero General Public License as published
7      by the Free Software Foundation, either version 3 of the License,
8      or (at your 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      Affero General Public License for more details.
14     
15      You should have received a copy of the GNU Affero General Public License
16      along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 /**
19  * @file hostlist/test_gnunet_daemon_hostlist.c
20  * @brief test for gnunet_daemon_hostslist.c
21  * @author Christian Grothoff
22  */
23 #include "platform.h"
24 #include "gnunet_util_lib.h"
25 #include "gnunet_arm_service.h"
26 #include "gnunet_transport_service.h"
27 #include "gnunet_transport_core_service.h"
28 #include "gnunet_transport_hello_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_CoreHandle *th;
44   struct GNUNET_MessageHeader *hello;
45   struct GNUNET_TRANSPORT_HelloGetHandle *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 (NULL != p1.th)
58   {
59     if (NULL != p1.ghh)
60     {
61       GNUNET_TRANSPORT_hello_get_cancel (p1.ghh);
62       p1.ghh = NULL;
63     }
64     GNUNET_TRANSPORT_core_disconnect (p1.th);
65     p1.th = NULL;
66   }
67   if (NULL != p2.th)
68   {
69     if (NULL != p2.ghh)
70     {
71       GNUNET_TRANSPORT_hello_get_cancel (p2.ghh);
72       p2.ghh = NULL;
73     }
74     GNUNET_TRANSPORT_core_disconnect (p2.th);
75     p2.th = NULL;
76   }
77   GNUNET_SCHEDULER_shutdown ();
78 }
79
80
81 /**
82  * Timeout, give up.
83  */
84 static void
85 timeout_error (void *cls)
86 {
87   timeout_task = NULL;
88   GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
89               "Timeout trying to connect peers, test failed.\n");
90   clean_up (NULL);
91 }
92
93
94 /**
95  * Function called to notify transport users that another
96  * peer connected to us.
97  *
98  * @param cls closure
99  * @param peer the peer that connected
100  * @param mq message queue to send messages to the peer
101  */
102 static void *
103 notify_connect (void *cls,
104                 const struct GNUNET_PeerIdentity *peer,
105                 struct GNUNET_MQ_Handle *mq)
106 {
107   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
108               "Peers connected, shutting down.\n");
109   ok = 0;
110   if (NULL != timeout_task)
111   {
112     GNUNET_SCHEDULER_cancel (timeout_task);
113     timeout_task = NULL;
114   }
115   GNUNET_SCHEDULER_add_now (&clean_up,
116                             NULL);
117   return 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_hello_get_cancel (p->ghh);
128   p->ghh = NULL;
129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
130               "Received HELLO, starting hostlist service.\n");
131 }
132
133
134 static void
135 setup_peer (struct PeerContext *p,
136             const char *cfgname)
137 {
138   char *binary;
139
140   binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
141   p->cfg = GNUNET_CONFIGURATION_create ();
142   p->arm_proc =
143     GNUNET_OS_start_process (GNUNET_YES,
144                              GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
145                              NULL,
146                              NULL,
147                              NULL,
148                              binary,
149                              "gnunet-service-arm",
150                              "-c",
151                              cfgname,
152                              NULL);
153   GNUNET_assert (GNUNET_OK ==
154                  GNUNET_CONFIGURATION_load (p->cfg,
155                                             cfgname));
156   p->th = GNUNET_TRANSPORT_core_connect (p->cfg,
157                                          NULL,
158                                          NULL,
159                                          p,
160                                          &notify_connect,
161                                          NULL,
162                                          NULL);
163   GNUNET_assert (NULL != p->th);
164   p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
165                                        GNUNET_TRANSPORT_AC_ANY,
166                                        &process_hello,
167                                        p);
168   GNUNET_free (binary);
169 }
170
171
172 static void
173 waitpid_task (void *cls)
174 {
175   struct PeerContext *p = cls;
176
177   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
178               "Killing ARM process.\n");
179   if (0 != GNUNET_OS_process_kill (p->arm_proc,
180                                    GNUNET_TERM_SIG))
181     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
182                          "kill");
183   if (GNUNET_OK !=
184       GNUNET_OS_process_wait (p->arm_proc))
185     GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
186                          "waitpid");
187   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188               "ARM process %u stopped\n",
189               GNUNET_OS_process_get_pid (p->arm_proc));
190   GNUNET_OS_process_destroy (p->arm_proc);
191   p->arm_proc = NULL;
192   GNUNET_CONFIGURATION_destroy (p->cfg);
193 }
194
195
196 static void
197 stop_arm (struct PeerContext *p)
198 {
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200               "Asking ARM to stop core service\n");
201   GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
202                                 &waitpid_task,
203                                 p);
204 }
205
206
207 /**
208  * Try again to connect to transport service.
209  */
210 static void
211 shutdown_task (void *cls)
212 {
213   stop_arm (&p1);
214   stop_arm (&p2);
215 }
216
217
218 static void
219 run (void *cls,
220      char *const *args,
221      const char *cfgfile,
222      const struct GNUNET_CONFIGURATION_Handle *cfg)
223 {
224   GNUNET_assert (ok == 1);
225   ok++;
226   timeout_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
227                                                &timeout_error,
228                                                NULL);
229   GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
230                                  NULL);
231   setup_peer (&p1,
232               "test_gnunet_daemon_hostlist_peer1.conf");
233   setup_peer (&p2,
234               "test_gnunet_daemon_hostlist_peer2.conf");
235 }
236
237
238 static int
239 check ()
240 {
241   char *const argv[] = {
242     "test-gnunet-daemon-hostlist",
243     "-c", "test_gnunet_daemon_hostlist_data.conf",
244     NULL
245   };
246   struct GNUNET_GETOPT_CommandLineOption options[] = {
247     GNUNET_GETOPT_OPTION_END
248   };
249   ok = 1;
250   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
251                       argv,
252                       "test-gnunet-daemon-hostlist",
253                       "nohelp",
254                       options,
255                       &run,
256                       &ok);
257   return ok;
258 }
259
260
261 int
262 main (int argc, char *argv[])
263 {
264   int ret;
265
266   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer1.conf",
267                              "GNUNET_TEST_HOME");
268   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer2.conf",
269                              "GNUNET_TEST_HOME");
270   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_data.conf",
271                              "GNUNET_TEST_HOME");
272   GNUNET_log_setup ("test-gnunet-daemon-hostlist",
273                     "WARNING",
274                     NULL);
275   ret = check ();
276   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer1.conf",
277                              "GNUNET_TEST_HOME");
278   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_peer2.conf",
279                              "GNUNET_TEST_HOME");
280   GNUNET_DISK_purge_cfg_dir ("test_gnunet_daemon_hostlist_data.conf",
281                              "GNUNET_TEST_HOME");
282   return ret;
283 }
284
285 /* end of test_gnunet_daemon_hostlist.c */