aboutsummaryrefslogtreecommitdiff
path: root/src/transport/test_transport_api_monitor_peers.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2014-06-23 11:31:44 +0000
committerChristian Grothoff <christian@grothoff.org>2014-06-23 11:31:44 +0000
commit7fea1d9e013cc441d72b155b3b06348b1d689261 (patch)
tree19642deeeecc1176ff77a1f8ee2ad0cb75df0716 /src/transport/test_transport_api_monitor_peers.c
parentb5582e492a12b2ef17c1daff0707cc0b3197aeca (diff)
downloadgnunet-7fea1d9e013cc441d72b155b3b06348b1d689261.tar.gz
gnunet-7fea1d9e013cc441d72b155b3b06348b1d689261.zip
-rename tests to caputre better what they test
Diffstat (limited to 'src/transport/test_transport_api_monitor_peers.c')
-rw-r--r--src/transport/test_transport_api_monitor_peers.c530
1 files changed, 530 insertions, 0 deletions
diff --git a/src/transport/test_transport_api_monitor_peers.c b/src/transport/test_transport_api_monitor_peers.c
new file mode 100644
index 000000000..fd6abe53b
--- /dev/null
+++ b/src/transport/test_transport_api_monitor_peers.c
@@ -0,0 +1,530 @@
1/*
2 This file is part of GNUnet.
3 (C) 2009, 2010 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 transport/test_transport_api_monitor_peers.c
22 * @brief base test case for transport peer monitor API
23 */
24#include "platform.h"
25#include "gnunet_transport_service.h"
26#include "transport-testing.h"
27
28/**
29 * How long until we give up on transmitting the message?
30 */
31#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
32
33/**
34 * How long until we give up on transmitting the message?
35 */
36#define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
37
38#define TEST_MESSAGE_SIZE 2600
39
40#define TEST_MESSAGE_TYPE 12345
41
42static char *test_source;
43
44static char *test_plugin;
45
46static char *test_name;
47
48static int ok;
49
50static int s_started;
51
52static int s_connected;
53
54static int s_sending;
55
56static GNUNET_SCHEDULER_TaskIdentifier die_task;
57
58static GNUNET_SCHEDULER_TaskIdentifier send_task;
59
60static struct PeerContext *p1;
61
62static struct PeerContext *p2;
63
64static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
65
66static struct GNUNET_TRANSPORT_TransmitHandle *th;
67
68static struct GNUNET_TRANSPORT_TESTING_handle *tth;
69
70static char *cfg_file_p1;
71
72static char *cfg_file_p2;
73
74static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p1;
75
76static struct GNUNET_TRANSPORT_PeerMonitoringContext *pmc_p2;
77
78static int p1_c = GNUNET_NO;
79
80static int p2_c = GNUNET_NO;
81
82static int p1_c_notify = GNUNET_NO;
83
84static int p2_c_notify = GNUNET_NO;
85
86
87static void
88end ()
89{
90 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
91 "Stopping peers\n");
92
93 if (send_task != GNUNET_SCHEDULER_NO_TASK)
94 GNUNET_SCHEDULER_cancel (send_task);
95
96 if (die_task != GNUNET_SCHEDULER_NO_TASK)
97 {
98 GNUNET_SCHEDULER_cancel (die_task);
99 die_task = GNUNET_SCHEDULER_NO_TASK;
100 }
101
102 if (th != NULL)
103 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
104 th = NULL;
105
106 if (NULL != p1)
107 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
108 p1 = NULL;
109 if (NULL != p2)
110 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
111 p2 = NULL;
112
113 if (NULL != pmc_p1)
114 {
115 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
116 pmc_p1 = NULL;
117 }
118 if (NULL != pmc_p2)
119 {
120 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
121 pmc_p2 = NULL;
122 }
123
124
125
126 ok = 0;
127}
128
129
130static void
131end_badly (void *cls,
132 const struct GNUNET_SCHEDULER_TaskContext *tc)
133{
134 die_task = GNUNET_SCHEDULER_NO_TASK;
135
136 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
137
138
139 if (send_task != GNUNET_SCHEDULER_NO_TASK)
140 GNUNET_SCHEDULER_cancel (send_task);
141
142 if (cc != NULL)
143 {
144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
145 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
146 cc = NULL;
147 }
148
149 if (th != NULL)
150 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
151 else
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
153
154 if (s_started == GNUNET_NO)
155 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
156 else
157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
158
159 if (s_connected == GNUNET_NO)
160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
161 else
162 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
163
164 if (s_sending == GNUNET_NO)
165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
166 else
167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
168
169 th = NULL;
170
171 if (NULL != pmc_p1)
172 {
173 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p1);
174 pmc_p1 = NULL;
175 }
176 if (NULL != pmc_p2)
177 {
178 GNUNET_TRANSPORT_monitor_peers_cancel (pmc_p2);
179 pmc_p2 = NULL;
180 }
181
182 if (p1 != NULL)
183 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
184 else
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
186 if (p2 != NULL)
187 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
188 else
189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
190
191 ok = GNUNET_SYSERR;
192}
193
194
195static void
196notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
197 const struct GNUNET_MessageHeader *message)
198{
199 struct PeerContext *p = cls;
200 struct PeerContext *t = NULL;
201
202 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
203 t = p1;
204 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
205 t = p2;
206 GNUNET_assert (t != NULL);
207
208 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
209
210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
211 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
212 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
213 GNUNET_i2s (&t->id));
214 GNUNET_free (ps);
215}
216
217
218static size_t
219notify_ready (void *cls, size_t size, void *buf)
220{
221 struct PeerContext *p = cls;
222 struct GNUNET_MessageHeader *hdr;
223
224 th = NULL;
225
226 if (buf == NULL)
227 {
228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
229 "Timeout occurred while waiting for transmit_ready\n");
230 if (GNUNET_SCHEDULER_NO_TASK != die_task)
231 GNUNET_SCHEDULER_cancel (die_task);
232 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
233 ok = 42;
234 return 0;
235 }
236
237 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
238 if (buf != NULL)
239 {
240 memset (buf, '\0', TEST_MESSAGE_SIZE);
241 hdr = buf;
242 hdr->size = htons (TEST_MESSAGE_SIZE);
243 hdr->type = htons (TEST_MESSAGE_TYPE);
244 }
245
246 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
247 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
248 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
249 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
250 GNUNET_i2s (&p->id));
251 GNUNET_free (ps);
252
253 return TEST_MESSAGE_SIZE;
254}
255
256
257static void
258sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
259{
260 send_task = GNUNET_SCHEDULER_NO_TASK;
261
262 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
263 return;
264 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
265
266 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
267 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
268 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
269 GNUNET_free (receiver_s);
270 s_sending = GNUNET_YES;
271 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
272 TIMEOUT_TRANSMIT, &notify_ready,
273 p1);
274}
275
276
277static void
278done ()
279{
280 if ((GNUNET_YES == p1_c) && (GNUNET_YES == p2_c) && p1_c_notify && p2_c_notify)
281 {
282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Both peers state to be connected\n");
283 ok = 0;
284 end();
285 }
286}
287
288
289static void
290notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
291{
292 static int c;
293
294 c++;
295 struct PeerContext *p = cls;
296 struct PeerContext *t = NULL;
297
298 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
299 {
300 p1_c_notify = GNUNET_YES;
301 t = p1;
302 }
303 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
304 {
305 p2_c_notify = GNUNET_YES;
306 t = p2;
307 }
308 GNUNET_assert (t != NULL);
309
310 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
311
312 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
313 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
314 t->no, GNUNET_i2s (peer));
315 if (p1_c_notify && p2_c_notify)
316 GNUNET_SCHEDULER_add_now(&done, NULL);
317 GNUNET_free (ps);
318}
319
320
321static void
322notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
323{
324 struct PeerContext *p = cls;
325 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
326
327 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
328 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
329 GNUNET_i2s (peer));
330
331 GNUNET_free (ps);
332
333 if (th != NULL)
334 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
335 th = NULL;
336}
337
338
339static void
340testing_connect_cb (struct PeerContext *p1,
341 struct PeerContext *p2,
342 void *cls)
343{
344 cc = NULL;
345 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
346
347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
348 "Peers connected: %u (%s) <-> %u (%s)\n",
349 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
350 GNUNET_free (p1_c);
351
352 s_connected = GNUNET_YES;
353 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
354}
355
356
357static void
358start_cb (struct PeerContext *p, void *cls)
359{
360 static int started;
361
362 started++;
363
364 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
365 "Peer %u (`%s') started\n", p->no,
366 GNUNET_i2s (&p->id));
367
368 if (started != 2)
369 return;
370 else
371 s_started = GNUNET_YES;
372 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
373
374 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
375 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
376 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
377 GNUNET_free (sender_c);
378
379 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
380 NULL);
381
382}
383
384
385static void
386monitor1_cb (void *cls,
387 const struct GNUNET_PeerIdentity *peer,
388 const struct GNUNET_HELLO_Address *address,
389 enum GNUNET_TRANSPORT_PeerState state,
390 struct GNUNET_TIME_Absolute state_timeout)
391{
392 if ((NULL == peer) || (NULL == p1))
393 return;
394
395 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
396 "Monitor 1: %s %s %s\n",
397 GNUNET_i2s (peer),
398 GNUNET_TRANSPORT_ps2s (state),
399 GNUNET_STRINGS_absolute_time_to_string(state_timeout));
400 if ((0 == memcmp (peer, &p2->id, sizeof (p2->id)) &&
401 (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
402 GNUNET_NO == p1_c) )
403 {
404 p1_c = GNUNET_YES;
405 GNUNET_SCHEDULER_add_now (&done, NULL);
406 }
407
408}
409
410
411static void
412monitor2_cb (void *cls,
413 const struct GNUNET_PeerIdentity *peer,
414 const struct GNUNET_HELLO_Address *address,
415 enum GNUNET_TRANSPORT_PeerState state,
416 struct GNUNET_TIME_Absolute state_timeout)
417{
418 if ((NULL == peer) || (NULL == p2))
419 return;
420
421 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
422 "Monitor 2: %s %s %s\n",
423 GNUNET_i2s (peer),
424 GNUNET_TRANSPORT_ps2s (state),
425 GNUNET_STRINGS_absolute_time_to_string(state_timeout));
426 if ((0 == memcmp (peer, &p1->id, sizeof (p1->id)) &&
427 (GNUNET_YES == GNUNET_TRANSPORT_is_connected(state)) &&
428 GNUNET_NO == p2_c) )
429 {
430 p2_c = GNUNET_YES;
431 GNUNET_SCHEDULER_add_now (&done, NULL);
432 }
433}
434
435
436
437static void
438run (void *cls, char *const *args, const char *cfgfile,
439 const struct GNUNET_CONFIGURATION_Handle *cfg)
440{
441 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
442
443 s_started = GNUNET_NO;
444 s_connected = GNUNET_NO;
445 s_sending = GNUNET_NO;
446
447 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
448 &notify_receive, &notify_connect,
449 &notify_disconnect, &start_cb,
450 NULL);
451 pmc_p1 = GNUNET_TRANSPORT_monitor_peers (p1->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor1_cb, NULL);
452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
453
454 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
455 &notify_receive, &notify_connect,
456 &notify_disconnect, &start_cb,
457 NULL);
458 pmc_p2 = GNUNET_TRANSPORT_monitor_peers (p2->cfg, NULL, GNUNET_NO, GNUNET_TIME_UNIT_FOREVER_REL, &monitor2_cb, NULL);
459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer 1 started\n");
460 if ((p1 == NULL) || (p2 == NULL))
461 {
462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
463 if (die_task != GNUNET_SCHEDULER_NO_TASK)
464 GNUNET_SCHEDULER_cancel (die_task);
465 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
466 return;
467 }
468}
469
470
471static int
472check ()
473{
474 static char *const argv[] = { "test-transport-api",
475 "-c",
476 "test_transport_api_data.conf",
477 NULL
478 };
479 static struct GNUNET_GETOPT_CommandLineOption options[] = {
480 GNUNET_GETOPT_OPTION_END
481 };
482
483 send_task = GNUNET_SCHEDULER_NO_TASK;
484
485 ok = 1;
486 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
487 "nohelp", options, &run, &ok);
488
489 return ok;
490}
491
492
493int
494main (int argc, char *argv[])
495{
496 int ret;
497
498 ok = 1;
499
500 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
501 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
502 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
503 &test_plugin);
504
505 GNUNET_log_setup (test_name,
506 "WARNING",
507 NULL);
508 tth = GNUNET_TRANSPORT_TESTING_init ();
509
510 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
511 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
512
513 ret = check ();
514
515 GNUNET_free (cfg_file_p1);
516 GNUNET_free (cfg_file_p2);
517
518 GNUNET_free (test_source);
519 GNUNET_free (test_plugin);
520 GNUNET_free (test_name);
521
522 GNUNET_TRANSPORT_TESTING_done (tth);
523
524 if (0 != ret)
525 return ret;
526 else
527 return ok;
528}
529
530/* end of test_transport_api_monitor_peers.c */