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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
21 * @file core/test_core_api.c
22 * @brief testcase for core_api.c
23 * @author Christian Grothoff
26 #include "gnunet_arm_service.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_transport_service.h"
30 #include "gnunet_transport_hello_service.h"
31 #include "gnunet_ats_service.h"
36 struct GNUNET_CONFIGURATION_Handle *cfg;
37 struct GNUNET_CORE_Handle *ch;
38 struct GNUNET_PeerIdentity id;
39 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
40 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
41 struct GNUNET_ATS_ConnectivityHandle *ats;
42 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
43 struct GNUNET_MessageHeader *hello;
45 struct GNUNET_OS_Process *arm_proc;
48 static struct PeerContext p1;
50 static struct PeerContext p2;
52 static struct GNUNET_SCHEDULER_Task *err_task;
60 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, \
61 "Now at stage %u at %s:%u\n", \
69 offer_hello_done(void *cls)
71 struct PeerContext *p = cls;
78 process_hello(void *cls, const struct GNUNET_MessageHeader *message)
80 struct PeerContext *p = cls;
82 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
83 "Received (my) HELLO from transport service\n");
84 GNUNET_assert(message != NULL);
85 if ((p == &p1) && (NULL == p2.oh))
87 GNUNET_TRANSPORT_offer_hello(p2.cfg, message, &offer_hello_done, &p2);
88 if ((p == &p2) && (NULL == p1.oh))
90 GNUNET_TRANSPORT_offer_hello(p1.cfg, message, &offer_hello_done, &p1);
95 terminate_peer(struct PeerContext *p)
99 GNUNET_CORE_disconnect(p->ch);
104 GNUNET_TRANSPORT_hello_get_cancel(p->ghh);
109 GNUNET_TRANSPORT_offer_hello_cancel(p->oh);
112 if (NULL != p->ats_sh)
114 GNUNET_ATS_connectivity_suggest_cancel(p->ats_sh);
119 GNUNET_ATS_connectivity_done(p->ats);
126 terminate_task(void *cls)
128 GNUNET_assert(ok == 6);
136 terminate_task_error(void *cls)
138 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "ENDING ANGRILY %u\n", ok);
147 connect_notify(void *cls,
148 const struct GNUNET_PeerIdentity *peer,
149 struct GNUNET_MQ_Handle *mq)
151 struct PeerContext *pc = cls;
152 struct GNUNET_MQ_Envelope *env;
153 struct GNUNET_MessageHeader *msg;
155 if (0 == memcmp(&pc->id, peer, sizeof(struct GNUNET_PeerIdentity)))
157 GNUNET_assert(pc->connect_status == 0);
158 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
159 "Encrypted connection established to peer `%s'\n",
161 pc->connect_status = 1;
164 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
165 "Asking core (1) for transmission to peer `%s'\n",
167 env = GNUNET_MQ_msg(msg, MTYPE);
168 /* enable corking for this test */
169 GNUNET_MQ_env_set_options(env,
170 GNUNET_MQ_PRIO_BEST_EFFORT |
171 GNUNET_MQ_PREF_CORK_ALLOWED);
172 /* now actually transmit message */
173 GNUNET_assert(ok == 4);
175 GNUNET_MQ_send(mq, env);
182 disconnect_notify(void *cls,
183 const struct GNUNET_PeerIdentity *peer,
186 struct PeerContext *pc = cls;
188 if (0 == memcmp(&pc->id, peer, sizeof(struct GNUNET_PeerIdentity)))
190 pc->connect_status = 0;
191 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
192 "Encrypted connection to `%s' cut\n",
198 handle_test(void *cls, const struct GNUNET_MessageHeader *message)
200 const struct GNUNET_PeerIdentity *peer = cls;
202 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
203 "Receiving message from `%s'.\n",
205 GNUNET_assert(ok == 5);
207 GNUNET_SCHEDULER_cancel(err_task);
208 err_task = GNUNET_SCHEDULER_add_now(&terminate_task, NULL);
213 init_notify(void *cls, const struct GNUNET_PeerIdentity *my_identity)
215 struct PeerContext *p = cls;
216 struct GNUNET_MQ_MessageHandler handlers[] =
217 { GNUNET_MQ_hd_fixed_size(test, MTYPE, struct GNUNET_MessageHeader, NULL),
218 GNUNET_MQ_handler_end() };
220 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
221 "Core connection to `%s' established\n",
222 GNUNET_i2s(my_identity));
223 p->id = *my_identity;
226 GNUNET_assert(ok == 2);
229 p2.ch = GNUNET_CORE_connect(p2.cfg,
238 GNUNET_assert(ok == 3);
240 GNUNET_assert(cls == &p2);
241 p1.ats_sh = GNUNET_ATS_connectivity_suggest(p1.ats, &p2.id, 1);
247 setup_peer(struct PeerContext *p, const char *cfgname)
251 binary = GNUNET_OS_get_libexec_binary_path("gnunet-service-arm");
252 p->cfg = GNUNET_CONFIGURATION_create();
253 p->arm_proc = GNUNET_OS_start_process(GNUNET_YES,
254 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
259 "gnunet-service-arm",
263 GNUNET_assert(GNUNET_OK == GNUNET_CONFIGURATION_load(p->cfg, cfgname));
264 p->ats = GNUNET_ATS_connectivity_init(p->cfg);
265 GNUNET_assert(NULL != p->ats);
266 p->ghh = GNUNET_TRANSPORT_hello_get(p->cfg,
267 GNUNET_TRANSPORT_AC_ANY,
278 const struct GNUNET_CONFIGURATION_Handle *cfg)
280 struct GNUNET_MQ_MessageHandler handlers[] =
281 { GNUNET_MQ_hd_fixed_size(test, MTYPE, struct GNUNET_MessageHeader, NULL),
282 GNUNET_MQ_handler_end() };
284 GNUNET_assert(ok == 1);
286 setup_peer(&p1, "test_core_api_peer1.conf");
287 setup_peer(&p2, "test_core_api_peer2.conf");
288 err_task = GNUNET_SCHEDULER_add_delayed(
289 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 300),
290 &terminate_task_error,
292 p1.ch = GNUNET_CORE_connect(p1.cfg,
302 stop_arm(struct PeerContext *p)
304 if (0 != GNUNET_OS_process_kill(p->arm_proc, GNUNET_TERM_SIG))
305 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "kill");
306 if (GNUNET_OK != GNUNET_OS_process_wait(p->arm_proc))
307 GNUNET_log_strerror(GNUNET_ERROR_TYPE_WARNING, "waitpid");
308 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
309 "ARM process %u stopped\n",
310 GNUNET_OS_process_get_pid(p->arm_proc));
311 GNUNET_OS_process_destroy(p->arm_proc);
313 GNUNET_CONFIGURATION_destroy(p->cfg);
318 main(int argc, char *argv1[])
320 char *const argv[] = { "test-core-api", "-c", "test_core_api_data.conf", NULL };
321 struct GNUNET_GETOPT_CommandLineOption options[] = { GNUNET_GETOPT_OPTION_END };
324 GNUNET_log_setup("test-core-api", "WARNING", NULL);
325 GNUNET_PROGRAM_run((sizeof(argv) / sizeof(char *)) - 1,
334 GNUNET_DISK_directory_remove("/tmp/test-gnunet-core-peer-1");
335 GNUNET_DISK_directory_remove("/tmp/test-gnunet-core-peer-2");
340 /* end of test_core_api.c */