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_api_scheduling_update_address.c
22 * @brief test updating networtk type of an address
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
30 #include "test_ats_api_common.h"
32 #define BIG_M_STRING "unlimited"
35 static GNUNET_SCHEDULER_TaskIdentifier die_task;
40 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
50 static struct Test_Address test_addr;
55 static struct PeerContext p;
61 struct GNUNET_HELLO_Address test_hello_address;
66 static void *test_session;
71 struct GNUNET_ATS_Information test_ats_info[3];
76 uint32_t test_ats_count;
78 unsigned long long int quota_out[GNUNET_ATS_NetworkTypeCount];
79 unsigned long long int quota_in[GNUNET_ATS_NetworkTypeCount];
82 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
84 die_task = GNUNET_SCHEDULER_NO_TASK;
86 if (sched_ats != NULL)
87 GNUNET_ATS_scheduling_done (sched_ats);
88 free_test_address (&test_addr);
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
97 if (die_task != GNUNET_SCHEDULER_NO_TASK)
99 GNUNET_SCHEDULER_cancel (die_task);
100 die_task = GNUNET_SCHEDULER_NO_TASK;
102 GNUNET_ATS_scheduling_done (sched_ats);
104 free_test_address (&test_addr);
108 find_ats_value (const struct GNUNET_ATS_Information *atsi,
113 for (c = 0; c < ats_count; c ++)
115 if (ntohl(atsi[c].type) == value)
116 return ntohl (atsi[c].value);
124 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
125 struct Session *session,
126 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
127 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
128 const struct GNUNET_ATS_Information *atsi,
131 static int stage = 0;
136 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
137 if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback for correct address `%s'\n",
140 stage, GNUNET_i2s (&address->peer));
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n",
146 stage, GNUNET_i2s (&address->peer));
148 GNUNET_SCHEDULER_add_now (&end, NULL);
152 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n", stage);
156 GNUNET_SCHEDULER_add_now (&end, NULL);
160 if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_WAN])
162 level = GNUNET_ERROR_TYPE_DEBUG;
168 level = GNUNET_ERROR_TYPE_ERROR;
173 GNUNET_log (level, "Stage %u: WAN outbound quota out %s: Received %llu, configured %llu\n",
176 (unsigned long long int) ntohl(bandwidth_out.value__),
177 quota_out[GNUNET_ATS_NET_WAN]);
179 if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_WAN])
181 level = GNUNET_ERROR_TYPE_DEBUG;
187 level = GNUNET_ERROR_TYPE_ERROR;
192 GNUNET_log (level, "Stage %u: WAN inbound quota out %s: Received %llu, configured %llu\n",
195 (unsigned long long int) ntohl(bandwidth_out.value__),
196 quota_out[GNUNET_ATS_NET_WAN]);
198 if (GNUNET_ATS_NET_WAN != find_ats_value (atsi, ats_count, GNUNET_ATS_NETWORK_TYPE))
200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Incorrect network type, exptected %s, got %s \n",
202 GNUNET_ATS_print_network_type(GNUNET_ATS_NET_WAN),
203 GNUNET_ATS_print_network_type(find_ats_value (atsi, ats_count, GNUNET_ATS_NETWORK_TYPE)));
209 GNUNET_SCHEDULER_add_now (&end, NULL);
214 /* Prepare ATS Information: change network */
215 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
216 test_ats_info[0].value = htonl(GNUNET_ATS_NET_LAN);
217 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
218 test_ats_info[1].value = htonl(3);
219 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
220 test_ats_info[1].value = htonl(30);
223 GNUNET_ATS_address_update (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
225 /* Request address */
226 GNUNET_ATS_suggest_address (sched_ats, &p.id);
231 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
232 if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n", stage,
235 GNUNET_i2s (&address->peer));
240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n", stage,
241 GNUNET_i2s (&address->peer));
245 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
247 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n");
249 GNUNET_SCHEDULER_add_now (&end, NULL);
253 if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_LAN])
255 level = GNUNET_ERROR_TYPE_DEBUG;
261 level = GNUNET_ERROR_TYPE_ERROR;
266 GNUNET_log (level, "Stage %u: LAN outbound quota out %s: Received %llu, configured %llu\n",
269 (unsigned long long int) ntohl(bandwidth_out.value__),
270 quota_out[GNUNET_ATS_NET_LAN]);
272 if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_LAN])
274 level = GNUNET_ERROR_TYPE_DEBUG;
280 level = GNUNET_ERROR_TYPE_ERROR;
285 GNUNET_log (level, "Stage %u: LAN inbound quota out %s: Received %llu, configured %llu\n",
288 (unsigned long long int) ntohl(bandwidth_out.value__),
289 quota_out[GNUNET_ATS_NET_LAN]);
291 if (GNUNET_ATS_NET_LAN != find_ats_value (atsi, ats_count, GNUNET_ATS_NETWORK_TYPE))
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Incorrect network type, exptected %s, got %s \n",
295 GNUNET_ATS_print_network_type(GNUNET_ATS_NET_LAN),
296 GNUNET_ATS_print_network_type(find_ats_value (atsi, ats_count, GNUNET_ATS_NETWORK_TYPE)));
300 GNUNET_SCHEDULER_add_now (&end, NULL);
307 const struct GNUNET_CONFIGURATION_Handle *cfg,
308 struct GNUNET_TESTING_Peer *peer)
310 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
312 load_quotas (cfg, quota_out, quota_in, GNUNET_ATS_NetworkTypeCount);
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.id.hashPubKey))
327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
333 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
336 GNUNET_i2s_full(&p.id));
338 /* Prepare ATS Information */
339 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
340 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
341 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
342 test_ats_info[1].value = htonl(1);
343 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
344 test_ats_info[1].value = htonl(10);
347 /* Adding address without session */
348 test_session = &test_addr;
349 create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
350 test_hello_address.peer = p.id;
351 test_hello_address.transport_name = test_addr.plugin;
352 test_hello_address.address = test_addr.addr;
353 test_hello_address.address_length = test_addr.addr_len;
354 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
356 /* Request address */
357 GNUNET_ATS_suggest_address (sched_ats, &p.id);
362 main (int argc, char *argv[])
364 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_update_address",
371 /* end of file test_ats_api_scheduling_update_address.c */