2 This file is part of GNUnet.
3 (C) 2010-2013 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_solver_alternative_after_delete_address.c
22 * @brief solver test: add 2 addresses, request address, delete, expect alternative
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_testbed_service.h"
29 #include "gnunet_ats_service.h"
30 #include "test_ats_api_common.h"
35 static struct GNUNET_SCHEDULER_Task * die_task;
40 static struct GNUNET_STATISTICS_Handle *stats;
45 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
50 static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
60 static struct Test_Address test_addr;
63 * Alternative test address
65 static struct Test_Address alt_test_addr;
70 static struct PeerContext p;
75 static struct GNUNET_HELLO_Address test_hello_address;
80 static struct GNUNET_HELLO_Address alt_test_hello_address;
85 static void *test_session;
90 static struct GNUNET_ATS_Information test_ats_info[2];
95 static uint32_t test_ats_count;
100 static int addresses_added;
102 static int first_address_suggested;
104 static int first_address_deleted;
106 static int second_address_deleted;
108 static int second_address_suggested = GNUNET_YES;
110 static struct GNUNET_HELLO_Address *first_suggestion;
112 static struct GNUNET_HELLO_Address *second_suggestion;
115 * 1st Address we will destroy.
117 static struct GNUNET_ATS_AddressRecord *ar;
120 * 2nd Address we will destroy.
122 static struct GNUNET_ATS_AddressRecord *ar2;
126 stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value,
131 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
133 if (NULL != die_task)
135 GNUNET_SCHEDULER_cancel (die_task);
138 if (NULL != connect_ats)
140 GNUNET_ATS_connectivity_done (connect_ats);
143 if (NULL != sched_ats)
145 GNUNET_ATS_scheduling_done (sched_ats);
149 GNUNET_STATISTICS_watch_cancel (stats, "ats", "# addresses", &stat_cb, NULL);
152 GNUNET_STATISTICS_destroy (stats, GNUNET_NO);
156 free_test_address (&test_addr);
157 GNUNET_free_non_null (first_suggestion);
158 GNUNET_free_non_null (second_suggestion);
164 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
176 if (NULL != die_task)
178 GNUNET_SCHEDULER_cancel (die_task);
181 GNUNET_SCHEDULER_add_now (&end_badly, NULL);
186 address_suggest_cb (void *cls,
187 const struct GNUNET_PeerIdentity *peer,
188 const struct GNUNET_HELLO_Address *address,
189 struct Session *session,
190 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
191 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
193 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
194 "Received a sugggestion for peer `%s'\n",
196 if (GNUNET_NO == first_address_suggested)
198 if (NULL == first_suggestion)
200 if ((NULL == address) || (NULL != session))
206 if ((ntohl(bandwidth_in.value__) == 0) ||
207 (ntohl(bandwidth_out.value__) == 0))
214 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
215 "Received 1st sugggestion for peer `%s' : `%s'\n",
216 GNUNET_i2s (&address->peer), (char *) address->address);
218 first_suggestion = GNUNET_HELLO_address_copy (address);
219 first_address_suggested = GNUNET_YES;
222 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
223 "Deleting 1st address for peer `%s' : `%s'\n",
224 GNUNET_i2s (&address->peer),
225 (char *) address->address);
226 if (0 == (strcmp ((char *) address->address,
229 GNUNET_assert (NULL != ar);
230 GNUNET_ATS_address_destroy (ar);
235 GNUNET_assert (NULL != ar2);
236 GNUNET_ATS_address_destroy (ar2);
240 first_address_deleted = GNUNET_YES;
245 if (GNUNET_YES == first_address_deleted)
247 if (NULL == second_suggestion)
249 if ((NULL == address) || (NULL != session))
256 if (0 != memcmp (address->address, first_suggestion->address,
257 (first_suggestion->address_length < address->address_length) ? first_suggestion->address_length : address->address_length))
259 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
260 "Received 2nd sugggestion for peer `%s' : `%s'\n",
261 GNUNET_i2s (&address->peer),
262 (char *) address->address);
263 second_suggestion = GNUNET_HELLO_address_copy (address);
264 second_address_suggested = GNUNET_YES;
266 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
267 "Deleting 2nd address for peer `%s' : `%s'\n",
268 GNUNET_i2s (&address->peer),
269 (char *) address->address);
272 GNUNET_assert (NULL == ar2);
273 GNUNET_ATS_address_destroy (ar);
278 GNUNET_assert (NULL != ar2);
279 GNUNET_ATS_address_destroy (ar2);
282 second_address_deleted = GNUNET_YES;
287 if (GNUNET_YES == second_address_deleted)
289 /* Expecting disconnect */
290 if ((ntohl(bandwidth_in.value__) == 0) &&
291 (ntohl(bandwidth_out.value__) == 0))
293 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
294 "ATS tells me to disconnect\n");
295 GNUNET_SCHEDULER_add_now (&end, NULL);
300 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
301 "Expected disconnect but received address `%s' with bandwidth \n",
302 (char *) address->address);
309 stat_cb(void *cls, const char *subsystem,
310 const char *name, uint64_t value,
313 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
314 "ATS statistics: `%s' `%s' %llu\n",
318 if ((GNUNET_NO == addresses_added) && (value == 2))
320 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
321 "All addresses added, requesting....\n");
322 /* We have 2 addresses, so we can request */
323 addresses_added = GNUNET_YES;
324 GNUNET_ATS_connectivity_suggest (connect_ats, &p.id);
332 const struct GNUNET_CONFIGURATION_Handle *mycfg,
333 struct GNUNET_TESTING_Peer *peer)
335 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
336 stats = GNUNET_STATISTICS_create ("ats", mycfg);
337 GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL);
339 connect_ats = GNUNET_ATS_connectivity_init (mycfg);
341 /* Connect to ATS scheduling */
342 sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL);
343 if (sched_ats == NULL)
345 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
346 "Could not connect to ATS scheduling!\n");
352 memset (&p.id, '1', sizeof (p.id));
353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n",
354 GNUNET_i2s_full(&p.id));
356 /* Prepare ATS Information */
357 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
358 test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN);
359 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
360 test_ats_info[1].value = htonl(1);
363 /* Adding address without session */
365 create_test_address (&test_addr, "test", test_session,
366 "test", strlen ("test") + 1);
367 test_hello_address.peer = p.id;
368 test_hello_address.transport_name = test_addr.plugin;
369 test_hello_address.address = test_addr.addr;
370 test_hello_address.address_length = test_addr.addr_len;
372 /* Adding alternative address without session */
374 create_test_address (&alt_test_addr, "test", test_session,
375 "alt_test", strlen ("alt_test") + 1);
376 alt_test_hello_address.peer = p.id;
377 alt_test_hello_address.transport_name = alt_test_addr.plugin;
378 alt_test_hello_address.address = alt_test_addr.addr;
379 alt_test_hello_address.address_length = alt_test_addr.addr_len;
383 ar = GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL,
384 test_ats_info, test_ats_count);
385 /* Adding alternative address */
386 ar2 = GNUNET_ATS_address_add (sched_ats, &alt_test_hello_address, NULL,
387 test_ats_info, test_ats_count);
392 main (int argc, char *argv[])
395 char *src_filename = GNUNET_strdup (__FILE__);
396 char *test_filename = GNUNET_strdup (argv[0]);
397 const char *config_file;
402 if (NULL == (sep = (strstr (src_filename, ".c"))))
409 if (NULL != (sep = strstr (test_filename, ".exe")))
412 if (NULL == (solver = strstr (test_filename, src_filename)))
417 solver += strlen (src_filename) +1;
419 if (0 == strcmp(solver, "proportional"))
421 config_file = "test_ats_solver_proportional.conf";
423 else if (0 == strcmp(solver, "mlp"))
425 config_file = "test_ats_solver_mlp.conf";
427 else if ((0 == strcmp(solver, "ril")))
429 config_file = "test_ats_solver_ril.conf";
434 GNUNET_free (src_filename);
435 GNUNET_free (test_filename);
439 GNUNET_free (src_filename);
440 GNUNET_free (test_filename);
442 if (0 != GNUNET_TESTING_peer_run ("test-ats-solver",
443 config_file, &run, NULL ))
444 return GNUNET_SYSERR;
449 /* end of file test_ats_solver_alternative_after_delete_address.c */