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-new.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);
109 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
110 struct Session *session,
111 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
112 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
113 const struct GNUNET_ATS_Information *atsi,
116 static int stage = 0;
121 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
122 if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback for correct address `%s'\n",
125 stage, GNUNET_i2s (&address->peer));
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n",
131 stage, GNUNET_i2s (&address->peer));
133 GNUNET_SCHEDULER_add_now (&end, NULL);
137 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n", stage);
141 GNUNET_SCHEDULER_add_now (&end, NULL);
145 if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_WAN])
147 level = GNUNET_ERROR_TYPE_DEBUG;
153 level = GNUNET_ERROR_TYPE_ERROR;
158 GNUNET_log (level, "Stage %u: WAN outbound quota out %s: Received %llu, configured %llu\n",
161 (unsigned long long int) ntohl(bandwidth_out.value__),
162 quota_out[GNUNET_ATS_NET_WAN]);
164 if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_WAN])
166 level = GNUNET_ERROR_TYPE_DEBUG;
172 level = GNUNET_ERROR_TYPE_ERROR;
178 GNUNET_log (level, "Stage %u: WAN inbound quota out %s: Received %llu, configured %llu\n",
181 (unsigned long long int) ntohl(bandwidth_out.value__),
182 quota_out[GNUNET_ATS_NET_WAN]);
186 GNUNET_SCHEDULER_add_now (&end, NULL);
191 /* Prepare ATS Information: change network */
192 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
193 test_ats_info[0].value = htonl(GNUNET_ATS_NET_LAN);
194 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
195 test_ats_info[1].value = htonl(3);
196 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
197 test_ats_info[1].value = htonl(30);
200 GNUNET_ATS_address_update (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
202 /* Request address */
203 GNUNET_ATS_suggest_address (sched_ats, &p.id);
208 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
209 if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n", stage,
212 GNUNET_i2s (&address->peer));
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n", stage,
218 GNUNET_i2s (&address->peer));
222 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n");
226 GNUNET_SCHEDULER_add_now (&end, NULL);
230 if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_LAN])
232 level = GNUNET_ERROR_TYPE_DEBUG;
238 level = GNUNET_ERROR_TYPE_ERROR;
243 GNUNET_log (level, "Stage %u: LAN outbound quota out %s: Received %llu, configured %llu\n",
246 (unsigned long long int) ntohl(bandwidth_out.value__),
247 quota_out[GNUNET_ATS_NET_LAN]);
249 if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_LAN])
251 level = GNUNET_ERROR_TYPE_DEBUG;
257 level = GNUNET_ERROR_TYPE_ERROR;
262 GNUNET_log (level, "Stage %u: LAN inbound quota out %s: Received %llu, configured %llu\n",
265 (unsigned long long int) ntohl(bandwidth_out.value__),
266 quota_out[GNUNET_ATS_NET_LAN]);
268 GNUNET_SCHEDULER_add_now (&end, NULL);
273 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
275 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
276 char * entry_in = NULL;
277 char * entry_out = NULL;
278 char * quota_out_str;
282 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
287 case GNUNET_ATS_NET_UNSPECIFIED:
288 entry_out = "UNSPECIFIED_QUOTA_OUT";
289 entry_in = "UNSPECIFIED_QUOTA_IN";
291 case GNUNET_ATS_NET_LOOPBACK:
292 entry_out = "LOOPBACK_QUOTA_OUT";
293 entry_in = "LOOPBACK_QUOTA_IN";
295 case GNUNET_ATS_NET_LAN:
296 entry_out = "LAN_QUOTA_OUT";
297 entry_in = "LAN_QUOTA_IN";
299 case GNUNET_ATS_NET_WAN:
300 entry_out = "WAN_QUOTA_OUT";
301 entry_in = "WAN_QUOTA_IN";
303 case GNUNET_ATS_NET_WLAN:
304 entry_out = "WLAN_QUOTA_OUT";
305 entry_in = "WLAN_QUOTA_IN";
311 if ((entry_in == NULL) || (entry_out == NULL))
315 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
317 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
318 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
319 out_dest[c] = UINT32_MAX;
321 GNUNET_free (quota_out_str);
322 quota_out_str = NULL;
324 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
325 out_dest[c] = UINT32_MAX;
327 out_dest[c] = UINT32_MAX;
330 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
332 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
333 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
334 in_dest[c] = UINT32_MAX;
336 GNUNET_free (quota_in_str);
339 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
341 in_dest[c] = UINT32_MAX;
345 in_dest[c] = UINT32_MAX;
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
350 return GNUNET_ATS_NetworkTypeCount;
355 const struct GNUNET_CONFIGURATION_Handle *cfg,
356 struct GNUNET_TESTING_Peer *peer)
358 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
360 load_quotas (cfg, quota_out, quota_in, GNUNET_ATS_NetworkTypeCount);
362 /* Connect to ATS scheduling */
363 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
364 if (sched_ats == NULL)
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
373 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
381 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
384 GNUNET_i2s_full(&p.id));
386 /* Prepare ATS Information */
387 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
388 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
389 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
390 test_ats_info[1].value = htonl(1);
391 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
392 test_ats_info[1].value = htonl(10);
395 /* Adding address without session */
396 test_session = &test_addr;
397 create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
398 test_hello_address.peer = p.id;
399 test_hello_address.transport_name = test_addr.plugin;
400 test_hello_address.address = test_addr.addr;
401 test_hello_address.address_length = test_addr.addr_len;
402 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
404 /* Request address */
405 GNUNET_ATS_suggest_address (sched_ats, &p.id);
410 main (int argc, char *argv[])
412 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_update_address",
419 /* end of file test_ats_api_scheduling_update_address.c */