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_simplistic_change_preference.c
29 * @brief test for changing preferences in 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;
51 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
62 static struct Test_Address test_addr;
67 static struct PeerContext p;
73 struct GNUNET_HELLO_Address test_hello_address;
78 static void *test_session;
83 struct GNUNET_ATS_Information test_ats_info[2];
88 uint32_t test_ats_count;
91 * Configured WAN out quota
93 unsigned long long wan_quota_out;
96 * Configured WAN in quota
98 unsigned long long wan_quota_in;
102 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
104 die_task = GNUNET_SCHEDULER_NO_TASK;
106 if (sched_ats != NULL)
107 GNUNET_ATS_scheduling_done (sched_ats);
108 if (perf_ats != NULL)
109 GNUNET_ATS_performance_done (perf_ats);
110 free_test_address (&test_addr);
118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
119 if (die_task != GNUNET_SCHEDULER_NO_TASK)
121 GNUNET_SCHEDULER_cancel (die_task);
122 die_task = GNUNET_SCHEDULER_NO_TASK;
124 GNUNET_ATS_performance_done (perf_ats);
126 GNUNET_ATS_scheduling_done (sched_ats);
128 free_test_address (&test_addr);
133 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
134 struct Session *session,
135 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
136 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
137 const struct GNUNET_ATS_Information *atsi,
140 static int stage = 0;
141 unsigned int bw_in = ntohl(bandwidth_in.value__);
142 unsigned int bw_out = ntohl(bandwidth_out.value__);
145 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
148 GNUNET_i2s (&address->peer));
153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
154 GNUNET_i2s (&address->peer));
158 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
164 if (bw_in > wan_quota_in)
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
167 bw_in, wan_quota_in);
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
172 bw_in, wan_quota_in);
174 if (bw_out > wan_quota_out)
176 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
177 bw_out, wan_quota_out);
181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
182 bw_out, wan_quota_out);
186 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
187 GNUNET_SCHEDULER_add_now (&end, NULL);
193 /* Change preference */
194 GNUNET_ATS_change_preference (perf_ats,
196 GNUNET_ATS_PREFERENCE_BANDWIDTH,(double) 1000, GNUNET_ATS_PREFERENCE_END);
198 /* Request address */
199 GNUNET_ATS_reset_backoff (sched_ats, &p.id);
200 GNUNET_ATS_suggest_address (sched_ats, &p.id);
205 /* Expecting callback for address with updated quota and no callback for address[1]*/
206 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address, test_session))
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with correct address `%s'\n",
209 GNUNET_i2s (&address->peer));
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with invalid address `%s'\n",
215 GNUNET_i2s (&address->peer));
219 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
221 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Callback with incorrect ats info \n");
225 if (bw_in > wan_quota_in)
227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
228 bw_in, wan_quota_in);
231 else if (p.bw_in_assigned > bw_in)
233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
234 bw_in, p.bw_in_assigned);
238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
239 bw_in, wan_quota_in);
241 if (bw_out > wan_quota_out)
243 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
244 bw_out, wan_quota_out);
247 else if (p.bw_out_assigned > bw_out)
249 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
250 bw_out, p.bw_out_assigned);
254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
255 bw_out, wan_quota_out);
259 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
260 GNUNET_SCHEDULER_add_now (&end, NULL);
265 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
266 GNUNET_SCHEDULER_add_now (&end, NULL);
274 const struct GNUNET_CONFIGURATION_Handle *cfg,
275 struct GNUNET_TESTING_Peer *peer)
279 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_str))
281 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
285 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
287 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
289 GNUNET_free (quota_str);
292 GNUNET_free (quota_str);
295 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_str))
297 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
301 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
303 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
304 GNUNET_free (quota_str);
308 GNUNET_free (quota_str);
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
314 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
316 /* Connect to ATS scheduling */
317 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
318 if (sched_ats == NULL)
320 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
326 perf_ats = GNUNET_ATS_performance_init (cfg, NULL, NULL);
327 if (perf_ats == NULL)
329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS performance!\n");
336 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
344 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
350 /* Prepare ATS Information */
351 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
352 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
353 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
354 test_ats_info[1].value = htonl(1);
357 /* Adding address with session */
358 test_session = &test_addr;
359 create_test_address (&test_addr, "test0", test_session, "test0", strlen ("test0") + 1);
360 test_hello_address.peer = p.id;
361 test_hello_address.transport_name = test_addr.plugin;
362 test_hello_address.address = test_addr.addr;
363 test_hello_address.address_length = test_addr.addr_len;
364 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
366 GNUNET_ATS_suggest_address (sched_ats, &p.id);
371 main (int argc, char *argv[])
373 if (0 != GNUNET_TESTING_peer_run ("test_ats_simplistic_change_preference",
381 /* end of file test_ats_simplistic_change_preference.c */