aboutsummaryrefslogtreecommitdiff
path: root/src/core/test_core_api_preferences.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/core/test_core_api_preferences.c')
-rw-r--r--src/core/test_core_api_preferences.c501
1 files changed, 0 insertions, 501 deletions
diff --git a/src/core/test_core_api_preferences.c b/src/core/test_core_api_preferences.c
deleted file mode 100644
index 6d7f52953..000000000
--- a/src/core/test_core_api_preferences.c
+++ /dev/null
@@ -1,501 +0,0 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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 3, 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 core/test_core_api_preferences.c
22 * @brief testcase for core_api.c's GNUNET_CORE_change_preference
23 */
24#include "platform.h"
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"
33
34#define VERBOSE GNUNET_EXTRA_LOGGING
35
36#define START_ARM GNUNET_YES
37
38#define MTYPE 12345
39
40struct PeerContext
41{
42 struct GNUNET_CONFIGURATION_Handle *cfg;
43 struct GNUNET_CORE_Handle *ch;
44 struct GNUNET_PeerIdentity id;
45 struct GNUNET_TRANSPORT_Handle *th;
46 struct GNUNET_TRANSPORT_GetHelloHandle *ghh;
47 struct GNUNET_MessageHeader *hello;
48 int connect_status;
49#if START_ARM
50 struct GNUNET_OS_Process *arm_proc;
51#endif
52};
53
54static struct PeerContext p1;
55
56static struct PeerContext p2;
57
58static GNUNET_SCHEDULER_TaskIdentifier err_task;
59
60static int ok;
61
62#if VERBOSE
63#define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
64#else
65#define OKPP do { ok++; } while (0)
66#endif
67
68static struct GNUNET_CORE_InformationRequestContext *irc;
69
70static struct GNUNET_CORE_TransmitHandle *th;
71
72static GNUNET_SCHEDULER_TaskIdentifier irc_task;
73
74static GNUNET_SCHEDULER_TaskIdentifier ask_task;
75
76static unsigned int total_reserve = 5;
77
78
79static void
80process_hello (void *cls, const struct GNUNET_MessageHeader *message);
81
82static void
83terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84{
85#if VERBOSE
86 fprintf (stderr, "Regular shutdown\n");
87#endif
88 GNUNET_assert (ok == 6);
89 GNUNET_assert (NULL == irc);
90 GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
91 GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
92 GNUNET_CORE_disconnect (p1.ch);
93 GNUNET_CORE_disconnect (p2.ch);
94 GNUNET_TRANSPORT_disconnect (p1.th);
95 GNUNET_TRANSPORT_disconnect (p2.th);
96 ok = 0;
97}
98
99
100static void
101terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102{
103 err_task = GNUNET_SCHEDULER_NO_TASK;
104#if VERBOSE
105 fprintf (stderr, "ENDING ANGRILY %u\n", ok);
106#endif
107 GNUNET_break (0);
108 if (NULL != irc)
109 {
110 GNUNET_CORE_peer_change_preference_cancel (irc);
111 irc = NULL;
112 }
113 if (GNUNET_SCHEDULER_NO_TASK != irc_task)
114 {
115 GNUNET_SCHEDULER_cancel (irc_task);
116 irc_task = GNUNET_SCHEDULER_NO_TASK;
117 }
118 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
119 {
120 GNUNET_SCHEDULER_cancel (ask_task);
121 ask_task = GNUNET_SCHEDULER_NO_TASK;
122 }
123 GNUNET_TRANSPORT_get_hello_cancel (p1.ghh);
124 GNUNET_TRANSPORT_get_hello_cancel (p2.ghh);
125 GNUNET_CORE_disconnect (p1.ch);
126 GNUNET_CORE_disconnect (p2.ch);
127 GNUNET_TRANSPORT_disconnect (p1.th);
128 GNUNET_TRANSPORT_disconnect (p2.th);
129 ok = 42;
130}
131
132
133static size_t
134transmit_ready (void *cls, size_t size, void *buf)
135{
136 struct PeerContext *p = cls;
137 struct GNUNET_MessageHeader *m;
138
139 th = NULL;
140 GNUNET_assert (ok == 4);
141 OKPP;
142 GNUNET_assert (p == &p1);
143 GNUNET_assert (buf != NULL);
144 m = (struct GNUNET_MessageHeader *) buf;
145 m->type = htons (MTYPE);
146 m->size = htons (sizeof (struct GNUNET_MessageHeader));
147 err_task =
148 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
149 (GNUNET_TIME_UNIT_SECONDS, 120),
150 &terminate_task_error, NULL);
151
152 return sizeof (struct GNUNET_MessageHeader);
153}
154
155static void
156preference_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
157 int32_t amount,
158 struct GNUNET_TIME_Relative res_delay);
159
160static void
161do_reserve (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162{
163 struct PeerContext *pc = cls;
164
165 irc_task = GNUNET_SCHEDULER_NO_TASK;
166 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
167 {
168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
169 "Aborted during attempt to request reservation\n");
170 return;
171 }
172 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
173 "Requesting reservatin of 32k from core in 1s!\n");
174 irc =
175 GNUNET_CORE_peer_change_preference (p1.ch, &p2.id,
176 1000000 /* bandwidth for 1s */ ,
177 0, &preference_cb, pc);
178}
179
180static void
181preference_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
182 int32_t amount,
183 struct GNUNET_TIME_Relative res_delay)
184{
185 struct PeerContext *pc = cls;
186
187 irc = NULL;
188 if (0 == amount)
189 {
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
191 "Delaying reservation request by %llu ms!\n",
192 (unsigned long long) res_delay.rel_value);
193 irc_task = GNUNET_SCHEDULER_add_delayed (res_delay, &do_reserve, pc);
194 return;
195 }
196 total_reserve--;
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reservation done!\n");
198 if (total_reserve > 0)
199 {
200 irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
201 return;
202 }
203 GNUNET_SCHEDULER_cancel (err_task);
204 err_task = GNUNET_SCHEDULER_NO_TASK;
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Asking core (1) for transmission to peer `%4s'\n",
207 GNUNET_i2s (&p2.id));
208 if (NULL ==
209 (th =
210 GNUNET_CORE_notify_transmit_ready (p1.ch, GNUNET_YES, 0,
211 GNUNET_TIME_relative_multiply
212 (GNUNET_TIME_UNIT_SECONDS, 45),
213 &p2.id,
214 sizeof (struct GNUNET_MessageHeader),
215 &transmit_ready, &p1)))
216 {
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
218 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
219 GNUNET_i2s (&p2.id));
220 }
221}
222
223
224static void
225connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
226 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
227{
228 struct PeerContext *pc = cls;
229
230 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
231 return;
232 GNUNET_assert (pc->connect_status == 0);
233 pc->connect_status = 1;
234 if (pc == &p1)
235 {
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237 "Encrypted connection established to peer `%4s'\n",
238 GNUNET_i2s (peer));
239 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
240 {
241 GNUNET_SCHEDULER_cancel (ask_task);
242 ask_task = GNUNET_SCHEDULER_NO_TASK;
243 }
244 GNUNET_SCHEDULER_cancel (err_task);
245 err_task =
246 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
247 (GNUNET_TIME_UNIT_SECONDS, 120),
248 &terminate_task_error, NULL);
249 irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
250 }
251}
252
253
254static void
255disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
256{
257 struct PeerContext *pc = cls;
258
259 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
260 return;
261 pc->connect_status = 0;
262 if (GNUNET_SCHEDULER_NO_TASK != irc_task)
263 {
264 GNUNET_SCHEDULER_cancel (irc_task);
265 irc_task = GNUNET_SCHEDULER_NO_TASK;
266 }
267 if (0 == memcmp (peer, &p1.id, sizeof (struct GNUNET_PeerIdentity)))
268 {
269 if (irc != NULL)
270 {
271 GNUNET_CORE_peer_change_preference_cancel (irc);
272 irc = NULL;
273 }
274 if (th != NULL)
275 {
276 GNUNET_CORE_notify_transmit_ready_cancel (th);
277 th = NULL;
278 }
279 }
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Encrypted connection to `%4s' cut\n",
281 GNUNET_i2s (peer));
282}
283
284
285static int
286inbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
287 const struct GNUNET_MessageHeader *message,
288 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
289{
290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
291 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
292 return GNUNET_OK;
293}
294
295
296static int
297outbound_notify (void *cls, const struct GNUNET_PeerIdentity *other,
298 const struct GNUNET_MessageHeader *message,
299 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
300{
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Core notifies about outbound data for `%4s'.\n",
303 GNUNET_i2s (other));
304 return GNUNET_OK;
305}
306
307
308
309static int
310process_mtype (void *cls, const struct GNUNET_PeerIdentity *peer,
311 const struct GNUNET_MessageHeader *message,
312 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
313{
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving message from `%4s'.\n",
315 GNUNET_i2s (peer));
316 GNUNET_assert (ok == 5);
317 OKPP;
318 GNUNET_SCHEDULER_cancel (err_task);
319 err_task = GNUNET_SCHEDULER_NO_TASK;
320 GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
321 return GNUNET_OK;
322}
323
324
325static struct GNUNET_CORE_MessageHandler handlers[] = {
326 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
327 {NULL, 0, 0}
328};
329
330
331
332static void
333ask_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
334{
335 ask_task =
336 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &ask_connect_task,
337 NULL);
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339 "Asking core (1) AGAIN to connect to peer `%4s'\n",
340 GNUNET_i2s (&p2.id));
341 GNUNET_TRANSPORT_try_connect (p1.th, &p2.id);
342}
343
344
345static void
346init_notify (void *cls, struct GNUNET_CORE_Handle *server,
347 const struct GNUNET_PeerIdentity *my_identity)
348{
349 struct PeerContext *p = cls;
350
351 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Core connection to `%4s' established\n",
352 GNUNET_i2s (my_identity));
353 GNUNET_assert (server != NULL);
354 p->id = *my_identity;
355 p->ch = server;
356 if (cls == &p1)
357 {
358 GNUNET_assert (ok == 2);
359 OKPP;
360 /* connect p2 */
361 GNUNET_CORE_connect (p2.cfg, 1, &p2, &init_notify, &connect_notify,
362 &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
363 &outbound_notify, GNUNET_YES, handlers);
364 }
365 else
366 {
367 GNUNET_assert (ok == 3);
368 OKPP;
369 GNUNET_assert (cls == &p2);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Asking core (1) to connect to peer `%4s'\n",
372 GNUNET_i2s (&p2.id));
373 err_task =
374 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
375 (GNUNET_TIME_UNIT_SECONDS, 60),
376 &terminate_task_error, NULL);
377 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
378 GNUNET_SCHEDULER_cancel (ask_task);
379 ask_task =
380 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
381 &ask_connect_task, NULL);
382 GNUNET_TRANSPORT_try_connect (p1.th, &p2.id);
383 }
384}
385
386
387static void
388process_hello (void *cls, const struct GNUNET_MessageHeader *message)
389{
390 struct PeerContext *p = cls;
391
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
393 "Received (my) `%s' from transport service\n", "HELLO");
394 GNUNET_assert (message != NULL);
395 p->hello = GNUNET_malloc (ntohs (message->size));
396 memcpy (p->hello, message, ntohs (message->size));
397 if ((p == &p1) && (p2.th != NULL))
398 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
399 if ((p == &p2) && (p1.th != NULL))
400 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
401
402 if ((p == &p1) && (p2.hello != NULL) && (p1.th != NULL))
403 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
404 if ((p == &p2) && (p1.hello != NULL) && (p2.th != NULL))
405 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
406}
407
408
409
410static void
411setup_peer (struct PeerContext *p, const char *cfgname)
412{
413 p->cfg = GNUNET_CONFIGURATION_create ();
414#if START_ARM
415 p->arm_proc =
416 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
417 "gnunet-service-arm",
418#if VERBOSE
419 "-L", "DEBUG",
420#endif
421 "-c", cfgname, NULL);
422#endif
423 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
424 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
425 GNUNET_assert (p->th != NULL);
426 p->ghh = GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
427}
428
429
430static void
431run (void *cls, char *const *args, const char *cfgfile,
432 const struct GNUNET_CONFIGURATION_Handle *cfg)
433{
434 GNUNET_assert (ok == 1);
435 OKPP;
436 setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
437 setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
438 GNUNET_CORE_connect (p1.cfg, 1, &p1, &init_notify, &connect_notify,
439 &disconnect_notify, NULL, &inbound_notify, GNUNET_YES,
440 &outbound_notify, GNUNET_YES, handlers);
441}
442
443static void
444stop_arm (struct PeerContext *p)
445{
446#if START_ARM
447 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
448 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
449 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
450 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ARM process %u stopped\n",
452 GNUNET_OS_process_get_pid (p->arm_proc));
453 GNUNET_OS_process_close (p->arm_proc);
454 p->arm_proc = NULL;
455#endif
456 GNUNET_CONFIGURATION_destroy (p->cfg);
457}
458
459static int
460check ()
461{
462 char *const argv[] = { "test-core-api-preferences",
463 "-c",
464 "test_core_api_data.conf",
465#if VERBOSE
466 "-L", "DEBUG",
467#endif
468 NULL
469 };
470 struct GNUNET_GETOPT_CommandLineOption options[] = {
471 GNUNET_GETOPT_OPTION_END
472 };
473 ok = 1;
474 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
475 "test-core-api-preferences", "nohelp", options, &run,
476 &ok);
477 stop_arm (&p1);
478 stop_arm (&p2);
479 return ok;
480}
481
482int
483main (int argc, char *argv[])
484{
485 int ret;
486
487 GNUNET_log_setup ("test-core-api",
488#if VERBOSE
489 "DEBUG",
490#else
491 "WARNING",
492#endif
493 NULL);
494 ret = check ();
495 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
496 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
497
498 return ret;
499}
500
501/* end of test_core_api_preferences.c */