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.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"
34 * How long until we give up on transmitting the message?
36 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
39 * How long until we give up on transmitting the message?
41 #define TIMEOUT_TRANSMIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43 #define TEST_MESSAGE_SIZE 2600
45 #define TEST_MESSAGE_TYPE 12345
47 static char *test_source;
49 static char *test_plugin;
51 static char *test_name;
57 static int s_connected;
61 static struct GNUNET_SCHEDULER_Task * die_task;
63 static struct GNUNET_SCHEDULER_Task * send_task;
65 static struct PeerContext *p1;
67 static struct PeerContext *p2;
69 static struct GNUNET_TRANSPORT_TESTING_ConnectRequest * cc;
71 static struct GNUNET_TRANSPORT_TransmitHandle *th;
73 static struct GNUNET_TRANSPORT_TESTING_handle *tth;
75 static char *cfg_file_p1;
77 static char *cfg_file_p2;
83 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping peers\n");
85 if (send_task != NULL)
86 GNUNET_SCHEDULER_cancel (send_task);
89 GNUNET_SCHEDULER_cancel (die_task);
92 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
95 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
96 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
101 end_badly (void *cls)
105 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Fail! Stopping peers\n");
108 if (send_task != NULL)
109 GNUNET_SCHEDULER_cancel (send_task);
113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Fail! Could not connect peers\n"));
114 GNUNET_TRANSPORT_TESTING_connect_peers_cancel (tth, cc);
119 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
123 if (s_started == GNUNET_NO)
124 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were not started \n"));
126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peers were started \n"));
128 if (s_connected == GNUNET_NO)
129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not connected\n"));
131 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were connected\n"));
133 if (s_sending == GNUNET_NO)
134 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were not ready to send data\n"));
136 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer were ready to send data\n"));
141 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p1);
143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 1 was not started\n"));
145 GNUNET_TRANSPORT_TESTING_stop_peer (tth, p2);
147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Peer 2 was not started\n"));
154 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
155 const struct GNUNET_MessageHeader *message)
157 struct PeerContext *p = cls;
158 struct PeerContext *t = NULL;
160 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
162 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
164 GNUNET_assert (t != NULL);
166 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
168 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
169 "Peer %u (`%4s') received message of type %d and size %u size from peer %u (`%4s')!\n",
170 p->no, ps, ntohs (message->type), ntohs (message->size), t->no,
171 GNUNET_i2s (&t->id));
174 if ((TEST_MESSAGE_TYPE == ntohs (message->type)) &&
175 (TEST_MESSAGE_SIZE == ntohs (message->size)))
190 notify_ready (void *cls, size_t size, void *buf)
192 struct PeerContext *p = cls;
193 struct GNUNET_MessageHeader *hdr;
199 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
200 "Timeout occurred while waiting for transmit_ready\n");
201 if (NULL != die_task)
202 GNUNET_SCHEDULER_cancel (die_task);
203 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
208 GNUNET_assert (size >= TEST_MESSAGE_SIZE);
211 memset (buf, '\0', TEST_MESSAGE_SIZE);
213 hdr->size = htons (TEST_MESSAGE_SIZE);
214 hdr->type = htons (TEST_MESSAGE_TYPE);
217 char *ps = GNUNET_strdup (GNUNET_i2s (&p2->id));
218 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
219 "Peer %u (`%4s') sending message with type %u and size %u bytes to peer %u (`%4s')\n",
220 p2->no, ps, ntohs (hdr->type), ntohs (hdr->size), p->no,
221 GNUNET_i2s (&p->id));
224 return TEST_MESSAGE_SIZE;
232 char *receiver_s = GNUNET_strdup (GNUNET_i2s (&p1->id));
234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
235 "Sending message from peer %u (`%4s') -> peer %u (`%s') !\n",
236 p2->no, GNUNET_i2s (&p2->id), p1->no, receiver_s);
237 GNUNET_free (receiver_s);
238 s_sending = GNUNET_YES;
239 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, TEST_MESSAGE_SIZE,
240 TIMEOUT_TRANSMIT, ¬ify_ready,
246 notify_connect (void *cls,
247 const struct GNUNET_PeerIdentity *peer)
252 struct PeerContext *p = cls;
253 struct PeerContext *t = NULL;
255 if (0 == memcmp (peer, &p1->id, sizeof (struct GNUNET_PeerIdentity)))
257 if (0 == memcmp (peer, &p2->id, sizeof (struct GNUNET_PeerIdentity)))
259 GNUNET_assert (t != NULL);
261 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
264 "Peer %u (`%4s'): peer %u (`%s') connected to me!\n", p->no, ps,
265 t->no, GNUNET_i2s (peer));
271 notify_disconnect (void *cls,
272 const struct GNUNET_PeerIdentity *peer)
274 struct PeerContext *p = cls;
275 char *ps = GNUNET_strdup (GNUNET_i2s (&p->id));
277 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
278 "Peer %u (`%4s'): peer (`%s') disconnected from me!\n",
286 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
293 testing_connect_cb (struct PeerContext *p1,
294 struct PeerContext *p2,
298 char *p1_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
300 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Peers connected: %u (%s) <-> %u (%s)\n",
301 p1->no, p1_c, p2->no, GNUNET_i2s (&p2->id));
304 s_connected = GNUNET_YES;
305 send_task = GNUNET_SCHEDULER_add_now (&sendtask, NULL);
310 start_cb (struct PeerContext *p,
317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
318 "Peer %u (`%s') started\n",
320 GNUNET_i2s (&p->id));
323 s_started = GNUNET_YES;
325 char *sender_c = GNUNET_strdup (GNUNET_i2s (&p1->id));
327 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
328 "Test tries to connect peer %u (`%s') -> peer %u (`%s')\n",
329 p1->no, sender_c, p2->no, GNUNET_i2s (&p2->id));
330 GNUNET_free (sender_c);
332 cc = GNUNET_TRANSPORT_TESTING_connect_peers (tth,
344 const struct GNUNET_CONFIGURATION_Handle *cfg)
346 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
350 s_started = GNUNET_NO;
351 s_connected = GNUNET_NO;
352 s_sending = GNUNET_NO;
354 p1 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p1, 1,
355 ¬ify_receive, ¬ify_connect,
356 ¬ify_disconnect, &start_cb,
359 p2 = GNUNET_TRANSPORT_TESTING_start_peer (tth, cfg_file_p2, 2,
360 ¬ify_receive, ¬ify_connect,
361 ¬ify_disconnect, &start_cb,
364 if ((p1 == NULL) || (p2 == NULL))
366 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
367 "Fail! Could not start peers!\n");
368 if (die_task != NULL)
369 GNUNET_SCHEDULER_cancel (die_task);
370 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
379 static char *const argv[] = {
380 "test-transport-api",
382 "test_transport_api_data.conf",
385 static struct GNUNET_GETOPT_CommandLineOption options[] = {
386 GNUNET_GETOPT_OPTION_END
392 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
405 main (int argc, char *argv[])
409 GNUNET_TRANSPORT_TESTING_get_test_name (argv[0],
411 GNUNET_TRANSPORT_TESTING_get_test_source_name (__FILE__,
413 GNUNET_TRANSPORT_TESTING_get_test_plugin_name (argv[0],
417 GNUNET_log_setup (test_name,
420 tth = GNUNET_TRANSPORT_TESTING_init ();
422 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0],
425 GNUNET_TRANSPORT_TESTING_get_config_name (argv[0],
431 GNUNET_free (cfg_file_p1);
432 GNUNET_free (cfg_file_p2);
434 GNUNET_free (test_source);
435 GNUNET_free (test_plugin);
436 GNUNET_free (test_name);
438 GNUNET_TRANSPORT_TESTING_done (tth);
443 /* end of test_transport_api.c */