2 This file is part of GNUnet.
3 Copyright (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_arm_service.h"
26 #include "gnunet_core_service.h"
27 #include "gnunet_getopt_lib.h"
28 #include "gnunet_os_lib.h"
29 #include "gnunet_program_lib.h"
30 #include "gnunet_scheduler_lib.h"
31 #include "gnunet_transport_service.h"
37 struct GNUNET_CONFIGURATION_Handle *cfg;
38 struct GNUNET_CORE_Handle *ch;
39 struct GNUNET_PeerIdentity id;
40 struct GNUNET_TRANSPORT_Handle *th;
41 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
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;
53 static struct GNUNET_SCHEDULER_Task * con_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 process_hello (void *cls,
62 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,
78 const struct GNUNET_SCHEDULER_TaskContext *tc)
80 GNUNET_assert (ok == 6);
81 GNUNET_CORE_disconnect (p1.ch);
83 GNUNET_CORE_disconnect (p2.ch);
85 GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
87 GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
89 GNUNET_TRANSPORT_disconnect (p1.th);
91 GNUNET_TRANSPORT_disconnect (p2.th);
95 GNUNET_SCHEDULER_cancel (con_task);
103 terminate_task_error (void *cls,
104 const struct GNUNET_SCHEDULER_TaskContext *tc)
106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
107 "ENDING ANGRILY %u\n",
112 GNUNET_CORE_disconnect (p1.ch);
117 GNUNET_CORE_disconnect (p2.ch);
122 GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
123 GNUNET_TRANSPORT_disconnect (p1.th);
128 GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
129 GNUNET_TRANSPORT_disconnect (p2.th);
132 if (NULL != con_task)
134 GNUNET_SCHEDULER_cancel (con_task);
142 transmit_ready (void *cls, size_t size, void *buf)
144 struct PeerContext *p = cls;
145 struct GNUNET_MessageHeader *m;
147 GNUNET_assert (ok == 4);
149 GNUNET_assert (p == &p1);
150 GNUNET_assert (NULL != buf);
151 m = (struct GNUNET_MessageHeader *) buf;
152 m->type = htons (MTYPE);
153 m->size = htons (sizeof (struct GNUNET_MessageHeader));
154 return sizeof (struct GNUNET_MessageHeader);
159 connect_notify (void *cls,
160 const struct GNUNET_PeerIdentity *peer)
162 struct PeerContext *pc = cls;
164 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
166 GNUNET_assert (pc->connect_status == 0);
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Encrypted connection established to peer `%4s'\n",
170 if (NULL != con_task)
172 GNUNET_SCHEDULER_cancel (con_task);
175 pc->connect_status = 1;
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Asking core (1) for transmission to peer `%4s'\n",
180 GNUNET_i2s (&p2.id));
182 GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_YES,
183 GNUNET_CORE_PRIO_BEST_EFFORT,
184 GNUNET_TIME_relative_multiply
185 (GNUNET_TIME_UNIT_SECONDS, 145),
187 sizeof (struct GNUNET_MessageHeader),
188 &transmit_ready, &p1))
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
192 GNUNET_i2s (&p2.id));
199 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
201 struct PeerContext *pc = cls;
203 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
205 pc->connect_status = 0;
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
212 inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
213 const struct GNUNET_MessageHeader *message)
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
222 outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
223 const struct GNUNET_MessageHeader *message)
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Core notifies about outbound data for `%4s'.\n",
233 process_mtype (void *cls,
234 const struct GNUNET_PeerIdentity *peer,
235 const struct GNUNET_MessageHeader *message)
237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
238 "Receiving message from `%4s'.\n",
240 GNUNET_assert (ok == 5);
242 GNUNET_SCHEDULER_cancel (err_task);
243 err_task = GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
248 static struct GNUNET_CORE_MessageHandler handlers[] = {
249 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
255 connect_task (void *cls,
256 const struct GNUNET_SCHEDULER_TaskContext *tc)
258 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
264 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
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, NULL, NULL); /*FIXME TRY_CONNECT change */
275 init_notify (void *cls,
276 const struct GNUNET_PeerIdentity *my_identity)
278 struct PeerContext *p = cls;
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Core connection to `%4s' established\n",
282 GNUNET_i2s (my_identity));
283 p->id = *my_identity;
286 GNUNET_assert (ok == 2);
290 GNUNET_CORE_connect (p2.cfg, &p2, &init_notify, &connect_notify,
291 &disconnect_notify, &inbound_notify, GNUNET_YES,
292 &outbound_notify, GNUNET_YES, handlers);
296 GNUNET_assert (ok == 3);
298 GNUNET_assert (cls == &p2);
299 con_task = GNUNET_SCHEDULER_add_now (&connect_task, NULL);
305 setup_peer (struct PeerContext *p,
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);
330 const struct GNUNET_CONFIGURATION_Handle *cfg)
332 GNUNET_assert (ok == 1);
334 setup_peer (&p1, "test_core_api_peer1.conf");
335 setup_peer (&p2, "test_core_api_peer2.conf");
337 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
338 (GNUNET_TIME_UNIT_SECONDS, 300),
339 &terminate_task_error, NULL);
341 GNUNET_CORE_connect (p1.cfg, &p1,
345 &inbound_notify, GNUNET_YES,
346 &outbound_notify, GNUNET_YES,
352 stop_arm (struct PeerContext *p)
354 if (0 != GNUNET_OS_process_kill (p->arm_proc, GNUNET_TERM_SIG))
355 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
356 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
357 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
359 GNUNET_OS_process_get_pid (p->arm_proc));
360 GNUNET_OS_process_destroy (p->arm_proc);
362 GNUNET_CONFIGURATION_destroy (p->cfg);
367 main (int argc, char *argv1[])
369 char *const argv[] = { "test-core-api",
371 "test_core_api_data.conf",
374 struct GNUNET_GETOPT_CommandLineOption options[] = {
375 GNUNET_GETOPT_OPTION_END
378 GNUNET_log_setup ("test-core-api",
381 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
382 "test-core-api", "nohelp", options, &run, &ok);
385 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
386 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
391 /* end of test_core_api.c */