2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2015 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
25 #include "gnunet_arm_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_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_Handle *th;
39 struct GNUNET_TRANSPORT_GetHelloHandle *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 process_hello (void *cls,
60 const struct GNUNET_MessageHeader *message)
62 struct PeerContext *p = cls;
64 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
65 "Received (my) `%s' from transport service\n", "HELLO");
66 GNUNET_assert (message != NULL);
67 if ((p == &p1) && (p2.th != NULL))
68 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
69 if ((p == &p2) && (p1.th != NULL))
70 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
75 terminate_peer (struct PeerContext *p)
79 GNUNET_CORE_disconnect (p->ch);
84 GNUNET_TRANSPORT_get_hello_cancel (p->ghh);
85 GNUNET_TRANSPORT_disconnect (p->th);
88 if (NULL != p->ats_sh)
90 GNUNET_ATS_connectivity_suggest_cancel (p->ats_sh);
95 GNUNET_ATS_connectivity_done (p->ats);
102 terminate_task (void *cls,
103 const struct GNUNET_SCHEDULER_TaskContext *tc)
105 GNUNET_assert (ok == 6);
106 terminate_peer (&p1);
107 terminate_peer (&p2);
113 terminate_task_error (void *cls,
114 const struct GNUNET_SCHEDULER_TaskContext *tc)
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "ENDING ANGRILY %u\n",
120 terminate_peer (&p1);
121 terminate_peer (&p2);
127 transmit_ready (void *cls, size_t size, void *buf)
129 struct PeerContext *p = cls;
130 struct GNUNET_MessageHeader *m;
132 GNUNET_assert (ok == 4);
134 GNUNET_assert (p == &p1);
135 GNUNET_assert (NULL != buf);
136 m = (struct GNUNET_MessageHeader *) buf;
137 m->type = htons (MTYPE);
138 m->size = htons (sizeof (struct GNUNET_MessageHeader));
139 return sizeof (struct GNUNET_MessageHeader);
144 connect_notify (void *cls,
145 const struct GNUNET_PeerIdentity *peer)
147 struct PeerContext *pc = cls;
149 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
151 GNUNET_assert (pc->connect_status == 0);
152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
153 "Encrypted connection established to peer `%4s'\n",
155 pc->connect_status = 1;
158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
159 "Asking core (1) for transmission to peer `%4s'\n",
160 GNUNET_i2s (&p2.id));
162 GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_YES,
163 GNUNET_CORE_PRIO_BEST_EFFORT,
164 GNUNET_TIME_relative_multiply
165 (GNUNET_TIME_UNIT_SECONDS, 145),
167 sizeof (struct GNUNET_MessageHeader),
168 &transmit_ready, &p1))
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
172 GNUNET_i2s (&p2.id));
179 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
181 struct PeerContext *pc = cls;
183 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
185 pc->connect_status = 0;
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
192 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
193 const struct GNUNET_MessageHeader *message)
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
202 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
203 const struct GNUNET_MessageHeader *message)
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Core notifies about outbound data for `%4s'.\n",
213 process_mtype (void *cls,
214 const struct GNUNET_PeerIdentity *peer,
215 const struct GNUNET_MessageHeader *message)
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218 "Receiving message from `%4s'.\n",
220 GNUNET_assert (ok == 5);
222 GNUNET_SCHEDULER_cancel (err_task);
223 err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
228 static struct GNUNET_CORE_MessageHandler handlers[] = {
229 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
235 init_notify (void *cls,
236 const struct GNUNET_PeerIdentity *my_identity)
238 struct PeerContext *p = cls;
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
241 "Core connection to `%4s' established\n",
242 GNUNET_i2s (my_identity));
243 p->id = *my_identity;
246 GNUNET_assert (ok == 2);
250 GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
251 &disconnect_notify, &inbound_notify, GNUNET_YES,
252 &outbound_notify, GNUNET_YES, handlers);
256 GNUNET_assert (ok == 3);
258 GNUNET_assert (cls == &p2);
259 p1.ats_sh = GNUNET_ATS_connectivity_suggest (p1.ats,
267 setup_peer (struct PeerContext *p,
272 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
273 p->cfg = GNUNET_CONFIGURATION_create ();
275 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
278 "gnunet-service-arm",
279 "-c", cfgname, NULL);
280 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
281 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
282 GNUNET_assert (NULL != p->th);
283 p->ats = GNUNET_ATS_connectivity_init (p->cfg);
284 GNUNET_assert (NULL != p->ats);
285 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
286 GNUNET_free (binary);
294 const struct GNUNET_CONFIGURATION_Handle *cfg)
296 GNUNET_assert (ok == 1);
298 setup_peer (&p1, "test_core_api_peer1.conf");
299 setup_peer (&p2, "test_core_api_peer2.conf");
301 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
302 (GNUNET_TIME_UNIT_SECONDS, 300),
303 &terminate_task_error, NULL);
305 GNUNET_CORE_connect (p1.cfg, &p1,
309 &inbound_notify, GNUNET_YES,
310 &outbound_notify, GNUNET_YES,
316 stop_arm (struct PeerContext *p)
318 if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
319 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
320 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
321 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
323 GNUNET_OS_process_get_pid (p->arm_proc));
324 GNUNET_OS_process_destroy (p->arm_proc);
326 GNUNET_CONFIGURATION_destroy (p->cfg);
331 main (int argc, char *argv1[])
333 char *const argv[] = { "test-core-api",
335 "test_core_api_data.conf",
338 struct GNUNET_GETOPT_CommandLineOption options[] = {
339 GNUNET_GETOPT_OPTION_END
342 GNUNET_log_setup ("test-core-api",
345 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
346 "test-core-api", "nohelp", options, &run, &ok);
349 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
350 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
355 /* end of test_core_api.c */