2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file transport/test_quota_compliance.c
20 * @brief base test case for transport implementations
22 * This test case tests quota compliance both on transport level
25 #include "gnunet_transport_service.h"
26 #include "gnunet_ats_service.h"
28 #include "transport-testing.h"
33 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 480)
35 #define DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 80)
38 static struct GNUNET_SCHEDULER_Task *measure_task;
40 static char *gen_cfgs[2];
42 static unsigned long long quota_in[] = { 10000, 10000 };
44 static unsigned long long quota_out[] = { 10000, 10000 };
46 static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
50 * Note that this value must not significantly exceed
51 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
52 * messages may be dropped even for a reliable transport.
54 #define TOTAL_MSGS (1024 * 32)
56 static unsigned long long total_bytes_recv;
58 static struct GNUNET_TIME_Absolute start_time;
64 unsigned long long delta;
65 unsigned long long datarate;
67 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
70 datarate = (total_bytes_recv * 1000 * 1000) / delta;
73 "Throughput was %llu b/s\n",
75 ccc->global_ret = GNUNET_OK;
76 if (datarate > 1.5 * quota_in[1])
78 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
79 "Datarate of %llu b/s significantly higher than allowed inbound quota of %llu b/s\n",
82 ccc->global_ret = GNUNET_SYSERR;
84 if (datarate > 1.5 * quota_out[0])
86 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
87 "Datarate of %llu b/s significantly higher than allowed outbound quota of %llu b/s\n",
90 ccc->global_ret = GNUNET_SYSERR;
92 if (GNUNET_OK == ccc->global_ret)
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95 "Datarate of %llu b/s complied to allowed outbound quota of %llu b/s and inbound quota of %llu b/s\n",
104 custom_shutdown (void *cls)
106 if (NULL != measure_task)
108 GNUNET_SCHEDULER_cancel (measure_task);
116 get_size (unsigned int iter)
120 ret = (iter * iter * iter) % 60000;
121 ret += sizeof (struct GNUNET_TRANSPORT_TESTING_TestMessage);
127 notify_receive (void *cls,
128 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
129 const struct GNUNET_PeerIdentity *sender,
130 const struct GNUNET_TRANSPORT_TESTING_TestMessage *hdr)
133 if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
135 total_bytes_recv += ntohs (hdr->header.size);
138 char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
140 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
141 "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
145 ntohs (hdr->header.size),
146 GNUNET_i2s (sender));
159 if ((DURATION.rel_value_us / 1000 / 1000LL) < counter)
161 FPRINTF (stderr, "%s", ".\n");
162 GNUNET_SCHEDULER_shutdown ();
165 FPRINTF (stderr, "%s", ".");
166 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
173 start_task (void *cls)
175 static struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
176 .num_messages = TOTAL_MSGS,
177 .get_size_cb = &get_size
181 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
184 start_time = GNUNET_TIME_absolute_get ();
185 GNUNET_SCHEDULER_add_now (&GNUNET_TRANSPORT_TESTING_simple_send,
191 generate_config (const char *cfg_file,
192 unsigned long long quota_in,
193 unsigned long long quota_out)
198 struct GNUNET_CONFIGURATION_Handle *cfg = GNUNET_CONFIGURATION_create ();
200 GNUNET_assert (GNUNET_OK ==
201 GNUNET_CONFIGURATION_load (cfg,
203 GNUNET_asprintf (&fname,
204 "q_in_%llu_q_out_%llu_%s",
208 GNUNET_CONFIGURATION_set_value_string (cfg,
212 for (int c = 0; c < GNUNET_ATS_NetworkTypeCount; c++)
214 GNUNET_asprintf (&in_name,
216 GNUNET_ATS_print_network_type (c));
217 GNUNET_asprintf (&out_name,
219 GNUNET_ATS_print_network_type (c));
220 GNUNET_CONFIGURATION_set_value_number (cfg,
224 GNUNET_CONFIGURATION_set_value_number (cfg,
228 GNUNET_free (in_name);
229 GNUNET_free (out_name);
231 GNUNET_assert (GNUNET_OK ==
232 GNUNET_CONFIGURATION_write (cfg,
234 GNUNET_CONFIGURATION_destroy (cfg);
241 struct GNUNET_TRANSPORT_TESTING_Handle *tth_,
242 const char *test_plugin_,
243 const char *test_name_,
244 unsigned int num_peers,
247 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
248 .connect_continuation = &start_task,
249 .config_file = "test_quota_compliance_data.conf",
250 .rec = ¬ify_receive,
251 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
252 .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
253 .shutdown_task = &custom_shutdown,
258 if (NULL != strstr (test_name_,
261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
262 "Running asymmetric test with sending peer unlimited, receiving peer (in/out): %llu/%llu b/s \n",
265 quota_out[0] = 1024 * 1024 * 1024;
266 quota_in[0] = 1024 * 1024 * 1024;
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271 "Running symmetric test with (in/out) %llu/%llu b/s \n",
275 for (unsigned int i=0;i<2;i++)
277 gen_cfgs[i] = generate_config (cfg_files[i],
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Generated config file `%s'\n",
285 return GNUNET_TRANSPORT_TESTING_connect_check (&my_ccc,
299 GNUNET_TRANSPORT_TESTING_main (2,
306 for (unsigned int i=0;i<2;i++)
308 if ( (NULL != gen_cfgs[i]) &&
309 (GNUNET_YES == GNUNET_DISK_file_test (gen_cfgs[i])) )
311 GNUNET_DISK_directory_remove (gen_cfgs[i]);
312 GNUNET_free (gen_cfgs[i]);
319 /* end of test_quota_compliance.c */