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 GNUNET_SCHEDULER_TaskIdentifier irc_task;
71 static GNUNET_SCHEDULER_TaskIdentifier ask_task;
73 static unsigned int total_reserve = 5;
78 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
81 fprintf(stderr, "Regular shutdown\n");
83 GNUNET_assert (ok == 6);
84 GNUNET_assert (NULL == irc);
85 GNUNET_CORE_disconnect (p1.ch);
86 GNUNET_CORE_disconnect (p2.ch);
87 GNUNET_TRANSPORT_disconnect (p1.th);
88 GNUNET_TRANSPORT_disconnect (p2.th);
94 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
96 err_task = GNUNET_SCHEDULER_NO_TASK;
98 fprintf(stderr, "ENDING ANGRILY %u\n", ok);
102 GNUNET_CORE_peer_change_preference_cancel (irc);
103 if (GNUNET_SCHEDULER_NO_TASK != irc_task)
104 GNUNET_SCHEDULER_cancel (irc_task);
105 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
107 GNUNET_SCHEDULER_cancel (ask_task);
108 ask_task = GNUNET_SCHEDULER_NO_TASK;
110 GNUNET_CORE_disconnect (p1.ch);
111 GNUNET_CORE_disconnect (p2.ch);
112 GNUNET_TRANSPORT_disconnect (p1.th);
113 GNUNET_TRANSPORT_disconnect (p2.th);
119 transmit_ready (void *cls, size_t size, void *buf)
121 struct PeerContext *p = cls;
122 struct GNUNET_MessageHeader *m;
124 GNUNET_assert (ok == 4);
126 GNUNET_assert (p == &p1);
127 GNUNET_assert (buf != NULL);
128 m = (struct GNUNET_MessageHeader *) buf;
129 m->type = htons (MTYPE);
130 m->size = htons (sizeof (struct GNUNET_MessageHeader));
132 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120),
133 &terminate_task_error, NULL);
135 return sizeof (struct GNUNET_MessageHeader);
139 preference_cb (void *cls,
141 GNUNET_PeerIdentity *peer,
142 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
144 struct GNUNET_TIME_Relative res_delay,
145 uint64_t preference);
148 do_reserve (void *cls,
149 const struct GNUNET_SCHEDULER_TaskContext *tc)
151 struct PeerContext *pc = cls;
153 irc_task = GNUNET_SCHEDULER_NO_TASK;
154 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "Aborted during attempt to request reservation\n");
160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
161 "Requesting reservatin of 32k from core in 1s!\n");
162 irc = GNUNET_CORE_peer_change_preference (p1.ch,
164 GNUNET_TIME_UNIT_SECONDS,
165 GNUNET_BANDWIDTH_VALUE_MAX,
166 1000000 /* bandwidth for 1s */,
173 preference_cb (void *cls,
175 GNUNET_PeerIdentity *peer,
176 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
178 struct GNUNET_TIME_Relative res_delay,
181 struct PeerContext *pc = cls;
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
187 "Delaying reservation request by %llu ms!\n",
188 (unsigned long long) res_delay.rel_value);
189 irc_task = GNUNET_SCHEDULER_add_delayed (res_delay,
194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
195 "Reservation done!\n");
196 if (total_reserve > 0)
198 irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
201 GNUNET_SCHEDULER_cancel (err_task);
202 err_task = GNUNET_SCHEDULER_NO_TASK;
203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
204 "Asking core (1) for transmission to peer `%4s'\n",
205 GNUNET_i2s (&p2.id));
206 if (NULL == GNUNET_CORE_notify_transmit_ready (p1.ch,
209 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 45),
211 sizeof (struct GNUNET_MessageHeader),
212 &transmit_ready, &p1))
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
216 GNUNET_i2s (&p2.id));
222 connect_notify (void *cls,
223 const struct GNUNET_PeerIdentity *peer,
224 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
226 struct PeerContext *pc = cls;
228 if (0 == memcmp (&pc->id,
230 sizeof (struct GNUNET_PeerIdentity)))
232 GNUNET_assert (pc->connect_status == 0);
233 pc->connect_status = 1;
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
237 "Encrypted connection established to peer `%4s'\n",
239 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
241 GNUNET_SCHEDULER_cancel (ask_task);
242 ask_task = GNUNET_SCHEDULER_NO_TASK;
244 GNUNET_SCHEDULER_cancel (err_task);
246 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 120),
247 &terminate_task_error, NULL);
248 irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
254 disconnect_notify (void *cls,
255 const struct GNUNET_PeerIdentity *peer)
257 struct PeerContext *pc = cls;
259 if (0 == memcmp (&pc->id,
261 sizeof (struct GNUNET_PeerIdentity)))
263 pc->connect_status = 0;
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
270 inbound_notify (void *cls,
271 const struct GNUNET_PeerIdentity *other,
272 const struct GNUNET_MessageHeader *message,
273 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
276 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
282 outbound_notify (void *cls,
283 const struct GNUNET_PeerIdentity *other,
284 const struct GNUNET_MessageHeader *message,
285 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
288 "Core notifies about outbound data for `%4s'.\n",
296 process_mtype (void *cls,
297 const struct GNUNET_PeerIdentity *peer,
298 const struct GNUNET_MessageHeader *message,
299 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
302 "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
303 GNUNET_assert (ok == 5);
305 GNUNET_SCHEDULER_cancel (err_task);
306 err_task = GNUNET_SCHEDULER_NO_TASK;
307 GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
312 static struct GNUNET_CORE_MessageHandler handlers[] = {
313 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
320 ask_connect_task (void *cls,
321 const struct GNUNET_SCHEDULER_TaskContext *tc)
323 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
324 GNUNET_SCHEDULER_cancel (ask_task);
326 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
327 &ask_connect_task, NULL);
328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
329 "Asking core (1) AGAIN to connect to peer `%4s'\n",
330 GNUNET_i2s (&p2.id));
331 GNUNET_CORE_peer_request_connect (p1.ch,
338 init_notify (void *cls,
339 struct GNUNET_CORE_Handle *server,
340 const struct GNUNET_PeerIdentity *my_identity,
341 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
343 struct PeerContext *p = cls;
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Core connection to `%4s' established\n",
347 GNUNET_i2s (my_identity));
348 GNUNET_assert (server != NULL);
349 p->id = *my_identity;
353 GNUNET_assert (ok == 2);
356 GNUNET_CORE_connect (p2.cfg, 1,
364 &outbound_notify, GNUNET_YES, handlers);
368 GNUNET_assert (ok == 3);
370 GNUNET_assert (cls == &p2);
371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "Asking core (1) to connect to peer `%4s'\n",
373 GNUNET_i2s (&p2.id));
375 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60),
376 &terminate_task_error, NULL);
377 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
378 GNUNET_SCHEDULER_cancel (ask_task);
380 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
381 &ask_connect_task, NULL);
382 GNUNET_CORE_peer_request_connect (p1.ch,
390 process_hello (void *cls,
391 const struct GNUNET_MessageHeader *message)
393 struct PeerContext *p = cls;
395 GNUNET_TRANSPORT_get_hello_cancel (p->th, &process_hello, p);
396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
397 "Received (my) `%s' from transport service\n",
399 GNUNET_assert (message != NULL);
400 p->hello = GNUNET_malloc (ntohs (message->size));
401 memcpy (p->hello, message, ntohs (message->size));
402 if ((p == &p1) && (p2.th != NULL))
403 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
404 if ((p == &p2) && (p1.th != NULL))
405 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
407 if ((p == &p1) && (p2.hello != NULL))
408 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
409 if ((p == &p2) && (p1.hello != NULL))
410 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
416 setup_peer (struct PeerContext *p, const char *cfgname)
418 p->cfg = GNUNET_CONFIGURATION_create ();
420 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
421 "gnunet-service-arm",
425 "-c", cfgname, NULL);
427 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
428 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
429 GNUNET_assert (p->th != NULL);
430 GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
438 const struct GNUNET_CONFIGURATION_Handle *cfg)
440 GNUNET_assert (ok == 1);
442 setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
443 setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
444 GNUNET_CORE_connect (p1.cfg, 1,
451 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
455 stop_arm (struct PeerContext *p)
458 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
459 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
460 if (GNUNET_OS_process_wait(p->arm_proc) != GNUNET_OK)
461 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
463 "ARM process %u stopped\n", GNUNET_OS_process_get_pid (p->arm_proc));
464 GNUNET_OS_process_close (p->arm_proc);
467 GNUNET_CONFIGURATION_destroy (p->cfg);
473 char *const argv[] = { "test-core-api-preferences",
475 "test_core_api_data.conf",
481 struct GNUNET_GETOPT_CommandLineOption options[] = {
482 GNUNET_GETOPT_OPTION_END
485 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
486 argv, "test-core-api-preferences", "nohelp", options, &run, &ok);
493 main (int argc, char *argv[])
497 GNUNET_log_setup ("test-core-api",
505 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
506 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
511 /* end of test_core_api_preferences.c */