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.c
22 * @brief base test case for transport implementations
24 * This test case serves as a base for tcp, udp, and udp-nat
25 * transport test cases. Based on the executable being run
26 * the correct test case will be performed. Conservation of
30 #include "gnunet_common.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_getopt_lib.h"
33 #include "gnunet_os_lib.h"
34 #include "gnunet_program_lib.h"
35 #include "gnunet_scheduler_lib.h"
36 #include "gnunet_transport_service.h"
37 #include "transport.h"
38 #include "transport-testing.h"
40 #define VERBOSE GNUNET_EXTRA_LOGGING
41 #define VERBOSE_ARM GNUNET_EXTRA_LOGGING
43 #define START_ARM GNUNET_YES
46 * How long until we give up on transmitting the message?
48 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
51 * How long until we give up on transmitting the message?
53 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
57 static char *test_source;
59 static char *test_plugin;
61 static char *test_name;
65 static GNUNET_SCHEDULER_TaskIdentifier die_task;
67 static GNUNET_SCHEDULER_TaskIdentifier send_task;
69 struct PeerContext *p1;
71 struct PeerContext *p2;
73 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
75 struct GNUNET_TRANSPORT_TransmitHandle *th;
77 struct GNUNET_TRANSPORT_TESTING_handle *tth;
84 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
86 #define OKPP do { ok++; } while (0)
93 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
95 if (send_task != GNUNET_SCHEDULER_NO_TASK)
96 GNUNET_SCHEDULER_cancel (send_task);
98 if (die_task != GNUNET_SCHEDULER_NO_TASK)
99 GNUNET_SCHEDULER_cancel (die_task);
102 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
105 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
106 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
110 end_badly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
112 die_task = GNUNET_SCHEDULER_NO_TASK;
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
117 if (send_task != GNUNET_SCHEDULER_NO_TASK)
118 GNUNET_SCHEDULER_cancel (send_task);
122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
123 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
128 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
132 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
134 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
141 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
142 const struct GNUNET_MessageHeader *message,
143 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
145 struct PeerContext *p = cls;
146 struct PeerContext *t = NULL;
148 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
150 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
152 GNUNET_assert (t != NULL);
154 char *ps = strdup (GNUNET_i2s (&p->id));
156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
157 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
158 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
159 GNUNET_i2s (&t->id));
161 if ((MTYPE == ntohs (message->type)) &&
162 (sizeof (struct GNUNET_MessageHeader) == ntohs (message->size)))
177 notify_ready (void *cls, size_t size, void *buf)
179 struct PeerContext *p = cls;
180 struct GNUNET_MessageHeader *hdr;
186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
187 "Timeout occurred while waiting for transmit_ready\n");
188 if (GNUNET_SCHEDULER_NO_TASK != die_task)
189 GNUNET_SCHEDULER_cancel (die_task);
190 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
195 GNUNET_assert (size >= 256);
200 hdr->size = htons (sizeof (struct GNUNET_MessageHeader));
201 hdr->type = htons (MTYPE);
203 char *ps = strdup (GNUNET_i2s (&p2->id));
205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
206 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
207 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
208 GNUNET_i2s (&p->id));
210 return sizeof (struct GNUNET_MessageHeader);
215 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
217 send_task = GNUNET_SCHEDULER_NO_TASK;
219 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
221 char *receiver_s = strdup (GNUNET_i2s (&p1->id));
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
225 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
226 GNUNET_free (receiver_s);
228 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, 256, 0,
229 TIMEOUT_TRANSMIT, ¬ify_ready,
235 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
236 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
241 struct PeerContext *p = cls;
242 struct PeerContext *t = NULL;
244 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
246 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
248 GNUNET_assert (t != NULL);
250 char *ps = strdup (GNUNET_i2s (&p->id));
252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
253 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
254 t->no, GNUNET_i2s (peer));
260 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
262 struct PeerContext *p = cls;
263 char *ps = strdup (GNUNET_i2s (&p->id));
265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
266 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
270 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
275 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
278 char *p1_c = strdup (GNUNET_i2s (&p1->id));
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
281 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
284 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
290 start_cb (struct PeerContext *p, void *cls)
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
297 GNUNET_i2s (&p->id));
302 char *sender_c = strdup (GNUNET_i2s (&p1->id));
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
306 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
308 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
314 run (void *cls, char *const *args, const char *cfgfile,
315 const struct GNUNET_CONFIGURATION_Handle *cfg)
317 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
319 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
320 ¬ify_receive, ¬ify_connect,
321 ¬ify_disconnect, &start_cb,
324 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
325 ¬ify_receive, ¬ify_connect,
326 ¬ify_disconnect, &start_cb,
329 if ((p1 == NULL) || (p2 == NULL))
331 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
332 if (die_task != GNUNET_SCHEDULER_NO_TASK)
333 GNUNET_SCHEDULER_cancel (die_task);
334 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
343 static char *const argv[] = { "test-transport-api",
345 "test_transport_api_data.conf",
351 static struct GNUNET_GETOPT_CommandLineOption options[] = {
352 GNUNET_GETOPT_OPTION_END
356 setTransportOptions ("test_transport_api_data.conf");
358 send_task = GNUNET_SCHEDULER_NO_TASK;
361 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
362 "nohelp", options, &run, &ok);
368 main (int argc, char *argv[])
373 tth = GNUNET_TRANSPORT_TESTING_init ();
375 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
376 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
378 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
380 GNUNET_log_setup (test_name,
388 if ((strcmp (test_plugin, "tcp_nat") == 0) ||
389 (strcmp (test_plugin, "udp_nat") == 0))
391 nat_res = GNUNET_OS_check_helper_binary ("gnunet-nat-server");
392 if (GNUNET_NO == nat_res)
394 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
395 "gnunet-nat-server", "SUID not set");
398 if (GNUNET_SYSERR == nat_res)
400 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Cannot run NAT test: `%s' %s \n",
401 "gnunet-nat-server", "file not found");
406 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
407 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
411 GNUNET_free (cfg_file_p1);
412 GNUNET_free (cfg_file_p2);
414 GNUNET_free (test_source);
415 GNUNET_free (test_plugin);
416 GNUNET_free (test_name);
418 GNUNET_TRANSPORT_TESTING_done (tth);
423 /* end of test_transport_api.c */