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_preferences.c
22 * @brief solver test: preference client handling
23 * @author Christian Grothoff
24 * @author Matthias Wachs
26 * FIXME: This test merely calls some of the API
27 * functions, it fails to check that
28 * preferences actually achieve anything.
31 #include "gnunet_util_lib.h"
32 #include "gnunet_testbed_service.h"
33 #include "gnunet_ats_service.h"
34 #include "test_ats_api_common.h"
39 static struct GNUNET_SCHEDULER_Task * die_task;
42 * Task to terminate the test
44 static struct GNUNET_SCHEDULER_Task *end_task;
49 static struct GNUNET_ATS_SchedulingHandle *sched_ats;
54 static struct GNUNET_ATS_ConnectivityHandle *connect_ats;
59 static struct GNUNET_ATS_PerformanceHandle *perf_ats;
69 static struct Test_Address test_addr;
74 static struct PeerContext p;
79 struct GNUNET_HELLO_Address test_hello_address;
84 static void *test_session;
89 static struct GNUNET_ATS_Information test_ats_info[3];
94 static uint32_t test_ats_count;
98 end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
100 GNUNET_log(GNUNET_ERROR_TYPE_INFO, "Done!\n");
102 if (perf_ats != NULL)
104 GNUNET_ATS_performance_done (perf_ats);
108 if (die_task != NULL )
110 GNUNET_SCHEDULER_cancel (die_task);
114 if (NULL != sched_ats)
116 GNUNET_ATS_scheduling_done (sched_ats);
119 if (NULL != connect_ats)
121 GNUNET_ATS_connectivity_done (connect_ats);
124 if (NULL != perf_ats)
126 GNUNET_ATS_performance_done (perf_ats);
129 free_test_address (&test_addr);
135 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 perf_info_cb (void *cls,
145 const struct GNUNET_HELLO_Address *address, int address_active,
146 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
147 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
148 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
152 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
153 "ATS performance info: `%s'\n",
154 GNUNET_i2s (&address->peer));
159 address_suggest_cb (void *cls,
160 const struct GNUNET_PeerIdentity *peer,
161 const struct GNUNET_HELLO_Address *address,
162 struct Session *session,
163 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
164 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in)
169 if (NULL == perf_ats)
171 for (c = 1; c < GNUNET_ATS_PreferenceCount; c++)
173 pref_val = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 10);
174 GNUNET_ATS_performance_change_preference (perf_ats,
175 &test_hello_address.peer,
176 GNUNET_ATS_PREFERENCE_LATENCY, pref_val,
177 GNUNET_ATS_PREFERENCE_END);
179 if (NULL == end_task)
180 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
188 run (void *cls, const struct GNUNET_CONFIGURATION_Handle *mycfg,
189 struct GNUNET_TESTING_Peer *peer)
192 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL );
193 connect_ats = GNUNET_ATS_connectivity_init (mycfg);
195 /* Connect to ATS scheduling */
196 sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL );
197 if (sched_ats == NULL )
199 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
200 "Could not connect to ATS scheduling!\n");
201 GNUNET_SCHEDULER_add_now (&end_badly, NULL );
205 perf_ats = GNUNET_ATS_performance_init (mycfg, &perf_info_cb, NULL);
206 if (perf_ats == NULL)
208 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
209 "Could not connect to ATS performance!\n");
210 GNUNET_SCHEDULER_add_now (&end_badly, NULL );
215 memset (&p.id, '1', sizeof(p.id));
217 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
218 "Created peer `%s'\n",
219 GNUNET_i2s_full (&p.id));
221 /* Prepare ATS Information */
222 test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE);
223 test_ats_info[0].value = htonl (GNUNET_ATS_NET_WAN);
224 test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE);
225 test_ats_info[1].value = htonl (1);
226 test_ats_info[2].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
227 test_ats_info[2].value = htonl (100);
230 /* Adding address without session */
232 create_test_address (&test_addr, "test",
233 test_session, "test",
234 strlen ("test") + 1);
235 test_hello_address.peer = p.id;
236 test_hello_address.transport_name = test_addr.plugin;
237 test_hello_address.address = test_addr.addr;
238 test_hello_address.address_length = test_addr.addr_len;
241 GNUNET_ATS_address_add (sched_ats, &test_hello_address, test_session,
242 test_ats_info, test_ats_count);
243 GNUNET_ATS_connectivity_suggest (connect_ats, &test_hello_address.peer);
248 main (int argc, char *argv[])
251 char *src_filename = GNUNET_strdup (__FILE__);
252 char *test_filename = GNUNET_strdup (argv[0]);
258 if (NULL == (sep = (strstr (src_filename, ".c"))))
265 if (NULL != (sep = strstr (test_filename, ".exe")))
268 if (NULL == (solver = strstr (test_filename, src_filename)))
273 solver += strlen (src_filename) + 1;
275 if (0 == strcmp (solver, "proportional"))
277 config_file = "test_ats_solver_proportional.conf";
279 else if (0 == strcmp (solver, "mlp"))
281 config_file = "test_ats_solver_mlp.conf";
283 else if ((0 == strcmp (solver, "ril")))
285 config_file = "test_ats_solver_ril.conf";
290 GNUNET_free(src_filename);
291 GNUNET_free(test_filename);
295 GNUNET_free(src_filename);
296 GNUNET_free(test_filename);
299 != GNUNET_TESTING_peer_run ("test-ats-solver", config_file, &run, NULL ))
300 return GNUNET_SYSERR;
305 /* end of file test_ats_solver_preferences.c */