2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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 GNUNET_ATS_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;
150 * Free `struct AddressSuggestData` entry.
154 * @param value the `struct AddressSuggestData` to release
155 * @return #GNUNET_OK (continue to iterate)
159 const struct GNUNET_PeerIdentity *key,
162 struct AddressSuggestData *asd = value;
164 GNUNET_assert (GNUNET_YES ==
165 GNUNET_CONTAINER_multipeermap_remove (p2asd,
168 GNUNET_free_non_null (asd->address);
175 * Free `struct AddressInformationData` entry.
179 * @param value the `struct AddressSuggestData` to release
180 * @return #GNUNET_OK (continue to iterate)
184 const struct GNUNET_PeerIdentity *key,
187 struct AddressInformationData *aid = value;
189 GNUNET_assert (GNUNET_YES ==
190 GNUNET_CONTAINER_multipeermap_remove (p2aid,
193 GNUNET_free (aid->address);
200 * Find latest address suggestion made for the given peer.
202 * @param pid peer to look up
203 * @return NULL if peer was never involved
205 static struct AddressSuggestData *
206 find_address_suggestion (const struct GNUNET_PeerIdentity *pid)
208 return GNUNET_CONTAINER_multipeermap_get (p2asd,
214 * Closure for #match_address()
216 struct MatchAddressContext
221 const struct GNUNET_HELLO_Address *addr;
224 * Where to return address information if found.
226 struct AddressInformationData *ret;
231 * Find matching address information.
233 * @param cls a `struct MatchAddressContext`
235 * @param value a `struct AddressInformationData`
236 * @return #GNUNET_OK if not found
239 match_address (void *cls,
240 const struct GNUNET_PeerIdentity *key,
243 struct MatchAddressContext *mac = cls;
244 struct AddressInformationData *aid = value;
246 if (0 == GNUNET_HELLO_address_cmp (mac->addr,
257 * Find latest address information made for the given address.
259 * @param addr address to look up
260 * @return NULL if peer was never involved
262 static struct AddressInformationData *
263 find_address_information (const struct GNUNET_HELLO_Address *addr)
265 struct MatchAddressContext mac;
269 GNUNET_CONTAINER_multipeermap_get_multiple (p2aid,
278 * Task run to terminate the testcase.
286 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
287 "Test failed at stage %u %s\n",
289 (NULL != test_commands[off].label)
290 ? test_commands[off].label
292 if (NULL != interpreter_task)
294 GNUNET_SCHEDULER_cancel (interpreter_task);
295 interpreter_task = NULL;
297 if (NULL != sched_ats)
299 GNUNET_ATS_scheduling_done (sched_ats);
304 GNUNET_ATS_connectivity_done (con_ats);
307 if (NULL != perf_ats)
309 GNUNET_ATS_performance_done (perf_ats);
314 GNUNET_CONTAINER_multipeermap_iterate (p2asd,
317 GNUNET_CONTAINER_multipeermap_destroy (p2asd);
322 GNUNET_CONTAINER_multipeermap_iterate (p2aid,
325 GNUNET_CONTAINER_multipeermap_destroy (p2aid);
332 * Main interpreter loop. Runs the steps of the test.
337 interpreter (void *cls);
341 * Run the interpreter next.
346 if (NULL != interpreter_task)
347 GNUNET_SCHEDULER_cancel (interpreter_task);
348 interpreter_task = GNUNET_SCHEDULER_add_now (&interpreter,
354 * Initialize public key of a peer based on a single number.
356 * @param pid number to use as the basis
357 * @param pk resulting fake public key
360 make_peer (uint32_t pid,
361 struct GNUNET_PeerIdentity *pk)
365 sizeof(struct GNUNET_PeerIdentity));
373 * Generate a fake address based on the given parameters.
375 * @param pid number of the peer
376 * @param num number of the address at peer @a pid
377 * @param addr_flags flags to use for the address
378 * @return the address
380 static struct GNUNET_HELLO_Address *
381 make_address (uint32_t pid,
383 enum GNUNET_HELLO_AddressInfo addr_flags)
385 struct GNUNET_PeerIdentity pk;
391 return GNUNET_HELLO_address_allocate (&pk,
400 * Our dummy sessions.
402 struct GNUNET_ATS_Session
405 * Field to avoid `0 == sizeof(struct GNUNET_ATS_Session)`.
407 unsigned int non_empty;
412 * Create a session instance for ATS.
414 * @param i which session number to return
415 * @return NULL if @a i is 0, otherwise a pointer unique to @a i
417 static struct GNUNET_ATS_Session *
418 make_session (unsigned int i)
420 struct GNUNET_ATS_Session *baseptr = NULL;
424 /* Yes, these are *intentionally* out-of-bounds,
425 and offset from NULL, as nobody should ever
426 use those other than to compare pointers! */
432 * Find a @a code command before the global #off with the
433 * specified @a label.
435 * @param code opcode to look for
436 * @param label label to look for, NULL for none
437 * @return previous command with the matching label
439 static struct Command *
440 find_command (enum CommandCode code,
447 for (i = off - 1; i >= 0; i--)
448 if ((code == test_commands[i].code) &&
449 (0 == strcmp (test_commands[i].label,
451 return &test_commands[i];
458 * Function called from #GNUNET_ATS_performance_list_addresses when
459 * we process a #CMD_LIST_ADDRESSES command.
461 * @param cls the `struct Command` that caused the call
462 * @param address the address, NULL if ATS service was disconnected
463 * @param address_active #GNUNET_YES if this address is actively used
464 * to maintain a connection to a peer;
465 * #GNUNET_NO if the address is not actively used;
466 * #GNUNET_SYSERR if this address is no longer available for ATS
467 * @param bandwidth_out assigned outbound bandwidth for the connection
468 * @param bandwidth_in assigned inbound bandwidth for the connection
469 * @param prop performance data for the address
473 const struct GNUNET_HELLO_Address *address,
475 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
476 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
477 const struct GNUNET_ATS_Properties *prop)
479 struct Command *c = cls;
480 struct CommandListAddresses *cmd = &c->details.list_addresses;
485 /* we are done with the iteration, continue to execute */
486 if ((cmd->calls < cmd->min_calls) &&
487 (cmd->active_calls < cmd->min_active_calls))
489 GNUNET_SCHEDULER_shutdown ();
496 switch (address_active)
510 if ((cmd->calls > cmd->max_calls) &&
511 (cmd->active_calls < cmd->max_active_calls))
514 GNUNET_ATS_performance_list_addresses_cancel (cmd->alh);
516 GNUNET_SCHEDULER_shutdown ();
523 * Function called with reservation result.
525 * @param cls closure with the reservation command (`struct Command`)
526 * @param peer identifies the peer
527 * @param amount set to the amount that was actually reserved or unreserved;
528 * either the full requested amount or zero (no partial reservations)
529 * @param res_delay if the reservation could not be satisfied (amount was 0), how
530 * long should the client wait until re-trying?
533 reservation_cb (void *cls,
534 const struct GNUNET_PeerIdentity *peer,
536 struct GNUNET_TIME_Relative res_delay)
538 struct Command *cmd = cls;
539 struct GNUNET_PeerIdentity pid;
541 cmd->details.reserve_bandwidth.rc = NULL;
542 make_peer (cmd->details.reserve_bandwidth.pid,
544 GNUNET_assert (0 == GNUNET_memcmp (peer,
546 switch (cmd->details.reserve_bandwidth.expected_result)
549 if (amount != cmd->details.reserve_bandwidth.amount)
551 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
552 "Unexpectedly failed to reserve %d/%d bytes with delay %s!\n",
554 (int) cmd->details.reserve_bandwidth.amount,
555 GNUNET_STRINGS_relative_time_to_string (res_delay,
558 GNUNET_SCHEDULER_shutdown ();
564 GNUNET_break ((0 != amount) ||
565 (0 != res_delay.rel_value_us));
570 (0 == res_delay.rel_value_us))
572 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
573 "Unexpectedly reserved %d bytes with delay %s!\n",
575 GNUNET_STRINGS_relative_time_to_string (res_delay,
578 GNUNET_SCHEDULER_shutdown ();
589 * Main interpreter loop. Runs the steps of the test.
594 interpreter (void *cls)
599 interpreter_task = NULL;
602 cmd = &test_commands[off];
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 (NULL != cmd->label) ? cmd->label : "");
612 GNUNET_SCHEDULER_shutdown ();
615 case CMD_ADD_ADDRESS:
617 struct GNUNET_HELLO_Address *addr;
618 struct GNUNET_ATS_Session *session;
620 addr = make_address (cmd->details.add_address.pid,
621 cmd->details.add_address.addr_num,
622 cmd->details.add_address.addr_flags);
623 session = make_session (cmd->details.add_address.session);
624 if (cmd->details.add_address.expect_fail)
625 GNUNET_log_skip (1, GNUNET_NO);
626 cmd->details.add_address.ar
627 = GNUNET_ATS_address_add (sched_ats,
630 &cmd->details.add_address.properties);
632 if (cmd->details.add_address.expect_fail)
634 GNUNET_log_skip (0, GNUNET_YES);
636 else if (NULL == cmd->details.add_address.ar)
639 GNUNET_SCHEDULER_shutdown ();
646 case CMD_DEL_ADDRESS:
650 add = find_command (CMD_ADD_ADDRESS,
651 cmd->details.del_address.add_label);
652 GNUNET_assert (NULL != add->details.add_address.ar);
653 GNUNET_ATS_address_destroy (add->details.add_address.ar);
654 add->details.add_address.ar = NULL;
659 case CMD_AWAIT_ADDRESS_SUGGESTION:
661 struct GNUNET_PeerIdentity pid;
662 struct GNUNET_HELLO_Address *addr;
664 struct AddressSuggestData *asd;
667 make_peer (cmd->details.await_address_suggestion.pid,
669 asd = find_address_suggestion (&pid);
672 if (GNUNET_NO == asd->active)
673 return; /* last suggestion was to disconnect, wait longer */
675 if (NULL != cmd->details.await_address_suggestion.add_label)
678 add = find_command (CMD_ADD_ADDRESS,
679 cmd->details.await_address_suggestion.add_label);
680 addr = make_address (add->details.add_address.pid,
681 add->details.add_address.addr_num,
682 add->details.add_address.addr_flags);
684 make_session (add->details.add_address.session)) &&
686 GNUNET_HELLO_address_cmp (addr,
691 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! */
714 case CMD_REQUEST_CONNECTION_START:
716 struct GNUNET_PeerIdentity pid;
718 make_peer (cmd->details.request_connection_start.pid,
720 cmd->details.request_connection_start.csh
721 = GNUNET_ATS_connectivity_suggest (con_ats,
728 case CMD_REQUEST_CONNECTION_STOP:
730 struct Command *start;
732 start = find_command (CMD_REQUEST_CONNECTION_START,
733 cmd->details.request_connection_stop.connect_label);
734 GNUNET_ATS_connectivity_suggest_cancel (
735 start->details.request_connection_start.csh);
736 start->details.request_connection_start.csh = NULL;
741 case CMD_AWAIT_ADDRESS_INFORMATION:
743 struct AddressInformationData *aid;
745 struct Command *update;
746 struct GNUNET_HELLO_Address *addr;
747 const struct GNUNET_ATS_Properties *cmp;
749 add = find_command (CMD_ADD_ADDRESS,
750 cmd->details.await_address_information.add_label);
751 update = find_command (CMD_UPDATE_ADDRESS,
752 cmd->details.await_address_information.
754 addr = make_address (add->details.add_address.pid,
755 add->details.add_address.addr_num,
756 add->details.add_address.addr_flags);
757 aid = find_address_information (addr);
760 cmp = &add->details.add_address.properties;
762 cmp = &update->details.update_address.properties;
764 (cmp->delay.rel_value_us == aid->properties.delay.rel_value_us) &&
765 (cmp->utilization_out == aid->properties.utilization_out) &&
766 (cmp->utilization_in == aid->properties.utilization_in) &&
767 (cmp->distance == aid->properties.distance) &&
768 (cmp->scope == aid->properties.scope))
776 case CMD_UPDATE_ADDRESS:
780 add = find_command (CMD_ADD_ADDRESS,
781 cmd->details.update_address.add_label);
782 GNUNET_assert (NULL != add->details.add_address.ar);
783 GNUNET_ATS_address_update (add->details.add_address.ar,
784 &cmd->details.update_address.properties);
789 case CMD_ADD_SESSION:
792 struct GNUNET_ATS_Session *session;
794 add = find_command (CMD_ADD_ADDRESS,
795 cmd->details.add_session.add_label);
796 session = make_session (cmd->details.add_session.session);
797 GNUNET_assert (NULL != add->details.add_address.ar);
798 GNUNET_ATS_address_add_session (add->details.add_address.ar,
804 case CMD_DEL_SESSION:
806 struct Command *add_address;
807 struct Command *add_session;
808 struct GNUNET_ATS_Session *session;
810 add_session = find_command (CMD_ADD_SESSION,
811 cmd->details.del_session.add_session_label);
812 add_address = find_command (CMD_ADD_ADDRESS,
813 add_session->details.add_session.add_label);
814 GNUNET_assert (NULL != add_address->details.add_address.ar);
815 session = make_session (add_session->details.add_session.session);
816 GNUNET_ATS_address_del_session (add_address->details.add_address.ar,
822 case CMD_CHANGE_PREFERENCE:
824 struct GNUNET_PeerIdentity pid;
826 make_peer (cmd->details.change_preference.pid,
828 GNUNET_ATS_performance_change_preference (perf_ats,
830 GNUNET_ATS_PREFERENCE_END);
835 case CMD_PROVIDE_FEEDBACK:
837 struct GNUNET_PeerIdentity pid;
839 make_peer (cmd->details.provide_feedback.pid,
841 GNUNET_ATS_performance_give_feedback (perf_ats,
843 cmd->details.provide_feedback.
845 GNUNET_ATS_PREFERENCE_END);
850 case CMD_LIST_ADDRESSES:
852 struct GNUNET_PeerIdentity pid;
854 make_peer (cmd->details.list_addresses.pid,
856 cmd->details.list_addresses.alh
857 = GNUNET_ATS_performance_list_addresses (perf_ats,
859 cmd->details.list_addresses.
866 case CMD_RESERVE_BANDWIDTH:
868 struct GNUNET_PeerIdentity pid;
870 make_peer (cmd->details.reserve_bandwidth.pid,
872 cmd->details.reserve_bandwidth.rc
873 = GNUNET_ATS_reserve_bandwidth (perf_ats,
875 cmd->details.reserve_bandwidth.amount,
883 interpreter_task = GNUNET_SCHEDULER_add_delayed (cmd->details.sleep.delay,
893 * Signature of a function called by ATS with the current bandwidth
894 * and address preferences as determined by ATS.
896 * @param cls closure, should point to "asc-closure"
897 * @param peer for which we suggest an address, NULL if ATS connection died
898 * @param address suggested address (including peer identity of the peer),
899 * may be NULL to signal disconnect from peer
900 * @param session session to use, NULL to establish a new outgoing session
901 * @param bandwidth_out assigned outbound bandwidth for the connection,
902 * 0 to signal disconnect
903 * @param bandwidth_in assigned inbound bandwidth for the connection,
904 * 0 to signal disconnect
907 address_suggest_cb (void *cls,
908 const struct GNUNET_PeerIdentity *peer,
909 const struct GNUNET_HELLO_Address *address,
910 struct GNUNET_ATS_Session *session,
911 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
912 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
914 const char *asc_cls = cls;
915 struct AddressSuggestData *asd;
917 GNUNET_break (0 == strcmp (asc_cls, "asc-closure"));
920 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
921 "Connection to ATS died, likely a crash!\n");
922 GNUNET_SCHEDULER_shutdown ();
924 /* This is what we should do if we wanted to continue past
926 GNUNET_CONTAINER_multipeermap_iterate (p2asd,
929 GNUNET_CONTAINER_multipeermap_iterate (p2aid,
936 asd = find_address_suggestion (peer);
939 asd = GNUNET_new (struct AddressSuggestData);
940 GNUNET_assert (GNUNET_YES ==
941 GNUNET_CONTAINER_multipeermap_put (p2asd,
944 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
946 if ((0 == ntohl (bandwidth_out.value__)) &&
947 (0 == ntohl (bandwidth_in.value__)))
948 asd->active = GNUNET_NO;
950 asd->active = GNUNET_YES;
951 asd->bandwidth_out = bandwidth_out;
952 asd->bandwidth_in = bandwidth_in;
953 asd->session = session;
954 GNUNET_free_non_null (asd->address);
957 asd->address = GNUNET_HELLO_address_copy (address);
958 if (NULL == interpreter_task)
964 * Signature of a function that is called with QoS information about an address.
966 * @param cls closure, should point to "aic-closure"
967 * @param address the address, NULL if ATS service was disconnected
968 * @param address_active #GNUNET_YES if this address is actively used
969 * to maintain a connection to a peer;
970 * #GNUNET_NO if the address is not actively used;
971 * #GNUNET_SYSERR if this address is no longer available for ATS
972 * @param bandwidth_out assigned outbound bandwidth for the connection
973 * @param bandwidth_in assigned inbound bandwidth for the connection
974 * @param prop performance data for the address
977 address_information_cb (void *cls,
978 const struct GNUNET_HELLO_Address *address,
980 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
981 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
982 const struct GNUNET_ATS_Properties *prop)
984 const char *aic_cls = cls;
985 struct AddressInformationData *aid;
987 GNUNET_break (0 == strcmp (aic_cls, "aic-closure"));
990 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
991 "Connection to ATS died, likely a crash!\n");
992 GNUNET_CONTAINER_multipeermap_iterate (p2aid,
998 aid = find_address_information (address);
1001 aid = GNUNET_new (struct AddressInformationData);
1002 aid->address = GNUNET_HELLO_address_copy (address);
1003 GNUNET_assert (GNUNET_YES ==
1004 GNUNET_CONTAINER_multipeermap_put (p2aid,
1007 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
1009 aid->active = address_active;
1010 aid->bandwidth_out = bandwidth_out;
1011 aid->bandwidth_in = bandwidth_in;
1012 aid->properties = *prop;
1013 if (NULL == interpreter_task)
1019 * Function run once the ATS service has been started.
1022 * @param cfg configuration for the testcase
1023 * @param peer handle to the peer
1027 const struct GNUNET_CONFIGURATION_Handle *cfg,
1028 struct GNUNET_TESTING_Peer *peer)
1030 p2asd = GNUNET_CONTAINER_multipeermap_create (128,
1032 p2aid = GNUNET_CONTAINER_multipeermap_create (128,
1034 GNUNET_SCHEDULER_add_delayed (TIMEOUT,
1038 sched_ats = GNUNET_ATS_scheduling_init (cfg,
1039 &address_suggest_cb,
1041 if (NULL == sched_ats)
1044 GNUNET_SCHEDULER_shutdown ();
1047 con_ats = GNUNET_ATS_connectivity_init (cfg);
1048 if (NULL == con_ats)
1051 GNUNET_SCHEDULER_shutdown ();
1054 perf_ats = GNUNET_ATS_performance_init (cfg,
1055 &address_information_cb,
1057 if (NULL == perf_ats)
1060 GNUNET_SCHEDULER_shutdown ();
1070 * @param argc length of @a argv
1071 * @param argv command line
1072 * @param cmds commands to run with the interpreter
1073 * @param timeout how long is the test allowed to take?
1074 * @return 0 on success
1077 TEST_ATS_run (int argc,
1079 struct Command *cmds,
1080 struct GNUNET_TIME_Relative timeout)
1082 char *test_filename = GNUNET_strdup (argv[0]);
1087 test_commands = cmds;
1089 if (NULL != (sep = strstr (test_filename, ".exe")))
1091 underscore = strrchr (test_filename, (int) '_');
1092 GNUNET_assert (NULL != underscore);
1093 GNUNET_asprintf (&config_file,
1094 "test_ats_api_%s.conf",
1097 if (0 != GNUNET_TESTING_peer_run ("test-ats-api",
1101 GNUNET_free (test_filename);
1102 GNUNET_free (config_file);
1107 /* end of test_ats_lib.c */