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_check_min_bw_alt.c
29 * @brief alternative suggestion on network change if no bw is available
30 * @author Christian Grothoff
31 * @author Matthias Wachs
34 #include "gnunet_ats_service.h"
35 #include "gnunet_testing_lib.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][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;
96 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
98 die_task = GNUNET_SCHEDULER_NO_TASK;
100 if (sched_ats != NULL)
101 GNUNET_ATS_scheduling_done (sched_ats);
102 free_test_address (&test_addr[0]);
103 free_test_address (&test_addr[1]);
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
112 if (die_task != GNUNET_SCHEDULER_NO_TASK)
114 GNUNET_SCHEDULER_cancel (die_task);
115 die_task = GNUNET_SCHEDULER_NO_TASK;
117 GNUNET_ATS_scheduling_done (sched_ats);
119 free_test_address (&test_addr[0]);
120 free_test_address (&test_addr[1]);
125 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
126 struct Session *session,
127 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
128 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
129 const struct GNUNET_ATS_Information *atsi,
132 static int stage = 0;
133 unsigned int bw_in = ntohl(bandwidth_in.value__);
134 unsigned int bw_out = ntohl(bandwidth_out.value__);
138 /* Initial suggestion for 1st address */
139 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n",
142 stage, GNUNET_i2s (&address->peer));
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with invalid address `%s'\n",
148 stage, GNUNET_i2s (&address->peer));
152 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info[0], test_ats_count))
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n");
158 if (bw_in > wan_quota_in)
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
161 stage, bw_in, wan_quota_in);
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Suggested WAN inbound quota %u, allowed quota %llu \n",
166 stage, bw_in, wan_quota_in);
168 if (bw_out > wan_quota_out)
170 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
171 stage, bw_out, wan_quota_out);
175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Suggested WAN outbound quota %u, allowed quota %llu \n",
176 stage, bw_out, wan_quota_out);
180 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
181 GNUNET_SCHEDULER_add_now (&end, NULL);
185 p[0].bw_out_assigned = bw_out;
186 p[0].bw_in_assigned = bw_in;
189 /* Add a 2nd address to give ATS an suggestion alternative */
190 /* Prepare ATS Information */
191 test_ats_info[1][0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
192 test_ats_info[1][0].value = htonl(GNUNET_ATS_NET_LAN);
193 test_ats_info[1][1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
194 test_ats_info[1][1].value = htonl(1);
197 /* Adding address with session */
198 test_session[1] = &test_addr[1];
199 create_test_address (&test_addr[1], "test1", test_session[1], "test1", strlen ("test1") + 1);
200 test_hello_address[1].peer = p[0].id;
201 test_hello_address[1].transport_name = test_addr[1].plugin;
202 test_hello_address[1].address = test_addr[1].addr;
203 test_hello_address[1].address_length = test_addr[1].addr_len;
204 GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], test_session[1],
205 test_ats_info[1], test_ats_count);
208 /* Changing 1st address to network with now bw available (WLAN) */
209 test_ats_info[0][0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
210 test_ats_info[0][0].value = htonl(GNUNET_ATS_NET_WLAN);
211 test_ats_info[0][1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
212 test_ats_info[0][1].value = htonl(1);
215 GNUNET_ATS_address_update (sched_ats, &test_hello_address[0], test_session[0],
216 test_ats_info[0], test_ats_count);
221 /* Bandwidth update to (in/out) 0/0 for 1st address */
222 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n",
225 stage, GNUNET_i2s (&address->peer));
230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with invalid address `%s'\n",
231 stage, GNUNET_i2s (&address->peer));
235 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info[0], test_ats_count))
237 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n");
241 if ((bw_in != 0) || (bw_out != 0))
243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: ATS did not set bandwidth to 0 but instead to %u/%u \n",
244 stage, bw_in, wan_quota_in);
250 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
251 GNUNET_SCHEDULER_add_now (&end, NULL);
254 p[0].bw_out_assigned = bw_out;
255 p[0].bw_in_assigned = bw_in;
261 /* Expecting suggestion of alternative 2nd address*/
262 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[1], test_session[1]))
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n",
265 stage, GNUNET_i2s (&address->peer));
270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with invalid address `%s'\n",
271 stage, GNUNET_i2s (&address->peer));
275 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info[1], test_ats_count))
277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n",
282 if ((bw_in == 0) || (bw_out == 0))
284 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: ATS did not set bandwidth correctly \n",
291 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
292 GNUNET_SCHEDULER_add_now (&end, NULL);
296 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
297 GNUNET_SCHEDULER_add_now (&end, NULL);
305 const struct GNUNET_CONFIGURATION_Handle *cfg,
306 struct GNUNET_TESTING_Peer *peer)
310 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_str))
312 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
316 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
318 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
320 GNUNET_free (quota_str);
323 GNUNET_free (quota_str);
326 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_str))
328 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
332 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
334 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
335 GNUNET_free (quota_str);
339 GNUNET_free (quota_str);
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
345 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
347 /* Connect to ATS scheduling */
348 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
349 if (sched_ats == NULL)
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
358 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
366 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
369 GNUNET_i2s(&p[0].id));
372 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
380 GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
383 GNUNET_i2s(&p[1].id));
385 /* Prepare ATS Information */
386 test_ats_info[0][0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
387 test_ats_info[0][0].value = htonl(GNUNET_ATS_NET_WAN);
388 test_ats_info[0][1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
389 test_ats_info[0][1].value = htonl(1);
392 /* Adding address with session */
393 test_session[0] = &test_addr[0];
394 create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
395 test_hello_address[0].peer = p[0].id;
396 test_hello_address[0].transport_name = test_addr[0].plugin;
397 test_hello_address[0].address = test_addr[0].addr;
398 test_hello_address[0].address_length = test_addr[0].addr_len;
399 GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0],
400 test_ats_info[0], test_ats_count);
402 GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
407 main (int argc, char *argv[])
409 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_check_min_bw_alt",
417 /* end of file test_ats_api_scheduling_check_min_bw_alt.c */