test_transport_api_reliability.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. This file is part of GNUnet.
  3. Copyright (C) 2009, 2010, 2016 GNUnet e.V.
  4. GNUnet is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Affero General Public License as published
  6. by the Free Software Foundation, either version 3 of the License,
  7. or (at your option) any later version.
  8. GNUnet is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Affero General Public License for more details.
  12. You should have received a copy of the GNU Affero General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. SPDX-License-Identifier: AGPL3.0-or-later
  15. */
  16. /**
  17. * @file transport/test_transport_api_reliability.c
  18. * @brief base test case for transport implementations
  19. *
  20. * This test case serves ensures that messages are reliably sent between peers
  21. *
  22. * This test sends TOTAL_MSGS with message type MTYPE from peer 1 to peer 2
  23. * and ensures that all message were received.
  24. */
  25. #include "platform.h"
  26. #include "gnunet_transport_service.h"
  27. #include "gauger.h"
  28. #include "transport-testing.h"
  29. /**
  30. * Allow making the problem "bigger".
  31. */
  32. #define FACTOR 1
  33. /**
  34. * Total number of messages to send
  35. *
  36. * Note that this value must not significantly exceed
  37. * 'MAX_PENDING' in 'gnunet-service-transport_clients.c', otherwise
  38. * messages may be dropped even for a reliable transport.
  39. */
  40. #define TOTAL_MSGS (1024 * 3 * FACTOR)
  41. /**
  42. * Testcase timeout
  43. */
  44. #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 450 \
  45. * FACTOR)
  46. /**
  47. * If we are in an "xhdr" test, the factor by which we divide
  48. * #TOTAL_MSGS for a more sane test duration.
  49. */
  50. static unsigned int xhdr = 1;
  51. static struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext *ccc;
  52. /**
  53. * Total amount of bytes sent
  54. */
  55. static unsigned long long total_bytes;
  56. /**
  57. * Time of start
  58. */
  59. static struct GNUNET_TIME_Absolute start_time;
  60. /**
  61. * No. of last message received
  62. */
  63. static unsigned int msg_recv;
  64. /**
  65. * Bitmap storing which messages were received
  66. */
  67. static char bitmap[TOTAL_MSGS / 8];
  68. /**
  69. * Get the desired message size for message number @a iter.
  70. */
  71. static size_t
  72. get_size (unsigned int iter)
  73. {
  74. size_t ret;
  75. ret = (iter * iter * iter);
  76. #ifndef __linux__
  77. /* FreeBSD/OSX etc. Unix DGRAMs do not work
  78. * with large messages */
  79. if (0 == strcmp ("unix", ccc->test_plugin))
  80. ret = sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 1024);
  81. #endif
  82. ret = sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage) + (ret % 60000);
  83. return ret;
  84. }
  85. /**
  86. * Implementation of the callback for obtaining the
  87. * size of messages for transmission. Counts the total
  88. * number of bytes sent as a side-effect.
  89. *
  90. * @param cnt_down count down from `TOTAL_MSGS - 1`
  91. * @return message size of the message
  92. */
  93. static size_t
  94. get_size_cnt (unsigned int cnt_down)
  95. {
  96. size_t ret = get_size (TOTAL_MSGS / xhdr - 1 - cnt_down);
  97. total_bytes += ret;
  98. return ret;
  99. }
  100. /**
  101. * Sets a bit active in the bitmap.
  102. *
  103. * @param bitIdx which bit to set
  104. * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
  105. */
  106. static int
  107. set_bit (unsigned int bitIdx)
  108. {
  109. size_t arraySlot;
  110. unsigned int targetBit;
  111. if (bitIdx >= sizeof(bitmap) * 8)
  112. {
  113. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  114. "tried to set bit %u of %u(!?!?)\n",
  115. bitIdx,
  116. (unsigned int) sizeof(bitmap) * 8);
  117. return GNUNET_SYSERR;
  118. }
  119. arraySlot = bitIdx / 8;
  120. targetBit = (1L << (bitIdx % 8));
  121. bitmap[arraySlot] |= targetBit;
  122. return GNUNET_OK;
  123. }
  124. /**
  125. * Obtain a bit from bitmap.
  126. * @param map the bitmap
  127. * @param bit index from bitmap
  128. *
  129. * @return Bit @a bit from @a map
  130. */
  131. static int
  132. get_bit (const char *map,
  133. unsigned int bit)
  134. {
  135. if (bit > TOTAL_MSGS)
  136. {
  137. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  138. "get bit %u of %u(!?!?)\n",
  139. bit,
  140. (unsigned int) sizeof(bitmap) * 8);
  141. return 0;
  142. }
  143. return ((map)[bit >> 3] & (1 << (bit & 7))) > 0;
  144. }
  145. static void
  146. custom_shutdown (void *cls)
  147. {
  148. unsigned long long delta;
  149. unsigned long long rate;
  150. int ok;
  151. /* Calculcate statistics */
  152. delta = GNUNET_TIME_absolute_get_duration (start_time).rel_value_us;
  153. if (0 == delta)
  154. delta = 1;
  155. rate = (1000LL * 1000ll * total_bytes) / (1024 * delta);
  156. fprintf (stderr,
  157. "\nThroughput was %llu KiBytes/s\n",
  158. rate);
  159. {
  160. char *value_name;
  161. GNUNET_asprintf (&value_name,
  162. "unreliable_%s",
  163. ccc->test_plugin);
  164. GAUGER ("TRANSPORT",
  165. value_name,
  166. (int) rate,
  167. "kb/s");
  168. GNUNET_free (value_name);
  169. }
  170. ok = 0;
  171. for (unsigned int i = 0; i < TOTAL_MSGS / xhdr; i++)
  172. {
  173. if (get_bit (bitmap, i) == 0)
  174. {
  175. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  176. "Did not receive message %d\n",
  177. i);
  178. ok = -1;
  179. }
  180. }
  181. if (0 != ok)
  182. ccc->global_ret = GNUNET_SYSERR; /* fail: messages missing! */
  183. }
  184. static void
  185. notify_receive (void *cls,
  186. struct GNUNET_TRANSPORT_TESTING_PeerContext *receiver,
  187. const struct GNUNET_PeerIdentity *sender,
  188. const struct GNUNET_TRANSPORT_TESTING_TestMessage *hdr)
  189. {
  190. static int n;
  191. unsigned int s;
  192. char cbuf[GNUNET_MAX_MESSAGE_SIZE - 1];
  193. if (GNUNET_TRANSPORT_TESTING_SIMPLE_MTYPE != ntohs (hdr->header.type))
  194. return;
  195. msg_recv = ntohl (hdr->num);
  196. s = get_size (ntohl (hdr->num));
  197. if (ntohs (hdr->header.size) != s)
  198. {
  199. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  200. "Expected message %u of size %u, got %u bytes of message %u\n",
  201. (uint32_t) ntohl (hdr->num),
  202. s,
  203. ntohs (hdr->header.size),
  204. (uint32_t) ntohl (hdr->num));
  205. ccc->global_ret = GNUNET_SYSERR;
  206. GNUNET_SCHEDULER_shutdown ();
  207. return;
  208. }
  209. memset (cbuf,
  210. ntohl (hdr->num),
  211. s - sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage));
  212. if (0 !=
  213. memcmp (cbuf,
  214. &hdr[1],
  215. s - sizeof(struct GNUNET_TRANSPORT_TESTING_TestMessage)))
  216. {
  217. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  218. "Expected message %u with bits %u, but body did not match\n",
  219. (uint32_t) ntohl (hdr->num),
  220. (unsigned char) ntohl (hdr->num));
  221. ccc->global_ret = GNUNET_SYSERR;
  222. GNUNET_SCHEDULER_shutdown ();
  223. return;
  224. }
  225. #if VERBOSE
  226. if (0 == ntohl (hdr->num) % 5)
  227. {
  228. GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
  229. "Got message %u of size %u\n",
  230. (uint32_t) ntohl (hdr->num),
  231. ntohs (hdr->header.size));
  232. }
  233. #endif
  234. n++;
  235. if (GNUNET_SYSERR == set_bit (ntohl (hdr->num)))
  236. {
  237. GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
  238. "Message id %u is bigger than maxmimum number of messages %u expected\n",
  239. (uint32_t) ntohl (hdr->num),
  240. TOTAL_MSGS / xhdr);
  241. }
  242. if (0 == (n % (TOTAL_MSGS / xhdr / 100)))
  243. {
  244. fprintf (stderr, "%s", ".");
  245. }
  246. if (n == TOTAL_MSGS / xhdr)
  247. {
  248. /* end testcase with success */
  249. ccc->global_ret = GNUNET_OK;
  250. GNUNET_SCHEDULER_shutdown ();
  251. }
  252. }
  253. int
  254. main (int argc, char *argv[])
  255. {
  256. if (0 == strstr (argv[0], "xhdr"))
  257. xhdr = 30;
  258. struct GNUNET_TRANSPORT_TESTING_SendClosure sc = {
  259. .num_messages = TOTAL_MSGS / xhdr,
  260. .get_size_cb = &get_size_cnt
  261. };
  262. struct GNUNET_TRANSPORT_TESTING_ConnectCheckContext my_ccc = {
  263. .connect_continuation = &GNUNET_TRANSPORT_TESTING_simple_send,
  264. .connect_continuation_cls = &sc,
  265. .config_file = "test_transport_api_data.conf",
  266. .rec = &notify_receive,
  267. .nc = &GNUNET_TRANSPORT_TESTING_log_connect,
  268. .nd = &GNUNET_TRANSPORT_TESTING_log_disconnect,
  269. .shutdown_task = &custom_shutdown,
  270. .timeout = TIMEOUT,
  271. .global_ret = GNUNET_SYSERR
  272. };
  273. ccc = &my_ccc;
  274. sc.ccc = ccc;
  275. start_time = GNUNET_TIME_absolute_get ();
  276. if (GNUNET_OK !=
  277. GNUNET_TRANSPORT_TESTING_main (2,
  278. &GNUNET_TRANSPORT_TESTING_connect_check,
  279. ccc))
  280. return 1;
  281. return 0;
  282. }
  283. /* end of test_transport_api_reliability.c */