aboutsummaryrefslogtreecommitdiff
path: root/src/core/test_core_api.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/test_core_api.c')
-rw-r--r--src/core/test_core_api.c368
1 files changed, 368 insertions, 0 deletions
diff --git a/src/core/test_core_api.c b/src/core/test_core_api.c
new file mode 100644
index 000000000..e1b3dcd0d
--- /dev/null
+++ b/src/core/test_core_api.c
@@ -0,0 +1,368 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009 Christian Grothoff (and other contributing authors)
4
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 2, or (at your
8 option) any later version.
9
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.
14
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.
19*/
20/**
21 * @file transport/test_core_api.c
22 * @brief testcase for core_api.c
23 *
24 * FIXME:
25 * - make sure connect callback is invoked properly as well!
26 */
27#include "platform.h"
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"
36
37#define VERBOSE GNUNET_NO
38
39#define START_ARM GNUNET_YES
40
41
42/**
43 * How long until we give up on transmitting the message?
44 */
45#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
46
47#define MTYPE 12345
48
49struct PeerContext
50{
51 struct GNUNET_CONFIGURATION_Handle *cfg;
52 struct GNUNET_CORE_Handle *ch;
53 struct GNUNET_PeerIdentity id; /* FIXME: this is always all-zeros! */
54 struct GNUNET_TRANSPORT_Handle *th;
55 struct GNUNET_MessageHeader *hello;
56#if START_ARM
57 pid_t arm_pid;
58#endif
59};
60
61static struct PeerContext p1;
62
63static struct PeerContext p2;
64
65static struct GNUNET_SCHEDULER_Handle *sched;
66
67static int ok;
68
69#if VERBOSE
70#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
71#else
72#define OKPP do { ok++; } while (0)
73#endif
74
75
76
77static void
78terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
79{
80 GNUNET_assert (ok == 6);
81 GNUNET_CORE_disconnect (p1.ch);
82 GNUNET_CORE_disconnect (p2.ch);
83 GNUNET_TRANSPORT_disconnect (p1.th);
84 GNUNET_TRANSPORT_disconnect (p2.th);
85 GNUNET_ARM_stop_service ("core", p1.cfg, sched, TIMEOUT, NULL, NULL);
86 GNUNET_ARM_stop_service ("core", p2.cfg, sched, TIMEOUT, NULL, NULL);
87 ok = 0;
88}
89
90
91static void
92connect_notify (void *cls,
93 const struct GNUNET_PeerIdentity *peer,
94 unsigned int bpm, struct GNUNET_TIME_Absolute last_activity)
95{
96 GNUNET_assert ((ok == 5) || (ok == 6));
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
98 "Encrypted connection established to peer `%4s' (%u bpm)\n",
99 GNUNET_i2s (peer), bpm);
100}
101
102
103static void
104disconnect_notify (void *cls,
105 const struct GNUNET_PeerIdentity *peer,
106 unsigned int bpm,
107 struct GNUNET_TIME_Absolute last_activity)
108{
109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
110 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
111}
112
113
114static unsigned int
115bfc_callback (void *cls,
116 const struct GNUNET_PeerIdentity *receiver,
117 void *position, unsigned int padding)
118{
119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
120 "Core requests data for `%4s', I have none.\n",
121 GNUNET_i2s (receiver));
122 return 0;
123}
124
125
126static int
127inbound_notify (void *cls,
128 const struct GNUNET_PeerIdentity *other,
129 const struct GNUNET_MessageHeader *message)
130{
131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
132 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
133 return GNUNET_OK;
134}
135
136
137static int
138outbound_notify (void *cls,
139 const struct GNUNET_PeerIdentity *other,
140 const struct GNUNET_MessageHeader *message)
141{
142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
143 "Core notifies about outbound data for `%4s'.\n",
144 GNUNET_i2s (other));
145 return GNUNET_OK;
146}
147
148
149static int
150process_mtype (void *cls,
151 const struct GNUNET_PeerIdentity *peer,
152 const struct GNUNET_MessageHeader *message)
153{
154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
155 "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
156 GNUNET_assert (ok == 5);
157 OKPP;
158 GNUNET_SCHEDULER_add_delayed (sched,
159 GNUNET_NO,
160 GNUNET_SCHEDULER_PRIORITY_KEEP,
161 GNUNET_SCHEDULER_NO_PREREQUISITE_TASK,
162 GNUNET_TIME_UNIT_ZERO, &terminate_task, NULL);
163 return GNUNET_OK;
164}
165
166
167static struct GNUNET_CORE_MessageHandler handlers[] = {
168 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
169 {NULL, 0, 0}
170};
171
172
173static size_t
174transmit_ready (void *cls, size_t size, void *buf)
175{
176 struct PeerContext *p = cls;
177 struct GNUNET_MessageHeader *m;
178
179 GNUNET_assert (ok == 4);
180 OKPP;
181 GNUNET_assert (p == &p1);
182 GNUNET_assert (buf != NULL);
183 m = (struct GNUNET_MessageHeader *) buf;
184 m->type = htons (MTYPE);
185 m->size = htons (sizeof (struct GNUNET_MessageHeader));
186 return sizeof (struct GNUNET_MessageHeader);
187}
188
189
190
191static void
192init_notify (void *cls,
193 struct GNUNET_CORE_Handle *server,
194 const struct GNUNET_PeerIdentity *my_identity,
195 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
196{
197 struct PeerContext *p = cls;
198
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Core connection to `%4s' established\n",
201 GNUNET_i2s (my_identity));
202 GNUNET_assert (server != NULL);
203 p->id = *my_identity;
204 p->ch = server;
205 if (cls == &p1)
206 {
207 GNUNET_assert (ok == 2);
208 OKPP;
209 /* connect p2 */
210 GNUNET_CORE_connect (sched,
211 p2.cfg,
212 TIMEOUT,
213 &p2,
214 &init_notify,
215 &connect_notify,
216 &disconnect_notify,
217 &bfc_callback,
218 &inbound_notify,
219 GNUNET_YES,
220 &outbound_notify, GNUNET_YES, handlers);
221 }
222 else
223 {
224 GNUNET_assert (ok == 3);
225 OKPP;
226 GNUNET_assert (cls == &p2);
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Asking core (1) for transmission to peer `%4s'\n",
229 GNUNET_i2s (&p2.id));
230 GNUNET_CORE_notify_transmit_ready (p1.ch,
231 0,
232 TIMEOUT,
233 &p2.id,
234 sizeof (struct GNUNET_MessageHeader),
235 &transmit_ready, &p1);
236
237 }
238}
239
240
241static void
242process_hello (void *cls,
243 struct GNUNET_TIME_Relative latency,
244 const struct GNUNET_PeerIdentity *peer,
245 const struct GNUNET_MessageHeader *message)
246{
247 struct PeerContext *p = cls;
248
249 GNUNET_assert (peer != NULL);
250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
251 "Received (my) `%s' from transport service of `%4s'\n",
252 "HELLO", GNUNET_i2s (peer));
253 GNUNET_assert (message != NULL);
254 p->hello = GNUNET_malloc (ntohs (message->size));
255 memcpy (p->hello, message, ntohs (message->size));
256 if ((p == &p1) && (p2.th != NULL))
257 GNUNET_TRANSPORT_offer_hello (p2.th, message);
258 if ((p == &p2) && (p1.th != NULL))
259 GNUNET_TRANSPORT_offer_hello (p1.th, message);
260
261 if ((p == &p1) && (p2.hello != NULL))
262 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello);
263 if ((p == &p2) && (p1.hello != NULL))
264 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello);
265}
266
267
268
269static void
270setup_peer (struct PeerContext *p, const char *cfgname)
271{
272 p->cfg = GNUNET_CONFIGURATION_create ();
273#if START_ARM
274 p->arm_pid = GNUNET_OS_start_process ("gnunet-service-arm",
275 "gnunet-service-arm",
276#if VERBOSE
277 "-L", "DEBUG",
278#endif
279 "-c", cfgname, NULL);
280 sleep (1); /* allow ARM to start */
281#endif
282 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
283 GNUNET_ARM_start_service ("core", p->cfg, sched, TIMEOUT, NULL, NULL);
284 p->th = GNUNET_TRANSPORT_connect (sched, p->cfg, p, NULL, NULL, NULL);
285 GNUNET_assert (p->th != NULL);
286 GNUNET_TRANSPORT_get_hello (p->th, TIMEOUT, &process_hello, p);
287}
288
289
290static void
291run (void *cls,
292 struct GNUNET_SCHEDULER_Handle *s,
293 char *const *args,
294 const char *cfgfile, struct GNUNET_CONFIGURATION_Handle *cfg)
295{
296 GNUNET_assert (ok == 1);
297 OKPP;
298 sched = s;
299 setup_peer (&p1, "test_core_api_peer1.conf");
300 setup_peer (&p2, "test_core_api_peer2.conf");
301 GNUNET_CORE_connect (sched,
302 p1.cfg,
303 TIMEOUT,
304 &p1,
305 &init_notify,
306 &connect_notify,
307 &disconnect_notify,
308 &bfc_callback,
309 &inbound_notify,
310 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
311}
312
313
314static void
315stop_arm (struct PeerContext *p)
316{
317#if START_ARM
318 if (0 != PLIBC_KILL (p->arm_pid, SIGTERM))
319 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
320 if (p->arm_pid != waitpid (p->arm_pid, NULL, 0))
321 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
323 "ARM process %u stopped\n", p->arm_pid);
324#endif
325 GNUNET_CONFIGURATION_destroy (p->cfg);
326}
327
328static int
329check ()
330{
331 char *const argv[] = { "test-core-api",
332 "-c",
333 "test_core_api_data.conf",
334#if VERBOSE
335 "-L", "DEBUG",
336#endif
337 NULL
338 };
339 struct GNUNET_GETOPT_CommandLineOption options[] = {
340 GNUNET_GETOPT_OPTION_END
341 };
342 sleep (1);
343 ok = 1;
344 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
345 argv, "test-core-api", "nohelp", options, &run, &ok);
346 stop_arm (&p1);
347 stop_arm (&p2);
348 return ok;
349}
350
351int
352main (int argc, char *argv[])
353{
354 int ret;
355
356 GNUNET_log_setup ("test-core-api",
357#if VERBOSE
358 "DEBUG",
359#else
360 "WARNING",
361#endif
362 NULL);
363 ret = check ();
364
365 return ret;
366}
367
368/* end of test_core_api.c */