2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2015, 2016 GNUnet e.V.
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.
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.
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/>.
19 * @file core/test_core_api.c
20 * @brief testcase for core_api.c
21 * @author Christian Grothoff
24 #include "gnunet_arm_service.h"
25 #include "gnunet_core_service.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_transport_service.h"
28 #include "gnunet_transport_hello_service.h"
29 #include "gnunet_ats_service.h"
35 struct GNUNET_CONFIGURATION_Handle *cfg;
36 struct GNUNET_CORE_Handle *ch;
37 struct GNUNET_PeerIdentity id;
38 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
39 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
40 struct GNUNET_ATS_ConnectivityHandle *ats;
41 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
42 struct GNUNET_MessageHeader *hello;
44 struct GNUNET_OS_Process *arm_proc;
47 static struct PeerContext p1;
49 static struct PeerContext p2;
51 static struct GNUNET_SCHEDULER_Task *err_task;
55 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
59 offer_hello_done (void *cls)
61 struct PeerContext *p = cls;
68 process_hello (void *cls,
69 const struct GNUNET_MessageHeader *message)
71 struct PeerContext *p = cls;
73 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
74 "Received (my) HELLO from transport service\n");
75 GNUNET_assert (message != NULL);
76 if ((p == &p1) && (NULL == p2.oh))
77 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg, message,
80 if ((p == &p2) && (NULL == p1.oh))
81 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
89 terminate_peer (struct PeerContext *p)
93 GNUNET_CORE_disconnect (p->ch);
98 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
103 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
106 if (NULL != p->ats_sh)
108 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
113 GNUNET_ATS_connectivity_done (p->ats);
120 terminate_task (void *cls)
122 GNUNET_assert (ok == 6);
123 terminate_peer (&p1);
124 terminate_peer (&p2);
130 terminate_task_error (void *cls)
132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
133 "ENDING ANGRILY %u\n",
136 terminate_peer (&p1);
137 terminate_peer (&p2);
143 connect_notify (void *cls,
144 const struct GNUNET_PeerIdentity *peer,
145 struct GNUNET_MQ_Handle *mq)
147 struct PeerContext *pc = cls;
148 struct GNUNET_MQ_Envelope *env;
149 struct GNUNET_MessageHeader *msg;
151 if (0 == memcmp (&pc->id,
153 sizeof (struct GNUNET_PeerIdentity)))
154 return (void *) peer;
155 GNUNET_assert (pc->connect_status == 0);
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "Encrypted connection established to peer `%s'\n",
159 pc->connect_status = 1;
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Asking core (1) for transmission to peer `%s'\n",
167 GNUNET_i2s (&p2.id));
168 env = GNUNET_MQ_msg (msg,
170 /* enable corking for this test */
171 extra = GNUNET_CORE_get_mq_options (GNUNET_YES,
172 GNUNET_CORE_PRIO_BEST_EFFORT,
174 GNUNET_MQ_env_set_options (env,
177 /* now actually transmit message */
178 GNUNET_assert (ok == 4);
183 return (void *) peer;
188 disconnect_notify (void *cls,
189 const struct GNUNET_PeerIdentity *peer,
192 struct PeerContext *pc = cls;
194 if (0 == memcmp (&pc->id,
196 sizeof (struct GNUNET_PeerIdentity)))
198 pc->connect_status = 0;
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Encrypted connection to `%s' cut\n",
206 handle_test (void *cls,
207 const struct GNUNET_MessageHeader *message)
209 const struct GNUNET_PeerIdentity *peer = cls;
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Receiving message from `%s'.\n",
214 GNUNET_assert (ok == 5);
216 GNUNET_SCHEDULER_cancel (err_task);
217 err_task = GNUNET_SCHEDULER_add_now (&terminate_task,
223 init_notify (void *cls,
224 const struct GNUNET_PeerIdentity *my_identity)
226 struct PeerContext *p = cls;
227 struct GNUNET_MQ_MessageHandler handlers[] = {
228 GNUNET_MQ_hd_fixed_size (test,
230 struct GNUNET_MessageHeader,
232 GNUNET_MQ_handler_end ()
235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Core connection to `%s' established\n",
237 GNUNET_i2s (my_identity));
238 p->id = *my_identity;
241 GNUNET_assert (ok == 2);
244 p2.ch = GNUNET_CORE_connect (p2.cfg,
253 GNUNET_assert (ok == 3);
255 GNUNET_assert (cls == &p2);
256 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
264 setup_peer (struct PeerContext *p,
269 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
270 p->cfg = GNUNET_CONFIGURATION_create ();
272 GNUNET_OS_start_process (GNUNET_YES,
273 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
276 "gnunet-service-arm",
280 GNUNET_assert (GNUNET_OK ==
281 GNUNET_CONFIGURATION_load (p->cfg,
283 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
284 GNUNET_assert (NULL != p->ats);
285 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
286 GNUNET_TRANSPORT_AC_ANY,
289 GNUNET_free (binary);
297 const struct GNUNET_CONFIGURATION_Handle *cfg)
299 struct GNUNET_MQ_MessageHandler handlers[] = {
300 GNUNET_MQ_hd_fixed_size (test,
302 struct GNUNET_MessageHeader,
304 GNUNET_MQ_handler_end ()
307 GNUNET_assert (ok == 1);
310 "test_core_api_peer1.conf");
312 "test_core_api_peer2.conf");
314 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
315 (GNUNET_TIME_UNIT_SECONDS, 300),
316 &terminate_task_error, NULL);
318 GNUNET_CORE_connect (p1.cfg,
328 stop_arm (struct PeerContext *p)
330 if (0 != GNUNET_OS_process_kill (p->arm_proc,
332 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
335 GNUNET_OS_process_wait (p->arm_proc))
336 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "ARM process %u stopped\n",
340 GNUNET_OS_process_get_pid (p->arm_proc));
341 GNUNET_OS_process_destroy (p->arm_proc);
343 GNUNET_CONFIGURATION_destroy (p->cfg);
351 char *const argv[] = {
354 "test_core_api_data.conf",
357 struct GNUNET_GETOPT_CommandLineOption options[] = {
358 GNUNET_GETOPT_OPTION_END
361 GNUNET_log_setup ("test-core-api",
364 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
373 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
374 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
379 /* end of test_core_api.c */