diff options
Diffstat (limited to 'src/ats/test_ats_solver_alternative_after_delete_address.c')
-rw-r--r-- | src/ats/test_ats_solver_alternative_after_delete_address.c | 455 |
1 files changed, 0 insertions, 455 deletions
diff --git a/src/ats/test_ats_solver_alternative_after_delete_address.c b/src/ats/test_ats_solver_alternative_after_delete_address.c deleted file mode 100644 index 77c903f1c..000000000 --- a/src/ats/test_ats_solver_alternative_after_delete_address.c +++ /dev/null | |||
@@ -1,455 +0,0 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2010-2013 Christian Grothoff (and other contributing authors) | ||
4 | |||
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. | ||
9 | |||
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. | ||
14 | |||
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. | ||
19 | */ | ||
20 | /** | ||
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 | ||
25 | */ | ||
26 | #include "platform.h" | ||
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" | ||
31 | |||
32 | /** | ||
33 | * Timeout task | ||
34 | */ | ||
35 | static struct GNUNET_SCHEDULER_Task * die_task; | ||
36 | |||
37 | /** | ||
38 | * Statistics handle | ||
39 | */ | ||
40 | static struct GNUNET_STATISTICS_Handle *stats; | ||
41 | |||
42 | /** | ||
43 | * Scheduling handle | ||
44 | */ | ||
45 | static struct GNUNET_ATS_SchedulingHandle *sched_ats; | ||
46 | |||
47 | /** | ||
48 | * Connectivity handle | ||
49 | */ | ||
50 | static struct GNUNET_ATS_ConnectivityHandle *connect_ats; | ||
51 | |||
52 | /** | ||
53 | * Return value | ||
54 | */ | ||
55 | static int ret; | ||
56 | |||
57 | /** | ||
58 | * Test address | ||
59 | */ | ||
60 | static struct Test_Address test_addr; | ||
61 | |||
62 | /** | ||
63 | * Alternative test address | ||
64 | */ | ||
65 | static struct Test_Address alt_test_addr; | ||
66 | |||
67 | /** | ||
68 | * Test peer | ||
69 | */ | ||
70 | static struct PeerContext p; | ||
71 | |||
72 | /** | ||
73 | * HELLO address | ||
74 | */ | ||
75 | static struct GNUNET_HELLO_Address test_hello_address; | ||
76 | |||
77 | /** | ||
78 | * HELLO address | ||
79 | */ | ||
80 | static struct GNUNET_HELLO_Address alt_test_hello_address; | ||
81 | |||
82 | /** | ||
83 | * Session | ||
84 | */ | ||
85 | static void *test_session; | ||
86 | |||
87 | /** | ||
88 | * Test ats info | ||
89 | */ | ||
90 | static struct GNUNET_ATS_Information test_ats_info[2]; | ||
91 | |||
92 | /** | ||
93 | * Test ats count | ||
94 | */ | ||
95 | static uint32_t test_ats_count; | ||
96 | |||
97 | /** | ||
98 | * Test state | ||
99 | */ | ||
100 | static int addresses_added; | ||
101 | |||
102 | static int first_address_suggested; | ||
103 | |||
104 | static int first_address_deleted; | ||
105 | |||
106 | static int second_address_deleted; | ||
107 | |||
108 | static int second_address_suggested = GNUNET_YES; | ||
109 | |||
110 | static struct GNUNET_HELLO_Address *first_suggestion; | ||
111 | |||
112 | static struct GNUNET_HELLO_Address *second_suggestion; | ||
113 | |||
114 | /** | ||
115 | * 1st Address we will destroy. | ||
116 | */ | ||
117 | static struct GNUNET_ATS_AddressRecord *ar; | ||
118 | |||
119 | /** | ||
120 | * 2nd Address we will destroy. | ||
121 | */ | ||
122 | static struct GNUNET_ATS_AddressRecord *ar2; | ||
123 | |||
124 | |||
125 | static int | ||
126 | stat_cb (void *cls, const char *subsystem, const char *name, uint64_t value, | ||
127 | int is_persistent); | ||
128 | |||
129 | |||
130 | static void | ||
131 | end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
132 | { | ||
133 | if (NULL != die_task) | ||
134 | { | ||
135 | GNUNET_SCHEDULER_cancel (die_task); | ||
136 | die_task = NULL; | ||
137 | } | ||
138 | if (NULL != connect_ats) | ||
139 | { | ||
140 | GNUNET_ATS_connectivity_done (connect_ats); | ||
141 | connect_ats = NULL; | ||
142 | } | ||
143 | if (NULL != sched_ats) | ||
144 | { | ||
145 | GNUNET_ATS_scheduling_done (sched_ats); | ||
146 | sched_ats = NULL; | ||
147 | } | ||
148 | GNUNET_STATISTICS_watch_cancel (stats, | ||
149 | "ats", | ||
150 | "# addresses", | ||
151 | &stat_cb, NULL); | ||
152 | if (NULL != stats) | ||
153 | { | ||
154 | GNUNET_STATISTICS_destroy (stats, GNUNET_NO); | ||
155 | stats = NULL; | ||
156 | } | ||
157 | free_test_address (&test_addr); | ||
158 | GNUNET_free_non_null (first_suggestion); | ||
159 | first_suggestion = NULL; | ||
160 | GNUNET_free_non_null (second_suggestion); | ||
161 | second_suggestion = NULL; | ||
162 | ret = 0; | ||
163 | } | ||
164 | |||
165 | |||
166 | static void | ||
167 | end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
168 | { | ||
169 | |||
170 | die_task = NULL; | ||
171 | end ( NULL, NULL); | ||
172 | ret = GNUNET_SYSERR; | ||
173 | } | ||
174 | |||
175 | |||
176 | static void | ||
177 | end_badly_now () | ||
178 | { | ||
179 | if (NULL != die_task) | ||
180 | { | ||
181 | GNUNET_SCHEDULER_cancel (die_task); | ||
182 | die_task = NULL; | ||
183 | } | ||
184 | GNUNET_SCHEDULER_add_now (&end_badly, NULL); | ||
185 | } | ||
186 | |||
187 | |||
188 | static void | ||
189 | address_suggest_cb (void *cls, | ||
190 | const struct GNUNET_PeerIdentity *peer, | ||
191 | const struct GNUNET_HELLO_Address *address, | ||
192 | struct Session *session, | ||
193 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out, | ||
194 | struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in) | ||
195 | { | ||
196 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
197 | "Received a sugggestion for peer `%s'\n", | ||
198 | GNUNET_i2s (peer)); | ||
199 | if (GNUNET_NO == first_address_suggested) | ||
200 | { | ||
201 | if (NULL == first_suggestion) | ||
202 | { | ||
203 | if ((NULL == address) || (NULL != session)) | ||
204 | { | ||
205 | GNUNET_break (0); | ||
206 | end_badly_now (); | ||
207 | return; | ||
208 | } | ||
209 | if ((ntohl(bandwidth_in.value__) == 0) || | ||
210 | (ntohl(bandwidth_out.value__) == 0)) | ||
211 | { | ||
212 | GNUNET_break (0); | ||
213 | end_badly_now (); | ||
214 | return; | ||
215 | } | ||
216 | |||
217 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
218 | "Received 1st sugggestion for peer `%s' : `%s'\n", | ||
219 | GNUNET_i2s (&address->peer), (char *) address->address); | ||
220 | |||
221 | first_suggestion = GNUNET_HELLO_address_copy (address); | ||
222 | first_address_suggested = GNUNET_YES; | ||
223 | |||
224 | |||
225 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
226 | "Deleting 1st address for peer `%s' : `%s'\n", | ||
227 | GNUNET_i2s (&address->peer), | ||
228 | (char *) address->address); | ||
229 | if (0 == (strcmp ((char *) address->address, | ||
230 | "test"))) | ||
231 | { | ||
232 | GNUNET_assert (NULL != ar); | ||
233 | GNUNET_ATS_address_destroy (ar); | ||
234 | ar = NULL; | ||
235 | } | ||
236 | else | ||
237 | { | ||
238 | GNUNET_assert (NULL != ar2); | ||
239 | GNUNET_ATS_address_destroy (ar2); | ||
240 | ar2 = NULL; | ||
241 | } | ||
242 | |||
243 | first_address_deleted = GNUNET_YES; | ||
244 | |||
245 | return; | ||
246 | } | ||
247 | } | ||
248 | if (GNUNET_YES == first_address_deleted) | ||
249 | { | ||
250 | if (NULL == second_suggestion) | ||
251 | { | ||
252 | if ((NULL == address) || (NULL != session)) | ||
253 | { | ||
254 | GNUNET_break (0); | ||
255 | end_badly_now (); | ||
256 | return; | ||
257 | } | ||
258 | |||
259 | if (0 != | ||
260 | memcmp (address->address, | ||
261 | first_suggestion->address, | ||
262 | (first_suggestion->address_length < address->address_length) | ||
263 | ? first_suggestion->address_length : address->address_length)) | ||
264 | { | ||
265 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
266 | "Received 2nd sugggestion for peer `%s' : `%s'\n", | ||
267 | GNUNET_i2s (&address->peer), | ||
268 | (char *) address->address); | ||
269 | second_suggestion = GNUNET_HELLO_address_copy (address); | ||
270 | second_address_suggested = GNUNET_YES; | ||
271 | |||
272 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
273 | "Deleting 2nd address for peer `%s' : `%s'\n", | ||
274 | GNUNET_i2s (&address->peer), | ||
275 | (char *) address->address); | ||
276 | if (NULL != ar) | ||
277 | { | ||
278 | GNUNET_assert (NULL == ar2); | ||
279 | GNUNET_ATS_address_destroy (ar); | ||
280 | ar = NULL; | ||
281 | } | ||
282 | else | ||
283 | { | ||
284 | GNUNET_assert (NULL != ar2); | ||
285 | GNUNET_ATS_address_destroy (ar2); | ||
286 | ar2 = NULL; | ||
287 | } | ||
288 | second_address_deleted = GNUNET_YES; | ||
289 | return; | ||
290 | } | ||
291 | } | ||
292 | } | ||
293 | if (GNUNET_YES == second_address_deleted) | ||
294 | { | ||
295 | /* Expecting disconnect */ | ||
296 | if ((ntohl(bandwidth_in.value__) == 0) && | ||
297 | (ntohl(bandwidth_out.value__) == 0)) | ||
298 | { | ||
299 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
300 | "ATS tells me to disconnect\n"); | ||
301 | GNUNET_SCHEDULER_add_now (&end, NULL); | ||
302 | return; | ||
303 | } | ||
304 | else | ||
305 | { | ||
306 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
307 | "Expected disconnect but received address `%s' with bandwidth \n", | ||
308 | (char *) address->address); | ||
309 | } | ||
310 | } | ||
311 | } | ||
312 | |||
313 | |||
314 | static int | ||
315 | stat_cb(void *cls, const char *subsystem, | ||
316 | const char *name, uint64_t value, | ||
317 | int is_persistent) | ||
318 | { | ||
319 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
320 | "ATS statistics: `%s' `%s' %llu\n", | ||
321 | subsystem, | ||
322 | name, | ||
323 | value); | ||
324 | if ((GNUNET_NO == addresses_added) && (value == 2)) | ||
325 | { | ||
326 | GNUNET_log (GNUNET_ERROR_TYPE_INFO, | ||
327 | "All addresses added, requesting....\n"); | ||
328 | /* We have 2 addresses, so we can request */ | ||
329 | addresses_added = GNUNET_YES; | ||
330 | GNUNET_ATS_connectivity_suggest (connect_ats, &p.id); | ||
331 | } | ||
332 | return GNUNET_OK; | ||
333 | } | ||
334 | |||
335 | |||
336 | static void | ||
337 | run (void *cls, | ||
338 | const struct GNUNET_CONFIGURATION_Handle *mycfg, | ||
339 | struct GNUNET_TESTING_Peer *peer) | ||
340 | { | ||
341 | die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL); | ||
342 | stats = GNUNET_STATISTICS_create ("ats", mycfg); | ||
343 | GNUNET_STATISTICS_watch (stats, "ats", "# addresses", &stat_cb, NULL); | ||
344 | |||
345 | connect_ats = GNUNET_ATS_connectivity_init (mycfg); | ||
346 | |||
347 | /* Connect to ATS scheduling */ | ||
348 | sched_ats = GNUNET_ATS_scheduling_init (mycfg, &address_suggest_cb, NULL); | ||
349 | if (sched_ats == NULL) | ||
350 | { | ||
351 | GNUNET_log (GNUNET_ERROR_TYPE_ERROR, | ||
352 | "Could not connect to ATS scheduling!\n"); | ||
353 | end_badly_now (); | ||
354 | return; | ||
355 | } | ||
356 | |||
357 | /* Set up peer */ | ||
358 | memset (&p.id, '1', sizeof (p.id)); | ||
359 | GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Created peer `%s'\n", | ||
360 | GNUNET_i2s_full(&p.id)); | ||
361 | |||
362 | /* Prepare ATS Information */ | ||
363 | test_ats_info[0].type = htonl (GNUNET_ATS_NETWORK_TYPE); | ||
364 | test_ats_info[0].value = htonl(GNUNET_ATS_NET_WAN); | ||
365 | test_ats_info[1].type = htonl (GNUNET_ATS_QUALITY_NET_DISTANCE); | ||
366 | test_ats_info[1].value = htonl(1); | ||
367 | test_ats_count = 2; | ||
368 | |||
369 | /* Adding address without session */ | ||
370 | test_session = NULL; | ||
371 | create_test_address (&test_addr, "test", test_session, | ||
372 | "test", strlen ("test") + 1); | ||
373 | test_hello_address.peer = p.id; | ||
374 | test_hello_address.transport_name = test_addr.plugin; | ||
375 | test_hello_address.address = test_addr.addr; | ||
376 | test_hello_address.address_length = test_addr.addr_len; | ||
377 | |||
378 | /* Adding alternative address without session */ | ||
379 | test_session = NULL; | ||
380 | create_test_address (&alt_test_addr, "test", test_session, | ||
381 | "alt_test", strlen ("alt_test") + 1); | ||
382 | alt_test_hello_address.peer = p.id; | ||
383 | alt_test_hello_address.transport_name = alt_test_addr.plugin; | ||
384 | alt_test_hello_address.address = alt_test_addr.addr; | ||
385 | alt_test_hello_address.address_length = alt_test_addr.addr_len; | ||
386 | |||
387 | |||
388 | /* Adding address */ | ||
389 | ar = GNUNET_ATS_address_add (sched_ats, &test_hello_address, NULL, | ||
390 | test_ats_info, test_ats_count); | ||
391 | /* Adding alternative address */ | ||
392 | ar2 = GNUNET_ATS_address_add (sched_ats, &alt_test_hello_address, NULL, | ||
393 | test_ats_info, test_ats_count); | ||
394 | } | ||
395 | |||
396 | |||
397 | int | ||
398 | main (int argc, char *argv[]) | ||
399 | { | ||
400 | char *sep; | ||
401 | char *src_filename = GNUNET_strdup (__FILE__); | ||
402 | char *test_filename = GNUNET_strdup (argv[0]); | ||
403 | const char *config_file; | ||
404 | char *solver; | ||
405 | |||
406 | ret = 0; | ||
407 | |||
408 | if (NULL == (sep = (strstr (src_filename, ".c")))) | ||
409 | { | ||
410 | GNUNET_break (0); | ||
411 | return -1; | ||
412 | } | ||
413 | sep[0] = '\0'; | ||
414 | |||
415 | if (NULL != (sep = strstr (test_filename, ".exe"))) | ||
416 | sep[0] = '\0'; | ||
417 | |||
418 | if (NULL == (solver = strstr (test_filename, src_filename))) | ||
419 | { | ||
420 | GNUNET_break (0); | ||
421 | return -1; | ||
422 | } | ||
423 | solver += strlen (src_filename) +1; | ||
424 | |||
425 | if (0 == strcmp(solver, "proportional")) | ||
426 | { | ||
427 | config_file = "test_ats_solver_proportional.conf"; | ||
428 | } | ||
429 | else if (0 == strcmp(solver, "mlp")) | ||
430 | { | ||
431 | config_file = "test_ats_solver_mlp.conf"; | ||
432 | } | ||
433 | else if ((0 == strcmp(solver, "ril"))) | ||
434 | { | ||
435 | config_file = "test_ats_solver_ril.conf"; | ||
436 | } | ||
437 | else | ||
438 | { | ||
439 | GNUNET_break (0); | ||
440 | GNUNET_free (src_filename); | ||
441 | GNUNET_free (test_filename); | ||
442 | return 1; | ||
443 | } | ||
444 | |||
445 | GNUNET_free (src_filename); | ||
446 | GNUNET_free (test_filename); | ||
447 | |||
448 | if (0 != GNUNET_TESTING_peer_run ("test-ats-solver", | ||
449 | config_file, &run, NULL )) | ||
450 | return GNUNET_SYSERR; | ||
451 | |||
452 | return ret; | ||
453 | } | ||
454 | |||
455 | /* end of file test_ats_solver_alternative_after_delete_address.c */ | ||