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 * Note that this value must not significantly exceed
49 * 'MAX_PENDING' in 'gnunet-service-transport.c', otherwise
50 * messages may be dropped even for a reliable transport.
52 #define TOTAL_MSGS (1024 * 2)
55 * How long until we give up on transmitting the message?
57 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1500)
63 static GNUNET_SCHEDULER_TaskIdentifier die_task;
65 struct PeerContext * p1;
67 struct PeerContext * p2;
69 struct GNUNET_TRANSPORT_TransmitHandle * th;
76 * Testcase specific declarations
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;
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Stopping peers\n");
115 delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value;
117 "\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,
165 const struct GNUNET_PeerIdentity *peer,
166 const struct GNUNET_MessageHeader *message,
167 const struct GNUNET_TRANSPORT_ATS_Information *ats,
172 char cbuf[GNUNET_SERVER_MAX_MESSAGE_SIZE - 1];
173 const struct TestMessage *hdr;
175 hdr = (const struct TestMessage*) message;
177 if (MTYPE != ntohs (message->type))
179 msg_recv_expected = n;
180 msg_recv = ntohl(hdr->num);
181 if (ntohs (message->size) != (s))
183 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
184 "Expected message %u of size %u, got %u bytes of message %u\n",
186 ntohs (message->size),
188 if (die_task != GNUNET_SCHEDULER_NO_TASK)
189 GNUNET_SCHEDULER_cancel (die_task);
190 test_failed = GNUNET_YES;
191 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
194 if (ntohl (hdr->num) != n)
196 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
197 "Expected message %u of size %u, got %u bytes of message %u\n",
199 ntohs (message->size),
201 if (die_task != GNUNET_SCHEDULER_NO_TASK)
202 GNUNET_SCHEDULER_cancel (die_task);
203 test_failed = GNUNET_YES;
204 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
207 memset (cbuf, n, s - sizeof (struct TestMessage));
208 if (0 != memcmp (cbuf,
210 s - sizeof (struct TestMessage)))
212 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
213 "Expected message %u with bits %u, but body did not match\n",
214 n, (unsigned char) n);
215 if (die_task != GNUNET_SCHEDULER_NO_TASK)
216 GNUNET_SCHEDULER_cancel (die_task);
217 test_failed = GNUNET_YES;
218 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
222 if (ntohl(hdr->num) % 5000 == 0)
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Got message %u of size %u\n",
227 ntohs (message->size));
231 if (0 == (n % (TOTAL_MSGS/100)))
233 fprintf (stderr, ".");
234 if (die_task != GNUNET_SCHEDULER_NO_TASK)
235 GNUNET_SCHEDULER_cancel (die_task);
236 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,"All messages received\n");
250 notify_ready (void *cls, size_t size, void *buf)
254 struct TestMessage hdr;
267 GNUNET_assert (size >= s);
268 GNUNET_assert (buf != NULL);
272 hdr.header.size = htons (s);
273 hdr.header.type = htons (MTYPE);
276 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
277 ret += sizeof (struct TestMessage);
278 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
279 ret += s - sizeof (struct TestMessage);
283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
284 "Sending message %u of size %u\n",
291 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
292 break; /* sometimes pack buffer full, sometimes not */
294 while (size - ret >= s);
298 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
308 "Returning total message block of size %u\n",
317 notify_connect (void *cls,
318 const struct GNUNET_PeerIdentity *peer,
319 const struct GNUNET_TRANSPORT_ATS_Information *ats,
323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
324 "Peer `%4s' connected to us (%p)!\n",
330 GNUNET_TRANSPORT_set_quota (p1->th,
332 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
333 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
337 GNUNET_TRANSPORT_set_quota (p2->th,
339 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024),
340 GNUNET_BANDWIDTH_value_init (1024 * 1024 * 1024));
346 notify_disconnect (void *cls,
347 const struct GNUNET_PeerIdentity *peer)
349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Peer `%4s' disconnected (%p)!\n",
351 GNUNET_i2s (peer), cls);
357 start_time = GNUNET_TIME_absolute_get ();
358 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
360 get_size (0), 0, TIMEOUT,
366 testing_connect_cb (struct PeerContext * p1, struct PeerContext * p2, void *cls)
368 char * p1_c = strdup (GNUNET_i2s(&p1->id));
369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n",
371 GNUNET_i2s (&p2->id));
374 // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
375 GNUNET_SCHEDULER_add_delayed(GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
381 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
383 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
386 p1 = GNUNET_TRANSPORT_TESTING_start_peer(cfg_file_p1,
391 p2 = GNUNET_TRANSPORT_TESTING_start_peer(cfg_file_p2,
397 GNUNET_TRANSPORT_TESTING_connect_peers(p1, p2, &testing_connect_cb, NULL);
403 static char *const argv[] = { "test-transport-api",
405 "test_transport_api_data.conf",
411 static struct GNUNET_GETOPT_CommandLineOption options[] = {
412 GNUNET_GETOPT_OPTION_END
416 setTransportOptions("test_transport_api_data.conf");
419 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
420 argv, "test-transport-api", "nohelp",
427 * Return the actual path to a file found in the current
428 * PATH environment variable.
430 * @param binary the name of the file to find
433 get_path_from_PATH (char *binary)
444 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
445 _("PATH environment variable is unset.\n"));
448 path = GNUNET_strdup (p); /* because we write on it */
449 buf = GNUNET_malloc (strlen (path) + 20);
452 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
455 sprintf (buf, "%s/%s", pos, binary);
456 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
463 sprintf (buf, "%s/%s", pos, binary);
464 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
475 * Check whether the suid bit is set on a file.
476 * Attempts to find the file using the current
477 * PATH environment variable as a search path.
479 * @param binary the name of the file to check
481 * @return GNUNET_YES if the binary is found and
482 * can be run properly, GNUNET_NO otherwise
485 check_gnunet_nat_binary(char *binary)
495 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
496 p = get_path_from_PATH (binaryexe);
499 p = get_path_from_PATH (binary);
503 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
504 _("Could not find binary `%s' in PATH!\n"),
508 if (0 != STAT (p, &statbuf))
510 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
511 _("stat (%s) failed: %s\n"),
515 return GNUNET_SYSERR;
519 if ( (0 != (statbuf.st_mode & S_ISUID)) &&
520 (statbuf.st_uid == 0) )
524 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
525 if (INVALID_SOCKET == rawsock)
527 DWORD err = GetLastError ();
528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
529 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n", err);
530 return GNUNET_NO; /* not running as administrator */
532 closesocket (rawsock);
538 main (int argc, char *argv[])
542 GNUNET_log_setup ("test-transport-api",
550 char * pch = strdup(argv[0]);
552 char * filename = NULL;
555 /* get executable filename */
556 pch = strtok (pch,"/");
559 pch = strtok (NULL, "/");
564 filename = strstr(filename, "tes");
565 if (NULL != (dotexe = strstr (filename, ".exe")))
568 /* create cfg filename */
569 GNUNET_asprintf(&cfg_file_p1, "%s_peer1.conf",filename);
570 GNUNET_asprintf(&cfg_file_p2, "%s_peer2.conf", filename);
571 GNUNET_free (backup);
573 if (strstr(argv[0], "tcp_nat") != NULL)
575 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
577 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
578 "`%s' not properly installed, cannot run NAT test!\n",
579 "gnunet-nat-server");
583 else if (strstr(argv[0], "udp_nat") != NULL)
585 if (GNUNET_YES != check_gnunet_nat_binary("gnunet-nat-server"))
587 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
588 "`%s' not properly installed, cannot run NAT test!\n",
589 "gnunet-nat-server");
596 GNUNET_free (cfg_file_p1);
597 GNUNET_free (cfg_file_p2);
603 /* end of test_transport_api_reliability.c */