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 transport/test_quota_compliance.c
22 * @brief base test case for transport implementations
24 * This test case tests quota compliance both on core and transport level
27 #include "gnunet_common.h"
28 #include "gnunet_hello_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_program_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_transport_service.h"
36 #include "transport.h"
37 #include "transport-testing.h"
39 #define VERBOSE GNUNET_EXTRA_LOGGING
41 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
43 #define START_ARM GNUNET_YES
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
51 * How long until we give up on transmitting the message?
53 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 2)
55 #define DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
57 static char *test_source;
59 static char *test_plugin;
61 static char *test_name;
65 static GNUNET_SCHEDULER_TaskIdentifier die_task;
67 static GNUNET_SCHEDULER_TaskIdentifier measure_task;
69 struct PeerContext *p1;
71 struct PeerContext *p2;
73 struct PeerContext * sender;
75 struct PeerContext * receiver;
77 struct GNUNET_TRANSPORT_TransmitHandle *th;
81 unsigned long long quota_in_p1;
82 unsigned long long quota_out_p1;
86 unsigned long long quota_in_p2;
87 unsigned long long quota_out_p2;
89 struct GNUNET_TRANSPORT_TESTING_handle * tth;
91 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
94 * Testcase specific declarations
98 * Note that this value must not significantly exceed
99 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
100 * messages may be dropped even for a reliable transport.
102 #define TOTAL_MSGS (1024 * 2)
108 struct GNUNET_MessageHeader header;
112 static int msg_scheduled;
114 static int msg_recv_expected;
117 static int test_failed;
118 static int test_connected;
120 static unsigned long long total_bytes_sent;
122 static struct GNUNET_TIME_Absolute start_time;
125 * END Testcase specific declarations
129 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
131 #define OKPP do { ok++; } while (0)
138 unsigned long long delta;
139 unsigned long long datarate;
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
143 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
144 datarate = (total_bytes_sent * 1000) / delta;
146 fprintf (stderr, "\nThroughput was %llu b/s\n",
149 test_failed = GNUNET_NO;
150 if (datarate > quota_in_p2)
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
153 "Datarate of %llu higher than allowed inbound quota of %llu\n", datarate, quota_in_p2);
154 test_failed = GNUNET_YES;
156 if (datarate > quota_out_p1)
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
159 "Datarate of %llu higher than allowed outbound quota of %llu\n", datarate, quota_out_p1);
160 test_failed = GNUNET_YES;
162 if (test_failed == GNUNET_NO)
164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
165 "Datarate of %llu complied to allowed outbound quota of %llu and inbound quota of %llu\n", datarate, quota_out_p1, quota_in_p2);
171 if (die_task != GNUNET_SCHEDULER_NO_TASK)
172 GNUNET_SCHEDULER_cancel (die_task);
175 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
179 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
181 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
182 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
189 die_task = GNUNET_SCHEDULER_NO_TASK;
190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
192 if (test_connected == GNUNET_YES)
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
198 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
202 GNUNET_TRANSPORT_TESTING_connect_peers_cancel(tth, cc);
205 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
207 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
214 get_size (unsigned int iter)
218 ret = (iter * iter * iter);
219 return sizeof (struct TestMessage) + (ret % 60000);
224 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
225 const struct GNUNET_MessageHeader *message,
226 const struct GNUNET_ATS_Information *ats,
231 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
232 const struct TestMessage *hdr;
234 hdr = (const struct TestMessage *) message;
236 if (MTYPE != ntohs (message->type))
238 msg_recv_expected = n;
239 msg_recv = ntohl (hdr->num);
240 if (ntohs (message->size) != (s))
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243 "Expected message %u of size %u, got %u bytes of message %u\n",
244 n, s, ntohs (message->size), ntohl (hdr->num));
245 if (die_task != GNUNET_SCHEDULER_NO_TASK)
246 GNUNET_SCHEDULER_cancel (die_task);
247 test_failed = GNUNET_YES;
248 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
251 if (ntohl (hdr->num) != n)
253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
254 "Expected message %u of size %u, got %u bytes of message %u\n",
255 n, s, ntohs (message->size), ntohl (hdr->num));
256 if (die_task != GNUNET_SCHEDULER_NO_TASK)
257 GNUNET_SCHEDULER_cancel (die_task);
258 test_failed = GNUNET_YES;
259 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
262 memset (cbuf, n, s - sizeof (struct TestMessage));
263 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
265 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
266 "Expected message %u with bits %u, but body did not match\n", n,
268 if (die_task != GNUNET_SCHEDULER_NO_TASK)
269 GNUNET_SCHEDULER_cancel (die_task);
270 test_failed = GNUNET_YES;
271 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
275 if (ntohl (hdr->num) % 5000 == 0)
277 struct PeerContext *p = cls;
278 char * ps = strdup(GNUNET_i2s(&p->id));
279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
280 p->no, ps, ntohl (hdr->num), ntohs (message->size), GNUNET_i2s(peer));
285 if (0 == (n % (TOTAL_MSGS / 100)))
287 fprintf (stderr, ".");
288 if (die_task != GNUNET_SCHEDULER_NO_TASK)
289 GNUNET_SCHEDULER_cancel (die_task);
290 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "\nAll messages received\n");
302 notify_ready (void *cls, size_t size, void *buf)
306 struct TestMessage hdr;
313 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
314 "Timeout occurred while waiting for transmit_ready for message %u of %u\n", msg_scheduled, TOTAL_MSGS);
315 if (GNUNET_SCHEDULER_NO_TASK != die_task)
316 GNUNET_SCHEDULER_cancel (die_task);
317 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
324 GNUNET_assert (size >= s);
325 GNUNET_assert (buf != NULL);
329 hdr.header.size = htons (s);
330 hdr.header.type = htons (MTYPE);
333 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
334 ret += sizeof (struct TestMessage);
335 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
336 ret += s - sizeof (struct TestMessage);
341 char * receiver_s = strdup(GNUNET_i2s (&receiver->id));
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Sending message of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
346 GNUNET_i2s (&sender->id), receiver->no, receiver_s);
347 GNUNET_free (receiver_s);
352 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
353 break; /* sometimes pack buffer full, sometimes not */
355 while (size - ret >= s);
359 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
361 ¬ify_ready, NULL);
366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
367 "Returning total message block of size %u\n", ret);
369 total_bytes_sent += ret;
372 fprintf (stderr, "\n");
373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
380 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
381 const struct GNUNET_ATS_Information *ats,
385 struct PeerContext *p = cls;
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to us!\n",
387 p->no, GNUNET_i2s (peer));
392 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
394 struct PeerContext *p = cls;
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') disconnected!\n",
396 p->no, GNUNET_i2s (peer));
398 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
406 start_time = GNUNET_TIME_absolute_get ();
407 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
408 TIMEOUT_TRANSMIT, ¬ify_ready,
413 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
415 char *p1_c = strdup (GNUNET_i2s (&p1->id));
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
418 p2->no, GNUNET_i2s (&p2->id));
422 test_connected = GNUNET_YES;
424 measure_task = GNUNET_SCHEDULER_add_delayed (DURATION, &end, NULL);
425 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
429 void start_cb (struct PeerContext * p,
435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n",
437 GNUNET_i2s (&p->id));
442 test_connected = GNUNET_NO;
447 char *sender_c = strdup (GNUNET_i2s (&sender->id));
448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Test tries to send from %u (%s) -> peer %u (%s)\n",
449 sender->no, sender_c,
450 receiver->no, GNUNET_i2s (&receiver->id));
452 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb, NULL);
457 generate_config (char * cfg_file_p1, unsigned long long quota_in, unsigned long long quota_out)
460 struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create();
461 GNUNET_CONFIGURATION_load (cfg, cfg_file_p1);
463 GNUNET_CONFIGURATION_set_value_number(cfg, "core", "TOTAL_QUOTA_IN", quota_in);
464 GNUNET_CONFIGURATION_set_value_number(cfg, "core", "TOTAL_QUOTA_OUT", quota_out);
466 GNUNET_asprintf (&fname, "q_in_%ull_q_out_%ull_%s", quota_in, quota_out, cfg_file_p1);
468 GNUNET_CONFIGURATION_write(cfg, fname);
469 GNUNET_CONFIGURATION_destroy(cfg);
475 run_measurement (unsigned long long p1_quota_in, unsigned long long p1_quota_out,
476 unsigned long long p2_quota_in, unsigned long long p2_quota_out)
478 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
480 /* setting ATS quota */
481 quota_out_p1 = p1_quota_out;
482 gen_cfg_p1 = generate_config(cfg_file_p1, p1_quota_in, p1_quota_out);
483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
484 "Generated config file `%s'\n",
487 quota_in_p2 = p2_quota_in;
488 gen_cfg_p2 = generate_config(cfg_file_p2, p2_quota_in, p2_quota_out);
489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
490 "Generated config file `%s'\n",
493 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, gen_cfg_p1, 1,
495 ¬ify_connect, ¬ify_disconnect,
499 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, gen_cfg_p2, 2,
501 ¬ify_connect, ¬ify_disconnect,
505 if ((p1 == NULL) || (p2 == NULL))
507 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
508 if (die_task != GNUNET_SCHEDULER_NO_TASK)
509 GNUNET_SCHEDULER_cancel (die_task);
510 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
516 run (void *cls, char *const *args, const char *cfgfile,
517 const struct GNUNET_CONFIGURATION_Handle *cfg)
519 run_measurement (10000, 10000, 10000, 10000);
525 static char *argv[] = { "test_transport-quota-compliance",
527 "test_transport_api_data.conf",
533 static struct GNUNET_GETOPT_CommandLineOption options[] = {
534 GNUNET_GETOPT_OPTION_END
538 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
539 "nohelp", options, &run, &ok);
545 main (int argc, char *argv[])
550 tth = GNUNET_TRANSPORT_TESTING_init ();
552 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
553 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
555 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
557 GNUNET_log_setup (test_name,
565 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
566 (strcmp (test_plugin, "udp_nat") == 0))
568 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
569 if (GNUNET_NO == nat_res)
571 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
572 "gnunet-nat-server", "SUID not set");
575 if (GNUNET_SYSERR == nat_res)
577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
578 "gnunet-nat-server", "file not found");
583 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
584 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
588 GNUNET_free (cfg_file_p1);
589 GNUNET_free (cfg_file_p2);
591 if (GNUNET_YES == GNUNET_DISK_file_test (gen_cfg_p1))
593 GNUNET_DISK_directory_remove (gen_cfg_p1);
594 GNUNET_free (gen_cfg_p1);
597 if (GNUNET_YES == GNUNET_DISK_file_test (gen_cfg_p2))
599 //GNUNET_DISK_directory_remove (gen_cfg_p2);
600 GNUNET_free (gen_cfg_p2);
603 GNUNET_free (test_source);
604 GNUNET_free (test_plugin);
605 GNUNET_free (test_name);
607 GNUNET_TRANSPORT_TESTING_done (tth);
613 /* end of test_quota_compliance.c */