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;
78 static void process_hello (void *cls,
79 const struct GNUNET_MessageHeader *message);
82 terminate_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 fprintf (stderr, "Regular shutdown\n");
87 GNUNET_assert (ok == 6);
88 GNUNET_assert (NULL == irc);
89 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &process_hello, &p1);
90 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &process_hello, &p2);
91 GNUNET_CORE_disconnect (p1.ch);
92 GNUNET_CORE_disconnect (p2.ch);
93 GNUNET_TRANSPORT_disconnect (p1.th);
94 GNUNET_TRANSPORT_disconnect (p2.th);
100 terminate_task_error (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
102 err_task = GNUNET_SCHEDULER_NO_TASK;
104 fprintf (stderr, "ENDING ANGRILY %u\n", ok);
109 GNUNET_CORE_peer_change_preference_cancel (irc);
112 if (GNUNET_SCHEDULER_NO_TASK != irc_task)
114 GNUNET_SCHEDULER_cancel (irc_task);
115 irc_task = GNUNET_SCHEDULER_NO_TASK;
117 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
119 GNUNET_SCHEDULER_cancel (ask_task);
120 ask_task = GNUNET_SCHEDULER_NO_TASK;
122 GNUNET_TRANSPORT_get_hello_cancel (p1.th, &process_hello, &p1);
123 GNUNET_TRANSPORT_get_hello_cancel (p2.th, &process_hello, &p2);
124 GNUNET_CORE_disconnect (p1.ch);
125 GNUNET_CORE_disconnect (p2.ch);
126 GNUNET_TRANSPORT_disconnect (p1.th);
127 GNUNET_TRANSPORT_disconnect (p2.th);
133 transmit_ready (void *cls, size_t size, void *buf)
135 struct PeerContext *p = cls;
136 struct GNUNET_MessageHeader *m;
139 GNUNET_assert (ok == 4);
141 GNUNET_assert (p == &p1);
142 GNUNET_assert (buf != NULL);
143 m = (struct GNUNET_MessageHeader *) buf;
144 m->type = htons (MTYPE);
145 m->size = htons (sizeof (struct GNUNET_MessageHeader));
147 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
148 (GNUNET_TIME_UNIT_SECONDS, 120),
149 &terminate_task_error, NULL);
151 return sizeof (struct GNUNET_MessageHeader);
155 preference_cb (void *cls,
157 GNUNET_PeerIdentity *peer,
158 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
160 struct GNUNET_TIME_Relative res_delay, uint64_t preference);
163 do_reserve (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
165 struct PeerContext *pc = cls;
167 irc_task = GNUNET_SCHEDULER_NO_TASK;
168 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "Aborted during attempt to request reservation\n");
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Requesting reservatin of 32k from core in 1s!\n");
176 irc = GNUNET_CORE_peer_change_preference (p1.ch,
178 GNUNET_TIME_UNIT_SECONDS,
179 GNUNET_BANDWIDTH_VALUE_MAX,
180 1000000 /* bandwidth for 1s */ ,
181 0, &preference_cb, pc);
185 preference_cb (void *cls,
187 GNUNET_PeerIdentity *peer,
188 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
190 struct GNUNET_TIME_Relative res_delay, uint64_t preference)
192 struct PeerContext *pc = cls;
197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
198 "Delaying reservation request by %llu ms!\n",
199 (unsigned long long) res_delay.rel_value);
200 irc_task = GNUNET_SCHEDULER_add_delayed (res_delay, &do_reserve, pc);
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "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
219 (GNUNET_TIME_UNIT_SECONDS,
222 GNUNET_MessageHeader),
223 &transmit_ready, &p1)))
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "RECEIVED NULL when asking core (1) for transmission to peer `%4s'\n",
227 GNUNET_i2s (&p2.id));
233 connect_notify (void *cls,
234 const struct GNUNET_PeerIdentity *peer,
235 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
237 struct PeerContext *pc = cls;
239 if (0 == memcmp (&pc->id, peer, 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
256 (GNUNET_TIME_UNIT_SECONDS, 120),
257 &terminate_task_error, NULL);
258 irc_task = GNUNET_SCHEDULER_add_now (&do_reserve, pc);
264 disconnect_notify (void *cls, const struct GNUNET_PeerIdentity *peer)
266 struct PeerContext *pc = cls;
268 if (0 == memcmp (&pc->id, peer, sizeof (struct GNUNET_PeerIdentity)))
270 pc->connect_status = 0;
271 if (GNUNET_SCHEDULER_NO_TASK != irc_task)
273 GNUNET_SCHEDULER_cancel (irc_task);
274 irc_task = GNUNET_SCHEDULER_NO_TASK;
276 if (0 == memcmp (peer, &p1.id, sizeof (struct GNUNET_PeerIdentity)))
280 GNUNET_CORE_peer_change_preference_cancel (irc);
285 GNUNET_CORE_notify_transmit_ready_cancel (th);
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Encrypted connection to `%4s' cut\n", GNUNET_i2s (peer));
295 inbound_notify (void *cls,
296 const struct GNUNET_PeerIdentity *other,
297 const struct GNUNET_MessageHeader *message,
298 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Core provides inbound data from `%4s'.\n", GNUNET_i2s (other));
307 outbound_notify (void *cls,
308 const struct GNUNET_PeerIdentity *other,
309 const struct GNUNET_MessageHeader *message,
310 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
313 "Core notifies about outbound data for `%4s'.\n",
321 process_mtype (void *cls,
322 const struct GNUNET_PeerIdentity *peer,
323 const struct GNUNET_MessageHeader *message,
324 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327 "Receiving message from `%4s'.\n", GNUNET_i2s (peer));
328 GNUNET_assert (ok == 5);
330 GNUNET_SCHEDULER_cancel (err_task);
331 err_task = GNUNET_SCHEDULER_NO_TASK;
332 GNUNET_SCHEDULER_add_now (&terminate_task, NULL);
337 static struct GNUNET_CORE_MessageHandler handlers[] = {
338 {&process_mtype, MTYPE, sizeof (struct GNUNET_MessageHeader)},
345 ask_connect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
348 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
349 &ask_connect_task, NULL);
350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
351 "Asking core (1) AGAIN to connect to peer `%4s'\n",
352 GNUNET_i2s (&p2.id));
353 GNUNET_CORE_peer_request_connect (p1.ch, &p2.id, NULL, NULL);
358 init_notify (void *cls,
359 struct GNUNET_CORE_Handle *server,
360 const struct GNUNET_PeerIdentity *my_identity,
361 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
363 struct PeerContext *p = cls;
365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366 "Core connection to `%4s' established\n",
367 GNUNET_i2s (my_identity));
368 GNUNET_assert (server != NULL);
369 p->id = *my_identity;
373 GNUNET_assert (ok == 2);
376 GNUNET_CORE_connect (p2.cfg, 1,
383 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
387 GNUNET_assert (ok == 3);
389 GNUNET_assert (cls == &p2);
390 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
391 "Asking core (1) to connect to peer `%4s'\n",
392 GNUNET_i2s (&p2.id));
394 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
395 (GNUNET_TIME_UNIT_SECONDS, 60),
396 &terminate_task_error, NULL);
397 if (GNUNET_SCHEDULER_NO_TASK != ask_task)
398 GNUNET_SCHEDULER_cancel (ask_task);
400 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
401 &ask_connect_task, NULL);
402 GNUNET_CORE_peer_request_connect (p1.ch, &p2.id, NULL, NULL);
408 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
410 struct PeerContext *p = cls;
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Received (my) `%s' from transport service\n", "HELLO");
414 GNUNET_assert (message != NULL);
415 p->hello = GNUNET_malloc (ntohs (message->size));
416 memcpy (p->hello, message, ntohs (message->size));
417 if ((p == &p1) && (p2.th != NULL))
418 GNUNET_TRANSPORT_offer_hello (p2.th, message, NULL, NULL);
419 if ((p == &p2) && (p1.th != NULL))
420 GNUNET_TRANSPORT_offer_hello (p1.th, message, NULL, NULL);
422 if ((p == &p1) && (p2.hello != NULL) && (p1.th != NULL))
423 GNUNET_TRANSPORT_offer_hello (p1.th, p2.hello, NULL, NULL);
424 if ((p == &p2) && (p1.hello != NULL) && (p2.th != NULL))
425 GNUNET_TRANSPORT_offer_hello (p2.th, p1.hello, NULL, NULL);
431 setup_peer (struct PeerContext *p, const char *cfgname)
433 p->cfg = GNUNET_CONFIGURATION_create ();
435 p->arm_proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
436 "gnunet-service-arm",
440 "-c", cfgname, NULL);
442 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (p->cfg, cfgname));
443 p->th = GNUNET_TRANSPORT_connect (p->cfg, NULL, p, NULL, NULL, NULL);
444 GNUNET_assert (p->th != NULL);
445 GNUNET_TRANSPORT_get_hello (p->th, &process_hello, p);
452 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
454 GNUNET_assert (ok == 1);
456 setup_peer (&p1, "test_core_quota_asymmetric_recv_limited_peer1.conf");
457 setup_peer (&p2, "test_core_quota_asymmetric_recv_limited_peer2.conf");
458 GNUNET_CORE_connect (p1.cfg, 1,
465 GNUNET_YES, &outbound_notify, GNUNET_YES, handlers);
469 stop_arm (struct PeerContext *p)
472 if (0 != GNUNET_OS_process_kill (p->arm_proc, SIGTERM))
473 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
474 if (GNUNET_OS_process_wait (p->arm_proc) != GNUNET_OK)
475 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "waitpid");
476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
477 "ARM process %u stopped\n",
478 GNUNET_OS_process_get_pid (p->arm_proc));
479 GNUNET_OS_process_close (p->arm_proc);
482 GNUNET_CONFIGURATION_destroy (p->cfg);
488 char *const argv[] = { "test-core-api-preferences",
490 "test_core_api_data.conf",
496 struct GNUNET_GETOPT_CommandLineOption options[] = {
497 GNUNET_GETOPT_OPTION_END
500 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
501 argv, "test-core-api-preferences", "nohelp", options,
509 main (int argc, char *argv[])
513 GNUNET_log_setup ("test-core-api",
521 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-1");
522 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-core-peer-2");
527 /* end of test_core_api_preferences.c */