aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_address_switch.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/transport/test_transport_address_switch.c')
-rw-r--r--src/transport/test_transport_address_switch.c677
1 files changed, 212 insertions, 465 deletions
diff --git a/src/transport/test_transport_address_switch.c b/src/transport/test_transport_address_switch.c
index a64dd6172..96e11b7de 100644
--- a/src/transport/test_transport_address_switch.c
+++ b/src/transport/test_transport_address_switch.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010, 2011 GNUnet e.V. 3 Copyright (C) 2009, 2010, 2011, 2016 GNUnet e.V.
4 4
5 GNUnet is free software; you can redistribute it and/or modify 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 6 it under the terms of the GNU General Public License as published
@@ -33,19 +33,20 @@
33 * addresses in connected state and a test message was successfully transmitted 33 * addresses in connected state and a test message was successfully transmitted
34 * after this switch. 34 * after this switch.
35 * 35 *
36 * Since it is not possible to trigger an address switch from 36 * Since it is not possible to trigger an address switch from outside,
37 * outside, the test still passes when no address switching attempt takes 37 * the test returns "77" (skipped) when no address switching attempt
38 * place. It fails if an address switch attempt fails. 38 * takes place. It fails if an address switch attempt fails.
39 *
40 * NOTE: The test seems largely useless right now, as we simply NEVER
41 * switch addresses under the test conditions. However, it may be a
42 * good starting point for a future test. For now, it always times
43 * out and returns "77" (skipped), so we set the timeout suitably low.
39 */ 44 */
40#include "platform.h" 45#include "platform.h"
41#include "gnunet_transport_service.h" 46#include "gnunet_transport_service.h"
42#include "gnunet_ats_service.h" 47#include "gnunet_ats_service.h"
43#include "gauger.h"
44#include "transport-testing.h" 48#include "transport-testing.h"
45 49
46/*
47 * Testcase specific declarations
48 */
49 50
50GNUNET_NETWORK_STRUCT_BEGIN 51GNUNET_NETWORK_STRUCT_BEGIN
51struct TestMessage 52struct TestMessage
@@ -68,84 +69,42 @@ GNUNET_NETWORK_STRUCT_END
68/** 69/**
69 * Testcase timeout 70 * Testcase timeout
70 */ 71 */
71#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120) 72#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
72 73
73/** 74/**
74 * How long until we give up on transmitting the message? 75 * How long until we give up on transmitting the message?
75 */ 76 */
76#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) 77#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
77 78
78#define DURATION GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
79 79
80static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
80 81
81/**
82 * Timeout task to send messages
83 */
84static struct GNUNET_SCHEDULER_Task *die_task;
85
86
87static struct GNUNET_SCHEDULER_Task *delayed_end_task;
88
89/**
90 * Measurement task to send messages
91 */
92static struct GNUNET_SCHEDULER_Task *measure_task; 82static struct GNUNET_SCHEDULER_Task *measure_task;
93 83
94
95static struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
96static char *cfg_file_p1;
97static struct GNUNET_STATISTICS_Handle *p1_stat;
98
99static struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
100static char *cfg_file_p2;
101static struct GNUNET_STATISTICS_Handle *p2_stat;
102
103static struct GNUNET_TRANSPORT_TESTING_PeerContext *sender;
104
105static struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver;
106
107static struct GNUNET_TRANSPORT_TransmitHandle *th; 84static struct GNUNET_TRANSPORT_TransmitHandle *th;
108 85
109static struct GNUNET_TRANSPORT_TESTING_Handle *tth;
110
111static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
112
113static int test_connected;
114
115static int res;
116
117
118/** 86/**
119 * Statistics about peer 1 87 * Statistics we track per peer.
120 */ 88 */
121static unsigned int p1_addresses_avail; 89struct PeerStats
122static unsigned int p1_switch_attempts; 90{
123static unsigned int p1_switch_success; 91 struct GNUNET_STATISTICS_Handle *stat;
124static unsigned int p1_switch_fail;
125 92
93 unsigned int addresses_avail;
126 94
127/** 95 unsigned int switch_attempts;
128 * Statistics about peer 2
129 */
130static unsigned int p2_switch_attempts;
131static unsigned int p2_switch_success;
132static unsigned int p2_switch_fail;
133static unsigned int p2_addresses_avail;
134 96
135/** 97 unsigned int switch_success;
136 * Transmission statistics 98
137 */ 99 unsigned int switch_fail;
100};
101
102static struct PeerStats stats[2];
138 103
139/* Amount of data transfered since last switch attempt */ 104/* Amount of data transfered since last switch attempt */
140static unsigned long long bytes_sent_after_switch; 105static unsigned long long bytes_sent_after_switch;
141static unsigned long long bytes_recv_after_switch;
142
143 106
144#if VERBOSE 107static unsigned long long bytes_recv_after_switch;
145#define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
146#else
147#define OKPP do { ok++; } while (0)
148#endif
149 108
150 109
151static int 110static int
@@ -155,17 +114,12 @@ stat_start_attempt_cb (void *cls,
155 uint64_t value, 114 uint64_t value,
156 int is_persistent) 115 int is_persistent)
157{ 116{
158 if (cls == p1) 117 struct PeerStats *stat = cls;
159 {
160 p1_switch_attempts++;
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(1:s)");
162 }
163 else if (cls == p2)
164 {
165 p2_switch_attempts++;
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(2:s)");
167 }
168 118
119 stat->switch_attempts++;
120 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
121 "Switch attempted (%p)",
122 stat);
169 bytes_recv_after_switch = 0; 123 bytes_recv_after_switch = 0;
170 bytes_sent_after_switch = 0; 124 bytes_sent_after_switch = 0;
171 125
@@ -180,17 +134,12 @@ stat_success_attempt_cb (void *cls,
180 uint64_t value, 134 uint64_t value,
181 int is_persistent) 135 int is_persistent)
182{ 136{
183 if (cls == p1) 137 struct PeerStats *stat = cls;
184 {
185 p1_switch_success++;
186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(1:+)");
187 }
188 if (cls == p2)
189 {
190 p2_switch_success++;
191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(2:+)");
192 }
193 138
139 stat->switch_success++;
140 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
141 "Switch succeeded (%p)",
142 stat);
194 return GNUNET_OK; 143 return GNUNET_OK;
195} 144}
196 145
@@ -202,20 +151,15 @@ stat_fail_attempt_cb (void *cls,
202 uint64_t value, 151 uint64_t value,
203 int is_persistent) 152 int is_persistent)
204{ 153{
154 struct PeerStats *stat = cls;
155
205 if (value == 0) 156 if (value == 0)
206 return GNUNET_OK; 157 return GNUNET_OK;
207 158
208 if (cls == p1) 159 stat->switch_fail++;
209 { 160 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
210 p1_switch_fail++; 161 "Switch failed (%p)",
211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(1:-)"); 162 stat);
212 }
213 if (cls == p2)
214 {
215 p2_switch_fail++;
216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "(2:-)");
217 }
218
219 return GNUNET_OK; 163 return GNUNET_OK;
220} 164}
221 165
@@ -227,206 +171,148 @@ stat_addresses_available (void *cls,
227 uint64_t value, 171 uint64_t value,
228 int is_persistent) 172 int is_persistent)
229{ 173{
230 if (cls == p1) 174 struct PeerStats *stat = cls;
231 { 175
232 p1_addresses_avail++; 176 stat->addresses_avail++;
233 }
234 if (cls == p2)
235 {
236 p2_addresses_avail++;
237 }
238 return GNUNET_OK; 177 return GNUNET_OK;
239} 178}
240 179
241 180
181/**
182 * List of statistics entries we care about.
183 */
184static struct WatchEntry {
185
186 /**
187 * Name of the statistic we watch.
188 */
189 const char *stat_name;
190
191 /**
192 * Handler to register;
193 */
194 GNUNET_STATISTICS_Iterator stat_handler;
195} watches[] = {
196 { "# Attempts to switch addresses", &stat_start_attempt_cb },
197 { "# Successful attempts to switch addresses", &stat_success_attempt_cb },
198 { "# Failed attempts to switch addresses (failed to send CONNECT CONT)", &stat_fail_attempt_cb },
199 { "# Failed attempts to switch addresses (failed to send CONNECT)", &stat_fail_attempt_cb },
200 { "# Failed attempts to switch addresses (no response)", &stat_fail_attempt_cb },
201 { "# transport addresses", &stat_addresses_available },
202 { NULL, NULL }
203};
204
205
242static void 206static void
243clean_up () 207custom_shutdown (void *cls)
244{ 208{
245 if (measure_task != NULL) 209 int result;
210
211 if (NULL != measure_task)
246 { 212 {
247 GNUNET_SCHEDULER_cancel (measure_task); 213 GNUNET_SCHEDULER_cancel (measure_task);
248 measure_task = NULL; 214 measure_task = NULL;
249 } 215 }
250 216 if (0 == stats[0].switch_attempts + stats[1].switch_attempts)
251 if (delayed_end_task != NULL)
252 { 217 {
253 GNUNET_SCHEDULER_cancel (delayed_end_task); 218 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
254 delayed_end_task = NULL; 219 "Test did not work, as peers didn't switch (flawed testcase)!\n");
220 ccc->global_ret = 77;
255 } 221 }
256 222 else
257 if (die_task != NULL)
258 { 223 {
259 GNUNET_SCHEDULER_cancel (die_task); 224 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 die_task = NULL; 225 "Fail (timeout)! No transmission after switch! Stopping peers\n");
226 ccc->global_ret = GNUNET_SYSERR;
261 } 227 }
262 228
263 if (NULL != p1_stat) 229 /* stop statistics */
264 { 230 for (unsigned int i=0;i<2;i++)
265 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
266 "# Attempts to switch addresses",
267 stat_start_attempt_cb, p1);
268 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
269 "# Successful attempts to switch addresses",
270 stat_success_attempt_cb, p1);
271 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
272 "# Failed attempts to switch addresses (failed to send CONNECT CONT)",
273 stat_fail_attempt_cb, p1);
274 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
275 "# Failed attempts to switch addresses (failed to send CONNECT)",
276 stat_fail_attempt_cb, p1);
277 GNUNET_STATISTICS_watch_cancel (p1_stat, "transport",
278 "# Failed attempts to switch addresses (no response)",
279 stat_fail_attempt_cb, p1);
280 GNUNET_STATISTICS_watch (p1_stat, "transport",
281 "# transport addresses",
282 stat_addresses_available, p1);
283 GNUNET_STATISTICS_destroy (p1_stat, GNUNET_NO);
284 p1_stat = NULL;
285 }
286 if (NULL != p2_stat)
287 { 231 {
288 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport", 232 if (NULL != stats[i].stat)
289 "# Attempts to switch addresses", stat_start_attempt_cb, p2); 233 {
290 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport", 234 for (unsigned int j=0;NULL != watches[j].stat_name; j++)
291 "# Successful attempts to switch addresses", stat_success_attempt_cb, p2); 235 GNUNET_STATISTICS_watch_cancel (stats[i].stat,
292 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport", 236 "transport",
293 "# Failed attempts to switch addresses (failed to send CONNECT CONT)", 237 watches[j].stat_name,
294 stat_fail_attempt_cb, p2); 238 watches[j].stat_handler,
295 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport", 239 &stats[i]);
296 "# Failed attempts to switch addresses (failed to send CONNECT)", 240
297 stat_fail_attempt_cb, p2); 241 GNUNET_STATISTICS_destroy (stats[i].stat,
298 GNUNET_STATISTICS_watch_cancel (p2_stat, "transport", 242 GNUNET_NO);
299 "# Failed attempts to switch addresses (no response)", 243 stats[i].stat = NULL;
300 stat_fail_attempt_cb, p2); 244 }
301 GNUNET_STATISTICS_watch (p2_stat, "transport",
302 "# transport addresses",
303 stat_addresses_available, p2);
304 GNUNET_STATISTICS_destroy (p2_stat, GNUNET_NO);
305 p2_stat = NULL;
306 } 245 }
307 246 if (NULL != th)
308 if (th != NULL)
309 { 247 {
310 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); 248 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
311 th = NULL; 249 th = NULL;
312 } 250 }
313 if (cc != NULL)
314 {
315 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
316 cc = NULL;
317 }
318 if (p1 != NULL)
319 {
320 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
321 p1 = NULL;
322 }
323 if (p2 != NULL)
324 {
325 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
326 p2 = NULL;
327 }
328}
329 251
330 252 result = 0;
331static void
332end ()
333{
334 int result = 0;
335
336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
337 "Stopping peers\n");
338
339 delayed_end_task = NULL;
340 FPRINTF (stderr, "\n"); 253 FPRINTF (stderr, "\n");
341 if (p1_switch_attempts > 0) 254 if (stats[0].switch_attempts > 0)
342 { 255 {
343 FPRINTF (stderr, 256 FPRINTF (stderr,
344 "Peer 1 tried %u times to switch and succeeded %u times, failed %u times\n", 257 "Peer 1 tried %u times to switch and succeeded %u times, failed %u times\n",
345 p1_switch_attempts, 258 stats[0].switch_attempts,
346 p1_switch_success, 259 stats[0].switch_success,
347 p1_switch_fail); 260 stats[0].switch_fail);
348 if (p1_switch_success != p1_switch_attempts) 261 if (stats[0].switch_success != stats[0].switch_attempts)
349 { 262 {
350 GNUNET_break (0); 263 GNUNET_break (0);
351 result ++; 264 result ++;
352 } 265 }
353 } 266 }
354 else if (p1_addresses_avail > 1) 267 else if (stats[0].addresses_avail > 1)
355 { 268 {
356 FPRINTF (stderr, 269 FPRINTF (stderr,
357 "Peer 1 had %u addresses available, but did not try to switch\n", 270 "Peer 1 had %u addresses available, but did not try to switch\n",
358 p1_addresses_avail); 271 stats[0].addresses_avail);
359 } 272 }
360 if (p2_switch_attempts > 0) 273 if (stats[1].switch_attempts > 0)
361 { 274 {
362 FPRINTF (stderr, 275 FPRINTF (stderr,
363 "Peer 2 tried %u times to switch and succeeded %u times, failed %u times\n", 276 "Peer 2 tried %u times to switch and succeeded %u times, failed %u times\n",
364 p2_switch_attempts, 277 stats[1].switch_attempts,
365 p2_switch_success, 278 stats[1].switch_success,
366 p2_switch_fail); 279 stats[1].switch_fail);
367 if (p2_switch_success != p2_switch_attempts) 280 if (stats[1].switch_success != stats[1].switch_attempts)
368 { 281 {
369 GNUNET_break (0); 282 GNUNET_break (0);
370 result++; 283 result++;
371 } 284 }
372 } 285 }
373 else if (p2_addresses_avail > 1) 286 else if (stats[1].addresses_avail > 1)
374 { 287 {
375 FPRINTF (stderr, 288 FPRINTF (stderr,
376 "Peer 2 had %u addresses available, but did not try to switch\n", 289 "Peer 2 had %u addresses available, but did not try to switch\n",
377 p2_addresses_avail); 290 stats[1].addresses_avail);
378 } 291 }
379 292
380 if ( ((p1_switch_attempts > 0) || (p2_switch_attempts > 0)) && 293 if ( ((stats[0].switch_attempts > 0) || (stats[1].switch_attempts > 0)) &&
381 (bytes_sent_after_switch == 0) ) 294 (bytes_sent_after_switch == 0) )
382 { 295 {
383 FPRINTF (stderr, "No data sent after switching!\n"); 296 FPRINTF (stderr, "No data sent after switching!\n");
384 GNUNET_break (0); 297 GNUNET_break (0);
385 res++; 298 result++;
386 } 299 }
387 if ( ((p1_switch_attempts > 0) || (p2_switch_attempts > 0)) && 300 if ( ((stats[0].switch_attempts > 0) || (stats[1].switch_attempts > 0)) &&
388 (bytes_recv_after_switch == 0) ) 301 (bytes_recv_after_switch == 0) )
389 { 302 {
390 FPRINTF (stderr, "No data received after switching!\n"); 303 FPRINTF (stderr, "No data received after switching!\n");
391 GNUNET_break (0); 304 GNUNET_break (0);
392 res++; 305 result++;
393 } 306 }
394 307 if (0 != result)
395 clean_up(); 308 ccc->global_ret = GNUNET_SYSERR;
396
397 res = result;
398}
399
400
401static void
402end_badly ()
403{
404 die_task = NULL;
405 clean_up();
406 if (0 == p1_switch_attempts + p2_switch_attempts)
407 {
408 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
409 "Test did not work, as peers didn't switch (flawed testcase)!\n");
410 res = 0;
411 }
412 else
413 {
414 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
415 "Fail (timeout)! No transmission after switch! Stopping peers\n");
416 res = GNUNET_YES;
417 }
418 if (test_connected == GNUNET_YES)
419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
420 "Peers got connected\n");
421 else
422 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
423 "Peers got NOT EVEN connected\n");
424} 309}
425 310
426 311
427static void 312static void
428notify_receive (void *cls, 313notify_receive (void *cls,
429 const struct GNUNET_PeerIdentity *peer, 314 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
315 const struct GNUNET_PeerIdentity *sender,
430 const struct GNUNET_MessageHeader *message) 316 const struct GNUNET_MessageHeader *message)
431{ 317{
432 const struct TestMessage *hdr; 318 const struct TestMessage *hdr;
@@ -435,28 +321,30 @@ notify_receive (void *cls,
435 if (MTYPE != ntohs (message->type)) 321 if (MTYPE != ntohs (message->type))
436 return; 322 return;
437 323
438 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; 324 {
439 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); 325 char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
440 326
441 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 327 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
442 "Peer %u (`%s') got message %u of size %u from peer (`%s')\n", p->no, ps, 328 "Peer %u (`%s') got message %u of size %u from peer (`%s')\n",
443 ntohl (hdr->num), 329 receiver->no,
444 ntohs (message->size), 330 ps,
445 GNUNET_i2s (peer)); 331 ntohl (hdr->num),
446 if ( ((p1_switch_attempts >= 1) || (p2_switch_attempts >= 1)) && 332 ntohs (message->size),
447 (p1_switch_attempts == p1_switch_fail + p1_switch_success) && 333 GNUNET_i2s (sender));
448 (p2_switch_attempts == p2_switch_fail + p2_switch_success) ) 334 GNUNET_free (ps);
335 }
336 if ( ((stats[0].switch_attempts >= 1) || (stats[1].switch_attempts >= 1)) &&
337 (stats[0].switch_attempts == stats[0].switch_fail + stats[0].switch_success) &&
338 (stats[1].switch_attempts == stats[1].switch_fail + stats[1].switch_success) )
449 { 339 {
450 bytes_recv_after_switch += ntohs(hdr->header.size); 340 bytes_recv_after_switch += ntohs(hdr->header.size);
451 if ((bytes_sent_after_switch > 0) && (bytes_recv_after_switch > 0)) 341 if ((bytes_sent_after_switch > 0) && (bytes_recv_after_switch > 0))
452 { 342 {
453 /* A peer switched addresses and sent and received data after the 343 /* A peer switched addresses and sent and received data after the
454 * switch operations */ 344 * switch operations */
455 end (); 345 GNUNET_SCHEDULER_shutdown ();
456 } 346 }
457 } 347 }
458
459 GNUNET_free(ps);
460} 348}
461 349
462 350
@@ -472,10 +360,8 @@ notify_ready (void *cls, size_t size, void *buf)
472 { 360 {
473 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
474 "Timeout occurred while waiting for transmit_ready for message\n"); 362 "Timeout occurred while waiting for transmit_ready for message\n");
475 if (NULL != die_task) 363 ccc->global_ret = GNUNET_SYSERR;
476 GNUNET_SCHEDULER_cancel (die_task); 364 GNUNET_SCHEDULER_shutdown ();
477 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
478 res = 1;
479 return 0; 365 return 0;
480 } 366 }
481 367
@@ -489,27 +375,32 @@ notify_ready (void *cls, size_t size, void *buf)
489 GNUNET_memcpy (&cbuf[0], &hdr, sizeof(struct TestMessage)); 375 GNUNET_memcpy (&cbuf[0], &hdr, sizeof(struct TestMessage));
490 memset (&cbuf[sizeof(struct TestMessage)], '0', MSIZE - sizeof(struct TestMessage)); 376 memset (&cbuf[sizeof(struct TestMessage)], '0', MSIZE - sizeof(struct TestMessage));
491 377
492 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&receiver->id)); 378 {
493 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 379 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
494 "Sending message %u of size %u from peer %u (`%4s') -> peer %u (`%s') !\n", 380
495 (unsigned int) (counter - 1), 381 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
496 MSIZE, 382 "Sending message %u of size %u from peer %u (`%4s') -> peer %u (`%s') !\n",
497 sender->no, 383 (unsigned int) (counter - 1),
498 GNUNET_i2s (&sender->id), 384 MSIZE,
499 receiver->no, 385 ccc->p[1]->no,
500 receiver_s); 386 GNUNET_i2s (&ccc->p[1]->id),
501 GNUNET_free(receiver_s); 387 ccc->p[0]->no,
388 receiver_s);
389 GNUNET_free(receiver_s);
390 }
502 391
503 if (th == NULL) 392 if (th == NULL)
504 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, 393 th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
505 &p1->id, 394 &ccc->p[0]->id,
506 MSIZE, 395 MSIZE,
507 TIMEOUT_TRANSMIT, 396 TIMEOUT_TRANSMIT,
508 &notify_ready, NULL); 397 &notify_ready,
398 NULL);
509 399
510 if ( ((p1_switch_attempts >= 1) || (p2_switch_attempts >= 1)) && 400 if ( ( (stats[0].switch_attempts >= 1) ||
511 (p1_switch_attempts == p1_switch_fail + p1_switch_success) && 401 (stats[1].switch_attempts >= 1) ) &&
512 (p2_switch_attempts == p2_switch_fail + p2_switch_success) ) 402 (stats[0].switch_attempts == stats[0].switch_fail + stats[0].switch_success) &&
403 (stats[1].switch_attempts == stats[1].switch_fail + stats[1].switch_success) )
513 { 404 {
514 bytes_sent_after_switch += MSIZE; 405 bytes_sent_after_switch += MSIZE;
515 } 406 }
@@ -518,32 +409,10 @@ notify_ready (void *cls, size_t size, void *buf)
518 409
519 410
520static void 411static void
521notify_connect (void *cls,
522 const struct GNUNET_PeerIdentity *peer)
523{
524 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
525
526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
527 "Peer %u (`%4s') connected to us!\n",
528 p->no,
529 GNUNET_i2s (peer));
530}
531
532
533static void
534notify_disconnect (void *cls, 412notify_disconnect (void *cls,
535 const struct GNUNET_PeerIdentity *peer) 413 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
414 const struct GNUNET_PeerIdentity *other)
536{ 415{
537 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
538
539 if (NULL != p1)
540 {
541 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
542 "Peer %u (`%4s') disconnected early!\n",
543 p->no,
544 GNUNET_i2s (peer));
545 GNUNET_SCHEDULER_shutdown ();
546 }
547 if (NULL != th) 416 if (NULL != th)
548 { 417 {
549 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); 418 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
@@ -553,24 +422,13 @@ notify_disconnect (void *cls,
553 422
554 423
555static void 424static void
556sendtask ()
557{
558 /* Transmit test messages */
559 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
560 &p1->id, MSIZE,
561 TIMEOUT_TRANSMIT,
562 &notify_ready, NULL);
563}
564
565
566static void
567progress_indicator (void *cls) 425progress_indicator (void *cls)
568{ 426{
569 static int counter; 427 static int counter;
570 428
571 measure_task = NULL; 429 measure_task = NULL;
572 counter++; 430 counter++;
573 if ((DURATION.rel_value_us / 1000 / 1000LL) < counter) 431 if ((TIMEOUT.rel_value_us / 1000 / 1000LL) < counter)
574 { 432 {
575 FPRINTF (stderr, "%s", ".\n"); 433 FPRINTF (stderr, "%s", ".\n");
576 } 434 }
@@ -578,184 +436,73 @@ progress_indicator (void *cls)
578 { 436 {
579 FPRINTF (stderr, "%s", "."); 437 FPRINTF (stderr, "%s", ".");
580 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 438 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
581 &progress_indicator, NULL); 439 &progress_indicator,
440 NULL);
582 } 441 }
583} 442}
584 443
585 444
586static void 445static void
587testing_connect_cb (void *cls) 446connected_cb (void *cls)
588{ 447{
589 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id)); 448 for (unsigned int i=0;i<2;i++)
590 449 {
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 450 stats[i].stat = GNUNET_STATISTICS_create ("transport",
592 "Peers connected: %u (%s) <-> %u (%s)\n", 451 ccc->p[i]->cfg);
593 p1->no, p1_c, p2->no, 452 if (NULL == stats[i].stat)
594 GNUNET_i2s (&p2->id)); 453 {
595 GNUNET_free (p1_c); 454 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
596 455 "Fail! Could not create statistics for peers!\n");
597 cc = NULL; 456 ccc->global_ret = GNUNET_SYSERR;
598 test_connected = GNUNET_YES; 457 GNUNET_SCHEDULER_shutdown ();
599 458 return;
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 459 }
601 "(i:s/+/-) \t i == peer 1/2, s/+/- : switch attempt/switch ok/switch fail\n"); 460 for (unsigned int j=0;NULL != watches[j].stat_name; j++)
602 461 {
462 GNUNET_STATISTICS_watch (stats[i].stat,
463 "transport",
464 watches[j].stat_name,
465 watches[j].stat_handler,
466 &stats[i]);
467 }
468 }
603 /* Show progress */ 469 /* Show progress */
470 ccc->global_ret = GNUNET_OK;
604 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 471 measure_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
605 &progress_indicator, 472 &progress_indicator,
606 NULL); 473 NULL);
607 /* Peers are connected, start transmit test messages */ 474 /* Peers are connected, start transmit test messages */
608 GNUNET_SCHEDULER_add_now (&sendtask, NULL); 475 th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
609} 476 &ccc->p[0]->id, MSIZE,
610 477 TIMEOUT_TRANSMIT,
611 478 &notify_ready, NULL);
612static void
613start_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p, void *cls)
614{
615 static int started;
616 started++;
617
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "Peer %u (`%s') started\n",
620 p->no,
621 GNUNET_i2s (&p->id));
622 if (started != 2)
623 return;
624 479
625 test_connected = GNUNET_NO;
626 sender = p2;
627 receiver = p1;
628
629 char *sender_c = GNUNET_strdup (GNUNET_i2s (&sender->id));
630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
631 "Test tries to send from %u (%s) -> peer %u (%s)\n",
632 sender->no,
633 sender_c,
634 receiver->no,
635 GNUNET_i2s (&receiver->id));
636 GNUNET_free (sender_c);
637
638 /* Connect the peers */
639 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
640 p2,
641 &testing_connect_cb,
642 NULL);
643} 480}
644 481
645 482
646static void
647run (void *cls,
648 char * const *args,
649 const char *cfgfile,
650 const struct GNUNET_CONFIGURATION_Handle *cfg)
651{
652 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
653
654 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
655 &notify_receive, &notify_connect, &notify_disconnect, &start_cb, NULL);
656
657 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
658 &notify_receive, &notify_connect, &notify_disconnect, &start_cb, NULL);
659
660 if ((p1 == NULL )|| (p2 == NULL))
661 {
662 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
663 GNUNET_SCHEDULER_shutdown ();
664 return;
665 }
666
667 /* Start to watch statistics for peer 1 */
668 p1_stat = GNUNET_STATISTICS_create ("transport", p1->cfg);
669 GNUNET_STATISTICS_watch (p1_stat, "transport",
670 "# Attempts to switch addresses",
671 stat_start_attempt_cb, p1);
672 GNUNET_STATISTICS_watch (p1_stat, "transport",
673 "# Successful attempts to switch addresses",
674 stat_success_attempt_cb, p1);
675 GNUNET_STATISTICS_watch (p1_stat, "transport",
676 "# Failed attempts to switch addresses (failed to send CONNECT CONT)",
677 stat_fail_attempt_cb, p1);
678 GNUNET_STATISTICS_watch (p1_stat, "transport",
679 "# Failed attempts to switch addresses (failed to send CONNECT)",
680 stat_fail_attempt_cb, p1);
681 GNUNET_STATISTICS_watch (p1_stat, "transport",
682 "# Failed attempts to switch addresses (no response)",
683 stat_fail_attempt_cb, p1);
684 GNUNET_STATISTICS_watch (p1_stat, "transport",
685 "# transport addresses",
686 stat_addresses_available, p1);
687
688 /* Start to watch statistics for peer 2 */
689 p2_stat = GNUNET_STATISTICS_create ("transport", p2->cfg);
690 GNUNET_STATISTICS_watch (p2_stat, "transport",
691 "# Attempts to switch addresses",
692 stat_start_attempt_cb, p2);
693 GNUNET_STATISTICS_watch (p2_stat, "transport",
694 "# Successful attempts to switch addresses",
695 stat_success_attempt_cb, p2);
696 GNUNET_STATISTICS_watch (p2_stat, "transport",
697 "# Failed attempts to switch addresses (failed to send CONNECT CONT)",
698 stat_fail_attempt_cb, p2);
699 GNUNET_STATISTICS_watch (p2_stat, "transport",
700 "# Failed attempts to switch addresses (failed to send CONNECT)",
701 stat_fail_attempt_cb, p2);
702 GNUNET_STATISTICS_watch (p2_stat, "transport",
703 "# Failed attempts to switch addresses (no response)",
704 stat_fail_attempt_cb, p2);
705 GNUNET_STATISTICS_watch (p2_stat, "transport",
706 "# transport addresses",
707 stat_addresses_available, p2);
708
709 if ((p1_stat == NULL )|| (p2_stat == NULL))
710 {
711 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
712 "Fail! Could not create statistics for peers!\n");
713 GNUNET_SCHEDULER_shutdown ();
714 return;
715 }
716}
717
718int 483int
719main (int argc, char *argv[]) 484main (int argc,
485 char *argv[])
720{ 486{
721 char *test_plugin; 487 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
722 char *test_source; 488 .connect_continuation = &connected_cb,
723 char *test_name; 489 .config_file = "test_transport_startonly.conf",
724 490 .rec = &notify_receive,
725 static char *argv_new[] = { "test-transport-address-switch", "-c", 491 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
726 "test_transport_startonly.conf", NULL }; 492 .nd = &notify_disconnect,
727 493 .shutdown_task = &custom_shutdown,
728 static struct GNUNET_GETOPT_CommandLineOption options[] = { 494 .timeout = TIMEOUT
729 GNUNET_GETOPT_OPTION_END }; 495 };
730 496 ccc = &my_ccc;
731 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv[0]); 497 int ret;
732 498
733 GNUNET_log_setup (test_name, "WARNING", NULL ); 499 ret = GNUNET_TRANSPORT_TESTING_main (2,
734 500 &GNUNET_TRANSPORT_TESTING_connect_check,
735 test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__); 501 ccc);
736 test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], 502 if (77 == ret)
737 test_source); 503 return 77;
738 504 if (GNUNET_OK != ret)
739 tth = GNUNET_TRANSPORT_TESTING_init (); 505 return 1;
740 506 return 0;
741 cfg_file_p1 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 1);
742 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Using cfg [%u] : %s \n", 1, cfg_file_p1);
743 cfg_file_p2 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 2);
744 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Using cfg [%u] : %s \n", 2, cfg_file_p2);
745
746 GNUNET_PROGRAM_run ((sizeof(argv_new) / sizeof(char *)) - 1, argv_new,
747 test_name, "nohelp", options, &run, NULL );
748
749 GNUNET_free(cfg_file_p1);
750 GNUNET_free(cfg_file_p2);
751
752 GNUNET_free(test_source);
753 GNUNET_free(test_plugin);
754 GNUNET_free(test_name);
755
756 GNUNET_TRANSPORT_TESTING_done (tth);
757
758 return res;
759} 507}
760
761/* end of test_transport_address_switch.c */ 508/* end of test_transport_address_switch.c */