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;
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]);
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
111 if (die_task != GNUNET_SCHEDULER_NO_TASK)
113 GNUNET_SCHEDULER_cancel (die_task);
114 die_task = GNUNET_SCHEDULER_NO_TASK;
116 GNUNET_ATS_scheduling_done (sched_ats);
118 free_test_address (&test_addr[0]);
123 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
124 struct Session *session,
125 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
126 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
127 const struct GNUNET_ATS_Information *atsi,
130 static int stage = 0;
131 unsigned int bw_in = ntohl(bandwidth_in.value__);
132 unsigned int bw_out = ntohl(bandwidth_out.value__);
135 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
138 GNUNET_i2s (&address->peer));
143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
144 GNUNET_i2s (&address->peer));
148 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
154 if (bw_in > wan_quota_in)
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
157 bw_in, wan_quota_in);
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
162 bw_in, wan_quota_in);
164 if (bw_out > wan_quota_out)
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
167 bw_out, wan_quota_out);
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
172 bw_out, wan_quota_out);
176 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[0].id);
177 GNUNET_SCHEDULER_add_now (&end, NULL);
180 p[0].bw_out_assigned = bw_out;
181 p[0].bw_in_assigned = bw_in;
184 /* Add a 2nd address */
185 /* Prepare ATS Information */
186 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
187 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
188 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
189 test_ats_info[1].value = htonl(1);
192 /* Adding address with session */
193 test_session[1] = &test_addr[1];
194 create_test_address (&test_addr[1], "test1", test_session[1], "test1", strlen ("test1") + 1);
195 test_hello_address[1].peer = p[1].id;
196 test_hello_address[1].transport_name = test_addr[1].plugin;
197 test_hello_address[1].address = test_addr[1].addr;
198 test_hello_address[1].address_length = test_addr[1].addr_len;
199 GNUNET_ATS_address_add (sched_ats, &test_hello_address[1], test_session[1], test_ats_info, test_ats_count);
203 /* Expecting callback for address[0] with updated quota and no callback for address[1]*/
204 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with correct address `%s'\n",
207 GNUNET_i2s (&address->peer));
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 1: Callback with invalid address `%s'\n",
213 GNUNET_i2s (&address->peer));
217 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 1: Callback with incorrect ats info \n");
223 if (bw_in > wan_quota_in)
225 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
226 bw_in, wan_quota_in);
229 else if (p[0].bw_in_assigned > bw_in)
231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
232 bw_in, p[0].bw_in_assigned);
236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
237 bw_in, wan_quota_in);
239 if (bw_out > wan_quota_out)
241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
242 bw_out, wan_quota_out);
245 else if (p[0].bw_out_assigned > bw_out)
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than last quota %llu \n",
248 bw_out, p[0].bw_out_assigned);
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
253 bw_out, wan_quota_out);
257 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
258 GNUNET_SCHEDULER_add_now (&end, NULL);
263 GNUNET_ATS_suggest_address_cancel (sched_ats, &p[1].id);
264 GNUNET_SCHEDULER_add_now (&end, NULL);
272 const struct GNUNET_CONFIGURATION_Handle *cfg,
273 struct GNUNET_TESTING_Peer *peer)
277 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_str))
279 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
283 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
285 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
287 GNUNET_free (quota_str);
290 GNUNET_free (quota_str);
293 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_str))
295 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
299 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
301 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
302 GNUNET_free (quota_str);
306 GNUNET_free (quota_str);
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
312 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
314 /* Connect to ATS scheduling */
315 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
316 if (sched_ats == NULL)
318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
325 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p[0].id.hashPubKey))
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
333 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p[0].id)));
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
336 GNUNET_i2s(&p[0].id));
339 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID1, &p[1].id.hashPubKey))
341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
347 GNUNET_assert (0 == strcmp (PEERID1, GNUNET_i2s_full (&p[1].id)));
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
350 GNUNET_i2s(&p[1].id));
352 /* Prepare ATS Information */
353 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
354 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
355 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
356 test_ats_info[1].value = htonl(1);
359 /* Adding address with session */
360 test_session[0] = &test_addr[0];
361 create_test_address (&test_addr[0], "test0", test_session[0], "test0", strlen ("test0") + 1);
362 test_hello_address[0].peer = p[0].id;
363 test_hello_address[0].transport_name = test_addr[0].plugin;
364 test_hello_address[0].address = test_addr[0].addr;
365 test_hello_address[0].address_length = test_addr[0].addr_len;
366 GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
368 GNUNET_ATS_suggest_address (sched_ats, &p[0].id);
373 main (int argc, char *argv[])
375 if (0 != GNUNET_TESTING_peer_run ("test_ats_simplististic",
383 /* end of file test_ats_api_bandwidth_consumption.c */