2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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_common.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_getopt_lib.h"
31 #include "gnunet_os_lib.h"
32 #include "gnunet_program_lib.h"
33 #include "gnunet_scheduler_lib.h"
34 #include "gnunet_transport_service.h"
35 #include "transport.h"
36 #include "transport-testing.h"
38 #define VERBOSE GNUNET_EXTRA_LOGGING
39 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
41 #define START_ARM GNUNET_YES
44 * How long until we give up on transmitting the message?
46 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
49 * How long until we give up on transmitting the message?
51 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
55 static char *test_source;
57 static char *test_plugin;
59 static char *test_name;
63 static GNUNET_SCHEDULER_TaskIdentifier die_task;
65 static GNUNET_SCHEDULER_TaskIdentifier send_task;
67 struct PeerContext *p1;
69 struct PeerContext *p2;
71 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc1;
72 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc2;
74 struct GNUNET_TRANSPORT_TransmitHandle *th;
76 struct GNUNET_TRANSPORT_TESTING_handle *tth;
83 #define OKPP do { ok++; FPRINTF (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
85 #define OKPP do { ok++; } while (0)
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
94 if (send_task != GNUNET_SCHEDULER_NO_TASK)
95 GNUNET_SCHEDULER_cancel (send_task);
97 if (die_task != GNUNET_SCHEDULER_NO_TASK)
98 GNUNET_SCHEDULER_cancel (die_task);
101 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
104 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
105 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
109 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
111 die_task = GNUNET_SCHEDULER_NO_TASK;
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
116 if (send_task != GNUNET_SCHEDULER_NO_TASK)
117 GNUNET_SCHEDULER_cancel (send_task);
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
122 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc2);
127 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
131 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
133 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
140 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
141 const struct GNUNET_MessageHeader *message,
142 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
144 struct PeerContext *p = cls;
145 struct PeerContext *t = NULL;
147 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
149 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
151 GNUNET_assert (t != NULL);
153 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
156 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
157 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
158 GNUNET_i2s (&t->id));
160 if ((MTYPE == ntohs (message->type)) &&
161 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
176 notify_ready (void *cls, size_t size, void *buf)
178 struct PeerContext *p = cls;
179 struct GNUNET_MessageHeader *hdr;
185 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
186 "Timeout occurred while waiting for transmit_ready\n");
187 if (GNUNET_SCHEDULER_NO_TASK != die_task)
188 GNUNET_SCHEDULER_cancel (die_task);
189 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
194 GNUNET_assert (size >= 256);
199 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
200 hdr->type = htons (MTYPE);
202 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
206 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
207 GNUNET_i2s (&p->id));
209 return sizeof (struct GNUNET_MessageHeader);
214 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
216 send_task = GNUNET_SCHEDULER_NO_TASK;
218 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
220 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
224 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
225 GNUNET_free (receiver_s);
227 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
228 TIMEOUT_TRANSMIT, ¬ify_ready,
234 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
235 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
240 struct PeerContext *p = cls;
241 struct PeerContext *t = NULL;
243 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
245 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
247 GNUNET_assert (t != NULL);
249 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
253 t->no, GNUNET_i2s (peer));
259 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
261 struct PeerContext *p = cls;
262 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
269 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
274 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
279 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc2);
284 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc1);
287 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
290 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
293 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
297 start_cb (struct PeerContext *p, void *cls)
303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
304 GNUNET_i2s (&p->id));
309 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Test tries to connect peer %u (`%s') <-> peer %u (`%s')\n",
313 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
316 GNUNET_TRANSPORT_TESTING_connect_peers (tth, p2, p1, &testing_connect_cb,
319 GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
324 run (void *cls, char *const *args, const char *cfgfile,
325 const struct GNUNET_CONFIGURATION_Handle *cfg)
327 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
329 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
330 ¬ify_receive, ¬ify_connect,
331 ¬ify_disconnect, &start_cb,
334 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
335 ¬ify_receive, ¬ify_connect,
336 ¬ify_disconnect, &start_cb,
339 if ((p1 == NULL) || (p2 == NULL))
341 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
342 if (die_task != GNUNET_SCHEDULER_NO_TASK)
343 GNUNET_SCHEDULER_cancel (die_task);
344 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
353 static char *const argv[] = { "test-transport-api",
355 "test_transport_api_data.conf",
361 static struct GNUNET_GETOPT_CommandLineOption options[] = {
362 GNUNET_GETOPT_OPTION_END
366 setTransportOptions ("test_transport_api_data.conf");
368 send_task = GNUNET_SCHEDULER_NO_TASK;
371 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
372 "nohelp", options, &run, &ok);
378 main (int argc, char *argv[])
383 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
385 GNUNET_log_setup (test_name,
393 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
394 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
397 tth = GNUNET_TRANSPORT_TESTING_init ();
399 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
400 (strcmp (test_plugin, "udp_nat") == 0))
402 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
403 if (GNUNET_NO == nat_res)
405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
406 "gnunet-nat-server", "SUID not set");
409 if (GNUNET_SYSERR == nat_res)
411 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
412 "gnunet-nat-server", "file not found");
417 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
418 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
422 GNUNET_free (cfg_file_p1);
423 GNUNET_free (cfg_file_p2);
425 GNUNET_free (test_source);
426 GNUNET_free (test_plugin);
427 GNUNET_free (test_name);
429 GNUNET_TRANSPORT_TESTING_done (tth);
434 /* end of test_transport_api_bidirectional_connect.c */