packettest.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /* test/packettest.c */
  2. /*
  3. * Written by Matt Caswell for the OpenSSL project.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2015 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@openssl.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include "../ssl/packet_locl.h"
  59. #define BUF_LEN 255
  60. static int test_PACKET_remaining(unsigned char buf[BUF_LEN])
  61. {
  62. PACKET pkt;
  63. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  64. || PACKET_remaining(&pkt) != BUF_LEN
  65. || !PACKET_forward(&pkt, BUF_LEN - 1)
  66. || PACKET_remaining(&pkt) != 1
  67. || !PACKET_forward(&pkt, 1)
  68. || PACKET_remaining(&pkt) != 0) {
  69. fprintf(stderr, "test_PACKET_remaining() failed\n");
  70. return 0;
  71. }
  72. return 1;
  73. }
  74. static int test_PACKET_get_1(unsigned char buf[BUF_LEN])
  75. {
  76. unsigned int i;
  77. PACKET pkt;
  78. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  79. || !PACKET_get_1(&pkt, &i)
  80. || i != 0x02
  81. || !PACKET_forward(&pkt, BUF_LEN - 2)
  82. || !PACKET_get_1(&pkt, &i)
  83. || i != 0xfe
  84. || PACKET_get_1(&pkt, &i)) {
  85. fprintf(stderr, "test_PACKET_get_1() failed\n");
  86. return 0;
  87. }
  88. return 1;
  89. }
  90. static int test_PACKET_get_4(unsigned char buf[BUF_LEN])
  91. {
  92. unsigned long i;
  93. PACKET pkt;
  94. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  95. || !PACKET_get_4(&pkt, &i)
  96. || i != 0x08060402UL
  97. || !PACKET_forward(&pkt, BUF_LEN - 8)
  98. || !PACKET_get_4(&pkt, &i)
  99. || i != 0xfefcfaf8UL
  100. || PACKET_get_4(&pkt, &i)) {
  101. fprintf(stderr, "test_PACKET_get_4() failed\n");
  102. return 0;
  103. }
  104. return 1;
  105. }
  106. static int test_PACKET_get_net_2(unsigned char buf[BUF_LEN])
  107. {
  108. unsigned int i;
  109. PACKET pkt;
  110. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  111. || !PACKET_get_net_2(&pkt, &i)
  112. || i != 0x0204
  113. || !PACKET_forward(&pkt, BUF_LEN - 4)
  114. || !PACKET_get_net_2(&pkt, &i)
  115. || i != 0xfcfe
  116. || PACKET_get_net_2(&pkt, &i)) {
  117. fprintf(stderr, "test_PACKET_get_net_2() failed\n");
  118. return 0;
  119. }
  120. return 1;
  121. }
  122. static int test_PACKET_get_net_3(unsigned char buf[BUF_LEN])
  123. {
  124. unsigned long i;
  125. PACKET pkt;
  126. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  127. || !PACKET_get_net_3(&pkt, &i)
  128. || i != 0x020406UL
  129. || !PACKET_forward(&pkt, BUF_LEN - 6)
  130. || !PACKET_get_net_3(&pkt, &i)
  131. || i != 0xfafcfeUL
  132. || PACKET_get_net_3(&pkt, &i)) {
  133. fprintf(stderr, "test_PACKET_get_net_3() failed\n");
  134. return 0;
  135. }
  136. return 1;
  137. }
  138. static int test_PACKET_get_net_4(unsigned char buf[BUF_LEN])
  139. {
  140. unsigned long i;
  141. PACKET pkt;
  142. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  143. || !PACKET_get_net_4(&pkt, &i)
  144. || i != 0x02040608UL
  145. || !PACKET_forward(&pkt, BUF_LEN - 8)
  146. || !PACKET_get_net_4(&pkt, &i)
  147. || i != 0xf8fafcfeUL
  148. || PACKET_get_net_4(&pkt, &i)) {
  149. fprintf(stderr, "test_PACKET_get_net_4() failed\n");
  150. return 0;
  151. }
  152. return 1;
  153. }
  154. static int test_PACKET_get_sub_packet(unsigned char buf[BUF_LEN])
  155. {
  156. PACKET pkt, subpkt;
  157. unsigned long i;
  158. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  159. || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
  160. || !PACKET_get_net_4(&subpkt, &i)
  161. || i != 0x02040608UL
  162. || PACKET_remaining(&subpkt)
  163. || !PACKET_forward(&pkt, BUF_LEN - 8)
  164. || !PACKET_get_sub_packet(&pkt, &subpkt, 4)
  165. || !PACKET_get_net_4(&subpkt, &i)
  166. || i != 0xf8fafcfeUL
  167. || PACKET_remaining(&subpkt)
  168. || PACKET_get_sub_packet(&pkt, &subpkt, 4)) {
  169. fprintf(stderr, "test_PACKET_get_sub_packet() failed\n");
  170. return 0;
  171. }
  172. return 1;
  173. }
  174. static int test_PACKET_get_bytes(unsigned char buf[BUF_LEN])
  175. {
  176. unsigned char *bytes;
  177. PACKET pkt;
  178. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  179. || !PACKET_get_bytes(&pkt, &bytes, 4)
  180. || bytes[0] != 2 || bytes[1] != 4
  181. || bytes[2] != 6 || bytes[3] != 8
  182. || PACKET_remaining(&pkt) != BUF_LEN -4
  183. || !PACKET_forward(&pkt, BUF_LEN - 8)
  184. || !PACKET_get_bytes(&pkt, &bytes, 4)
  185. || bytes[0] != 0xf8 || bytes[1] != 0xfa
  186. || bytes[2] != 0xfc || bytes[3] != 0xfe
  187. || PACKET_remaining(&pkt)) {
  188. fprintf(stderr, "test_PACKET_get_bytes() failed\n");
  189. return 0;
  190. }
  191. return 1;
  192. }
  193. static int test_PACKET_copy_bytes(unsigned char buf[BUF_LEN])
  194. {
  195. unsigned char bytes[4];
  196. PACKET pkt;
  197. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  198. || !PACKET_copy_bytes(&pkt, bytes, 4)
  199. || bytes[0] != 2 || bytes[1] != 4
  200. || bytes[2] != 6 || bytes[3] != 8
  201. || PACKET_remaining(&pkt) != BUF_LEN - 4
  202. || !PACKET_forward(&pkt, BUF_LEN - 8)
  203. || !PACKET_copy_bytes(&pkt, bytes, 4)
  204. || bytes[0] != 0xf8 || bytes[1] != 0xfa
  205. || bytes[2] != 0xfc || bytes[3] != 0xfe
  206. || PACKET_remaining(&pkt)) {
  207. fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
  208. return 0;
  209. }
  210. return 1;
  211. }
  212. static int test_PACKET_copy_all(unsigned char buf[BUF_LEN])
  213. {
  214. unsigned char tmp[BUF_LEN];
  215. PACKET pkt;
  216. size_t len;
  217. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  218. || !PACKET_copy_all(&pkt, tmp, BUF_LEN, &len)
  219. || len != BUF_LEN
  220. || memcmp(buf, tmp, BUF_LEN) != 0
  221. || PACKET_remaining(&pkt) != BUF_LEN
  222. || PACKET_copy_all(&pkt, tmp, BUF_LEN - 1, &len)) {
  223. fprintf(stderr, "test_PACKET_copy_bytes() failed\n");
  224. return 0;
  225. }
  226. return 1;
  227. }
  228. static int test_PACKET_memdup(unsigned char buf[BUF_LEN])
  229. {
  230. unsigned char *data = NULL;
  231. size_t len;
  232. PACKET pkt;
  233. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  234. || !PACKET_memdup(&pkt, &data, &len)
  235. || len != BUF_LEN
  236. || memcmp(data, PACKET_data(&pkt), len)
  237. || !PACKET_forward(&pkt, 10)
  238. || !PACKET_memdup(&pkt, &data, &len)
  239. || len != BUF_LEN - 10
  240. || memcmp(data, PACKET_data(&pkt), len)) {
  241. fprintf(stderr, "test_PACKET_memdup() failed\n");
  242. OPENSSL_free(data);
  243. return 0;
  244. }
  245. OPENSSL_free(data);
  246. return 1;
  247. }
  248. static int test_PACKET_strndup()
  249. {
  250. char buf[10], buf2[10];
  251. char *data = NULL;
  252. PACKET pkt;
  253. memset(buf, 'x', 10);
  254. memset(buf2, 'y', 10);
  255. buf2[5] = '\0';
  256. if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10)
  257. || !PACKET_strndup(&pkt, &data)
  258. || strlen(data) != 10
  259. || strncmp(data, buf, 10)
  260. || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10)
  261. || !PACKET_strndup(&pkt, &data)
  262. || strlen(data) != 5
  263. || strcmp(data, buf2)) {
  264. fprintf(stderr, "test_PACKET_strndup failed\n");
  265. OPENSSL_free(data);
  266. return 0;
  267. }
  268. OPENSSL_free(data);
  269. return 1;
  270. }
  271. static int test_PACKET_forward(unsigned char buf[BUF_LEN])
  272. {
  273. unsigned char *byte;
  274. PACKET pkt;
  275. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  276. || !PACKET_forward(&pkt, 1)
  277. || !PACKET_get_bytes(&pkt, &byte, 1)
  278. || byte[0] != 4
  279. || !PACKET_forward(&pkt, BUF_LEN - 3)
  280. || !PACKET_get_bytes(&pkt, &byte, 1)
  281. || byte[0] != 0xfe) {
  282. fprintf(stderr, "test_PACKET_forward() failed\n");
  283. return 0;
  284. }
  285. return 1;
  286. }
  287. static int test_PACKET_buf_init()
  288. {
  289. unsigned char buf[BUF_LEN];
  290. PACKET pkt;
  291. /* Also tests PACKET_remaining() */
  292. if ( !PACKET_buf_init(&pkt, buf, 4)
  293. || PACKET_remaining(&pkt) != 4
  294. || !PACKET_buf_init(&pkt, buf, BUF_LEN)
  295. || PACKET_remaining(&pkt) != BUF_LEN
  296. || PACKET_buf_init(&pkt, buf, -1)) {
  297. fprintf(stderr, "test_PACKET_buf_init() failed\n");
  298. return 0;
  299. }
  300. return 1;
  301. }
  302. static int test_PACKET_null_init()
  303. {
  304. PACKET pkt;
  305. PACKET_null_init(&pkt);
  306. if ( PACKET_remaining(&pkt) != 0
  307. || PACKET_forward(&pkt, 1)) {
  308. fprintf(stderr, "test_PACKET_null_init() failed\n");
  309. return 0;
  310. }
  311. return 1;
  312. }
  313. static int test_PACKET_equal(unsigned char buf[BUF_LEN])
  314. {
  315. PACKET pkt;
  316. if ( !PACKET_buf_init(&pkt, buf, 4)
  317. || !PACKET_equal(&pkt, buf, 4)
  318. || PACKET_equal(&pkt, buf + 1, 4)
  319. || !PACKET_buf_init(&pkt, buf, BUF_LEN)
  320. || !PACKET_equal(&pkt, buf, BUF_LEN)
  321. || PACKET_equal(&pkt, buf, BUF_LEN - 1)
  322. || PACKET_equal(&pkt, buf, BUF_LEN + 1)
  323. || PACKET_equal(&pkt, buf, 0)) {
  324. fprintf(stderr, "test_PACKET_equal() failed\n");
  325. return 0;
  326. }
  327. return 1;
  328. }
  329. static int test_PACKET_get_length_prefixed_1()
  330. {
  331. unsigned char buf[BUF_LEN];
  332. const size_t len = 16;
  333. unsigned int i;
  334. PACKET pkt, short_pkt, subpkt;
  335. buf[0] = len;
  336. for (i = 1; i < BUF_LEN; i++) {
  337. buf[i] = (i * 2) & 0xff;
  338. }
  339. if ( !PACKET_buf_init(&pkt, buf, BUF_LEN)
  340. || !PACKET_buf_init(&short_pkt, buf, len)
  341. || !PACKET_get_length_prefixed_1(&pkt, &subpkt)
  342. || PACKET_remaining(&subpkt) != len
  343. || !PACKET_get_net_2(&subpkt, &i)
  344. || i != 0x0204
  345. || PACKET_get_length_prefixed_1(&short_pkt, &subpkt)
  346. || PACKET_remaining(&short_pkt) != len) {
  347. fprintf(stderr, "test_PACKET_get_length_prefixed_1() failed\n");
  348. return 0;
  349. }
  350. return 1;
  351. }
  352. static int test_PACKET_get_length_prefixed_2()
  353. {
  354. unsigned char buf[1024];
  355. const size_t len = 516; /* 0x0204 */
  356. unsigned int i;
  357. PACKET pkt, short_pkt, subpkt;
  358. for (i = 1; i <= 1024; i++) {
  359. buf[i-1] = (i * 2) & 0xff;
  360. }
  361. if ( !PACKET_buf_init(&pkt, buf, 1024)
  362. || !PACKET_buf_init(&short_pkt, buf, len)
  363. || !PACKET_get_length_prefixed_2(&pkt, &subpkt)
  364. || PACKET_remaining(&subpkt) != len
  365. || !PACKET_get_net_2(&subpkt, &i)
  366. || i != 0x0608
  367. || PACKET_get_length_prefixed_2(&short_pkt, &subpkt)
  368. || PACKET_remaining(&short_pkt) != len) {
  369. fprintf(stderr, "test_PACKET_get_length_prefixed_2() failed\n");
  370. return 0;
  371. }
  372. return 1;
  373. }
  374. static int test_PACKET_get_length_prefixed_3()
  375. {
  376. unsigned char buf[1024];
  377. const size_t len = 516; /* 0x000204 */
  378. unsigned int i;
  379. PACKET pkt, short_pkt, subpkt;
  380. for (i = 0; i < 1024; i++) {
  381. buf[i] = (i * 2) & 0xff;
  382. }
  383. if ( !PACKET_buf_init(&pkt, buf, 1024)
  384. || !PACKET_buf_init(&short_pkt, buf, len)
  385. || !PACKET_get_length_prefixed_3(&pkt, &subpkt)
  386. || PACKET_remaining(&subpkt) != len
  387. || !PACKET_get_net_2(&subpkt, &i)
  388. || i != 0x0608
  389. || PACKET_get_length_prefixed_3(&short_pkt, &subpkt)
  390. || PACKET_remaining(&short_pkt) != len) {
  391. fprintf(stderr, "test_PACKET_get_length_prefixed_3() failed\n");
  392. return 0;
  393. }
  394. return 1;
  395. }
  396. int main(int argc, char **argv)
  397. {
  398. unsigned char buf[BUF_LEN];
  399. unsigned int i;
  400. for (i=1; i<=BUF_LEN; i++) {
  401. buf[i-1] = (i * 2) & 0xff;
  402. }
  403. i = 0;
  404. if ( !test_PACKET_buf_init()
  405. || !test_PACKET_null_init()
  406. || !test_PACKET_remaining(buf)
  407. || !test_PACKET_equal(buf)
  408. || !test_PACKET_get_1(buf)
  409. || !test_PACKET_get_4(buf)
  410. || !test_PACKET_get_net_2(buf)
  411. || !test_PACKET_get_net_3(buf)
  412. || !test_PACKET_get_net_4(buf)
  413. || !test_PACKET_get_sub_packet(buf)
  414. || !test_PACKET_get_bytes(buf)
  415. || !test_PACKET_copy_bytes(buf)
  416. || !test_PACKET_copy_all(buf)
  417. || !test_PACKET_memdup(buf)
  418. || !test_PACKET_strndup()
  419. || !test_PACKET_forward(buf)
  420. || !test_PACKET_get_length_prefixed_1()
  421. || !test_PACKET_get_length_prefixed_2()
  422. || !test_PACKET_get_length_prefixed_3()) {
  423. return 1;
  424. }
  425. printf("PASS\n");
  426. return 0;
  427. }