2 This file is part of GNUnet.
3 (C) 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 ats/test_ats_mlp.c
22 * @brief test for the MLP solver
23 * @author Christian Grothoff
24 * @author Matthias Wachs
28 * @file ats/test_ats_api_scheduling_add_address.c
29 * @brief test for ats simplistic solver
30 * @author Christian Grothoff
31 * @author Matthias Wachs
34 #include "gnunet_ats_service.h"
35 #include "gnunet_testing_lib-new.h"
37 #include "test_ats_api_common.h"
39 #define DEBUG_ATS_INFO GNUNET_NO
41 static GNUNET_SCHEDULER_TaskIdentifier die_task;
46 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
56 static struct Test_Address test_addr[2];
61 static struct PeerContext p[2];
67 struct GNUNET_HELLO_Address test_hello_address[2];
72 static void *test_session[2];
77 struct GNUNET_ATS_Information test_ats_info[2];
82 uint32_t test_ats_count;
85 * Configured WAN out quota
87 unsigned long long wan_quota_out;
90 * Configured WAN in quota
92 unsigned long long wan_quota_in;
95 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
97 dest->plugin = GNUNET_strdup (plugin);
98 dest->session = session;
99 dest->addr = GNUNET_malloc (addrlen);
100 memcpy (dest->addr, addr, addrlen);
101 dest->addr_len = addrlen;
105 free_test_address (struct Test_Address *dest)
107 GNUNET_free (dest->plugin);
108 GNUNET_free (dest->addr);
113 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 die_task = GNUNET_SCHEDULER_NO_TASK;
117 if (sched_ats != NULL)
118 GNUNET_ATS_scheduling_done (sched_ats);
119 free_test_address (&test_addr[0]);
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
128 if (die_task != GNUNET_SCHEDULER_NO_TASK)
130 GNUNET_SCHEDULER_cancel (die_task);
131 die_task = GNUNET_SCHEDULER_NO_TASK;
133 GNUNET_ATS_scheduling_done (sched_ats);
135 free_test_address (&test_addr[0]);
139 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
140 const struct GNUNET_HELLO_Address *address2, void *session2)
142 if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
145 return GNUNET_SYSERR;
147 if (0 != strcmp (address1->transport_name, address2->transport_name))
149 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
150 return GNUNET_SYSERR;
152 if (address1->address_length != address2->address_length)
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
155 return GNUNET_SYSERR;
158 else if (0 != memcmp (address1->address, address2->address, address2->address_length))
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
161 return GNUNET_SYSERR;
163 if (session1 != session2)
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
167 return GNUNET_SYSERR;
175 compare_ats (const struct GNUNET_ATS_Information *ats_is, uint32_t ats_count_is,
176 const struct GNUNET_ATS_Information *ats_should, uint32_t ats_count_should)
180 char *prop[] = GNUNET_ATS_PropertyStrings;
187 for (c_o = 0; c_o < ats_count_is; c_o++)
189 for (c_i = 0; c_i < ats_count_should; c_i++)
191 type1 = ntohl(ats_is[c_o].type);
192 type2 = ntohl(ats_should[c_i].type);
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS type `%s'\n",
199 val1 = ntohl(ats_is[c_o].value);
200 val2 = ntohl(ats_should[c_i].value);
204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS value `%s' not equal: %u != %u\n",
213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS value `%s' equal: %u == %u\n",
225 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
226 struct Session *session,
227 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
228 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
229 const struct GNUNET_ATS_Information *atsi,
232 static int stage = 0;
233 unsigned int bw_in = ntohl(bandwidth_in.value__);
234 unsigned int bw_out = ntohl(bandwidth_out.value__);
237 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
240 GNUNET_i2s (&address->peer));
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
246 GNUNET_i2s (&address->peer));
250 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
252 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
256 if (bw_in > wan_quota_in)
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
259 bw_in, wan_quota_in);
263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
264 bw_in, wan_quota_in);
266 if (bw_out > wan_quota_out)
268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
269 bw_out, wan_quota_out);
273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
274 bw_out, wan_quota_out);
278 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
279 GNUNET_SCHEDULER_add_now (&end, NULL);
282 p[0].bw_out_assigned = bw_out;
283 p[0].bw_in_assigned = bw_in;
286 /* Add a 2nd address */
287 /* Prepare ATS Information */
288 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
289 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
290 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
291 test_ats_info[1].value = htonl(1);
294 /* Adding address with session */
295 test_session[1] = &test_addr[1];
296 create_test_address (&test_addr[1], "test1", test_session[1], "test1", strlen ("test1") + 1);
297 test_hello_address[1].peer = p[1].id;
298 test_hello_address[1].transport_name = test_addr[1].plugin;
299 test_hello_address[1].address = test_addr[1].addr;
300 test_hello_address[1].address_length = test_addr[1].addr_len;
301 GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], test_session[1], test_ats_info, test_ats_count);
305 /* Expecting callback for address[0] with updated quota and no callback for address[1]*/
306 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with correct address `%s'\n",
309 GNUNET_i2s (&address->peer));
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with invalid address `%s'\n",
315 GNUNET_i2s (&address->peer));
319 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
321 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Callback with incorrect ats info \n");
325 if (bw_in > wan_quota_in)
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
328 bw_in, wan_quota_in);
331 else if (p[0].bw_in_assigned > bw_in)
333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
334 bw_in, p[0].bw_in_assigned);
338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
339 bw_in, wan_quota_in);
341 if (bw_out > wan_quota_out)
343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
344 bw_out, wan_quota_out);
347 else if (p[0].bw_out_assigned > bw_out)
349 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
350 bw_out, p[0].bw_out_assigned);
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
355 bw_out, wan_quota_out);
359 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
360 GNUNET_SCHEDULER_add_now (&end, NULL);
365 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
366 GNUNET_SCHEDULER_add_now (&end, NULL);
374 const struct GNUNET_CONFIGURATION_Handle *cfg,
375 struct GNUNET_TESTING_Peer *peer)
379 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_str))
381 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
385 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
387 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
389 GNUNET_free (quota_str);
392 GNUNET_free (quota_str);
395 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_str))
397 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
401 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
403 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
404 GNUNET_free (quota_str);
408 GNUNET_free (quota_str);
410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
414 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
416 /* Connect to ATS scheduling */
417 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
418 if (sched_ats == NULL)
420 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
427 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
435 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
438 GNUNET_i2s(&p[0].id));
441 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
443 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
449 GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
452 GNUNET_i2s(&p[1].id));
454 /* Prepare ATS Information */
455 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
456 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
457 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
458 test_ats_info[1].value = htonl(1);
461 /* Adding address with session */
462 test_session[0] = &test_addr[0];
463 create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
464 test_hello_address[0].peer = p[0].id;
465 test_hello_address[0].transport_name = test_addr[0].plugin;
466 test_hello_address[0].address = test_addr[0].addr;
467 test_hello_address[0].address_length = test_addr[0].addr_len;
468 GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
470 GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
475 main (int argc, char *argv[])
477 if (0 != GNUNET_TESTING_peer_run ("test_ats_simplististic",
485 /* end of file test_ats_api_bandwidth_consumption.c */