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_reliability.c
22 * @brief base test case for transport implementations
24 * This test case serves as a base for tcp and http
25 * transport test cases to check that the transports
26 * achieve reliable message delivery.
29 #include "gnunet_common.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_getopt_lib.h"
32 #include "gnunet_os_lib.h"
33 #include "gnunet_program_lib.h"
34 #include "gnunet_scheduler_lib.h"
35 #include "gnunet_server_lib.h"
36 #include "gnunet_transport_service.h"
38 #include "transport.h"
39 #include "transport-testing.h"
41 #define VERBOSE GNUNET_NO
43 #define VERBOSE_ARM GNUNET_NO
45 #define START_ARM GNUNET_YES
48 * How long until we give up on transmitting the message?
50 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
54 static GNUNET_SCHEDULER_TaskIdentifier die_task;
56 struct PeerContext *p1;
58 struct PeerContext *p2;
60 struct GNUNET_TRANSPORT_TransmitHandle *th;
67 * Testcase specific declarations
71 * Note that this value must not significantly exceed
72 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
73 * messages may be dropped even for a reliable transport.
75 #define TOTAL_MSGS (1024 * 2)
81 struct GNUNET_MessageHeader header;
85 static int msg_scheduled;
87 static int msg_recv_expected;
90 static int test_failed;
92 static unsigned long long total_bytes;
94 static struct GNUNET_TIME_Absolute start_time;
97 * END Testcase specific declarations
101 #define OKPP do { ok++; fprintf (stderr, "Now at stage %u at %s:%u\n", ok, __FILE__, __LINE__); } while (0)
103 #define OKPP do { ok++; } while (0)
110 unsigned long long delta;
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
116 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
117 fprintf (stderr, "\nThroughput was %llu kb/s\n",
118 total_bytes * 1000 / 1024 / delta);
119 //GNUNET_asprintf(&value_name, "reliable_%s", test_name);
120 //GAUGER ("TRANSPORT", value_name, (int)(total_bytes * 1000 / 1024 /delta), "kb/s");
121 //GNUNET_free(value_name);
123 if (die_task != GNUNET_SCHEDULER_NO_TASK)
124 GNUNET_SCHEDULER_cancel (die_task);
127 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
130 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
131 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
137 die_task = GNUNET_SCHEDULER_NO_TASK;
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Fail! Stopping peers\n");
141 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
145 GNUNET_TRANSPORT_TESTING_stop_peer (p1);
147 GNUNET_TRANSPORT_TESTING_stop_peer (p2);
154 get_size (unsigned int iter)
158 ret = (iter * iter * iter);
159 return sizeof (struct TestMessage) + (ret % 60000);
164 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
165 const struct GNUNET_MessageHeader *message,
166 const struct GNUNET_TRANSPORT_ATS_Information *ats,
171 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
172 const struct TestMessage *hdr;
174 hdr = (const struct TestMessage *) message;
176 if (MTYPE != ntohs (message->type))
178 msg_recv_expected = n;
179 msg_recv = ntohl (hdr->num);
180 if (ntohs (message->size) != (s))
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 "Expected message %u of size %u, got %u bytes of message %u\n",
184 n, s, ntohs (message->size), ntohl (hdr->num));
185 if (die_task != GNUNET_SCHEDULER_NO_TASK)
186 GNUNET_SCHEDULER_cancel (die_task);
187 test_failed = GNUNET_YES;
188 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
191 if (ntohl (hdr->num) != n)
193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
194 "Expected message %u of size %u, got %u bytes of message %u\n",
195 n, s, ntohs (message->size), ntohl (hdr->num));
196 if (die_task != GNUNET_SCHEDULER_NO_TASK)
197 GNUNET_SCHEDULER_cancel (die_task);
198 test_failed = GNUNET_YES;
199 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
202 memset (cbuf, n, s - sizeof (struct TestMessage));
203 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
205 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
206 "Expected message %u with bits %u, but body did not match\n", n,
208 if (die_task != GNUNET_SCHEDULER_NO_TASK)
209 GNUNET_SCHEDULER_cancel (die_task);
210 test_failed = GNUNET_YES;
211 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
215 if (ntohl (hdr->num) % 5000 == 0)
217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Got message %u of size %u\n",
218 ntohl (hdr->num), ntohs (message->size));
222 if (0 == (n % (TOTAL_MSGS / 100)))
224 fprintf (stderr, ".");
225 if (die_task != GNUNET_SCHEDULER_NO_TASK)
226 GNUNET_SCHEDULER_cancel (die_task);
227 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages received\n");
239 notify_ready (void *cls, size_t size, void *buf)
243 struct TestMessage hdr;
256 GNUNET_assert (size >= s);
257 GNUNET_assert (buf != NULL);
261 hdr.header.size = htons (s);
262 hdr.header.type = htons (MTYPE);
265 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
266 ret += sizeof (struct TestMessage);
267 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
268 ret += s - sizeof (struct TestMessage);
272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message %u of size %u\n", n,
278 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
279 break; /* sometimes pack buffer full, sometimes not */
281 while (size - ret >= s);
285 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, s, 0,
286 TIMEOUT, ¬ify_ready,
292 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
293 "Returning total message block of size %u\n", ret);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
305 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
306 const struct GNUNET_TRANSPORT_ATS_Information *ats,
310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' connected to us (%p)!\n",
311 GNUNET_i2s (peer), cls);
315 GNUNET_TRANSPORT_set_quota (p1->th, &p2->id,
316 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
318 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
323 GNUNET_TRANSPORT_set_quota (p2->th, &p1->id,
324 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
326 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
333 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer `%4s' disconnected (%p)!\n",
336 GNUNET_i2s (peer), cls);
342 start_time = GNUNET_TIME_absolute_get ();
343 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th, &p1->id, get_size (0), 0,
344 TIMEOUT, ¬ify_ready, NULL);
348 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
350 char *p1_c = strdup (GNUNET_i2s (&p1->id));
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n", p1_c,
353 GNUNET_i2s (&p2->id));
356 // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
357 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
361 run (void *cls, char *const *args, const char *cfgfile,
362 const struct GNUNET_CONFIGURATION_Handle *cfg)
364 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
366 p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1, ¬ify_receive,
367 ¬ify_connect, ¬ify_disconnect,
369 p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2, ¬ify_receive,
370 ¬ify_connect, ¬ify_disconnect,
373 GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, NULL);
379 static char *const argv[] = { "test-transport-api",
381 "test_transport_api_data.conf",
387 static struct GNUNET_GETOPT_CommandLineOption options[] = {
388 GNUNET_GETOPT_OPTION_END
392 setTransportOptions ("test_transport_api_data.conf");
395 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
396 "test-transport-api", "nohelp", options, &run, &ok);
402 * Return the actual path to a file found in the current
403 * PATH environment variable.
405 * @param binary the name of the file to find
408 get_path_from_PATH (char *binary)
419 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
420 _("PATH environment variable is unset.\n"));
423 path = GNUNET_strdup (p); /* because we write on it */
424 buf = GNUNET_malloc (strlen (path) + 20);
427 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
430 sprintf (buf, "%s/%s", pos, binary);
431 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
438 sprintf (buf, "%s/%s", pos, binary);
439 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
450 * Check whether the suid bit is set on a file.
451 * Attempts to find the file using the current
452 * PATH environment variable as a search path.
454 * @param binary the name of the file to check
456 * @return GNUNET_YES if the binary is found and
457 * can be run properly, GNUNET_NO otherwise
460 check_gnunet_nat_binary (char *binary)
472 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
473 p = get_path_from_PATH (binaryexe);
476 p = get_path_from_PATH (binary);
480 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
481 _("Could not find binary `%s' in PATH!\n"), binary);
484 if (0 != STAT (p, &statbuf))
486 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("stat (%s) failed: %s\n"), p,
489 return GNUNET_SYSERR;
493 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
497 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
498 if (INVALID_SOCKET == rawsock)
500 DWORD err = GetLastError ();
502 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
503 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n",
505 return GNUNET_NO; /* not running as administrator */
507 closesocket (rawsock);
513 main (int argc, char *argv[])
517 GNUNET_log_setup ("test-transport-api",
525 char *pch = strdup (argv[0]);
527 char *filename = NULL;
530 /* get executable filename */
531 pch = strtok (pch, "/");
534 pch = strtok (NULL, "/");
539 filename = strstr (filename, "tes");
540 if (NULL != (dotexe = strstr (filename, ".exe")))
543 /* create cfg filename */
544 GNUNET_asprintf (&cfg_file_p1, "%s_peer1.conf", filename);
545 GNUNET_asprintf (&cfg_file_p2, "%s_peer2.conf", filename);
546 GNUNET_free (backup);
548 if (strstr (argv[0], "tcp_nat") != NULL)
550 if (GNUNET_YES != check_gnunet_nat_binary ("gnunet-nat-server"))
552 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
553 "`%s' not properly installed, cannot run NAT test!\n",
554 "gnunet-nat-server");
558 else if (strstr (argv[0], "udp_nat") != NULL)
560 if (GNUNET_YES != check_gnunet_nat_binary ("gnunet-nat-server"))
562 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
563 "`%s' not properly installed, cannot run NAT test!\n",
564 "gnunet-nat-server");
571 GNUNET_free (cfg_file_p1);
572 GNUNET_free (cfg_file_p2);
578 /* end of test_transport_api_reliability.c */