packettest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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 "../ssl/packet_locl.h"
  10. #include "testutil.h"
  11. #define BUF_LEN 255
  12. static unsigned char smbuf[BUF_LEN];
  13. static int test_PACKET_remaining(void)
  14. {
  15. PACKET pkt;
  16. if (!TEST_true(PACKET_buf_init(&pkt, smbuf, sizeof(smbuf)))
  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, sizeof(smbuf)))
  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];
  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 = {0};
  295. buf1[0] = (unsigned char)len;
  296. for (i = 1; i < BUF_LEN; i++)
  297. buf1[i] = (i * 2) & 0xff;
  298. if (!TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN))
  299. || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len))
  300. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &subpkt))
  301. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)
  302. || !TEST_true(PACKET_get_net_2(&subpkt, &i))
  303. || !TEST_uint_eq(i, 0x0204)
  304. || !TEST_false(PACKET_get_length_prefixed_1(&short_pkt, &subpkt))
  305. || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len))
  306. return 0;
  307. return 1;
  308. }
  309. static int test_PACKET_get_length_prefixed_2(void)
  310. {
  311. unsigned char buf1[1024];
  312. const size_t len = 516; /* 0x0204 */
  313. unsigned int i;
  314. PACKET pkt, short_pkt, subpkt = {0};
  315. for (i = 1; i <= 1024; i++)
  316. buf1[i - 1] = (i * 2) & 0xff;
  317. if (!TEST_true(PACKET_buf_init(&pkt, buf1, 1024))
  318. || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len))
  319. || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &subpkt))
  320. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)
  321. || !TEST_true(PACKET_get_net_2(&subpkt, &i))
  322. || !TEST_uint_eq(i, 0x0608)
  323. || !TEST_false(PACKET_get_length_prefixed_2(&short_pkt, &subpkt))
  324. || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len))
  325. return 0;
  326. return 1;
  327. }
  328. static int test_PACKET_get_length_prefixed_3(void)
  329. {
  330. unsigned char buf1[1024];
  331. const size_t len = 516; /* 0x000204 */
  332. unsigned int i;
  333. PACKET pkt, short_pkt, subpkt = {0};
  334. for (i = 0; i < 1024; i++)
  335. buf1[i] = (i * 2) & 0xff;
  336. if (!TEST_true(PACKET_buf_init(&pkt, buf1, 1024))
  337. || !TEST_true(PACKET_buf_init(&short_pkt, buf1, len))
  338. || !TEST_true(PACKET_get_length_prefixed_3(&pkt, &subpkt))
  339. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len)
  340. || !TEST_true(PACKET_get_net_2(&subpkt, &i))
  341. || !TEST_uint_eq(i, 0x0608)
  342. || !TEST_false(PACKET_get_length_prefixed_3(&short_pkt, &subpkt))
  343. || !TEST_size_t_eq(PACKET_remaining(&short_pkt), len))
  344. return 0;
  345. return 1;
  346. }
  347. static int test_PACKET_as_length_prefixed_1(void)
  348. {
  349. unsigned char buf1[BUF_LEN];
  350. const size_t len = 16;
  351. unsigned int i;
  352. PACKET pkt, exact_pkt, subpkt = {0};
  353. buf1[0] = (unsigned char)len;
  354. for (i = 1; i < BUF_LEN; i++)
  355. buf1[i] = (i * 2) & 0xff;
  356. if (!TEST_true(PACKET_buf_init(&pkt, buf1, BUF_LEN))
  357. || !TEST_true(PACKET_buf_init(&exact_pkt, buf1, len + 1))
  358. || !TEST_false(PACKET_as_length_prefixed_1(&pkt, &subpkt))
  359. || !TEST_size_t_eq(PACKET_remaining(&pkt), BUF_LEN)
  360. || !TEST_true(PACKET_as_length_prefixed_1(&exact_pkt, &subpkt))
  361. || !TEST_size_t_eq(PACKET_remaining(&exact_pkt), 0)
  362. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len))
  363. return 0;
  364. return 1;
  365. }
  366. static int test_PACKET_as_length_prefixed_2(void)
  367. {
  368. unsigned char buf[1024];
  369. const size_t len = 516; /* 0x0204 */
  370. unsigned int i;
  371. PACKET pkt, exact_pkt, subpkt = {0};
  372. for (i = 1; i <= 1024; i++)
  373. buf[i-1] = (i * 2) & 0xff;
  374. if (!TEST_true(PACKET_buf_init(&pkt, buf, 1024))
  375. || !TEST_true(PACKET_buf_init(&exact_pkt, buf, len + 2))
  376. || !TEST_false(PACKET_as_length_prefixed_2(&pkt, &subpkt))
  377. || !TEST_size_t_eq(PACKET_remaining(&pkt), 1024)
  378. || !TEST_true(PACKET_as_length_prefixed_2(&exact_pkt, &subpkt))
  379. || !TEST_size_t_eq(PACKET_remaining(&exact_pkt), 0)
  380. || !TEST_size_t_eq(PACKET_remaining(&subpkt), len))
  381. return 0;
  382. return 1;
  383. }
  384. int setup_tests(void)
  385. {
  386. unsigned int i;
  387. for (i = 1; i <= BUF_LEN; i++)
  388. smbuf[i - 1] = (i * 2) & 0xff;
  389. ADD_TEST(test_PACKET_buf_init);
  390. ADD_TEST(test_PACKET_null_init);
  391. ADD_TEST(test_PACKET_remaining);
  392. ADD_TEST(test_PACKET_end);
  393. ADD_TEST(test_PACKET_equal);
  394. ADD_TEST(test_PACKET_get_1);
  395. ADD_TEST(test_PACKET_get_4);
  396. ADD_TEST(test_PACKET_get_net_2);
  397. ADD_TEST(test_PACKET_get_net_3);
  398. ADD_TEST(test_PACKET_get_net_4);
  399. ADD_TEST(test_PACKET_get_sub_packet);
  400. ADD_TEST(test_PACKET_get_bytes);
  401. ADD_TEST(test_PACKET_copy_bytes);
  402. ADD_TEST(test_PACKET_copy_all);
  403. ADD_TEST(test_PACKET_memdup);
  404. ADD_TEST(test_PACKET_strndup);
  405. ADD_TEST(test_PACKET_contains_zero_byte);
  406. ADD_TEST(test_PACKET_forward);
  407. ADD_TEST(test_PACKET_get_length_prefixed_1);
  408. ADD_TEST(test_PACKET_get_length_prefixed_2);
  409. ADD_TEST(test_PACKET_get_length_prefixed_3);
  410. ADD_TEST(test_PACKET_as_length_prefixed_1);
  411. ADD_TEST(test_PACKET_as_length_prefixed_2);
  412. return 1;
  413. }