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
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.
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.
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.
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"
37 struct GNUNET_CONFIGURATION_Handle *cfg;
38 struct GNUNET_CORE_Handle *ch;
39 struct GNUNET_PeerIdentity id;
40 struct GNUNET_TRANSPORT_OfferHelloHandle *oh;
41 struct GNUNET_TRANSPORT_HelloGetHandle *ghh;
42 struct GNUNET_ATS_ConnectivityHandle *ats;
43 struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
44 struct GNUNET_MessageHeader *hello;
46 struct GNUNET_OS_Process *arm_proc;
49 static struct PeerContext p1;
51 static struct PeerContext p2;
53 static struct GNUNET_SCHEDULER_Task *err_task;
57 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
61 offer_hello_done (void *cls)
63 struct PeerContext *p = cls;
70 process_hello (void *cls,
71 const struct GNUNET_MessageHeader *message)
73 struct PeerContext *p = cls;
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76 "Received (my) HELLO from transport service\n");
77 GNUNET_assert (message != NULL);
78 if ((p == &p1) && (NULL == p2.oh))
79 p2.oh = GNUNET_TRANSPORT_offer_hello (p2.cfg, message,
82 if ((p == &p2) && (NULL == p1.oh))
83 p1.oh = GNUNET_TRANSPORT_offer_hello (p1.cfg,
91 terminate_peer (struct PeerContext *p)
95 GNUNET_CORE_disconnect (p->ch);
100 GNUNET_TRANSPORT_hello_get_cancel (p->ghh);
105 GNUNET_TRANSPORT_offer_hello_cancel (p->oh);
108 if (NULL != p->ats_sh)
110 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
115 GNUNET_ATS_connectivity_done (p->ats);
122 terminate_task (void *cls)
124 GNUNET_assert (ok == 6);
125 terminate_peer (&p1);
126 terminate_peer (&p2);
132 terminate_task_error (void *cls)
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "ENDING ANGRILY %u\n",
138 terminate_peer (&p1);
139 terminate_peer (&p2);
145 connect_notify (void *cls,
146 const struct GNUNET_PeerIdentity *peer,
147 struct GNUNET_MQ_Handle *mq)
149 struct PeerContext *pc = cls;
150 struct GNUNET_MQ_Envelope *env;
151 struct GNUNET_MessageHeader *msg;
153 if (0 == memcmp (&pc->id,
155 sizeof (struct GNUNET_PeerIdentity)))
156 return (void *) peer;
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;
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Asking core (1) for transmission to peer `%s'\n",
169 GNUNET_i2s (&p2.id));
170 env = GNUNET_MQ_msg (msg,
172 /* enable corking for this test */
173 extra = GNUNET_CORE_get_mq_options (GNUNET_YES,
174 GNUNET_CORE_PRIO_BEST_EFFORT,
176 GNUNET_MQ_env_set_options (env,
179 /* now actually transmit message */
180 GNUNET_assert (ok == 4);
185 return (void *) peer;
190 disconnect_notify (void *cls,
191 const struct GNUNET_PeerIdentity *peer,
194 struct PeerContext *pc = cls;
196 if (0 == memcmp (&pc->id,
198 sizeof (struct GNUNET_PeerIdentity)))
200 pc->connect_status = 0;
201 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
202 "Encrypted connection to `%s' cut\n",
208 handle_test (void *cls,
209 const struct GNUNET_MessageHeader *message)
211 const struct GNUNET_PeerIdentity *peer = cls;
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
214 "Receiving message from `%s'.\n",
216 GNUNET_assert (ok == 5);
218 GNUNET_SCHEDULER_cancel (err_task);
219 err_task = GNUNET_SCHEDULER_add_now (&terminate_task,
225 init_notify (void *cls,
226 const struct GNUNET_PeerIdentity *my_identity)
228 struct PeerContext *p = cls;
229 struct GNUNET_MQ_MessageHandler handlers[] = {
230 GNUNET_MQ_hd_fixed_size (test,
232 struct GNUNET_MessageHeader,
234 GNUNET_MQ_handler_end ()
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Core connection to `%s' established\n",
239 GNUNET_i2s (my_identity));
240 p->id = *my_identity;
243 GNUNET_assert (ok == 2);
246 p2.ch = GNUNET_CORE_connect (p2.cfg,
255 GNUNET_assert (ok == 3);
257 GNUNET_assert (cls == &p2);
258 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
266 setup_peer (struct PeerContext *p,
271 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
272 p->cfg = GNUNET_CONFIGURATION_create ();
274 GNUNET_OS_start_process (GNUNET_YES,
275 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
278 "gnunet-service-arm",
282 GNUNET_assert (GNUNET_OK ==
283 GNUNET_CONFIGURATION_load (p->cfg,
285 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
286 GNUNET_assert (NULL != p->ats);
287 p->ghh = GNUNET_TRANSPORT_hello_get (p->cfg,
288 GNUNET_TRANSPORT_AC_ANY,
291 GNUNET_free (binary);
299 const struct GNUNET_CONFIGURATION_Handle *cfg)
301 struct GNUNET_MQ_MessageHandler handlers[] = {
302 GNUNET_MQ_hd_fixed_size (test,
304 struct GNUNET_MessageHeader,
306 GNUNET_MQ_handler_end ()
309 GNUNET_assert (ok == 1);
312 "test_core_api_peer1.conf");
314 "test_core_api_peer2.conf");
316 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
317 (GNUNET_TIME_UNIT_SECONDS, 300),
318 &terminate_task_error, NULL);
320 GNUNET_CORE_connect (p1.cfg,
330 stop_arm (struct PeerContext *p)
332 if (0 != GNUNET_OS_process_kill (p->arm_proc,
334 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
337 GNUNET_OS_process_wait (p->arm_proc))
338 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING,
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
341 "ARM process %u stopped\n",
342 GNUNET_OS_process_get_pid (p->arm_proc));
343 GNUNET_OS_process_destroy (p->arm_proc);
345 GNUNET_CONFIGURATION_destroy (p->cfg);
353 char *const argv[] = {
356 "test_core_api_data.conf",
359 struct GNUNET_GETOPT_CommandLineOption options[] = {
360 GNUNET_GETOPT_OPTION_END
363 GNUNET_log_setup ("test-core-api",
366 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
375 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
376 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
381 /* end of test_core_api.c */