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 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_NO
41 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
43 #define START_ARM GNUNET_YES
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
51 * How long until we give up on transmitting the message?
53 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
55 #define DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
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;
95 * Testcase specific declarations
99 * Note that this value must not significantly exceed
100 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
101 * messages may be dropped even for a reliable transport.
103 #define TOTAL_MSGS (1024 * 2)
107 GNUNET_NETWORK_STRUCT_BEGIN
111 struct GNUNET_MessageHeader header;
114 GNUNET_NETWORK_STRUCT_END
116 static int msg_scheduled;
118 static int msg_recv_expected;
121 static int test_failed;
122 static int test_connected;
124 static unsigned long long total_bytes_sent;
126 static struct GNUNET_TIME_Absolute start_time;
129 * END Testcase specific declarations
133 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
135 #define OKPP do { ok++; } while (0)
142 unsigned long long delta;
143 unsigned long long datarate;
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
147 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
148 datarate = (total_bytes_sent * 1000) / delta;
150 FPRINTF (stderr, "Throughput was %llu b/s\n", datarate);
152 test_failed = GNUNET_NO;
153 if (datarate > quota_in_p2)
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
156 "Datarate of %llu b/s higher than allowed inbound quota of %llu b/s\n",
157 datarate, quota_in_p2);
158 test_failed = GNUNET_YES;
160 if (datarate > quota_out_p1)
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
163 "Datarate of %llu b/s higher than allowed outbound quota of %llu b/s\n",
164 datarate, quota_out_p1);
165 test_failed = GNUNET_YES;
167 if (test_failed == GNUNET_NO)
169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
170 "Datarate of %llu b/s complied to allowed outbound quota of %llu b/s and inbound quota of %llu b/s\n",
171 datarate, quota_out_p1, quota_in_p2);
174 if (die_task != GNUNET_SCHEDULER_NO_TASK)
175 GNUNET_SCHEDULER_cancel (die_task);
178 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
182 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
184 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
185 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
192 die_task = GNUNET_SCHEDULER_NO_TASK;
193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
195 if (measure_task != GNUNET_SCHEDULER_NO_TASK)
196 GNUNET_SCHEDULER_cancel (measure_task);
198 if (test_connected == GNUNET_YES)
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got connected\n");
201 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Peers got NOT connected\n");
204 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
208 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
211 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
213 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
220 get_size (unsigned int iter)
224 ret = (iter * iter * iter);
225 return sizeof (struct TestMessage) + (ret % 60000);
230 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
231 const struct GNUNET_MessageHeader *message,
232 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
236 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
237 const struct TestMessage *hdr;
239 hdr = (const struct TestMessage *) message;
241 if (MTYPE != ntohs (message->type))
243 msg_recv_expected = n;
244 msg_recv = ntohl (hdr->num);
245 if (ntohs (message->size) != (s))
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248 "Expected message %u of size %u, got %u bytes of message %u\n",
249 n, s, ntohs (message->size), ntohl (hdr->num));
250 if (die_task != GNUNET_SCHEDULER_NO_TASK)
251 GNUNET_SCHEDULER_cancel (die_task);
252 test_failed = GNUNET_YES;
253 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
256 if (ntohl (hdr->num) != n)
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 "Expected message %u of size %u, got %u bytes of message %u\n",
260 n, s, ntohs (message->size), ntohl (hdr->num));
261 if (die_task != GNUNET_SCHEDULER_NO_TASK)
262 GNUNET_SCHEDULER_cancel (die_task);
263 test_failed = GNUNET_YES;
264 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
267 memset (cbuf, n, s - sizeof (struct TestMessage));
268 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
271 "Expected message %u with bits %u, but body did not match\n", n,
273 if (die_task != GNUNET_SCHEDULER_NO_TASK)
274 GNUNET_SCHEDULER_cancel (die_task);
275 test_failed = GNUNET_YES;
276 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
280 if (ntohl (hdr->num) % 5000 == 0)
282 struct PeerContext *p = cls;
283 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
285 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
286 "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
287 p->no, ps, ntohl (hdr->num), ntohs (message->size),
297 notify_ready (void *cls, size_t size, void *buf)
301 struct TestMessage hdr;
308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
309 "Timeout occurred while waiting for transmit_ready for message %u of %u\n",
310 msg_scheduled, TOTAL_MSGS);
311 if (GNUNET_SCHEDULER_NO_TASK != die_task)
312 GNUNET_SCHEDULER_cancel (die_task);
313 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
320 GNUNET_assert (size >= s);
321 GNUNET_assert (buf != NULL);
325 hdr.header.size = htons (s);
326 hdr.header.type = htons (MTYPE);
329 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
330 ret += sizeof (struct TestMessage);
331 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
332 ret += s - sizeof (struct TestMessage);
337 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id));
339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
340 "Sending message of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
341 n, sender->no, GNUNET_i2s (&sender->id), receiver->no,
343 GNUNET_free (receiver_s);
348 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
349 break; /* sometimes pack buffer full, sometimes not */
351 while (size - ret >= s);
355 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
357 ¬ify_ready, NULL);
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Returning total message block of size %u\n", ret);
365 total_bytes_sent += ret;
368 FPRINTF (stderr, "%s", "\n");
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
376 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
377 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
380 struct PeerContext *p = cls;
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') connected to us!\n",
383 p->no, GNUNET_i2s (peer));
388 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
390 struct PeerContext *p = cls;
392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%4s') disconnected!\n", p->no,
395 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
403 start_time = GNUNET_TIME_absolute_get ();
404 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
405 TIMEOUT_TRANSMIT, ¬ify_ready,
411 measure (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
415 measure_task = GNUNET_SCHEDULER_NO_TASK;
418 if ((DURATION.rel_value / 1000) < counter)
420 FPRINTF (stderr, "%s", ".\n");
421 GNUNET_SCHEDULER_add_now (&end, NULL);
425 FPRINTF (stderr, "%s", ".");
427 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &measure, NULL);
433 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
435 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
438 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
442 test_connected = GNUNET_YES;
445 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &measure, NULL);
446 GNUNET_SCHEDULER_add_now (&sendtask, NULL);
451 start_cb (struct PeerContext *p, void *cls)
457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
458 GNUNET_i2s (&p->id));
463 test_connected = GNUNET_NO;
468 char *sender_c = GNUNET_strdup (GNUNET_i2s (&sender->id));
470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
471 "Test tries to send from %u (%s) -> peer %u (%s)\n", sender->no,
472 sender_c, receiver->no, GNUNET_i2s (&receiver->id));
474 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
480 generate_config (char *cfg_file, unsigned long long quota_in,
481 unsigned long long quota_out)
484 struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();
486 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_load (cfg, cfg_file));
487 GNUNET_asprintf (&fname, "q_in_%llu_q_out_%llu_%s", quota_in, quota_out,
489 GNUNET_CONFIGURATION_set_value_string (cfg, "PATHS", "DEFAULTCONFIG", fname);
490 GNUNET_CONFIGURATION_set_value_number (cfg, "ats", "WAN_QUOTA_IN", quota_in);
491 GNUNET_CONFIGURATION_set_value_number (cfg, "ats", "WAN_QUOTA_OUT",
493 GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_write (cfg, fname));
494 GNUNET_CONFIGURATION_destroy (cfg);
499 run_measurement (unsigned long long p1_quota_in,
500 unsigned long long p1_quota_out,
501 unsigned long long p2_quota_in,
502 unsigned long long p2_quota_out)
504 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
506 /* setting ATS quota */
507 quota_out_p1 = p1_quota_out;
508 gen_cfg_p1 = generate_config (cfg_file_p1, p1_quota_in, p1_quota_out);
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generated config file `%s'\n",
512 quota_in_p2 = p2_quota_in;
513 gen_cfg_p2 = generate_config (cfg_file_p2, p2_quota_in, p2_quota_out);
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Generated config file `%s'\n",
517 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, gen_cfg_p1, 1, ¬ify_receive,
518 ¬ify_connect, ¬ify_disconnect,
521 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, gen_cfg_p2, 2, ¬ify_receive,
522 ¬ify_connect, ¬ify_disconnect,
525 if ((p1 == NULL) || (p2 == NULL))
527 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
528 if (die_task != GNUNET_SCHEDULER_NO_TASK)
529 GNUNET_SCHEDULER_cancel (die_task);
530 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
536 run (void *cls, char *const *args, const char *cfgfile,
537 const struct GNUNET_CONFIGURATION_Handle *cfg)
539 unsigned long long p1_quota_in = 10000;
540 unsigned long long p1_quota_out = 10000;
541 unsigned long long p2_quota_in = 10000;
542 unsigned long long p2_quota_out = 10000;
544 if (NULL != strstr (test_name, "asymmetric"))
546 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
547 "Running asymmetric test with sending peer unlimited, receiving peer (in/out): %llu/%llu b/s \n",
548 p2_quota_in, p2_quota_out);
549 p1_quota_out = 1024 * 1024 * 1024;
550 p1_quota_in = 1024 * 1024 * 1024;
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Running symmetric test with (in/out) %llu/%llu b/s \n",
556 p2_quota_in, p2_quota_out);
558 run_measurement (p1_quota_in, p1_quota_out, p2_quota_in, p2_quota_out);
564 static char *argv[] = { "test_transport-quota-compliance",
572 static struct GNUNET_GETOPT_CommandLineOption options[] = {
573 GNUNET_GETOPT_OPTION_END
577 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
578 "nohelp", options, &run, &ok);
584 main (int argc, char *argv[])
588 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
590 GNUNET_log_setup (test_name,
598 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
599 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
602 tth = GNUNET_TRANSPORT_TESTING_init ();
604 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
605 (strcmp (test_plugin, "udp_nat") == 0))
607 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
608 if (GNUNET_NO == nat_res)
610 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
611 "gnunet-nat-server", "SUID not set");
614 if (GNUNET_SYSERR == nat_res)
616 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
617 "gnunet-nat-server", "file not found");
622 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
623 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
627 GNUNET_free (cfg_file_p1);
628 GNUNET_free (cfg_file_p2);
630 if (GNUNET_YES == GNUNET_DISK_file_test (gen_cfg_p1))
632 GNUNET_DISK_directory_remove (gen_cfg_p1);
633 GNUNET_free (gen_cfg_p1);
636 if (GNUNET_YES == GNUNET_DISK_file_test (gen_cfg_p2))
638 GNUNET_DISK_directory_remove (gen_cfg_p2);
639 GNUNET_free (gen_cfg_p2);
642 GNUNET_free (test_source);
643 GNUNET_free (test_plugin);
644 GNUNET_free (test_name);
646 GNUNET_TRANSPORT_TESTING_done (tth);
652 /* end of test_quota_compliance.c */