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 static GNUNET_SCHEDULER_TaskIdentifier die_task;
44 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
54 static struct Test_Address test_addr[2];
59 static struct PeerContext p;
64 struct GNUNET_HELLO_Address test_hello_address[2];
69 static void *test_session[2];
74 struct GNUNET_ATS_Information test_ats_info[2];
79 uint32_t test_ats_count;
82 * Configured WAN out quota
84 unsigned long long wan_quota_out;
87 * Configured WAN in quota
89 unsigned long long wan_quota_in;
92 create_test_address (struct Test_Address *dest, char * plugin, void *session, void *addr, size_t addrlen)
94 dest->plugin = GNUNET_strdup (plugin);
95 dest->session = session;
96 dest->addr = GNUNET_malloc (addrlen);
97 memcpy (dest->addr, addr, addrlen);
98 dest->addr_len = addrlen;
102 free_test_address (struct Test_Address *dest)
104 GNUNET_free (dest->plugin);
105 GNUNET_free (dest->addr);
110 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 die_task = GNUNET_SCHEDULER_NO_TASK;
114 if (sched_ats != NULL)
115 GNUNET_ATS_scheduling_done (sched_ats);
116 free_test_address (&test_addr[0]);
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
125 if (die_task != GNUNET_SCHEDULER_NO_TASK)
127 GNUNET_SCHEDULER_cancel (die_task);
128 die_task = GNUNET_SCHEDULER_NO_TASK;
130 GNUNET_ATS_scheduling_done (sched_ats);
132 free_test_address (&test_addr[0]);
136 compare_addresses (const struct GNUNET_HELLO_Address *address1, void *session1,
137 const struct GNUNET_HELLO_Address *address2, void *session2)
139 if (0 != memcmp (&address1->peer, &address2->peer, sizeof (struct GNUNET_PeerIdentity)))
141 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid peer id'\n");
142 return GNUNET_SYSERR;
144 if (0 != strcmp (address1->transport_name, address2->transport_name))
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid plugin'\n");
147 return GNUNET_SYSERR;
149 if (address1->address_length != address2->address_length)
151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address length'\n");
152 return GNUNET_SYSERR;
155 else if (0 != memcmp (address1->address, address2->address, address2->address_length))
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid address'\n");
158 return GNUNET_SYSERR;
160 if (session1 != session2)
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggestion with invalid session1 %p vs session2 %p'\n",
164 return GNUNET_SYSERR;
172 compare_ats (const struct GNUNET_ATS_Information *ats_is, uint32_t ats_count_is,
173 const struct GNUNET_ATS_Information *ats_should, uint32_t ats_count_should)
177 char *prop[] = GNUNET_ATS_PropertyStrings;
184 for (c_o = 0; c_o < ats_count_is; c_o++)
186 for (c_i = 0; c_i < ats_count_should; c_i++)
188 type1 = ntohl(ats_is[c_o].type);
189 type2 = ntohl(ats_should[c_i].type);
192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS type `%s'\n",
194 val1 = ntohl(ats_is[c_o].value);
195 val2 = ntohl(ats_should[c_i].value);
198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "ATS value `%s' not equal: %u != %u\n",
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS value `%s' equal: %u == %u\n",
216 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
217 struct Session *session,
218 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
219 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
220 const struct GNUNET_ATS_Information *atsi,
223 if (GNUNET_OK == compare_addresses (address, session, &test_hello_address[0], test_session[0]))
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with correct address `%s'\n",
226 GNUNET_i2s (&address->peer));
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage 0: Callback with invalid address `%s'\n",
232 GNUNET_i2s (&address->peer));
236 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
238 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage 0: Callback with incorrect ats info \n");
242 if (ntohl(bandwidth_in.value__) > wan_quota_in)
244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN inbound quota %u bigger than allowed quota %llu \n",
245 (unsigned int) ntohl(bandwidth_in.value__), wan_quota_in);
249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN inbound quota %u, allowed quota %llu \n",
250 (unsigned int) ntohl(bandwidth_in.value__), wan_quota_in);
252 if (ntohl(bandwidth_out.value__) > wan_quota_out)
254 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Suggested WAN outbound quota %u bigger than allowed quota %llu \n",
255 (unsigned int) ntohl(bandwidth_out.value__), wan_quota_out);
259 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Suggested WAN outbound quota %u, allowed quota %llu \n",
260 (unsigned int) ntohl(bandwidth_out.value__), wan_quota_out);
265 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
266 GNUNET_SCHEDULER_add_now (&end, NULL);
271 const struct GNUNET_CONFIGURATION_Handle *cfg,
272 struct GNUNET_TESTING_Peer *peer)
276 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_OUT", "a_str))
278 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
282 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_out))
284 fprintf (stderr, "Cannot load WAN outbound quota from configuration, exit!\n");
286 GNUNET_free (quota_str);
289 GNUNET_free (quota_str);
292 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_string(cfg, "ats", "WAN_QUOTA_IN", "a_str))
294 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
298 if (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_str, &wan_quota_in))
300 fprintf (stderr, "Cannot load WAN inbound quota from configuration, exit!\n");
301 GNUNET_free (quota_str);
305 GNUNET_free (quota_str);
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN inbound quota: %llu\n", wan_quota_in);
308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Configured WAN outbound quota: %llu\n", wan_quota_out);
311 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
313 /* Connect to ATS scheduling */
314 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
315 if (sched_ats == NULL)
317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
324 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID, &p.id.hashPubKey))
326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
332 GNUNET_assert (0 == strcmp (PEERID, GNUNET_i2s_full (&p.id)));
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
337 /* Prepare ATS Information */
338 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
339 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
340 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
341 test_ats_info[1].value = htonl(1);
344 /* Adding address without session */
345 test_session[0] = &test_addr[0];
346 create_test_address (&test_addr[0], "test", test_session, "test", strlen ("test") + 1);
347 test_hello_address[0].peer = p.id;
348 test_hello_address[0].transport_name = test_addr[0].plugin;
349 test_hello_address[0].address = test_addr[0].addr;
350 test_hello_address[0].address_length = test_addr[0].addr_len;
351 GNUNET_ATS_address_add (sched_ats, &test_hello_address[0], test_session[0], test_ats_info, test_ats_count);
353 GNUNET_ATS_suggest_address (sched_ats, &p.id);
358 main (int argc, char *argv[])
360 if (0 != GNUNET_TESTING_peer_run ("test_ats_simplististic",
368 /* end of file test_ats_api_bandwidth_consumption.c */