2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file transport/test_transport_api_bidirectional_connect.c
22 * @brief base test case for transport implementations
24 * Perform a 3-way handshake connection set up in both directions at
25 * (almost) the same time
28 #include "gnunet_transport_service.h"
29 #include "transport-testing.h"
33 * How long until we give up on transmitting the message?
35 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
38 * How long until we give up on transmitting the message?
40 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
44 static char *test_source;
46 static char *test_plugin;
48 static char *test_name;
52 static struct GNUNET_SCHEDULER_Task * die_task;
54 static struct GNUNET_SCHEDULER_Task * send_task;
56 static struct PeerContext *p1;
58 static struct PeerContext *p2;
60 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc1;
61 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc2;
63 static struct GNUNET_TRANSPORT_TransmitHandle *th;
65 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
67 static char *cfg_file_p1;
69 static char *cfg_file_p2;
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
77 if (send_task != NULL)
79 GNUNET_SCHEDULER_cancel (send_task);
84 GNUNET_SCHEDULER_cancel (die_task);
88 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
92 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
93 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
102 if (send_task != NULL)
104 GNUNET_SCHEDULER_cancel (send_task);
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
111 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc2);
116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
117 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc1);
122 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
126 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
128 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
135 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
136 const struct GNUNET_MessageHeader *message)
138 struct PeerContext *p = cls;
139 struct PeerContext *t = NULL;
141 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
143 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
145 GNUNET_assert (t != NULL);
147 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
150 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
151 GNUNET_i2s (&t->id));
153 if ((MTYPE == ntohs (message->type)) &&
154 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
169 notify_ready (void *cls, size_t size, void *buf)
171 struct PeerContext *p = cls;
172 struct GNUNET_MessageHeader *hdr;
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 "Timeout occurred while waiting for transmit_ready\n");
180 if (NULL != die_task)
181 GNUNET_SCHEDULER_cancel (die_task);
182 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
187 GNUNET_assert (size >= 256);
189 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
190 hdr->type = htons (MTYPE);
193 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
196 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
202 GNUNET_i2s (&p->id));
206 return sizeof (struct GNUNET_MessageHeader);
213 const struct GNUNET_SCHEDULER_TaskContext *tc;
216 tc = GNUNET_SCHEDULER_get_task_context ();
217 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
219 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
223 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
224 GNUNET_free (receiver_s);
226 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256,
227 TIMEOUT_TRANSMIT, ¬ify_ready,
233 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
238 struct PeerContext *p = cls;
239 struct PeerContext *t = NULL;
241 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
243 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
245 GNUNET_assert (t != NULL);
248 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
250 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
251 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n",
262 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
264 struct PeerContext *p = cls;
267 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
269 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
270 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
276 if (NULL != send_task)
278 GNUNET_SCHEDULER_cancel(send_task);
284 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
291 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
293 static int connected = GNUNET_NO;
295 if ((cls == cc1) && (NULL != cc2))
297 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc2);
299 if ((cls == cc2) && (NULL != cc1))
301 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc1);
311 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
314 "Peers connected: %u (%s) <-> %u (%s)\n",
318 GNUNET_i2s (&p2->id));
321 send_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
328 start_cb (struct PeerContext *p, void *cls)
334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
335 "Peer %u (`%s') started\n",
337 GNUNET_i2s (&p->id));
342 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
345 "Test tries to connect peer %u (`%s') <-> peer %u (`%s')\n",
346 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
347 GNUNET_free (sender_c);
349 GNUNET_TRANSPORT_TESTING_connect_peers (tth, p2, p1, &testing_connect_cb,
352 GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
358 run (void *cls, char *const *args, const char *cfgfile,
359 const struct GNUNET_CONFIGURATION_Handle *cfg)
361 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
363 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
364 ¬ify_receive, ¬ify_connect,
365 ¬ify_disconnect, &start_cb,
368 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
369 ¬ify_receive, ¬ify_connect,
370 ¬ify_disconnect, &start_cb,
373 if ((p1 == NULL) || (p2 == NULL))
375 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
376 if (die_task != NULL)
377 GNUNET_SCHEDULER_cancel (die_task);
378 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
387 static char *const argv[] = { "test-transport-api",
389 "test_transport_api_data.conf",
392 static struct GNUNET_GETOPT_CommandLineOption options[] = {
393 GNUNET_GETOPT_OPTION_END
397 setTransportOptions ("test_transport_api_data.conf");
402 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
403 "nohelp", options, &run, &ok);
409 main (int argc, char *argv[])
413 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
415 GNUNET_log_setup (test_name,
419 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
420 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
423 tth = GNUNET_TRANSPORT_TESTING_init ();
425 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
426 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
430 GNUNET_free (cfg_file_p1);
431 GNUNET_free (cfg_file_p2);
433 GNUNET_free (test_source);
434 GNUNET_free (test_plugin);
435 GNUNET_free (test_name);
437 GNUNET_TRANSPORT_TESTING_done (tth);
442 /* end of test_transport_api_bidirectional_connect.c */