packettest.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  1. /*
  2. * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include "internal/packet.h"
  10. #include "testutil.h"
  11. #define BUF_LEN 255
  12. static unsigned char smbuf[BUF_LEN + 1];
  13. static int test_PACKET_remaining(void)
  14. {
  15. PACKET pkt;
  16. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  17. || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
  18. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 1))
  19. || !TEST_size_t_eq(PACKET_remaining(&pkt), 1)
  20. || !TEST_true(PACKET_forward(&pkt, 1))
  21. || !TEST_size_t_eq(PACKET_remaining(&pkt), 0))
  22. return 0;
  23. return 1;
  24. }
  25. static int test_PACKET_end(void)
  26. {
  27. PACKET pkt;
  28. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  29. || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
  30. || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN)
  31. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 1))
  32. || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN)
  33. || !TEST_true(PACKET_forward(&pkt, 1))
  34. || !TEST_ptr_eq(PACKET_end(&pkt), smbuf + BUF_LEN))
  35. return 0;
  36. return 1;
  37. }
  38. static int test_PACKET_get_1(void)
  39. {
  40. unsigned int i = 0;
  41. PACKET pkt;
  42. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  43. || !TEST_true(PACKET_get_1(&pkt, &i))
  44. || !TEST_uint_eq(i, 0x02)
  45. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 2))
  46. || !TEST_true(PACKET_get_1(&pkt, &i))
  47. || !TEST_uint_eq(i, 0xfe)
  48. || !TEST_false(PACKET_get_1(&pkt, &i)))
  49. return 0;
  50. return 1;
  51. }
  52. static int test_PACKET_get_4(void)
  53. {
  54. unsigned long i = 0;
  55. PACKET pkt;
  56. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  57. || !TEST_true(PACKET_get_4(&pkt, &i))
  58. || !TEST_ulong_eq(i, 0x08060402UL)
  59. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
  60. || !TEST_true(PACKET_get_4(&pkt, &i))
  61. || !TEST_ulong_eq(i, 0xfefcfaf8UL)
  62. || !TEST_false(PACKET_get_4(&pkt, &i)))
  63. return 0;
  64. return 1;
  65. }
  66. static int test_PACKET_get_net_2(void)
  67. {
  68. unsigned int i = 0;
  69. PACKET pkt;
  70. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  71. || !TEST_true(PACKET_get_net_2(&pkt, &i))
  72. || !TEST_uint_eq(i, 0x0204)
  73. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 4))
  74. || !TEST_true(PACKET_get_net_2(&pkt, &i))
  75. || !TEST_uint_eq(i, 0xfcfe)
  76. || !TEST_false(PACKET_get_net_2(&pkt, &i)))
  77. return 0;
  78. return 1;
  79. }
  80. static int test_PACKET_get_net_3(void)
  81. {
  82. unsigned long i = 0;
  83. PACKET pkt;
  84. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  85. || !TEST_true(PACKET_get_net_3(&pkt, &i))
  86. || !TEST_ulong_eq(i, 0x020406UL)
  87. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 6))
  88. || !TEST_true(PACKET_get_net_3(&pkt, &i))
  89. || !TEST_ulong_eq(i, 0xfafcfeUL)
  90. || !TEST_false(PACKET_get_net_3(&pkt, &i)))
  91. return 0;
  92. return 1;
  93. }
  94. static int test_PACKET_get_net_4(void)
  95. {
  96. unsigned long i = 0;
  97. PACKET pkt;
  98. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  99. || !TEST_true(PACKET_get_net_4(&pkt, &i))
  100. || !TEST_ulong_eq(i, 0x02040608UL)
  101. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
  102. || !TEST_true(PACKET_get_net_4(&pkt, &i))
  103. || !TEST_ulong_eq(i, 0xf8fafcfeUL)
  104. || !TEST_false(PACKET_get_net_4(&pkt, &i)))
  105. return 0;
  106. return 1;
  107. }
  108. static int test_PACKET_get_sub_packet(void)
  109. {
  110. PACKET pkt, subpkt;
  111. unsigned long i = 0;
  112. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  113. || !TEST_true(PACKET_get_sub_packet(&pkt, &subpkt, 4))
  114. || !TEST_true(PACKET_get_net_4(&subpkt, &i))
  115. || !TEST_ulong_eq(i, 0x02040608UL)
  116. || !TEST_size_t_eq(PACKET_remaining(&subpkt), 0)
  117. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
  118. || !TEST_true(PACKET_get_sub_packet(&pkt, &subpkt, 4))
  119. || !TEST_true(PACKET_get_net_4(&subpkt, &i))
  120. || !TEST_ulong_eq(i, 0xf8fafcfeUL)
  121. || !TEST_size_t_eq(PACKET_remaining(&subpkt), 0)
  122. || !TEST_false(PACKET_get_sub_packet(&pkt, &subpkt, 4)))
  123. return 0;
  124. return 1;
  125. }
  126. static int test_PACKET_get_bytes(void)
  127. {
  128. const unsigned char *bytes = NULL;
  129. PACKET pkt;
  130. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  131. || !TEST_true(PACKET_get_bytes(&pkt, &bytes, 4))
  132. || !TEST_uchar_eq(bytes[0], 2)
  133. || !TEST_uchar_eq(bytes[1], 4)
  134. || !TEST_uchar_eq(bytes[2], 6)
  135. || !TEST_uchar_eq(bytes[3], 8)
  136. || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN -4)
  137. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
  138. || !TEST_true(PACKET_get_bytes(&pkt, &bytes, 4))
  139. || !TEST_uchar_eq(bytes[0], 0xf8)
  140. || !TEST_uchar_eq(bytes[1], 0xfa)
  141. || !TEST_uchar_eq(bytes[2], 0xfc)
  142. || !TEST_uchar_eq(bytes[3], 0xfe)
  143. || !TEST_false(PACKET_remaining(&pkt)))
  144. return 0;
  145. return 1;
  146. }
  147. static int test_PACKET_copy_bytes(void)
  148. {
  149. unsigned char bytes[4];
  150. PACKET pkt;
  151. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  152. || !TEST_true(PACKET_copy_bytes(&pkt, bytes, 4))
  153. || !TEST_char_eq(bytes[0], 2)
  154. || !TEST_char_eq(bytes[1], 4)
  155. || !TEST_char_eq(bytes[2], 6)
  156. || !TEST_char_eq(bytes[3], 8)
  157. || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN - 4)
  158. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 8))
  159. || !TEST_true(PACKET_copy_bytes(&pkt, bytes, 4))
  160. || !TEST_uchar_eq(bytes[0], 0xf8)
  161. || !TEST_uchar_eq(bytes[1], 0xfa)
  162. || !TEST_uchar_eq(bytes[2], 0xfc)
  163. || !TEST_uchar_eq(bytes[3], 0xfe)
  164. || !TEST_false(PACKET_remaining(&pkt)))
  165. return 0;
  166. return 1;
  167. }
  168. static int test_PACKET_copy_all(void)
  169. {
  170. unsigned char tmp[BUF_LEN];
  171. PACKET pkt;
  172. size_t len;
  173. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  174. || !TEST_true(PACKET_copy_all(&pkt, tmp, BUF_LEN, &len))
  175. || !TEST_size_t_eq(len, BUF_LEN)
  176. || !TEST_mem_eq(smbuf, BUF_LEN, tmp, BUF_LEN)
  177. || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
  178. || !TEST_false(PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)))
  179. return 0;
  180. return 1;
  181. }
  182. static int test_PACKET_memdup(void)
  183. {
  184. unsigned char *data = NULL;
  185. size_t len;
  186. PACKET pkt;
  187. int result = 0;
  188. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  189. || !TEST_true(PACKET_memdup(&pkt, &data, &len))
  190. || !TEST_size_t_eq(len, BUF_LEN)
  191. || !TEST_mem_eq(data, len, PACKET_data(&pkt), len)
  192. || !TEST_true(PACKET_forward(&pkt, 10))
  193. || !TEST_true(PACKET_memdup(&pkt, &data, &len))
  194. || !TEST_size_t_eq(len, BUF_LEN - 10)
  195. || !TEST_mem_eq(data, len, PACKET_data(&pkt), len))
  196. goto end;
  197. result = 1;
  198. end:
  199. OPENSSL_free(data);
  200. return result;
  201. }
  202. static int test_PACKET_strndup(void)
  203. {
  204. char buf1[10], buf2[10];
  205. char *data = NULL;
  206. PACKET pkt;
  207. int result = 0;
  208. memset(buf1, 'x', 10);
  209. memset(buf2, 'y', 10);
  210. buf2[5] = '\0';
  211. if (!TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf1, 10))
  212. || !TEST_true(PACKET_strndup(&pkt, &data))
  213. || !TEST_size_t_eq(strlen(data), 10)
  214. || !TEST_strn_eq(data, buf1, 10)
  215. || !TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf2, 10))
  216. || !TEST_true(PACKET_strndup(&pkt, &data))
  217. || !TEST_size_t_eq(strlen(data), 5)
  218. || !TEST_str_eq(data, buf2))
  219. goto end;
  220. result = 1;
  221. end:
  222. OPENSSL_free(data);
  223. return result;
  224. }
  225. static int test_PACKET_contains_zero_byte(void)
  226. {
  227. char buf1[10], buf2[10];
  228. PACKET pkt;
  229. memset(buf1, 'x', 10);
  230. memset(buf2, 'y', 10);
  231. buf2[5] = '\0';
  232. if (!TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf1, 10))
  233. || !TEST_false(PACKET_contains_zero_byte(&pkt))
  234. || !TEST_true(PACKET_buf_init(&pkt, (unsigned char*)buf2, 10))
  235. || !TEST_true(PACKET_contains_zero_byte(&pkt)))
  236. return 0;
  237. return 1;
  238. }
  239. static int test_PACKET_forward(void)
  240. {
  241. const unsigned char *byte = NULL;
  242. PACKET pkt;
  243. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  244. || !TEST_true(PACKET_forward(&pkt, 1))
  245. || !TEST_true(PACKET_get_bytes(&pkt, &byte, 1))
  246. || !TEST_uchar_eq(byte[0], 4)
  247. || !TEST_true(PACKET_forward(&pkt, BUF_LEN - 3))
  248. || !TEST_true(PACKET_get_bytes(&pkt, &byte, 1))
  249. || !TEST_uchar_eq(byte[0], 0xfe))
  250. return 0;
  251. return 1;
  252. }
  253. static int test_PACKET_buf_init(void)
  254. {
  255. unsigned char buf1[BUF_LEN] = { 0 };
  256. PACKET pkt;
  257. /* Also tests PACKET_remaining() */
  258. if (!TEST_true(PACKET_buf_init(&pkt, buf1, 4))
  259. || !TEST_size_t_eq(PACKET_remaining(&pkt), 4)
  260. || !TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN))
  261. || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
  262. || !TEST_false(PACKET_buf_init(&pkt, buf1, -1)))
  263. return 0;
  264. return 1;
  265. }
  266. static int test_PACKET_null_init(void)
  267. {
  268. PACKET pkt;
  269. PACKET_null_init(&pkt);
  270. if (!TEST_size_t_eq(PACKET_remaining(&pkt), 0)
  271. || !TEST_false(PACKET_forward(&pkt, 1)))
  272. return 0;
  273. return 1;
  274. }
  275. static int test_PACKET_equal(void)
  276. {
  277. PACKET pkt;
  278. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, 4))
  279. || !TEST_true(PACKET_equal(&pkt, smbuf, 4))
  280. || !TEST_false(PACKET_equal(&pkt, smbuf + 1, 4))
  281. || !TEST_true(PACKET_buf_init(&pkt, smbuf, BUF_LEN))
  282. || !TEST_true(PACKET_equal(&pkt, smbuf, BUF_LEN))
  283. || !TEST_false(PACKET_equal(&pkt, smbuf, BUF_LEN - 1))
  284. || !TEST_false(PACKET_equal(&pkt, smbuf, BUF_LEN + 1))
  285. || !TEST_false(PACKET_equal(&pkt, smbuf, 0)))
  286. return 0;
  287. return 1;
  288. }
  289. static int test_PACKET_get_length_prefixed_1(void)
  290. {
  291. unsigned char buf1[BUF_LEN];
  292. const size_t len = 16;
  293. unsigned int i;
  294. PACKET pkt, short_pkt, subpkt;
  295. memset(&subpkt, 0, sizeof(subpkt));
  296. buf1[0] = (unsigned char)len;
  297. for (i = 1; i < BUF_LEN; i++)
  298. buf1[i] = (i * 2) & 0xff;
  299. if (!TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN))
  300. || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len))
  301. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &subpkt))
  302. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)
  303. || !TEST_true(PACKET_get_net_2(&subpkt, &i))
  304. || !TEST_uint_eq(i, 0x0204)
  305. || !TEST_false(PACKET_get_length_prefixed_1(&short_pkt, &subpkt))
  306. || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len))
  307. return 0;
  308. return 1;
  309. }
  310. static int test_PACKET_get_length_prefixed_2(void)
  311. {
  312. unsigned char buf1[1024];
  313. const size_t len = 516; /* 0x0204 */
  314. unsigned int i;
  315. PACKET pkt, short_pkt, subpkt;
  316. memset(&subpkt, 0, sizeof(subpkt));
  317. for (i = 1; i <= 1024; i++)
  318. buf1[i - 1] = (i * 2) & 0xff;
  319. if (!TEST_true(PACKET_buf_init(&pkt, buf1, 1024))
  320. || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len))
  321. || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &subpkt))
  322. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)
  323. || !TEST_true(PACKET_get_net_2(&subpkt, &i))
  324. || !TEST_uint_eq(i, 0x0608)
  325. || !TEST_false(PACKET_get_length_prefixed_2(&short_pkt, &subpkt))
  326. || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len))
  327. return 0;
  328. return 1;
  329. }
  330. static int test_PACKET_get_length_prefixed_3(void)
  331. {
  332. unsigned char buf1[1024];
  333. const size_t len = 516; /* 0x000204 */
  334. unsigned int i;
  335. PACKET pkt, short_pkt, subpkt;
  336. memset(&subpkt, 0, sizeof(subpkt));
  337. for (i = 0; i < 1024; i++)
  338. buf1[i] = (i * 2) & 0xff;
  339. if (!TEST_true(PACKET_buf_init(&pkt, buf1, 1024))
  340. || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len))
  341. || !TEST_true(PACKET_get_length_prefixed_3(&pkt, &subpkt))
  342. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)
  343. || !TEST_true(PACKET_get_net_2(&subpkt, &i))
  344. || !TEST_uint_eq(i, 0x0608)
  345. || !TEST_false(PACKET_get_length_prefixed_3(&short_pkt, &subpkt))
  346. || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len))
  347. return 0;
  348. return 1;
  349. }
  350. static int test_PACKET_as_length_prefixed_1(void)
  351. {
  352. unsigned char buf1[BUF_LEN];
  353. const size_t len = 16;
  354. unsigned int i;
  355. PACKET pkt, exact_pkt, subpkt;
  356. memset(&subpkt, 0, sizeof(subpkt));
  357. buf1[0] = (unsigned char)len;
  358. for (i = 1; i < BUF_LEN; i++)
  359. buf1[i] = (i * 2) & 0xff;
  360. if (!TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN))
  361. || !TEST_true(PACKET_buf_init(&exact_pkt, buf1, len + 1))
  362. || !TEST_false(PACKET_as_length_prefixed_1(&pkt, &subpkt))
  363. || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
  364. || !TEST_true(PACKET_as_length_prefixed_1(&exact_pkt, &subpkt))
  365. || !TEST_size_t_eq(PACKET_remaining(&exact_pkt), 0)
  366. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len))
  367. return 0;
  368. return 1;
  369. }
  370. static int test_PACKET_as_length_prefixed_2(void)
  371. {
  372. unsigned char buf[1024];
  373. const size_t len = 516; /* 0x0204 */
  374. unsigned int i;
  375. PACKET pkt, exact_pkt, subpkt;
  376. memset(&subpkt, 0, sizeof(subpkt));
  377. for (i = 1; i <= 1024; i++)
  378. buf[i-1] = (i * 2) & 0xff;
  379. if (!TEST_true(PACKET_buf_init(&pkt, buf, 1024))
  380. || !TEST_true(PACKET_buf_init(&exact_pkt, buf, len + 2))
  381. || !TEST_false(PACKET_as_length_prefixed_2(&pkt, &subpkt))
  382. || !TEST_size_t_eq(PACKET_remaining(&pkt), 1024)
  383. || !TEST_true(PACKET_as_length_prefixed_2(&exact_pkt, &subpkt))
  384. || !TEST_size_t_eq(PACKET_remaining(&exact_pkt), 0)
  385. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len))
  386. return 0;
  387. return 1;
  388. }
  389. static int test_PACKET_get_quic_vlint(void)
  390. {
  391. struct quic_test_case {
  392. unsigned char buf[16];
  393. size_t expected_read_count;
  394. uint64_t value;
  395. };
  396. static const struct quic_test_case cases[] = {
  397. { {0x00}, 1, 0 },
  398. { {0x01}, 1, 1 },
  399. { {0x3e}, 1, 62 },
  400. { {0x3f}, 1, 63 },
  401. { {0x40,0x00}, 2, 0 },
  402. { {0x40,0x01}, 2, 1 },
  403. { {0x40,0x02}, 2, 2 },
  404. { {0x40,0xff}, 2, 255 },
  405. { {0x41,0x00}, 2, 256 },
  406. { {0x7f,0xfe}, 2, 16382 },
  407. { {0x7f,0xff}, 2, 16383 },
  408. { {0x80,0x00,0x00,0x00}, 4, 0 },
  409. { {0x80,0x00,0x00,0x01}, 4, 1 },
  410. { {0x80,0x00,0x01,0x02}, 4, 258 },
  411. { {0x80,0x18,0x49,0x65}, 4, 1591653 },
  412. { {0xbe,0x18,0x49,0x65}, 4, 1041779045 },
  413. { {0xbf,0xff,0xff,0xff}, 4, 1073741823 },
  414. { {0xc0,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, 8, 0 },
  415. { {0xc0,0x00,0x00,0x00,0x00,0x00,0x01,0x02}, 8, 258 },
  416. { {0xfd,0x1f,0x59,0x8d,0xc9,0xf8,0x71,0x8a}, 8, 4404337426105397642 },
  417. };
  418. PACKET pkt;
  419. size_t i;
  420. uint64_t v;
  421. for (i = 0; i < OSSL_NELEM(cases); ++i) {
  422. memset(&pkt, 0, sizeof(pkt));
  423. v = 55;
  424. if (!TEST_true(PACKET_buf_init(&pkt, cases[i].buf, sizeof(cases[i].buf)))
  425. || !TEST_true(PACKET_get_quic_vlint(&pkt, &v))
  426. || !TEST_uint64_t_eq(v, cases[i].value)
  427. || !TEST_size_t_eq(PACKET_remaining(&pkt),
  428. sizeof(cases[i].buf) - cases[i].expected_read_count)
  429. )
  430. return 0;
  431. }
  432. return 1;
  433. }
  434. static int test_PACKET_get_quic_length_prefixed(void)
  435. {
  436. struct quic_test_case {
  437. unsigned char buf[16];
  438. size_t enclen, len;
  439. int fail;
  440. };
  441. static const struct quic_test_case cases[] = {
  442. /* success cases */
  443. { {0x00}, 1, 0, 0 },
  444. { {0x01}, 1, 1, 0 },
  445. { {0x02}, 1, 2, 0 },
  446. { {0x03}, 1, 3, 0 },
  447. { {0x04}, 1, 4, 0 },
  448. { {0x05}, 1, 5, 0 },
  449. /* failure cases */
  450. { {0x10}, 1, 0, 1 },
  451. { {0x3f}, 1, 0, 1 },
  452. };
  453. size_t i;
  454. PACKET pkt, subpkt = {0};
  455. for (i = 0; i < OSSL_NELEM(cases); ++i) {
  456. memset(&pkt, 0, sizeof(pkt));
  457. if (!TEST_true(PACKET_buf_init(&pkt, cases[i].buf,
  458. cases[i].fail
  459. ? sizeof(cases[i].buf)
  460. : cases[i].enclen + cases[i].len)))
  461. return 0;
  462. if (!TEST_int_eq(PACKET_get_quic_length_prefixed(&pkt, &subpkt), !cases[i].fail))
  463. return 0;
  464. if (cases[i].fail) {
  465. if (!TEST_ptr_eq(pkt.curr, cases[i].buf))
  466. return 0;
  467. continue;
  468. }
  469. if (!TEST_ptr_eq(subpkt.curr, cases[i].buf + cases[i].enclen))
  470. return 0;
  471. if (!TEST_size_t_eq(subpkt.remaining, cases[i].len))
  472. return 0;
  473. }
  474. return 1;
  475. }
  476. int setup_tests(void)
  477. {
  478. unsigned int i;
  479. for (i = 1; i <= BUF_LEN; i++)
  480. smbuf[i - 1] = (i * 2) & 0xff;
  481. ADD_TEST(test_PACKET_buf_init);
  482. ADD_TEST(test_PACKET_null_init);
  483. ADD_TEST(test_PACKET_remaining);
  484. ADD_TEST(test_PACKET_end);
  485. ADD_TEST(test_PACKET_equal);
  486. ADD_TEST(test_PACKET_get_1);
  487. ADD_TEST(test_PACKET_get_4);
  488. ADD_TEST(test_PACKET_get_net_2);
  489. ADD_TEST(test_PACKET_get_net_3);
  490. ADD_TEST(test_PACKET_get_net_4);
  491. ADD_TEST(test_PACKET_get_sub_packet);
  492. ADD_TEST(test_PACKET_get_bytes);
  493. ADD_TEST(test_PACKET_copy_bytes);
  494. ADD_TEST(test_PACKET_copy_all);
  495. ADD_TEST(test_PACKET_memdup);
  496. ADD_TEST(test_PACKET_strndup);
  497. ADD_TEST(test_PACKET_contains_zero_byte);
  498. ADD_TEST(test_PACKET_forward);
  499. ADD_TEST(test_PACKET_get_length_prefixed_1);
  500. ADD_TEST(test_PACKET_get_length_prefixed_2);
  501. ADD_TEST(test_PACKET_get_length_prefixed_3);
  502. ADD_TEST(test_PACKET_as_length_prefixed_1);
  503. ADD_TEST(test_PACKET_as_length_prefixed_2);
  504. ADD_TEST(test_PACKET_get_quic_vlint);
  505. ADD_TEST(test_PACKET_get_quic_length_prefixed);
  506. return 1;
  507. }