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"
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, 30)
53 #define TEST_MESSAGE_SIZE 2600
55 #define TEST_MESSAGE_TYPE 12345
57 static char *test_source;
59 static char *test_plugin;
61 static char *test_name;
67 static int s_connected;
71 static GNUNET_SCHEDULER_TaskIdentifier die_task;
73 static GNUNET_SCHEDULER_TaskIdentifier send_task;
75 static struct PeerContext *p1;
77 static struct PeerContext *p2;
79 static GNUNET_TRANSPORT_TESTING_ConnectRequest cc;
81 static struct GNUNET_TRANSPORT_TransmitHandle *th;
83 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
85 static char *cfg_file_p1;
87 static char *cfg_file_p2;
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);
130 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
132 if (s_started == GNUNET_NO)
133 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
137 if (s_connected == GNUNET_NO)
138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
142 if (s_sending == GNUNET_NO)
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
150 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
152 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
154 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
156 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
163 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
164 const struct GNUNET_MessageHeader *message,
165 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
167 struct PeerContext *p = cls;
168 struct PeerContext *t = NULL;
170 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
172 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
174 GNUNET_assert (t != NULL);
176 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
179 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
180 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
181 GNUNET_i2s (&t->id));
184 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
185 (TEST_MESSAGE_SIZE == ntohs (message->size)))
200 notify_ready (void *cls, size_t size, void *buf)
202 struct PeerContext *p = cls;
203 struct GNUNET_MessageHeader *hdr;
209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
210 "Timeout occurred while waiting for transmit_ready\n");
211 if (GNUNET_SCHEDULER_NO_TASK != die_task)
212 GNUNET_SCHEDULER_cancel (die_task);
213 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
218 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
221 memset (buf, '\0', TEST_MESSAGE_SIZE);
223 hdr->size = htons (TEST_MESSAGE_SIZE);
224 hdr->type = htons (TEST_MESSAGE_TYPE);
227 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
229 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
230 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
231 GNUNET_i2s (&p->id));
234 return TEST_MESSAGE_SIZE;
239 sendtask (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
241 send_task = GNUNET_SCHEDULER_NO_TASK;
243 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
245 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
249 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
250 GNUNET_free (receiver_s);
251 s_sending = GNUNET_YES;
252 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE, 0,
253 TIMEOUT_TRANSMIT, ¬ify_ready,
259 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
260 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
265 struct PeerContext *p = cls;
266 struct PeerContext *t = NULL;
268 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
270 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
272 GNUNET_assert (t != NULL);
274 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
278 t->no, GNUNET_i2s (peer));
284 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
286 struct PeerContext *p = cls;
287 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
290 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n", p->no, ps,
296 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
302 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
305 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %u (%s) <-> %u (%s)\n",
308 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
311 s_connected = GNUNET_YES;
312 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
317 start_cb (struct PeerContext *p, void *cls)
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %u (`%s') started\n", p->no,
324 GNUNET_i2s (&p->id));
329 s_started = GNUNET_YES;
330 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
332 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
333 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
334 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
335 GNUNET_free (sender_c);
337 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth, p1, p2, &testing_connect_cb,
344 run (void *cls, char *const *args, const char *cfgfile,
345 const struct GNUNET_CONFIGURATION_Handle *cfg)
347 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
349 s_started = GNUNET_NO;
350 s_connected = GNUNET_NO;
351 s_sending = GNUNET_NO;
353 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
354 ¬ify_receive, ¬ify_connect,
355 ¬ify_disconnect, &start_cb,
358 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
359 ¬ify_receive, ¬ify_connect,
360 ¬ify_disconnect, &start_cb,
363 if ((p1 == NULL) || (p2 == NULL))
365 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Fail! Could not start peers!\n");
366 if (die_task != GNUNET_SCHEDULER_NO_TASK)
367 GNUNET_SCHEDULER_cancel (die_task);
368 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
377 static char *const argv[] = { "test-transport-api",
379 "test_transport_api_data.conf",
382 static struct GNUNET_GETOPT_CommandLineOption options[] = {
383 GNUNET_GETOPT_OPTION_END
387 setTransportOptions ("test_transport_api_data.conf");
389 send_task = GNUNET_SCHEDULER_NO_TASK;
392 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv, test_name,
393 "nohelp", options, &run, &ok);
399 main (int argc, char *argv[])
403 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0], &test_name);
404 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__, &test_source);
405 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0], test_source,
408 GNUNET_log_setup (test_name,
411 tth = GNUNET_TRANSPORT_TESTING_init ();
413 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p1, 1);
414 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0], &cfg_file_p2, 2);
418 GNUNET_free (cfg_file_p1);
419 GNUNET_free (cfg_file_p2);
421 GNUNET_free (test_source);
422 GNUNET_free (test_plugin);
423 GNUNET_free (test_name);
425 GNUNET_TRANSPORT_TESTING_done (tth);
430 /* end of test_transport_api.c */