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_transport_service.h"
31 #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, 30)
37 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
39 static struct PeerContext *p1;
40 static struct PeerContext *p2;
42 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
44 struct GNUNET_TRANSPORT_TESTING_handle *tth;
46 static int connected = GNUNET_NO;
53 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
55 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
56 GNUNET_SCHEDULER_cancel (timeout_task);
58 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
59 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
61 GNUNET_TRANSPORT_TESTING_done (tth);
67 timeout_task = GNUNET_SCHEDULER_NO_TASK;
68 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
72 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
77 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
79 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
82 GNUNET_TRANSPORT_TESTING_done (tth);
88 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
90 char *ps = GNUNET_strdup (GNUNET_i2s (&p1->id));
92 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
93 "Peer %u (`%4s') connected to peer %u (`%s')!\n", p1->no, ps,
94 p2->no, GNUNET_i2s (&p2->id));
96 GNUNET_SCHEDULER_add_now (&end, NULL);
100 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
101 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' connected \n",
109 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%s' disconnected \n",
116 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
117 const struct GNUNET_MessageHeader *message,
118 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Receiving\n");
124 start_cb (struct PeerContext *p, void *cls)
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
131 GNUNET_i2s (&p->id));
136 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
140 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
141 GNUNET_free (sender_c);
143 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
149 run (void *cls, char *const *args, const char *cfgfile,
150 const struct GNUNET_CONFIGURATION_Handle *cfg)
152 tth = GNUNET_TRANSPORT_TESTING_init ();
155 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES, &end_badly, NULL);
157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Starting peer\n");
158 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
159 "test_transport_api_tcp_peer1.conf",
160 1, ¬ify_receive, ¬ify_connect,
161 ¬ify_disconnect, &start_cb, p1);
163 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth,
164 "test_transport_api_tcp_peer2.conf",
165 2, ¬ify_receive, ¬ify_connect,
166 ¬ify_disconnect, &start_cb, p2);
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "Peer1 was not started successfully\n");
172 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
173 GNUNET_SCHEDULER_cancel (timeout_task);
174 timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Peer2 was not started successfully\n");
180 if (timeout_task != GNUNET_SCHEDULER_NO_TASK)
181 GNUNET_SCHEDULER_cancel (timeout_task);
182 timeout_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
187 main (int argc, char *argv[])
189 GNUNET_log_setup ("test_transport_testing",
193 char *const argv_1[] = { "test_transport_testing",
195 "test_transport_api_data.conf",
199 struct GNUNET_GETOPT_CommandLineOption options[] = {
200 GNUNET_GETOPT_OPTION_END
203 GNUNET_PROGRAM_run ((sizeof (argv_1) / sizeof (char *)) - 1, argv_1,
204 "test_transport_testing", "nohelp", options, &run, &ret);
209 /* end of test_transport_api.c */