2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 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_lib.c
22 * @brief test ATS library with a generic interpreter for running ATS tests
23 * @author Christian Grothoff
26 #include "gnunet_util_lib.h"
27 #include "gnunet_ats_service.h"
28 #include "gnunet_testing_lib.h"
29 #include "test_ats_lib.h"
32 * Information about the last address suggestion we got for a peer.
34 struct AddressSuggestData
37 * Which session were we given?
39 struct Session *session;
42 * What address was assigned?
44 struct GNUNET_HELLO_Address *address;
47 * Outbound bandwidth assigned.
49 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
52 * Inbound bandwidth assigned.
54 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
57 * Was the bandwidth assigned non-zero?
64 * Information about the last address information we got for an address.
66 struct AddressInformationData
69 * What address is this data about?
71 struct GNUNET_HELLO_Address *address;
74 * Which properties were given?
76 struct GNUNET_ATS_Properties properties;
79 * Outbound bandwidth reported.
81 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
84 * Inbound bandwidth reported.
86 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
89 * Was the address said to be 'active'?
98 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
101 * Connectivity handle
103 static struct GNUNET_ATS_ConnectivityHandle *con_ats;
108 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
111 * Handle for the interpreter task.
113 static struct GNUNET_SCHEDULER_Task *interpreter_task;
116 * Map from peer identities to the last address suggestion
117 * `struct AddressSuggestData` we got for the respective peer.
119 static struct GNUNET_CONTAINER_MultiPeerMap *p2asd;
122 * Map from peer identities to the last address information
123 * sets for all addresses of this peer. Each peer is mapped
124 * to one or more `struct AddressInformationData` entries.
126 static struct GNUNET_CONTAINER_MultiPeerMap *p2aid;
129 * Global timeout for the test.
131 static struct GNUNET_TIME_Relative TIMEOUT;
134 * Return value from #main().
139 * Current global command offset into the #commands array.
141 static unsigned int off;
144 * Commands for the current test.
146 static struct Command *test_commands;
151 * Free `struct AddressSuggestData` entry.
155 * @param value the `struct AddressSuggestData` to release
156 * @return #GNUNET_OK (continue to iterate)
160 const struct GNUNET_PeerIdentity *key,
163 struct AddressSuggestData *asd = value;
165 GNUNET_assert (GNUNET_YES ==
166 GNUNET_CONTAINER_multipeermap_remove (p2asd,
169 GNUNET_free_non_null (asd->address);
176 * Free `struct AddressInformationData` entry.
180 * @param value the `struct AddressSuggestData` to release
181 * @return #GNUNET_OK (continue to iterate)
185 const struct GNUNET_PeerIdentity *key,
188 struct AddressInformationData *aid = value;
190 GNUNET_assert (GNUNET_YES ==
191 GNUNET_CONTAINER_multipeermap_remove (p2aid,
194 GNUNET_free (aid->address);
201 * Find latest address suggestion made for the given peer.
203 * @param pid peer to look up
204 * @return NULL if peer was never involved
206 static struct AddressSuggestData *
207 find_address_suggestion (const struct GNUNET_PeerIdentity *pid)
209 return GNUNET_CONTAINER_multipeermap_get (p2asd,
215 * Closure for #match_address()
217 struct MatchAddressContext
222 const struct GNUNET_HELLO_Address *addr;
225 * Where to return address information if found.
227 struct AddressInformationData *ret;
232 * Find matching address information.
234 * @param cls a `struct MatchAddressContext`
236 * @param value a `struct AddressInformationData`
237 * @return #GNUNET_OK if not found
240 match_address (void *cls,
241 const struct GNUNET_PeerIdentity *key,
244 struct MatchAddressContext *mac = cls;
245 struct AddressInformationData *aid = value;
247 if (0 == GNUNET_HELLO_address_cmp (mac->addr,
258 * Find latest address information made for the given address.
260 * @param addr address to look up
261 * @return NULL if peer was never involved
263 static struct AddressInformationData *
264 find_address_information (const struct GNUNET_HELLO_Address *addr)
266 struct MatchAddressContext mac;
270 GNUNET_CONTAINER_multipeermap_get_multiple (p2aid,
279 * Task run to terminate the testcase.
286 const struct GNUNET_SCHEDULER_TaskContext *tc)
289 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
290 "Test failed at stage %u %s\n",
292 (NULL != test_commands[off].label)
293 ? test_commands[off].label
295 if (NULL != interpreter_task)
297 GNUNET_SCHEDULER_cancel (interpreter_task);
298 interpreter_task = NULL;
300 if (NULL != sched_ats)
302 GNUNET_ATS_scheduling_done (sched_ats);
307 GNUNET_ATS_connectivity_done (con_ats);
310 if (NULL != perf_ats)
312 GNUNET_ATS_performance_done (perf_ats);
317 GNUNET_CONTAINER_multipeermap_iterate (p2asd,
320 GNUNET_CONTAINER_multipeermap_destroy (p2asd);
325 GNUNET_CONTAINER_multipeermap_iterate (p2aid,
328 GNUNET_CONTAINER_multipeermap_destroy (p2aid);
335 * Main interpreter loop. Runs the steps of the test.
341 interpreter (void *cls,
342 const struct GNUNET_SCHEDULER_TaskContext *tc);
346 * Run the interpreter next.
351 if (NULL != interpreter_task)
352 GNUNET_SCHEDULER_cancel (interpreter_task);
353 interpreter_task = GNUNET_SCHEDULER_add_now (&interpreter,
359 * Initialize public key of a peer based on a single number.
361 * @param pid number to use as the basis
362 * @param pk resulting fake public key
365 make_peer (uint32_t pid,
366 struct GNUNET_PeerIdentity *pk)
370 sizeof (struct GNUNET_PeerIdentity));
378 * Generate a fake address based on the given parameters.
380 * @param pid number of the peer
381 * @param num number of the address at peer @a pid
382 * @param addr_flags flags to use for the address
383 * @return the address
385 static struct GNUNET_HELLO_Address *
386 make_address (uint32_t pid,
388 enum GNUNET_HELLO_AddressInfo addr_flags)
390 struct GNUNET_PeerIdentity pk;
396 return GNUNET_HELLO_address_allocate (&pk,
405 * Our dummy sessions.
410 * Field to avoid `0 == sizeof(struct Session)`.
412 unsigned int non_empty;
417 * Create a session instance for ATS.
419 * @param i which session number to return
420 * @return NULL if @a i is 0, otherwise a pointer unique to @a i
422 static struct Session *
423 make_session (unsigned int i)
425 struct Session *baseptr = NULL;
429 /* Yes, these are *intentionally* out-of-bounds,
430 and offset from NULL, as nobody should ever
431 use those other than to compare pointers! */
437 * Find a @a code command before the global #off with the
438 * specified @a label.
440 * @param code opcode to look for
441 * @param label label to look for, NULL for none
442 * @return previous command with the matching label
444 static struct Command *
445 find_command (enum CommandCode code,
452 for (i=off-1;i>=0;i--)
453 if ( (code == test_commands[i].code) &&
454 (0 == strcmp (test_commands[i].label,
456 return &test_commands[i];
463 * Function called from #GNUNET_ATS_performance_list_addresses when
464 * we process a #CMD_LIST_ADDRESSES command.
466 * @param cls the `struct Command` that caused the call
467 * @param address the address, NULL if ATS service was disconnected
468 * @param address_active #GNUNET_YES if this address is actively used
469 * to maintain a connection to a peer;
470 * #GNUNET_NO if the address is not actively used;
471 * #GNUNET_SYSERR if this address is no longer available for ATS
472 * @param bandwidth_out assigned outbound bandwidth for the connection
473 * @param bandwidth_in assigned inbound bandwidth for the connection
474 * @param prop performance data for the address
478 const struct GNUNET_HELLO_Address *address,
480 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
481 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
482 const struct GNUNET_ATS_Properties *prop)
484 struct Command *c = cls;
485 struct CommandListAddresses *cmd = &c->details.list_addresses;
490 /* we are done with the iteration, continue to execute */
491 if ( (cmd->calls < cmd->min_calls) &&
492 (cmd->active_calls < cmd->min_active_calls) )
494 GNUNET_SCHEDULER_shutdown ();
501 switch (address_active)
513 if ( (cmd->calls > cmd->max_calls) &&
514 (cmd->active_calls < cmd->max_active_calls) )
517 GNUNET_ATS_performance_list_addresses_cancel (cmd->alh);
519 GNUNET_SCHEDULER_shutdown ();
526 * Function called with reservation result.
528 * @param cls closure with the reservation command (`struct Command`)
529 * @param peer identifies the peer
530 * @param amount set to the amount that was actually reserved or unreserved;
531 * either the full requested amount or zero (no partial reservations)
532 * @param res_delay if the reservation could not be satisfied (amount was 0), how
533 * long should the client wait until re-trying?
536 reservation_cb (void *cls,
537 const struct GNUNET_PeerIdentity *peer,
539 struct GNUNET_TIME_Relative res_delay)
541 struct Command *cmd = cls;
542 struct GNUNET_PeerIdentity pid;
544 cmd->details.reserve_bandwidth.rc = NULL;
545 make_peer (cmd->details.reserve_bandwidth.pid,
547 GNUNET_assert (0 == memcmp (peer,
549 sizeof (struct GNUNET_PeerIdentity)));
550 switch (cmd->details.reserve_bandwidth.expected_result)
553 if (amount != cmd->details.reserve_bandwidth.amount)
555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
556 "Unexpectedly failed to reserve %d/%d bytes with delay %s!\n",
558 (int) cmd->details.reserve_bandwidth.amount,
559 GNUNET_STRINGS_relative_time_to_string (res_delay,
562 GNUNET_SCHEDULER_shutdown ();
567 GNUNET_break ( (0 != amount) ||
568 (0 != res_delay.rel_value_us) );
571 if ( (amount != 0) ||
572 (0 == res_delay.rel_value_us) )
574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
575 "Unexpectedly reserved %d bytes with delay %s!\n",
577 GNUNET_STRINGS_relative_time_to_string (res_delay,
580 GNUNET_SCHEDULER_shutdown ();
591 * Main interpreter loop. Runs the steps of the test.
597 interpreter (void *cls,
598 const struct GNUNET_SCHEDULER_TaskContext *tc)
603 interpreter_task = NULL;
606 cmd = &test_commands[off];
607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
611 (NULL != cmd->label) ? cmd->label : "");
616 GNUNET_SCHEDULER_shutdown ();
618 case CMD_ADD_ADDRESS:
620 struct GNUNET_HELLO_Address *addr;
621 struct Session *session;
623 addr = make_address (cmd->details.add_address.pid,
624 cmd->details.add_address.addr_num,
625 cmd->details.add_address.addr_flags);
626 session = make_session (cmd->details.add_address.session);
627 if (cmd->details.add_address.expect_fail)
628 GNUNET_log_skip (1, GNUNET_NO);
629 cmd->details.add_address.ar
630 = GNUNET_ATS_address_add (sched_ats,
633 &cmd->details.add_address.properties);
635 if (cmd->details.add_address.expect_fail)
637 GNUNET_log_skip (0, GNUNET_YES);
639 else if (NULL == cmd->details.add_address.ar)
642 GNUNET_SCHEDULER_shutdown ();
648 case CMD_DEL_ADDRESS:
652 add = find_command (CMD_ADD_ADDRESS,
653 cmd->details.del_address.add_label);
654 GNUNET_assert (NULL != add->details.add_address.ar);
655 GNUNET_ATS_address_destroy (add->details.add_address.ar);
656 add->details.add_address.ar = NULL;
660 case CMD_AWAIT_ADDRESS_SUGGESTION:
662 struct GNUNET_PeerIdentity pid;
663 struct GNUNET_HELLO_Address *addr;
665 struct AddressSuggestData *asd;
668 make_peer (cmd->details.await_address_suggestion.pid,
670 asd = find_address_suggestion (&pid);
673 if (GNUNET_NO == asd->active)
674 return; /* last suggestion was to disconnect, wait longer */
676 if (NULL != cmd->details.await_address_suggestion.add_label)
679 add = find_command (CMD_ADD_ADDRESS,
680 cmd->details.await_address_suggestion.add_label);
681 addr = make_address (add->details.add_address.pid,
682 add->details.add_address.addr_num,
683 add->details.add_address.addr_flags);
684 if ( (asd->session ==
685 make_session (add->details.add_address.session)) &&
687 GNUNET_HELLO_address_cmp (addr,
692 if (GNUNET_NO == done)
697 case CMD_AWAIT_DISCONNECT_SUGGESTION:
699 struct GNUNET_PeerIdentity pid;
700 struct AddressSuggestData *asd;
702 make_peer (cmd->details.await_disconnect_suggestion.pid,
704 asd = find_address_suggestion (&pid);
706 return; /* odd, no suggestion at all yet!? */
707 if (GNUNET_YES == asd->active)
708 return; /* last suggestion was to activate, wait longer */
709 /* last suggestion was to deactivate, condition satisfied! */
713 case CMD_REQUEST_CONNECTION_START:
715 struct GNUNET_PeerIdentity pid;
717 make_peer (cmd->details.request_connection_start.pid,
719 cmd->details.request_connection_start.csh
720 = GNUNET_ATS_connectivity_suggest (con_ats,
725 case CMD_REQUEST_CONNECTION_STOP:
727 struct Command *start;
729 start = find_command (CMD_REQUEST_CONNECTION_START,
730 cmd->details.request_connection_stop.connect_label);
731 GNUNET_ATS_connectivity_suggest_cancel (start->details.request_connection_start.csh);
732 start->details.request_connection_start.csh = NULL;
736 case CMD_AWAIT_ADDRESS_INFORMATION:
738 struct AddressInformationData *aid;
740 struct Command *update;
741 struct GNUNET_HELLO_Address *addr;
742 const struct GNUNET_ATS_Properties *cmp;
744 add = find_command (CMD_ADD_ADDRESS,
745 cmd->details.await_address_information.add_label);
746 update = find_command (CMD_UPDATE_ADDRESS,
747 cmd->details.await_address_information.update_label);
748 addr = make_address (add->details.add_address.pid,
749 add->details.add_address.addr_num,
750 add->details.add_address.addr_flags);
751 aid = find_address_information (addr);
754 cmp = &add->details.add_address.properties;
756 cmp = &update->details.update_address.properties;
757 if ( (NULL != aid) &&
760 sizeof (struct GNUNET_ATS_Properties))) )
767 case CMD_UPDATE_ADDRESS:
771 add = find_command (CMD_ADD_ADDRESS,
772 cmd->details.update_address.add_label);
773 GNUNET_assert (NULL != add->details.add_address.ar);
774 GNUNET_ATS_address_update (add->details.add_address.ar,
775 &cmd->details.update_address.properties);
779 case CMD_ADD_SESSION:
782 struct Session *session;
784 add = find_command (CMD_ADD_ADDRESS,
785 cmd->details.add_session.add_label);
786 session = make_session (cmd->details.add_session.session);
787 GNUNET_assert (NULL != add->details.add_address.ar);
788 GNUNET_ATS_address_add_session (add->details.add_address.ar,
793 case CMD_DEL_SESSION:
795 struct Command *add_address;
796 struct Command *add_session;
797 struct Session *session;
799 add_session = find_command (CMD_ADD_SESSION,
800 cmd->details.del_session.add_session_label);
801 add_address = find_command (CMD_ADD_ADDRESS,
802 add_session->details.add_session.add_label);
803 GNUNET_assert (NULL != add_address->details.add_address.ar);
804 session = make_session (add_session->details.add_session.session);
805 GNUNET_ATS_address_del_session (add_address->details.add_address.ar,
810 case CMD_CHANGE_PREFERENCE:
812 struct GNUNET_PeerIdentity pid;
814 make_peer (cmd->details.change_preference.pid,
816 GNUNET_ATS_performance_change_preference (perf_ats,
818 GNUNET_ATS_PREFERENCE_END);
822 case CMD_PROVIDE_FEEDBACK:
824 struct GNUNET_PeerIdentity pid;
826 make_peer (cmd->details.provide_feedback.pid,
828 GNUNET_ATS_performance_give_feedback (perf_ats,
830 cmd->details.provide_feedback.scope,
831 GNUNET_ATS_PREFERENCE_END);
835 case CMD_LIST_ADDRESSES:
837 struct GNUNET_PeerIdentity pid;
839 make_peer (cmd->details.list_addresses.pid,
841 cmd->details.list_addresses.alh
842 = GNUNET_ATS_performance_list_addresses (perf_ats,
844 cmd->details.list_addresses.all,
849 case CMD_RESERVE_BANDWIDTH:
851 struct GNUNET_PeerIdentity pid;
853 make_peer (cmd->details.reserve_bandwidth.pid,
855 cmd->details.reserve_bandwidth.rc
856 = GNUNET_ATS_reserve_bandwidth (perf_ats,
858 cmd->details.reserve_bandwidth.amount,
865 interpreter_task = GNUNET_SCHEDULER_add_delayed (cmd->details.sleep.delay,
875 * Signature of a function called by ATS with the current bandwidth
876 * and address preferences as determined by ATS.
878 * @param cls closure, should point to "asc-closure"
879 * @param peer for which we suggest an address, NULL if ATS connection died
880 * @param address suggested address (including peer identity of the peer),
881 * may be NULL to signal disconnect from peer
882 * @param session session to use, NULL to establish a new outgoing session
883 * @param bandwidth_out assigned outbound bandwidth for the connection,
884 * 0 to signal disconnect
885 * @param bandwidth_in assigned inbound bandwidth for the connection,
886 * 0 to signal disconnect
889 address_suggest_cb (void *cls,
890 const struct GNUNET_PeerIdentity *peer,
891 const struct GNUNET_HELLO_Address *address,
892 struct Session *session,
893 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
894 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
896 const char *asc_cls = cls;
897 struct AddressSuggestData *asd;
899 GNUNET_break (0 == strcmp (asc_cls, "asc-closure"));
902 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
903 "Connection to ATS died, likely a crash!\n");
904 GNUNET_SCHEDULER_shutdown ();
906 /* This is what we should do if we wanted to continue past
908 GNUNET_CONTAINER_multipeermap_iterate (p2asd,
911 GNUNET_CONTAINER_multipeermap_iterate (p2aid,
918 asd = find_address_suggestion (peer);
921 asd = GNUNET_new (struct AddressSuggestData);
922 GNUNET_assert (GNUNET_YES ==
923 GNUNET_CONTAINER_multipeermap_put (p2asd,
926 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
928 if ( (0 == ntohl (bandwidth_out.value__)) &&
929 (0 == ntohl (bandwidth_in.value__)) )
930 asd->active = GNUNET_NO;
932 asd->active = GNUNET_YES;
933 asd->bandwidth_out = bandwidth_out;
934 asd->bandwidth_in = bandwidth_in;
935 asd->session = session;
936 GNUNET_free_non_null (asd->address);
939 asd->address = GNUNET_HELLO_address_copy (address);
940 if (NULL == interpreter_task)
946 * Signature of a function that is called with QoS information about an address.
948 * @param cls closure, should point to "aic-closure"
949 * @param address the address, NULL if ATS service was disconnected
950 * @param address_active #GNUNET_YES if this address is actively used
951 * to maintain a connection to a peer;
952 * #GNUNET_NO if the address is not actively used;
953 * #GNUNET_SYSERR if this address is no longer available for ATS
954 * @param bandwidth_out assigned outbound bandwidth for the connection
955 * @param bandwidth_in assigned inbound bandwidth for the connection
956 * @param prop performance data for the address
959 address_information_cb (void *cls,
960 const struct GNUNET_HELLO_Address *address,
962 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
963 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
964 const struct GNUNET_ATS_Properties *prop)
966 const char *aic_cls = cls;
967 struct AddressInformationData *aid;
969 GNUNET_break (0 == strcmp (aic_cls, "aic-closure"));
972 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
973 "Connection to ATS died, likely a crash!\n");
974 GNUNET_CONTAINER_multipeermap_iterate (p2aid,
980 aid = find_address_information (address);
983 aid = GNUNET_new (struct AddressInformationData);
984 aid->address = GNUNET_HELLO_address_copy (address);
985 GNUNET_assert (GNUNET_YES ==
986 GNUNET_CONTAINER_multipeermap_put (p2aid,
989 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
991 aid->active = address_active;
992 aid->bandwidth_out = bandwidth_out;
993 aid->bandwidth_in = bandwidth_in;
994 aid->properties = *prop;
995 if (NULL == interpreter_task)
1001 * Function run once the ATS service has been started.
1004 * @param cfg configuration for the testcase
1005 * @param peer handle to the peer
1009 const struct GNUNET_CONFIGURATION_Handle *cfg,
1010 struct GNUNET_TESTING_Peer *peer)
1012 p2asd = GNUNET_CONTAINER_multipeermap_create (128,
1014 p2aid = GNUNET_CONTAINER_multipeermap_create (128,
1016 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1020 sched_ats = GNUNET_ATS_scheduling_init (cfg,
1021 &address_suggest_cb,
1023 if (NULL == sched_ats)
1026 GNUNET_SCHEDULER_shutdown ();
1029 con_ats = GNUNET_ATS_connectivity_init (cfg);
1030 if (NULL == con_ats)
1033 GNUNET_SCHEDULER_shutdown ();
1036 perf_ats = GNUNET_ATS_performance_init (cfg,
1037 &address_information_cb,
1039 if (NULL == perf_ats)
1042 GNUNET_SCHEDULER_shutdown ();
1052 * @param argc length of @a argv
1053 * @param argv command line
1054 * @param cmds commands to run with the interpreter
1055 * @param timeout how long is the test allowed to take?
1056 * @return 0 on success
1059 TEST_ATS_run (int argc,
1061 struct Command *cmds,
1062 struct GNUNET_TIME_Relative timeout)
1064 char *test_filename = GNUNET_strdup (argv[0]);
1069 test_commands = cmds;
1071 if (NULL != (sep = strstr (test_filename, ".exe")))
1073 underscore = strrchr (test_filename, (int) '_');
1074 GNUNET_assert (NULL != underscore);
1075 GNUNET_asprintf (&config_file,
1076 "test_ats_api_%s.conf",
1079 if (0 != GNUNET_TESTING_peer_run ("test-ats-api",
1083 GNUNET_free (test_filename);
1084 GNUNET_free (config_file);
1088 /* end of test_ats_lib.c */