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;
118 "\nThroughput was %llu kb/s\n", 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",
185 n, s, ntohs (message->size), ntohl (hdr->num));
186 if (die_task != GNUNET_SCHEDULER_NO_TASK)
187 GNUNET_SCHEDULER_cancel (die_task);
188 test_failed = GNUNET_YES;
189 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
192 if (ntohl (hdr->num) != n)
194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195 "Expected message %u of size %u, got %u bytes of message %u\n",
196 n, s, ntohs (message->size), ntohl (hdr->num));
197 if (die_task != GNUNET_SCHEDULER_NO_TASK)
198 GNUNET_SCHEDULER_cancel (die_task);
199 test_failed = GNUNET_YES;
200 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
203 memset (cbuf, n, s - sizeof (struct TestMessage));
204 if (0 != memcmp (cbuf, &hdr[1], s - sizeof (struct TestMessage)))
206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
207 "Expected message %u with bits %u, but body did not match\n",
208 n, (unsigned char) n);
209 if (die_task != GNUNET_SCHEDULER_NO_TASK)
210 GNUNET_SCHEDULER_cancel (die_task);
211 test_failed = GNUNET_YES;
212 die_task = GNUNET_SCHEDULER_add_now (&end_badly, NULL);
216 if (ntohl (hdr->num) % 5000 == 0)
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Got message %u of size %u\n",
220 ntohl (hdr->num), ntohs (message->size));
224 if (0 == (n % (TOTAL_MSGS / 100)))
226 fprintf (stderr, ".");
227 if (die_task != GNUNET_SCHEDULER_NO_TASK)
228 GNUNET_SCHEDULER_cancel (die_task);
229 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages received\n");
241 notify_ready (void *cls, size_t size, void *buf)
245 struct TestMessage hdr;
258 GNUNET_assert (size >= s);
259 GNUNET_assert (buf != NULL);
263 hdr.header.size = htons (s);
264 hdr.header.type = htons (MTYPE);
267 memcpy (&cbuf[ret], &hdr, sizeof (struct TestMessage));
268 ret += sizeof (struct TestMessage);
269 memset (&cbuf[ret], n, s - sizeof (struct TestMessage));
270 ret += s - sizeof (struct TestMessage);
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Sending message %u of size %u\n", n, s);
280 if (0 == GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16))
281 break; /* sometimes pack buffer full, sometimes not */
283 while (size - ret >= s);
287 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
290 ¬ify_ready, NULL);
295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
296 "Returning total message block of size %u\n", ret);
301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "All messages sent\n");
308 notify_connect (void *cls,
309 const struct GNUNET_PeerIdentity *peer,
310 const struct GNUNET_TRANSPORT_ATS_Information *ats,
314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
315 "Peer `%4s' connected to us (%p)!\n", GNUNET_i2s (peer), cls);
319 GNUNET_TRANSPORT_set_quota (p1->th,
321 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
323 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
328 GNUNET_TRANSPORT_set_quota (p2->th,
330 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
332 GNUNET_BANDWIDTH_value_init (1024 * 1024 *
339 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
341 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
342 "Peer `%4s' disconnected (%p)!\n", GNUNET_i2s (peer), cls);
348 start_time = GNUNET_TIME_absolute_get ();
349 th = GNUNET_TRANSPORT_notify_transmit_ready (p2->th,
351 get_size (0), 0, TIMEOUT,
352 ¬ify_ready, NULL);
356 testing_connect_cb (struct PeerContext *p1, struct PeerContext *p2, void *cls)
358 char *p1_c = strdup (GNUNET_i2s (&p1->id));
360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peers connected: %s <-> %s\n",
361 p1_c, GNUNET_i2s (&p2->id));
364 // FIXME: THIS IS REQUIRED! SEEMS TO BE A BUG!
365 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &sendtask, NULL);
371 const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg)
373 die_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end_badly, NULL);
375 p1 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p1,
378 ¬ify_disconnect, NULL);
379 p2 = GNUNET_TRANSPORT_TESTING_start_peer (cfg_file_p2,
382 ¬ify_disconnect, NULL);
384 GNUNET_TRANSPORT_TESTING_connect_peers (p1, p2, &testing_connect_cb, NULL);
390 static char *const argv[] = { "test-transport-api",
392 "test_transport_api_data.conf",
398 static struct GNUNET_GETOPT_CommandLineOption options[] = {
399 GNUNET_GETOPT_OPTION_END
403 setTransportOptions ("test_transport_api_data.conf");
406 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
407 argv, "test-transport-api", "nohelp", options, &run, &ok);
413 * Return the actual path to a file found in the current
414 * PATH environment variable.
416 * @param binary the name of the file to find
419 get_path_from_PATH (char *binary)
430 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
431 _("PATH environment variable is unset.\n"));
434 path = GNUNET_strdup (p); /* because we write on it */
435 buf = GNUNET_malloc (strlen (path) + 20);
438 while (NULL != (end = strchr (pos, PATH_SEPARATOR)))
441 sprintf (buf, "%s/%s", pos, binary);
442 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
449 sprintf (buf, "%s/%s", pos, binary);
450 if (GNUNET_DISK_file_test (buf) == GNUNET_YES)
461 * Check whether the suid bit is set on a file.
462 * Attempts to find the file using the current
463 * PATH environment variable as a search path.
465 * @param binary the name of the file to check
467 * @return GNUNET_YES if the binary is found and
468 * can be run properly, GNUNET_NO otherwise
471 check_gnunet_nat_binary (char *binary)
483 GNUNET_asprintf (&binaryexe, "%s.exe", binary);
484 p = get_path_from_PATH (binaryexe);
487 p = get_path_from_PATH (binary);
491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
492 _("Could not find binary `%s' in PATH!\n"), binary);
495 if (0 != STAT (p, &statbuf))
497 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
498 _("stat (%s) failed: %s\n"), p, STRERROR (errno));
500 return GNUNET_SYSERR;
504 if ((0 != (statbuf.st_mode & S_ISUID)) && (statbuf.st_uid == 0))
508 rawsock = socket (AF_INET, SOCK_RAW, IPPROTO_ICMP);
509 if (INVALID_SOCKET == rawsock)
511 DWORD err = GetLastError ();
513 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
514 "socket (AF_INET, SOCK_RAW, IPPROTO_ICMP) have failed! GLE = %d\n",
516 return GNUNET_NO; /* not running as administrator */
518 closesocket (rawsock);
524 main (int argc, char *argv[])
528 GNUNET_log_setup ("test-transport-api",
536 char *pch = strdup (argv[0]);
538 char *filename = NULL;
541 /* get executable filename */
542 pch = strtok (pch, "/");
545 pch = strtok (NULL, "/");
550 filename = strstr (filename, "tes");
551 if (NULL != (dotexe = strstr (filename, ".exe")))
554 /* create cfg filename */
555 GNUNET_asprintf (&cfg_file_p1, "%s_peer1.conf", filename);
556 GNUNET_asprintf (&cfg_file_p2, "%s_peer2.conf", filename);
557 GNUNET_free (backup);
559 if (strstr (argv[0], "tcp_nat") != NULL)
561 if (GNUNET_YES != check_gnunet_nat_binary ("gnunet-nat-server"))
563 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
564 "`%s' not properly installed, cannot run NAT test!\n",
565 "gnunet-nat-server");
569 else if (strstr (argv[0], "udp_nat") != NULL)
571 if (GNUNET_YES != check_gnunet_nat_binary ("gnunet-nat-server"))
573 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
574 "`%s' not properly installed, cannot run NAT test!\n",
575 "gnunet-nat-server");
582 GNUNET_free (cfg_file_p1);
583 GNUNET_free (cfg_file_p2);
589 /* end of test_transport_api_reliability.c */