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 an address: add address, get and compare it, update it
23 * get it again and compre
24 * @author Christian Grothoff
25 * @author Matthias Wachs
28 #include "gnunet_ats_service.h"
29 #include "gnunet_testing_lib-new.h"
31 #include "test_ats_api_common.h"
33 #define BIG_M_STRING "unlimited"
36 static GNUNET_SCHEDULER_TaskIdentifier die_task;
41 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
51 static struct Test_Address test_addr;
56 static struct PeerContext p;
62 struct GNUNET_HELLO_Address test_hello_address;
67 static void *test_session;
72 struct GNUNET_ATS_Information test_ats_info[3];
77 uint32_t test_ats_count;
79 unsigned long long int quota_out[GNUNET_ATS_NetworkTypeCount];
80 unsigned long long int quota_in[GNUNET_ATS_NetworkTypeCount];
83 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
85 die_task = GNUNET_SCHEDULER_NO_TASK;
87 if (sched_ats != NULL)
88 GNUNET_ATS_scheduling_done (sched_ats);
89 free_test_address (&test_addr);
97 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
98 if (die_task != GNUNET_SCHEDULER_NO_TASK)
100 GNUNET_SCHEDULER_cancel (die_task);
101 die_task = GNUNET_SCHEDULER_NO_TASK;
103 GNUNET_ATS_scheduling_done (sched_ats);
105 free_test_address (&test_addr);
110 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
111 struct Session *session,
112 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
113 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
114 const struct GNUNET_ATS_Information *atsi,
117 static int stage = 0;
120 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
121 if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback for correct address `%s'\n",
124 stage, GNUNET_i2s (&address->peer));
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n",
130 stage, GNUNET_i2s (&address->peer));
132 GNUNET_SCHEDULER_add_now (&end, NULL);
136 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n", stage);
140 GNUNET_SCHEDULER_add_now (&end, NULL);
144 if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_WAN])
146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: WAN quota out correct \n", stage);
150 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: WAN quota out wrong \n", stage);
152 if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_WAN])
154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: WAN quota in correct \n", stage);
158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: WAN quota in wrong \n", stage);
162 /* Prepare ATS Information: change network */
163 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
164 test_ats_info[0].value = htonl(GNUNET_ATS_NET_LAN);
165 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
166 test_ats_info[1].value = htonl(3);
167 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
168 test_ats_info[1].value = htonl(30);
171 GNUNET_ATS_address_update (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
173 /* Request address */
174 GNUNET_ATS_suggest_address (sched_ats, &p.id);
179 GNUNET_ATS_suggest_address_cancel (sched_ats, &p.id);
180 if (GNUNET_OK == compare_addresses(address, session, &test_hello_address, test_session))
182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stage %u: Callback with correct address `%s'\n", stage,
183 GNUNET_i2s (&address->peer));
188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect address `%s'\n", stage,
189 GNUNET_i2s (&address->peer));
193 if (GNUNET_OK != compare_ats(atsi, ats_count, test_ats_info, test_ats_count))
195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: Callback with incorrect ats info \n");
197 GNUNET_SCHEDULER_add_now (&end, NULL);
201 if (ntohl(bandwidth_out.value__) == quota_out[GNUNET_ATS_NET_LAN])
203 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: LAN quota out correct \n", stage);
208 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: LAN quota out wrong \n", stage);
211 if (ntohl(bandwidth_in.value__) == quota_in[GNUNET_ATS_NET_LAN])
213 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: LAN quota in correct \n", stage);
217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %u: LAN quota in wrong \n", stage);
221 GNUNET_SCHEDULER_add_now (&end, NULL);
226 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
228 int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
229 char * entry_in = NULL;
230 char * entry_out = NULL;
231 char * quota_out_str;
235 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
240 case GNUNET_ATS_NET_UNSPECIFIED:
241 entry_out = "UNSPECIFIED_QUOTA_OUT";
242 entry_in = "UNSPECIFIED_QUOTA_IN";
244 case GNUNET_ATS_NET_LOOPBACK:
245 entry_out = "LOOPBACK_QUOTA_OUT";
246 entry_in = "LOOPBACK_QUOTA_IN";
248 case GNUNET_ATS_NET_LAN:
249 entry_out = "LAN_QUOTA_OUT";
250 entry_in = "LAN_QUOTA_IN";
252 case GNUNET_ATS_NET_WAN:
253 entry_out = "WAN_QUOTA_OUT";
254 entry_in = "WAN_QUOTA_IN";
256 case GNUNET_ATS_NET_WLAN:
257 entry_out = "WLAN_QUOTA_OUT";
258 entry_in = "WLAN_QUOTA_IN";
264 if ((entry_in == NULL) || (entry_out == NULL))
268 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
270 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
271 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
272 out_dest[c] = UINT32_MAX;
274 GNUNET_free (quota_out_str);
275 quota_out_str = NULL;
277 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
278 out_dest[c] = UINT32_MAX;
280 out_dest[c] = UINT32_MAX;
283 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
285 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
286 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
287 in_dest[c] = UINT32_MAX;
289 GNUNET_free (quota_in_str);
292 else if (GNUNET_ATS_NET_UNSPECIFIED == quotas[c])
294 in_dest[c] = UINT32_MAX;
298 in_dest[c] = UINT32_MAX;
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota: %s %u, %s %u\n", entry_in, in_dest[c], entry_out, out_dest[c]);
303 return GNUNET_ATS_NetworkTypeCount;
308 const struct GNUNET_CONFIGURATION_Handle *cfg,
309 struct GNUNET_TESTING_Peer *peer)
311 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
313 load_quotas (cfg, quota_out, quota_in, GNUNET_ATS_NetworkTypeCount);
315 /* Connect to ATS scheduling */
316 sched_ats = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
317 if (sched_ats == NULL)
319 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not connect to ATS scheduling!\n");
326 if (GNUNET_SYSERR == GNUNET_CRYPTO_hash_from_string(PEERID0, &p.id.hashPubKey))
328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Could not setup peer!\n");
334 GNUNET_assert (0 == strcmp (PEERID0, GNUNET_i2s_full (&p.id)));
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
337 GNUNET_i2s_full(&p.id));
339 /* Prepare ATS Information */
340 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
341 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
342 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
343 test_ats_info[1].value = htonl(1);
344 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
345 test_ats_info[1].value = htonl(10);
348 /* Adding address without session */
349 test_session = &test_addr;
350 create_test_address (&test_addr, "test", &test_addr, "test", strlen ("test") + 1);
351 test_hello_address.peer = p.id;
352 test_hello_address.transport_name = test_addr.plugin;
353 test_hello_address.address = test_addr.addr;
354 test_hello_address.address_length = test_addr.addr_len;
355 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session, test_ats_info, test_ats_count);
357 /* Request address */
358 GNUNET_ATS_suggest_address (sched_ats, &p.id);
363 main (int argc, char *argv[])
365 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_scheduling_update_address",
372 /* end of file test_ats_api_scheduling_update_address.c */