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_performance.c
22 * @brief test adding addresses in automatic transport selection performance API
23 * @author Christian Grothoff
24 * @author Matthias Wachs
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
31 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
33 static GNUNET_SCHEDULER_TaskIdentifier die_task;
35 struct GNUNET_CONFIGURATION_Handle *cfg;
37 static struct GNUNET_ATS_SchedulingHandle *atsh;
38 static struct GNUNET_ATS_PerformanceHandle *ph;
39 struct GNUNET_ATS_AddressListHandle* phal;
51 struct GNUNET_ATS_Information *ats;
59 struct GNUNET_PeerIdentity id;
66 static struct PeerContext p[2];
68 static struct Address p0_addresses[2];
69 static struct Address p1_addresses[2];
71 struct GNUNET_HELLO_Address p0_ha[2];
72 struct GNUNET_HELLO_Address p1_ha[2];
73 struct GNUNET_HELLO_Address *s_ha[2];
75 static unsigned int stage = 0;
78 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
80 die_task = GNUNET_SCHEDULER_NO_TASK;
82 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Timeout in stage %u\n", stage);
85 GNUNET_ATS_scheduling_done (atsh);
87 GNUNET_ATS_performance_list_addresses_cancel (phal);
90 GNUNET_ATS_performance_done (ph);
93 GNUNET_free_non_null (p0_addresses[0].addr);
94 GNUNET_free_non_null (p0_addresses[1].addr);
95 GNUNET_free_non_null (p1_addresses[0].addr);
96 GNUNET_free_non_null (p1_addresses[1].addr);
105 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Shutting down\n");
106 if (die_task != GNUNET_SCHEDULER_NO_TASK)
108 GNUNET_SCHEDULER_cancel (die_task);
109 die_task = GNUNET_SCHEDULER_NO_TASK;
112 GNUNET_ATS_scheduling_done (atsh);
114 GNUNET_ATS_performance_list_addresses_cancel (phal);
117 GNUNET_ATS_performance_done (ph);
120 GNUNET_free_non_null (p0_addresses[0].addr);
121 GNUNET_free_non_null (p0_addresses[1].addr);
122 GNUNET_free_non_null (p1_addresses[0].addr);
123 GNUNET_free_non_null (p1_addresses[1].addr);
129 test_performance_api (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
131 void all_active_addresses_peer_cb (void *cls,
132 const struct GNUNET_HELLO_Address *address,
134 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
135 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
136 const struct GNUNET_ATS_Information *ats,
140 int fail = GNUNET_NO;
144 if (0 == memcmp (&address->peer, &p[0].id,
145 sizeof (struct GNUNET_PeerIdentity)))
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
148 "Did not expected callback for peer 0 address `%s', got address `%s'!\n",
149 s_ha[0]->address, address->address);
150 GNUNET_ATS_performance_list_addresses_cancel (phal);
154 if (0 == memcmp (&address->peer, &p[1].id,
155 sizeof (struct GNUNET_PeerIdentity)))
157 if (0 == strcmp(address->address, s_ha[1]->address))
159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
160 "Callback for peer 1 suggested address %s\n",
166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
167 "Expected callback for peer 1 address `%s', got address `%s'!\n",
168 s_ha[1]->address, address->address);
169 GNUNET_ATS_performance_list_addresses_cancel (phal);
174 if ((address == NULL) || (GNUNET_YES == fail))
177 if ((1 == cb) && (GNUNET_NO == fail))
179 /* Received all addresses + terminator cb, next stage */
180 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i: SUCCESS\n", stage);
181 GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i: FAIL\n", stage);
187 GNUNET_SCHEDULER_add_now (&end, NULL);
194 void all_active_addresses_cb (void *cls,
195 const struct GNUNET_HELLO_Address *address,
197 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
198 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
199 const struct GNUNET_ATS_Information *ats,
203 int fail = GNUNET_NO;
207 if (0 == memcmp (&address->peer, &p[0].id,
208 sizeof (struct GNUNET_PeerIdentity)))
210 if (0 == strcmp(address->address, s_ha[0]->address))
212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
213 "Callback for peer 0 suggested address %s\n",
219 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
220 "Expected callback for peer 0 address `%s', got address `%s'!\n",
221 s_ha[0]->address, address->address);
222 GNUNET_ATS_performance_list_addresses_cancel (phal);
227 if (0 == memcmp (&address->peer, &p[1].id,
228 sizeof (struct GNUNET_PeerIdentity)))
230 if (0 == strcmp(address->address, s_ha[1]->address))
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Callback for peer 1 suggested address %s\n",
239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
240 "Expected callback for peer 1 address `%s', got address `%s'!\n",
241 s_ha[1]->address, address->address);
242 GNUNET_ATS_performance_list_addresses_cancel (phal);
247 if ((address == NULL) || (GNUNET_YES == fail))
250 if ((2 == cb) && (GNUNET_NO == fail))
252 /* Received all addresses + terminator cb, next stage */
253 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i: SUCCESS\n", stage);
254 GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i: FAIL\n", stage);
260 GNUNET_SCHEDULER_add_now (&end, NULL);
269 void all_addresses_peer_cb (void *cls,
270 const struct GNUNET_HELLO_Address *address,
272 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
273 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
274 const struct GNUNET_ATS_Information *ats,
278 int fail = GNUNET_NO;
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
283 "Callback for peer `%s' address `%s'\n",
284 GNUNET_i2s (&address->peer), address->address);
286 if (0 != memcmp (&address->peer, &p[1].id,
287 sizeof (struct GNUNET_PeerIdentity)))
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290 "Stage %i: Received address for wrong peer\n", stage);
291 GNUNET_ATS_performance_list_addresses_cancel (phal);
298 if ((NULL == address) || (fail))
301 if ((2 == cb) && (GNUNET_NO == fail))
303 /* Received all addresses + terminator cb, next stage */
304 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i: SUCCESS\n", stage);
305 GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
310 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i: FAIL\n", stage);
311 GNUNET_SCHEDULER_add_now (&end, NULL);
318 void all_addresses_cb (void *cls,
319 const struct GNUNET_HELLO_Address *address,
321 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
322 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
323 const struct GNUNET_ATS_Information *ats,
330 if (0 == memcmp (&address->peer, &p[0].id,
331 sizeof (struct GNUNET_PeerIdentity)))
333 if (0 == strcmp(address->address, p0_addresses[0].addr))
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 0\n");
338 if (0 == strcmp(address->address, p0_addresses[1].addr))
340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 0 address 1\n");
344 if (0 == memcmp (&address->peer, &p[1].id,
345 sizeof (struct GNUNET_PeerIdentity)))
347 if (0 == strcmp(address->address, p1_addresses[0].addr))
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 0\n");
352 if (0 == strcmp(address->address, p1_addresses[1].addr))
354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Callback for peer 1 address 1\n");
362 if (((1 << 4) - 1) == cb)
364 /* Received all addresses + terminator cb, next stage */
365 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stage %i: SUCCESS\n", stage);
366 GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Stage %i: FAIL\n", stage);
372 GNUNET_SCHEDULER_add_now (&end, NULL);
380 test_performance_api (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
383 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL, NULL, NULL);
386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize performance handle\n");
391 case 1: /* Get all peers, all addresses */
392 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 1: \n");
393 phal = GNUNET_ATS_performance_list_addresses (ph,
396 &all_addresses_cb, NULL);
397 GNUNET_assert (NULL != phal);
399 case 2: /* Get specific peer, all addresses */
400 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 2: \n");
401 phal = GNUNET_ATS_performance_list_addresses (ph,
404 &all_addresses_peer_cb, NULL);
405 GNUNET_assert (NULL != phal);
407 case 3: /* Get all peers, active addresses */
408 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 3: \n");
409 phal = GNUNET_ATS_performance_list_addresses (ph,
412 &all_active_addresses_cb, NULL);
413 GNUNET_assert (NULL != phal);
415 case 4: /* Get specific peers, active addresses */
416 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Run stage 4: \n");
417 phal = GNUNET_ATS_performance_list_addresses (ph,
420 &all_active_addresses_peer_cb, NULL);
421 GNUNET_assert (NULL != phal);
425 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "All tests successful, shutdown... \n");
426 GNUNET_SCHEDULER_add_now (&end, NULL);
433 address_suggest_cb (void *cls, const struct GNUNET_HELLO_Address *address,
434 struct Session *session,
435 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
436 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
437 const struct GNUNET_ATS_Information *ats,
440 static int suggest_p0 = GNUNET_NO;
441 static int suggest_p1 = GNUNET_NO;
442 static int running = GNUNET_NO;
444 if ((GNUNET_NO == suggest_p0) && (0 == memcmp (&address->peer, &p[0].id,
445 sizeof (struct GNUNET_PeerIdentity))))
447 suggest_p0 = GNUNET_YES;;
450 GNUNET_free (s_ha[0]);
451 s_ha[0] = GNUNET_HELLO_address_copy (address);
452 GNUNET_ATS_suggest_address_cancel (atsh, &p[0].id);
454 if ((GNUNET_NO == suggest_p1) && (0 == memcmp (&address->peer, &p[1].id,
455 sizeof (struct GNUNET_PeerIdentity))))
457 suggest_p1 = GNUNET_YES;
460 GNUNET_free (s_ha[1]);
461 s_ha[1] = GNUNET_HELLO_address_copy (address);
462 GNUNET_ATS_suggest_address_cancel (atsh, &p[1].id);
466 if ((GNUNET_NO == running) && (GNUNET_YES == suggest_p0) && (GNUNET_YES == suggest_p1))
468 running = GNUNET_YES;
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Have address suggestion for both peers\n");
470 GNUNET_SCHEDULER_add_now (&test_performance_api, NULL);
478 const struct GNUNET_CONFIGURATION_Handle *mycfg,
479 struct GNUNET_TESTING_Peer *peer)
482 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
483 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
487 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
488 &p[0].id.hashPubKey);
490 p0_addresses[0].plugin = "test";
491 p0_addresses[0].session = NULL;
492 p0_addresses[0].addr = GNUNET_strdup ("test_p0_a0");
493 p0_addresses[0].addr_len = strlen (p0_addresses[0].addr) + 1;
495 p0_ha[0].address = p0_addresses[0].addr;
496 p0_ha[0].address_length = p0_addresses[0].addr_len;
497 p0_ha[0].peer = p[0].id;
498 p0_ha[0].transport_name = p0_addresses[0].plugin;
500 p0_addresses[1].plugin = "test";
501 p0_addresses[1].session = NULL;
502 p0_addresses[1].addr = GNUNET_strdup ("test_p0_a1");
503 p0_addresses[1].addr_len = strlen(p0_addresses[1].addr) + 1;
505 p0_ha[1].address = p0_addresses[1].addr;
506 p0_ha[1].address_length = p0_addresses[1].addr_len;
507 p0_ha[1].peer = p[0].id;
508 p0_ha[1].transport_name = p0_addresses[1].plugin;
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 0: `%s'\n",
511 GNUNET_i2s (&p[0].id));
513 GNUNET_CRYPTO_hash_create_random (GNUNET_CRYPTO_QUALITY_WEAK,
514 &p[1].id.hashPubKey);
516 p1_addresses[0].plugin = "test";
517 p1_addresses[0].session = NULL;
518 p1_addresses[0].addr = GNUNET_strdup ("test_p1_a0");
519 p1_addresses[0].addr_len = strlen(p1_addresses[0].addr) + 1;
521 p1_ha[0].address = p1_addresses[0].addr;
522 p1_ha[0].address_length = p1_addresses[0].addr_len;
523 p1_ha[0].peer = p[1].id;
524 p1_ha[0].transport_name = p1_addresses[0].plugin;
526 p1_addresses[1].plugin = "test";
527 p1_addresses[1].session = NULL;
528 p1_addresses[1].addr = GNUNET_strdup ("test_p1_a1");
529 p1_addresses[1].addr_len = strlen(p1_addresses[1].addr) + 1;
531 p1_ha[1].address = p1_addresses[1].addr;
532 p1_ha[1].address_length = p1_addresses[1].addr_len;
533 p1_ha[1].peer = p[1].id;
534 p1_ha[1].transport_name = p1_addresses[1].plugin;
537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer 1: `%s'\n",
538 GNUNET_i2s (&p[1].id));
542 atsh = GNUNET_ATS_scheduling_init (cfg, &address_suggest_cb, NULL);
550 GNUNET_ATS_address_add (atsh, &p0_ha[0], NULL, NULL, 0);
551 GNUNET_ATS_address_add (atsh, &p0_ha[1], NULL, NULL, 0);
553 GNUNET_ATS_address_add (atsh, &p1_ha[0], NULL, NULL, 0);
554 GNUNET_ATS_address_add (atsh, &p1_ha[1], NULL, NULL, 0);
557 GNUNET_ATS_suggest_address (atsh, &p[0].id);
558 GNUNET_ATS_suggest_address (atsh, &p[1].id);
563 main (int argc, char *argv[])
565 if (0 != GNUNET_TESTING_peer_run ("test_ats_api_performance",
572 /* end of file test_ats_api_performance.c */