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 * - make sure connect callback is invoked properly as well!
28 #include "gnunet_common.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_core_service.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_scheduler_lib.h"
35 #include "gnunet_transport_service.h"
37 #define VERBOSE GNUNET_NO
38 #define START_ARM GNUNET_YES
44 struct GNUNET_CONFIGURATION_Handle *cfg;
45 struct GNUNET_CORE_Handle *ch;
46 struct GNUNET_PeerIdentity id;
47 struct GNUNET_TRANSPORT_Handle *th;
48 struct GNUNET_MessageHeader *hello;
51 struct GNUNET_OS_Process *arm_proc;
55 static struct PeerContext p1;
57 static struct PeerContext p2;
59 static GNUNET_SCHEDULER_TaskIdentifier err_task;
64 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
66 #define OKPP do { ok++; } while (0)
72 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
74 GNUNET_assert (ok == 6);
75 GNUNET_CORE_disconnect (p1.ch);
76 GNUNET_CORE_disconnect (p2.ch);
77 GNUNET_TRANSPORT_disconnect (p1.th);
78 GNUNET_TRANSPORT_disconnect (p2.th);
84 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
87 fprintf(stderr, "ENDING ANGRILY %u\n", ok);
90 GNUNET_CORE_disconnect (p1.ch);
91 GNUNET_CORE_disconnect (p2.ch);
92 GNUNET_TRANSPORT_disconnect (p1.th);
93 GNUNET_TRANSPORT_disconnect (p2.th);
99 transmit_ready (void *cls, size_t size, void *buf)
101 struct PeerContext *p = cls;
102 struct GNUNET_MessageHeader *m;
104 GNUNET_assert (ok == 4);
106 GNUNET_assert (p == &p1);
107 GNUNET_assert (buf != NULL);
108 m = (struct GNUNET_MessageHeader *) buf;
109 m->type = htons (MTYPE);
110 m->size = htons (sizeof (struct GNUNET_MessageHeader));
112 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120),
113 &terminate_task_error, NULL);
115 return sizeof (struct GNUNET_MessageHeader);
120 connect_notify (void *cls,
121 const struct GNUNET_PeerIdentity *peer,
122 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
124 struct PeerContext *pc = cls;
126 if (0 == memcmp (&pc->id,
128 sizeof (struct GNUNET_PeerIdentity)))
130 GNUNET_assert (pc->connect_status == 0);
131 pc->connect_status = 1;
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Encrypted connection established to peer `%4s'\n",
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Asking core (1) for transmission to peer `%4s'\n",
139 GNUNET_i2s (&p2.id));
140 if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
143 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
145 sizeof (struct GNUNET_MessageHeader),
146 &transmit_ready, &p1))
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
150 GNUNET_i2s (&p2.id));
157 disconnect_notify (void *cls,
158 const struct GNUNET_PeerIdentity *peer)
160 struct PeerContext *pc = cls;
162 if (0 == memcmp (&pc->id,
164 sizeof (struct GNUNET_PeerIdentity)))
166 pc->connect_status = 0;
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
173 inbound_notify (void *cls,
174 const struct GNUNET_PeerIdentity *other,
175 const struct GNUNET_MessageHeader *message,
176 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
185 outbound_notify (void *cls,
186 const struct GNUNET_PeerIdentity *other,
187 const struct GNUNET_MessageHeader *message,
188 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 "Core notifies about outbound data for `%4s'.\n",
199 process_mtype (void *cls,
200 const struct GNUNET_PeerIdentity *peer,
201 const struct GNUNET_MessageHeader *message,
202 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
206 GNUNET_assert (ok == 5);
208 GNUNET_SCHEDULER_cancel (err_task);
209 GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
214 static struct GNUNET_CORE_MessageHandler handlers[] = {
215 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
221 init_notify (void *cls,
222 struct GNUNET_CORE_Handle *server,
223 const struct GNUNET_PeerIdentity *my_identity,
224 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
226 struct PeerContext *p = cls;
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Core connection to `%4s' established\n",
230 GNUNET_i2s (my_identity));
231 GNUNET_assert (server != NULL);
232 p->id = *my_identity;
236 GNUNET_assert (ok == 2);
239 GNUNET_CORE_connect (p2.cfg, 1,
247 &outbound_notify, GNUNET_YES, handlers);
251 GNUNET_assert (ok == 3);
253 GNUNET_assert (cls == &p2);
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
255 "Asking core (1) to connect to peer `%4s'\n",
256 GNUNET_i2s (&p2.id));
257 GNUNET_CORE_peer_request_connect (p1.ch,
258 GNUNET_TIME_UNIT_SECONDS,
266 process_hello (void *cls,
267 const struct GNUNET_MessageHeader *message)
269 struct PeerContext *p = cls;
271 GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
273 "Received (my) `%s' from transport service\n",
275 GNUNET_assert (message != NULL);
276 p->hello = GNUNET_malloc (ntohs (message->size));
277 memcpy (p->hello, message, ntohs (message->size));
278 if ((p == &p1) && (p2.th != NULL))
279 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
280 if ((p == &p2) && (p1.th != NULL))
281 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
283 if ((p == &p1) && (p2.hello != NULL))
284 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
285 if ((p == &p2) && (p1.hello != NULL))
286 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
292 setup_peer (struct PeerContext *p, const char *cfgname)
294 p->cfg = GNUNET_CONFIGURATION_create ();
296 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
297 "gnunet-service-arm",
301 "-c", cfgname, NULL);
303 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
304 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
305 GNUNET_assert (p->th != NULL);
306 GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
314 const struct GNUNET_CONFIGURATION_Handle *cfg)
316 GNUNET_assert (ok == 1);
318 setup_peer (&p1, "test_core_api_peer1.conf");
319 setup_peer (&p2, "test_core_api_peer2.conf");
320 GNUNET_CORE_connect (p1.cfg, 1,
327 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
331 stop_arm (struct PeerContext *p)
334 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
335 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
336 if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
337 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
340 GNUNET_OS_process_close (p->arm_proc);
343 GNUNET_CONFIGURATION_destroy (p->cfg);
349 char *const argv[] = { "test-core-api",
351 "test_core_api_data.conf",
357 struct GNUNET_GETOPT_CommandLineOption options[] = {
358 GNUNET_GETOPT_OPTION_END
361 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
362 argv, "test-core-api", "nohelp", options, &run, &ok);
369 main (int argc, char *argv[])
373 GNUNET_log_setup ("test-core-api",
381 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
382 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
387 /* end of test_core_api.c */