2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
21 * @file core/test_core_api.c
22 * @brief testcase for core_api.c
25 #include "gnunet_common.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_core_service.h"
28 #include "gnunet_getopt_lib.h"
29 #include "gnunet_os_lib.h"
30 #include "gnunet_program_lib.h"
31 #include "gnunet_scheduler_lib.h"
32 #include "gnunet_transport_service.h"
38 struct GNUNET_CONFIGURATION_Handle *cfg;
39 struct GNUNET_CORE_Handle *ch;
40 struct GNUNET_PeerIdentity id;
41 struct GNUNET_TRANSPORT_Handle *th;
42 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
43 struct GNUNET_MessageHeader *hello;
45 struct GNUNET_OS_Process *arm_proc;
48 static struct PeerContext p1;
50 static struct PeerContext p2;
52 static GNUNET_SCHEDULER_TaskIdentifier err_task;
54 static GNUNET_SCHEDULER_TaskIdentifier con_task;
58 #define OKPP do { ok++; GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
62 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
64 struct PeerContext *p = cls;
66 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
67 "Received (my) `%s' from transport service\n", "HELLO");
68 GNUNET_assert (message != NULL);
69 if ((p == &p1) && (p2.th != NULL))
70 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
71 if ((p == &p2) && (p1.th != NULL))
72 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
77 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79 GNUNET_assert (ok == 6);
80 GNUNET_CORE_disconnect (p1.ch);
82 GNUNET_CORE_disconnect (p2.ch);
84 GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
86 GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
88 GNUNET_TRANSPORT_disconnect (p1.th);
90 GNUNET_TRANSPORT_disconnect (p2.th);
92 if (GNUNET_SCHEDULER_NO_TASK != con_task)
94 GNUNET_SCHEDULER_cancel (con_task);
95 con_task = GNUNET_SCHEDULER_NO_TASK;
102 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105 "ENDING ANGRILY %u\n", ok);
109 GNUNET_CORE_disconnect (p1.ch);
114 GNUNET_CORE_disconnect (p2.ch);
119 GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
120 GNUNET_TRANSPORT_disconnect (p1.th);
125 GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
126 GNUNET_TRANSPORT_disconnect (p2.th);
129 if (GNUNET_SCHEDULER_NO_TASK != con_task)
131 GNUNET_SCHEDULER_cancel (con_task);
132 con_task = GNUNET_SCHEDULER_NO_TASK;
139 transmit_ready (void *cls, size_t size, void *buf)
141 struct PeerContext *p = cls;
142 struct GNUNET_MessageHeader *m;
144 GNUNET_assert (ok == 4);
146 GNUNET_assert (p == &p1);
147 GNUNET_assert (buf != NULL);
148 m = (struct GNUNET_MessageHeader *) buf;
149 m->type = htons (MTYPE);
150 m->size = htons (sizeof (struct GNUNET_MessageHeader));
151 return sizeof (struct GNUNET_MessageHeader);
156 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
157 const struct GNUNET_ATS_Information *atsi,
158 unsigned int atsi_count)
160 struct PeerContext *pc = cls;
162 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
164 GNUNET_assert (pc->connect_status == 0);
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Encrypted connection established to peer `%4s'\n",
168 if (GNUNET_SCHEDULER_NO_TASK != con_task)
170 GNUNET_SCHEDULER_cancel (con_task);
171 con_task = GNUNET_SCHEDULER_NO_TASK;
173 pc->connect_status = 1;
176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
177 "Asking core (1) for transmission to peer `%4s'\n",
178 GNUNET_i2s (&p2.id));
180 GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_YES, 0,
181 GNUNET_TIME_relative_multiply
182 (GNUNET_TIME_UNIT_SECONDS, 145),
184 sizeof (struct GNUNET_MessageHeader),
185 &transmit_ready, &p1))
187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
188 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
189 GNUNET_i2s (&p2.id));
196 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
198 struct PeerContext *pc = cls;
200 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
202 pc->connect_status = 0;
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
209 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
210 const struct GNUNET_MessageHeader *message,
211 const struct GNUNET_ATS_Information *atsi,
212 unsigned int atsi_count)
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
221 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
222 const struct GNUNET_MessageHeader *message,
223 const struct GNUNET_ATS_Information *atsi,
224 unsigned int atsi_count)
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Core notifies about outbound data for `%4s'.\n",
235 process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
236 const struct GNUNET_MessageHeader *message,
237 const struct GNUNET_ATS_Information *atsi,
238 unsigned int atsi_count)
240 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving message from `%4s'.\n",
242 GNUNET_assert (ok == 5);
244 GNUNET_SCHEDULER_cancel (err_task);
245 err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
250 static struct GNUNET_CORE_MessageHandler handlers[] = {
251 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
257 connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
261 con_task = GNUNET_SCHEDULER_NO_TASK;
265 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &connect_task,
267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
268 "Asking transport (1) to connect to peer `%4s'\n",
269 GNUNET_i2s (&p2.id));
270 GNUNET_TRANSPORT_try_connect (p1.th, &p2.id);
275 init_notify (void *cls, struct GNUNET_CORE_Handle *server,
276 const struct GNUNET_PeerIdentity *my_identity)
278 struct PeerContext *p = cls;
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core connection to `%4s' established\n",
281 GNUNET_i2s (my_identity));
282 GNUNET_assert (server != NULL);
283 p->id = *my_identity;
287 GNUNET_assert (ok == 2);
291 GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
292 &disconnect_notify, &inbound_notify, GNUNET_YES,
293 &outbound_notify, GNUNET_YES, handlers);
297 GNUNET_assert (ok == 3);
299 GNUNET_assert (cls == &p2);
300 con_task = GNUNET_SCHEDULER_add_now (&connect_task, NULL);
306 setup_peer (struct PeerContext *p, const char *cfgname)
310 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-arm");
311 p->cfg = GNUNET_CONFIGURATION_create ();
313 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
316 "gnunet-service-arm",
317 "-c", cfgname, NULL);
318 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
319 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
320 GNUNET_assert (p->th != NULL);
321 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
322 GNUNET_free (binary);
327 run (void *cls, char *const *args, const char *cfgfile,
328 const struct GNUNET_CONFIGURATION_Handle *cfg)
330 GNUNET_assert (ok == 1);
332 setup_peer (&p1, "test_core_api_peer1.conf");
333 setup_peer (&p2, "test_core_api_peer2.conf");
335 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
336 (GNUNET_TIME_UNIT_SECONDS, 300),
337 &terminate_task_error, NULL);
339 GNUNET_CORE_connect (p1.cfg, &p1, &init_notify, &connect_notify,
340 &disconnect_notify, &inbound_notify, GNUNET_YES,
341 &outbound_notify, GNUNET_YES, handlers);
346 stop_arm (struct PeerContext *p)
348 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
349 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
350 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
351 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
353 GNUNET_OS_process_get_pid (p->arm_proc));
354 GNUNET_OS_process_destroy (p->arm_proc);
356 GNUNET_CONFIGURATION_destroy (p->cfg);
363 char *const argv[] = { "test-core-api",
365 "test_core_api_data.conf",
368 struct GNUNET_GETOPT_CommandLineOption options[] = {
369 GNUNET_GETOPT_OPTION_END
372 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
373 "test-core-api", "nohelp", options, &run, &ok);
380 main (int argc, char *argv[])
384 GNUNET_log_setup ("test-core-api",
388 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
389 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
394 /* end of test_core_api.c */