2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 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_preferences.c
22 * @brief testcase for core_api.c's GNUNET_CORE_change_preference
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"
34 #define VERBOSE GNUNET_NO
36 #define START_ARM GNUNET_YES
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_MessageHeader *hello;
49 struct GNUNET_OS_Process *arm_proc;
53 static struct PeerContext p1;
55 static struct PeerContext p2;
57 static GNUNET_SCHEDULER_TaskIdentifier err_task;
62 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
64 #define OKPP do { ok++; } while (0)
67 static struct GNUNET_CORE_InformationRequestContext *irc;
69 static struct GNUNET_CORE_TransmitHandle *th;
71 static GNUNET_SCHEDULER_TaskIdentifier irc_task;
73 static GNUNET_SCHEDULER_TaskIdentifier ask_task;
75 static unsigned int total_reserve = 5;
80 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
83 fprintf(stderr, "Regular shutdown\n");
85 GNUNET_assert (ok == 6);
86 GNUNET_assert (NULL == irc);
87 GNUNET_CORE_disconnect (p1.ch);
88 GNUNET_CORE_disconnect (p2.ch);
89 GNUNET_TRANSPORT_disconnect (p1.th);
90 GNUNET_TRANSPORT_disconnect (p2.th);
96 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 err_task = GNUNET_SCHEDULER_NO_TASK;
100 fprintf(stderr, "ENDING ANGRILY %u\n", ok);
105 GNUNET_CORE_peer_change_preference_cancel (irc);
108 if (GNUNET_SCHEDULER_NO_TASK != irc_task)
110 GNUNET_SCHEDULER_cancel (irc_task);
111 irc_task = GNUNET_SCHEDULER_NO_TASK;
113 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
115 GNUNET_SCHEDULER_cancel (ask_task);
116 ask_task = GNUNET_SCHEDULER_NO_TASK;
118 GNUNET_CORE_disconnect (p1.ch);
119 GNUNET_CORE_disconnect (p2.ch);
120 GNUNET_TRANSPORT_disconnect (p1.th);
121 GNUNET_TRANSPORT_disconnect (p2.th);
127 transmit_ready (void *cls, size_t size, void *buf)
129 struct PeerContext *p = cls;
130 struct GNUNET_MessageHeader *m;
133 GNUNET_assert (ok == 4);
135 GNUNET_assert (p == &p1);
136 GNUNET_assert (buf != NULL);
137 m = (struct GNUNET_MessageHeader *) buf;
138 m->type = htons (MTYPE);
139 m->size = htons (sizeof (struct GNUNET_MessageHeader));
141 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120),
142 &terminate_task_error, NULL);
144 return sizeof (struct GNUNET_MessageHeader);
148 preference_cb (void *cls,
150 GNUNET_PeerIdentity *peer,
151 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
153 struct GNUNET_TIME_Relative res_delay,
154 uint64_t preference);
157 do_reserve (void *cls,
158 const struct GNUNET_SCHEDULER_TaskContext *tc)
160 struct PeerContext *pc = cls;
162 irc_task = GNUNET_SCHEDULER_NO_TASK;
163 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Aborted during attempt to request reservation\n");
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Requesting reservatin of 32k from core in 1s!\n");
171 irc = GNUNET_CORE_peer_change_preference (p1.ch,
173 GNUNET_TIME_UNIT_SECONDS,
174 GNUNET_BANDWIDTH_VALUE_MAX,
175 1000000 /* bandwidth for 1s */,
182 preference_cb (void *cls,
184 GNUNET_PeerIdentity *peer,
185 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
187 struct GNUNET_TIME_Relative res_delay,
190 struct PeerContext *pc = cls;
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Delaying reservation request by %llu ms!\n",
197 (unsigned long long) res_delay.rel_value);
198 irc_task = GNUNET_SCHEDULER_add_delayed (res_delay,
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Reservation done!\n");
205 if (total_reserve > 0)
207 irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
210 GNUNET_SCHEDULER_cancel (err_task);
211 err_task = GNUNET_SCHEDULER_NO_TASK;
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Asking core (1) for transmission to peer `%4s'\n",
214 GNUNET_i2s (&p2.id));
215 if (NULL == (th = GNUNET_CORE_notify_transmit_ready (p1.ch,
218 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
220 sizeof (struct GNUNET_MessageHeader),
221 &transmit_ready, &p1)))
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
225 GNUNET_i2s (&p2.id));
231 connect_notify (void *cls,
232 const struct GNUNET_PeerIdentity *peer,
233 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
235 struct PeerContext *pc = cls;
237 if (0 == memcmp (&pc->id,
239 sizeof (struct GNUNET_PeerIdentity)))
241 GNUNET_assert (pc->connect_status == 0);
242 pc->connect_status = 1;
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "Encrypted connection established to peer `%4s'\n",
248 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
250 GNUNET_SCHEDULER_cancel (ask_task);
251 ask_task = GNUNET_SCHEDULER_NO_TASK;
253 GNUNET_SCHEDULER_cancel (err_task);
255 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120),
256 &terminate_task_error, NULL);
257 irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
263 disconnect_notify (void *cls,
264 const struct GNUNET_PeerIdentity *peer)
266 struct PeerContext *pc = cls;
268 if (0 == memcmp (&pc->id,
270 sizeof (struct GNUNET_PeerIdentity)))
272 pc->connect_status = 0;
273 if (GNUNET_SCHEDULER_NO_TASK != irc_task)
275 GNUNET_SCHEDULER_cancel (irc_task);
276 irc_task = GNUNET_SCHEDULER_NO_TASK;
278 if (0 == memcmp (peer, &p1.id, sizeof (struct GNUNET_PeerIdentity)))
282 GNUNET_CORE_peer_change_preference_cancel (irc);
287 GNUNET_CORE_notify_transmit_ready_cancel (th);
291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
292 "Encrypted connection to `%4s' cut\n",
298 inbound_notify (void *cls,
299 const struct GNUNET_PeerIdentity *other,
300 const struct GNUNET_MessageHeader *message,
301 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
304 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
310 outbound_notify (void *cls,
311 const struct GNUNET_PeerIdentity *other,
312 const struct GNUNET_MessageHeader *message,
313 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
315 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
316 "Core notifies about outbound data for `%4s'.\n",
324 process_mtype (void *cls,
325 const struct GNUNET_PeerIdentity *peer,
326 const struct GNUNET_MessageHeader *message,
327 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
330 "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
331 GNUNET_assert (ok == 5);
333 GNUNET_SCHEDULER_cancel (err_task);
334 err_task = GNUNET_SCHEDULER_NO_TASK;
335 GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
340 static struct GNUNET_CORE_MessageHandler handlers[] = {
341 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
348 ask_connect_task (void *cls,
349 const struct GNUNET_SCHEDULER_TaskContext *tc)
352 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
353 &ask_connect_task, NULL);
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
355 "Asking core (1) AGAIN to connect to peer `%4s'\n",
356 GNUNET_i2s (&p2.id));
357 GNUNET_CORE_peer_request_connect (p1.ch,
364 init_notify (void *cls,
365 struct GNUNET_CORE_Handle *server,
366 const struct GNUNET_PeerIdentity *my_identity,
367 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
369 struct PeerContext *p = cls;
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "Core connection to `%4s' established\n",
373 GNUNET_i2s (my_identity));
374 GNUNET_assert (server != NULL);
375 p->id = *my_identity;
379 GNUNET_assert (ok == 2);
382 GNUNET_CORE_connect (p2.cfg, 1,
390 &outbound_notify, GNUNET_YES, handlers);
394 GNUNET_assert (ok == 3);
396 GNUNET_assert (cls == &p2);
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Asking core (1) to connect to peer `%4s'\n",
399 GNUNET_i2s (&p2.id));
401 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
402 &terminate_task_error, NULL);
403 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
404 GNUNET_SCHEDULER_cancel (ask_task);
406 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
407 &ask_connect_task, NULL);
408 GNUNET_CORE_peer_request_connect (p1.ch,
416 process_hello (void *cls,
417 const struct GNUNET_MessageHeader *message)
419 struct PeerContext *p = cls;
421 GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
423 "Received (my) `%s' from transport service\n",
425 GNUNET_assert (message != NULL);
426 p->hello = GNUNET_malloc (ntohs (message->size));
427 memcpy (p->hello, message, ntohs (message->size));
428 if ((p == &p1) && (p2.th != NULL))
429 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
430 if ((p == &p2) && (p1.th != NULL))
431 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
433 if ((p == &p1) && (p2.hello != NULL))
434 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
435 if ((p == &p2) && (p1.hello != NULL))
436 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
442 setup_peer (struct PeerContext *p, const char *cfgname)
444 p->cfg = GNUNET_CONFIGURATION_create ();
446 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
447 "gnunet-service-arm",
451 "-c", cfgname, NULL);
453 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
454 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
455 GNUNET_assert (p->th != NULL);
456 GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
464 const struct GNUNET_CONFIGURATION_Handle *cfg)
466 GNUNET_assert (ok == 1);
468 setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
469 setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
470 GNUNET_CORE_connect (p1.cfg, 1,
477 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
481 stop_arm (struct PeerContext *p)
484 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
485 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
486 if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
487 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
490 GNUNET_OS_process_close (p->arm_proc);
493 GNUNET_CONFIGURATION_destroy (p->cfg);
499 char *const argv[] = { "test-core-api-preferences",
501 "test_core_api_data.conf",
507 struct GNUNET_GETOPT_CommandLineOption options[] = {
508 GNUNET_GETOPT_OPTION_END
511 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
512 argv, "test-core-api-preferences", "nohelp", options, &run, &ok);
519 main (int argc, char *argv[])
523 GNUNET_log_setup ("test-core-api",
531 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
532 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
537 /* end of test_core_api_preferences.c */