aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2016-07-10 17:34:25 +0000
committerChristian Grothoff <christian@grothoff.org>2016-07-10 17:34:25 +0000
commit611a879420bf940dff9039305314489586776896 (patch)
tree9ee98beec16d2df7841a850280165487b9036e65 /src
parent8f62c823dfb8150c3bc970932d042be2feb83e25 (diff)
downloadgnunet-611a879420bf940dff9039305314489586776896.tar.gz
gnunet-611a879420bf940dff9039305314489586776896.zip
modify manipulation_recv_tcp test to use new transport-testing lib
Diffstat (limited to 'src')
-rw-r--r--src/transport/test_transport_api_manipulation_recv_tcp.c427
1 files changed, 91 insertions, 336 deletions
diff --git a/src/transport/test_transport_api_manipulation_recv_tcp.c b/src/transport/test_transport_api_manipulation_recv_tcp.c
index 03aced539..023d5a714 100644
--- a/src/transport/test_transport_api_manipulation_recv_tcp.c
+++ b/src/transport/test_transport_api_manipulation_recv_tcp.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 GNUnet e.V. 3 Copyright (C) 2009, 2010, 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
@@ -45,116 +45,37 @@
45 45
46#define TEST_MESSAGE_TYPE 12345 46#define TEST_MESSAGE_TYPE 12345
47 47
48static char *test_source;
49 48
50static char *test_plugin; 49static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
51
52static char *test_name;
53
54static int ok;
55
56static int s_started;
57
58static int s_connected;
59
60static int s_sending;
61
62static struct GNUNET_SCHEDULER_Task * die_task;
63
64static struct GNUNET_SCHEDULER_Task * send_task;
65
66static struct GNUNET_TRANSPORT_TESTING_PeerContext *p1;
67
68static struct GNUNET_TRANSPORT_TESTING_PeerContext *p2;
69
70static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
71 50
72static struct GNUNET_TRANSPORT_TransmitHandle *th; 51static struct GNUNET_TRANSPORT_TransmitHandle *th;
73 52
74static struct GNUNET_TRANSPORT_TESTING_Handle *tth; 53static struct GNUNET_SCHEDULER_Task *send_task;
75
76static char *cfg_file_p1;
77
78static char *cfg_file_p2;
79 54
80static int messages_recv; 55static int messages_recv;
81 56
82static struct GNUNET_TIME_Absolute start_normal; 57static struct GNUNET_TIME_Absolute start_normal;
58
83static struct GNUNET_TIME_Relative dur_normal; 59static struct GNUNET_TIME_Relative dur_normal;
84 60
85static struct GNUNET_TIME_Absolute start_delayed; 61static struct GNUNET_TIME_Absolute start_delayed;
86static struct GNUNET_TIME_Relative dur_delayed;
87
88static void
89end ()
90{
91 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
92
93 if (send_task != NULL)
94 GNUNET_SCHEDULER_cancel (send_task);
95 62
96 if (die_task != NULL) 63static struct GNUNET_TIME_Relative dur_delayed;
97 GNUNET_SCHEDULER_cancel (die_task);
98
99 if (th != NULL)
100 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
101 th = NULL;
102
103 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
104 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
105}
106 64
107 65
108static void 66static void
109end_badly (void *cls) 67custom_shutdown (void *cls)
110{ 68{
111 die_task = NULL; 69 if (NULL != th)
112
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
114
115
116 if (send_task != NULL)
117 GNUNET_SCHEDULER_cancel (send_task);
118
119 if (cc != NULL)
120 { 70 {
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
122 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (cc);
123 cc = NULL;
124 }
125
126 if (th != NULL)
127 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); 71 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
128 else 72 th = NULL;
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n")); 73 }
130 74 if (NULL != send_task)
131 if (s_started == GNUNET_NO) 75 {
132 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n")); 76 GNUNET_SCHEDULER_cancel (send_task);
133 else 77 send_task = NULL;
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n")); 78 }
135
136 if (s_connected == GNUNET_NO)
137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
138 else
139 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
140
141 if (s_sending == GNUNET_NO)
142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
143 else
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
145
146 th = NULL;
147
148 if (p1 != NULL)
149 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
150 else
151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
152 if (p2 != NULL)
153 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
154 else
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
156
157 ok = GNUNET_SYSERR;
158} 79}
159 80
160 81
@@ -163,37 +84,34 @@ sendtask (void *cls);
163 84
164 85
165static void 86static void
166notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer, 87notify_receive (void *cls,
88 struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
89 const struct GNUNET_PeerIdentity *sender,
167 const struct GNUNET_MessageHeader *message) 90 const struct GNUNET_MessageHeader *message)
168{ 91{
169 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; 92 {
170 struct GNUNET_TRANSPORT_TESTING_PeerContext *t = NULL; 93 char *ps = GNUNET_strdup (GNUNET_i2s (&receiver->id));
171
172 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
173 t = p1;
174 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
175 t = p2;
176 GNUNET_assert (t != NULL);
177
178 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
179
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
182 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
183 GNUNET_i2s (&t->id));
184 GNUNET_free (ps);
185 94
95 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
96 "Peer %u (`%s') received message of type %d and size %u size from peer %s)!\n",
97 receiver->no,
98 ps,
99 ntohs (message->type),
100 ntohs (message->size),
101 GNUNET_i2s (sender));
102 GNUNET_free (ps);
103 }
186 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) && 104 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
187 (TEST_MESSAGE_SIZE == ntohs (message->size))) 105 (TEST_MESSAGE_SIZE == ntohs (message->size)))
188 { 106 {
189 ok = 0; 107 if (GNUNET_NO == ccc->global_ret)
190 108 ccc->global_ret = GNUNET_OK;
191 } 109 }
192 else 110 else
193 { 111 {
194 GNUNET_break (0); 112 GNUNET_break (0);
195 ok = 1; 113 ccc->global_ret = GNUNET_SYSERR;
196 end (); 114 GNUNET_SCHEDULER_shutdown ();
197 return; 115 return;
198 } 116 }
199 117
@@ -213,8 +131,8 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
213 delay.rel_value_us = 1000 * 1000LL; 131 delay.rel_value_us = 1000 * 1000LL;
214 memset (&prop, 0, sizeof (prop)); 132 memset (&prop, 0, sizeof (prop));
215 prop.distance = 10; 133 prop.distance = 10;
216 GNUNET_TRANSPORT_set_traffic_metric (p1->th, 134 GNUNET_TRANSPORT_set_traffic_metric (ccc->p[0]->th,
217 &p2->id, 135 &ccc->p[1]->id,
218 &prop, 136 &prop,
219 delay, 137 delay,
220 GNUNET_TIME_UNIT_ZERO); 138 GNUNET_TIME_UNIT_ZERO);
@@ -223,26 +141,25 @@ notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
223 if (2 == messages_recv) 141 if (2 == messages_recv)
224 { 142 {
225 /* Received manipulated message */ 143 /* Received manipulated message */
226 dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed); 144 dur_delayed = GNUNET_TIME_absolute_get_duration(start_delayed);
227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228 "Received delayed message %u after %s\n", 146 "Received delayed message %u after %s\n",
229 messages_recv, 147 messages_recv,
230 GNUNET_STRINGS_relative_time_to_string (dur_delayed, 148 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
231 GNUNET_YES)); 149 GNUNET_YES));
232 if (dur_delayed.rel_value_us < 1000 * 1000LL) 150 if (dur_delayed.rel_value_us < GNUNET_TIME_UNIT_SECONDS.rel_value_us)
233 { 151 {
234 GNUNET_break (0); 152 GNUNET_break (0);
235 ok += 1; 153 ccc->global_ret = GNUNET_SYSERR;
236 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
237 "Delayed message was not delayed correctly: took only %s\n", 155 "Delayed message was not delayed correctly: took only %s\n",
238 GNUNET_STRINGS_relative_time_to_string (dur_delayed, 156 GNUNET_STRINGS_relative_time_to_string (dur_delayed,
239 GNUNET_YES)); 157 GNUNET_YES));
240 } 158 }
241 /* shutdown */ 159 /* shutdown */
242 end (); 160 GNUNET_SCHEDULER_shutdown ();
243 } 161 }
244 162 messages_recv++;
245 messages_recv ++;
246} 163}
247 164
248 165
@@ -253,15 +170,12 @@ notify_ready (void *cls, size_t size, void *buf)
253 struct GNUNET_MessageHeader *hdr; 170 struct GNUNET_MessageHeader *hdr;
254 171
255 th = NULL; 172 th = NULL;
256 173 if (NULL == buf)
257 if (buf == NULL)
258 { 174 {
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 175 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 "Timeout occurred while waiting for transmit_ready\n"); 176 "Timeout occurred while waiting for transmit_ready\n");
261 if (NULL != die_task) 177 GNUNET_SCHEDULER_shutdown ();
262 GNUNET_SCHEDULER_cancel (die_task); 178 ccc->global_ret = 42;
263 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
264 ok = 42;
265 return 0; 179 return 0;
266 } 180 }
267 181
@@ -272,11 +186,11 @@ notify_ready (void *cls, size_t size, void *buf)
272 hdr->type = htons (TEST_MESSAGE_TYPE); 186 hdr->type = htons (TEST_MESSAGE_TYPE);
273 187
274 { 188 {
275 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id)); 189 char *ps = GNUNET_strdup (GNUNET_i2s (&ccc->p[1]->id));
276 190
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n", 192 "Peer %u (`%s') sending message with type %u and size %u bytes to peer %u (`%s')\n",
279 p2->no, 193 ccc->p[1]->no,
280 ps, 194 ps,
281 ntohs (hdr->type), 195 ntohs (hdr->type),
282 ntohs (hdr->size), 196 ntohs (hdr->size),
@@ -292,15 +206,17 @@ notify_ready (void *cls, size_t size, void *buf)
292static void 206static void
293sendtask (void *cls) 207sendtask (void *cls)
294{ 208{
209 if (NULL == send_task)
210 ccc->global_ret = GNUNET_NO; /* first call! */
295 send_task = NULL; 211 send_task = NULL;
296 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id)); 212 {
297 213 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&ccc->p[0]->id));
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
300 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
301 GNUNET_free (receiver_s);
302
303 214
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
217 ccc->p[1]->no, GNUNET_i2s (&ccc->p[1]->id), ccc->p[0]->no, receiver_s);
218 GNUNET_free (receiver_s);
219 }
304 if (0 == messages_recv) 220 if (0 == messages_recv)
305 { 221 {
306 start_normal = GNUNET_TIME_absolute_get(); 222 start_normal = GNUNET_TIME_absolute_get();
@@ -310,64 +226,24 @@ sendtask (void *cls)
310 start_delayed = GNUNET_TIME_absolute_get(); 226 start_delayed = GNUNET_TIME_absolute_get();
311 } 227 }
312 228
313 s_sending = GNUNET_YES; 229 th = GNUNET_TRANSPORT_notify_transmit_ready (ccc->p[1]->th,
314 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, 230 &ccc->p[0]->id,
315 &p1->id,
316 TEST_MESSAGE_SIZE, 231 TEST_MESSAGE_SIZE,
317 TIMEOUT_TRANSMIT, 232 TIMEOUT_TRANSMIT,
318 &notify_ready, 233 &notify_ready,
319 p1); 234 ccc->p[0]);
320}
321
322
323static void
324notify_connect (void *cls,
325 const struct GNUNET_PeerIdentity *peer)
326{
327 static int c;
328
329 c++;
330 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls;
331 struct GNUNET_TRANSPORT_TESTING_PeerContext *t = NULL;
332
333 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
334 t = p1;
335 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
336 t = p2;
337 GNUNET_assert (t != NULL);
338
339 {
340 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
341
342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
343 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
344 p->no,
345 ps,
346 t->no,
347 GNUNET_i2s (peer));
348 GNUNET_free (ps);
349 }
350} 235}
351 236
352 237
353static void 238static void
354notify_disconnect (void *cls, 239notify_disconnect (void *cls,
355 const struct GNUNET_PeerIdentity *peer) 240 struct GNUNET_TRANSPORT_TESTING_PeerContext *me,
241 const struct GNUNET_PeerIdentity *other)
356{ 242{
357 struct GNUNET_TRANSPORT_TESTING_PeerContext *p = cls; 243 GNUNET_TRANSPORT_TESTING_log_disconnect (cls,
358 244 me,
359 { 245 other);
360 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id)); 246 if (NULL != th)
361
362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
363 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
364 p->no,
365 ps,
366 GNUNET_i2s (peer));
367 GNUNET_free (ps);
368 }
369
370 if (th != NULL)
371 { 247 {
372 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th); 248 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
373 th = NULL; 249 th = NULL;
@@ -375,147 +251,26 @@ notify_disconnect (void *cls,
375} 251}
376 252
377 253
378static void
379testing_connect_cb (void *cls)
380{
381 cc = NULL;
382
383 {
384 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
385
386 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387 "Peers connected: %u (%s) <-> %u (%s)\n",
388 p1->no,
389 p1_c,
390 p2->no,
391 GNUNET_i2s (&p2->id));
392 GNUNET_free (p1_c);
393 }
394
395 s_connected = GNUNET_YES;
396 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
397}
398
399
400static void
401start_cb (struct GNUNET_TRANSPORT_TESTING_PeerContext *p, void *cls)
402{
403 static int started;
404 started++;
405
406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
407 "Peer %u (`%s') started\n",
408 p->no,
409 GNUNET_i2s (&p->id));
410
411 if (started != 2)
412 return;
413 else
414 s_started = GNUNET_YES;
415 {
416 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
417
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
420 p1->no,
421 sender_c,
422 p2->no,
423 GNUNET_i2s (&p2->id));
424 GNUNET_free (sender_c);
425 }
426
427 cc = GNUNET_TRANSPORT_TESTING_connect_peers (p1,
428 p2,
429 &testing_connect_cb,
430 NULL);
431
432}
433
434
435static void
436run (void *cls, char *const *args, const char *cfgfile,
437 const struct GNUNET_CONFIGURATION_Handle *cfg)
438{
439 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
440
441 s_started = GNUNET_NO;
442 s_connected = GNUNET_NO;
443 s_sending = GNUNET_NO;
444
445 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
446 cfg_file_p1, 1,
447 &notify_receive, &notify_connect,
448 &notify_disconnect, &start_cb,
449 NULL);
450
451 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
452 cfg_file_p2, 2,
453 &notify_receive, &notify_connect,
454 &notify_disconnect, &start_cb,
455 NULL);
456
457 if ((p1 == NULL) || (p2 == NULL))
458 {
459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
460 if (die_task != NULL)
461 GNUNET_SCHEDULER_cancel (die_task);
462 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
463 return;
464 }
465}
466
467
468static int
469check ()
470{
471 static char *const argv[] = { "test-transport-api-manipulation",
472 "-c",
473 "test_transport_api_data.conf",
474 NULL
475 };
476 static struct GNUNET_GETOPT_CommandLineOption options[] = {
477 GNUNET_GETOPT_OPTION_END
478 };
479
480 send_task = NULL;
481
482 ok = 1;
483 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
484 "nohelp", options, &run, &ok);
485
486 return ok;
487}
488
489int 254int
490main (int argc, char *argv[]) 255main (int argc, char *argv[])
491{ 256{
492 int ret; 257 struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
493 258 .connect_continuation = &sendtask,
494 test_name = GNUNET_TRANSPORT_TESTING_get_test_name (argv[0]); 259 .config_file = "test_transport_api_data.conf",
495 test_source = GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__); 260 .rec = &notify_receive,
496 test_plugin = GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], 261 .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
497 test_source); 262 .nd = &notify_disconnect,
498 263 .shutdown_task = &custom_shutdown,
499 GNUNET_log_setup (test_name, 264 .timeout = TIMEOUT
500 "WARNING", 265 };
501 NULL);
502 tth = GNUNET_TRANSPORT_TESTING_init ();
503
504 cfg_file_p1 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 1);
505 cfg_file_p2 = GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], 2);
506
507 ret = check ();
508
509 GNUNET_free (cfg_file_p1);
510 GNUNET_free (cfg_file_p2);
511
512 GNUNET_free (test_source);
513 GNUNET_free (test_plugin);
514 GNUNET_free (test_name);
515
516 GNUNET_TRANSPORT_TESTING_done (tth);
517 266
518 return ret; 267 ccc = &my_ccc;
268 if (GNUNET_OK !=
269 GNUNET_TRANSPORT_TESTING_main (2,
270 &GNUNET_TRANSPORT_TESTING_connect_check,
271 ccc))
272 return 1;
273 return 0;
519} 274}
520 275
521/* end of test_transport_api_manipulation_recv_tcp.c */ 276/* end of test_transport_api_manipulation_recv_tcp.c */