quic_wire_test.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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 "internal/quic_wire.h"
  11. #include "testutil.h"
  12. struct encode_test_case {
  13. int (*serializer)(WPACKET *pkt);
  14. const unsigned char *expect_buf;
  15. size_t expect_buf_len;
  16. /*
  17. * fail: -1 if not truncated (function should test for success), else number
  18. * of bytes to which the input has been truncated (function should test that
  19. * decoding fails)
  20. */
  21. int (*deserializer)(PACKET *pkt, ossl_ssize_t fail);
  22. };
  23. /* 1. PADDING */
  24. static int encode_case_1_enc(WPACKET *pkt)
  25. {
  26. if (!TEST_int_eq(ossl_quic_wire_encode_padding(pkt, 3), 1))
  27. return 0;
  28. return 1;
  29. }
  30. static int encode_case_1_dec(PACKET *pkt, ossl_ssize_t fail)
  31. {
  32. if (fail >= 0)
  33. /* No failure modes for padding */
  34. return 1;
  35. if (!TEST_int_eq(ossl_quic_wire_decode_padding(pkt), 3))
  36. return 0;
  37. return 1;
  38. }
  39. static const unsigned char encode_case_1_expect[] = {
  40. 0, 0, 0
  41. };
  42. /* 2. PING */
  43. static int encode_case_2_enc(WPACKET *pkt)
  44. {
  45. if (!TEST_int_eq(ossl_quic_wire_encode_frame_ping(pkt), 1))
  46. return 0;
  47. return 1;
  48. }
  49. static int encode_case_2_dec(PACKET *pkt, ossl_ssize_t fail)
  50. {
  51. if (!TEST_int_eq(ossl_quic_wire_decode_frame_ping(pkt), fail < 0))
  52. return 0;
  53. return 1;
  54. }
  55. static const unsigned char encode_case_2_expect[] = {
  56. 0x01
  57. };
  58. /* 3. ACK */
  59. static const OSSL_QUIC_ACK_RANGE encode_case_3_ranges[] = {
  60. { 20, 30 },
  61. { 0, 10 }
  62. };
  63. static const OSSL_QUIC_FRAME_ACK encode_case_3_f = {
  64. (OSSL_QUIC_ACK_RANGE *)encode_case_3_ranges,
  65. OSSL_NELEM(encode_case_3_ranges),
  66. { OSSL_TIME_MS },
  67. 60, 70, 80, 1
  68. };
  69. static int encode_case_3_enc(WPACKET *pkt)
  70. {
  71. if (!TEST_int_eq(ossl_quic_wire_encode_frame_ack(pkt, 3, &encode_case_3_f), 1))
  72. return 0;
  73. return 1;
  74. }
  75. static int encode_case_3_dec(PACKET *pkt, ossl_ssize_t fail)
  76. {
  77. OSSL_QUIC_ACK_RANGE ranges[4] = {0};
  78. OSSL_QUIC_FRAME_ACK f = {0};
  79. uint64_t total_ranges = 0, peek_total_ranges = 0;
  80. int ret;
  81. f.ack_ranges = ranges;
  82. f.num_ack_ranges = OSSL_NELEM(ranges);
  83. ret = ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &peek_total_ranges);
  84. if (fail < 0 && !TEST_int_eq(ret, 1))
  85. return 0;
  86. if (!TEST_int_eq(ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges), fail < 0))
  87. return 0;
  88. if (ret == 1 && !TEST_uint64_t_eq(peek_total_ranges, 2))
  89. return 0;
  90. if (fail >= 0)
  91. return 1;
  92. if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
  93. return 0;
  94. if (!TEST_mem_eq(f.ack_ranges, f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE),
  95. encode_case_3_f.ack_ranges,
  96. encode_case_3_f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE)))
  97. return 0;
  98. if (!TEST_uint64_t_eq(ossl_time2ticks(f.delay_time),
  99. ossl_time2ticks(encode_case_3_f.delay_time)))
  100. return 0;
  101. if (!TEST_true(f.ecn_present))
  102. return 0;
  103. if (!TEST_uint64_t_eq(f.ect0, encode_case_3_f.ect0))
  104. return 0;
  105. if (!TEST_uint64_t_eq(f.ect1, encode_case_3_f.ect1))
  106. return 0;
  107. if (!TEST_uint64_t_eq(f.ecnce, encode_case_3_f.ecnce))
  108. return 0;
  109. return 1;
  110. }
  111. static const unsigned char encode_case_3_expect[] = {
  112. 0x03, /* Type */
  113. 0x1E, /* Largest Acknowledged */
  114. 0x40, 0x7d, /* ACK Delay */
  115. 1, /* ACK Range Count */
  116. 10, /* First ACK Range */
  117. 8, /* Gap */
  118. 10, /* Length */
  119. 0x3c, /* ECT0 Count */
  120. 0x40, 0x46, /* ECT1 Count */
  121. 0x40, 0x50, /* ECNCE Count */
  122. };
  123. /* 4. RESET_STREAM */
  124. static const OSSL_QUIC_FRAME_RESET_STREAM encode_case_4_f = {
  125. 0x1234, 0x9781, 0x11717
  126. };
  127. static int encode_case_4_enc(WPACKET *pkt)
  128. {
  129. if (!TEST_int_eq(ossl_quic_wire_encode_frame_reset_stream(pkt,
  130. &encode_case_4_f), 1))
  131. return 0;
  132. return 1;
  133. }
  134. static int encode_case_4_dec(PACKET *pkt, ossl_ssize_t fail)
  135. {
  136. OSSL_QUIC_FRAME_RESET_STREAM f = {0};
  137. if (!TEST_int_eq(ossl_quic_wire_decode_frame_reset_stream(pkt, &f), fail < 0))
  138. return 0;
  139. if (fail >= 0)
  140. return 1;
  141. if (!TEST_mem_eq(&f, sizeof(f), &encode_case_4_f, sizeof(encode_case_4_f)))
  142. return 0;
  143. return 1;
  144. }
  145. static const unsigned char encode_case_4_expect[] = {
  146. 0x04, /* Type */
  147. 0x52, 0x34, /* Stream ID */
  148. 0x80, 0x00, 0x97, 0x81, /* App Error Code */
  149. 0x80, 0x01, 0x17, 0x17, /* Final Size */
  150. };
  151. /* 5. STOP_SENDING */
  152. static const OSSL_QUIC_FRAME_STOP_SENDING encode_case_5_f = {
  153. 0x1234, 0x9781
  154. };
  155. static int encode_case_5_enc(WPACKET *pkt)
  156. {
  157. if (!TEST_int_eq(ossl_quic_wire_encode_frame_stop_sending(pkt,
  158. &encode_case_5_f), 1))
  159. return 0;
  160. return 1;
  161. }
  162. static int encode_case_5_dec(PACKET *pkt, ossl_ssize_t fail)
  163. {
  164. OSSL_QUIC_FRAME_STOP_SENDING f = {0};
  165. if (!TEST_int_eq(ossl_quic_wire_decode_frame_stop_sending(pkt, &f), fail < 0))
  166. return 0;
  167. if (fail >= 0)
  168. return 1;
  169. if (!TEST_mem_eq(&f, sizeof(f), &encode_case_5_f, sizeof(encode_case_5_f)))
  170. return 0;
  171. return 1;
  172. }
  173. static const unsigned char encode_case_5_expect[] = {
  174. 0x05, /* Type */
  175. 0x52, 0x34, /* Stream ID */
  176. 0x80, 0x00, 0x97, 0x81 /* App Error Code */
  177. };
  178. /* 6. CRYPTO */
  179. static const unsigned char encode_case_6_data[] = {
  180. 93, 18, 17, 102, 33
  181. };
  182. static const OSSL_QUIC_FRAME_CRYPTO encode_case_6_f = {
  183. 0x1234, sizeof(encode_case_6_data), encode_case_6_data
  184. };
  185. static int encode_case_6_enc(WPACKET *pkt)
  186. {
  187. if (!TEST_ptr(ossl_quic_wire_encode_frame_crypto(pkt,
  188. &encode_case_6_f)))
  189. return 0;
  190. return 1;
  191. }
  192. static int encode_case_6_dec(PACKET *pkt, ossl_ssize_t fail)
  193. {
  194. OSSL_QUIC_FRAME_CRYPTO f = {0};
  195. if (!TEST_int_eq(ossl_quic_wire_decode_frame_crypto(pkt, &f), fail < 0))
  196. return 0;
  197. if (fail >= 0)
  198. return 1;
  199. if (!TEST_uint64_t_eq(f.offset, 0x1234))
  200. return 0;
  201. if (!TEST_mem_eq(f.data, f.len, encode_case_6_data, sizeof(encode_case_6_data)))
  202. return 0;
  203. return 1;
  204. }
  205. static const unsigned char encode_case_6_expect[] = {
  206. 0x06, /* Type */
  207. 0x52, 0x34, /* Offset */
  208. 0x05, /* Length */
  209. 93, 18, 17, 102, 33 /* Data */
  210. };
  211. /* 7. NEW_TOKEN */
  212. static const unsigned char encode_case_7_token[] = {
  213. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
  214. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  215. };
  216. static int encode_case_7_enc(WPACKET *pkt)
  217. {
  218. if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_token(pkt,
  219. encode_case_7_token,
  220. sizeof(encode_case_7_token)), 1))
  221. return 0;
  222. return 1;
  223. }
  224. static int encode_case_7_dec(PACKET *pkt, ossl_ssize_t fail)
  225. {
  226. const unsigned char *token = NULL;
  227. size_t token_len = 0;
  228. if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_token(pkt,
  229. &token,
  230. &token_len), fail < 0))
  231. return 0;
  232. if (fail >= 0)
  233. return 1;
  234. if (!TEST_mem_eq(token, token_len,
  235. encode_case_7_token, sizeof(encode_case_7_token)))
  236. return 0;
  237. return 1;
  238. }
  239. static const unsigned char encode_case_7_expect[] = {
  240. 0x07, /* Type */
  241. 0x10, /* Length */
  242. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Token */
  243. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  244. };
  245. /* 8. STREAM (no length, no offset, no fin) */
  246. static const unsigned char encode_case_8_data[] = {
  247. 0xde, 0x06, 0xcb, 0x76, 0x5d
  248. };
  249. static const OSSL_QUIC_FRAME_STREAM encode_case_8_f = {
  250. 0x1234, 0, 5, encode_case_8_data, 0, 0
  251. };
  252. static int encode_case_8_enc(WPACKET *pkt)
  253. {
  254. if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
  255. &encode_case_8_f)))
  256. return 0;
  257. return 1;
  258. }
  259. static int encode_case_8_dec(PACKET *pkt, ossl_ssize_t fail)
  260. {
  261. OSSL_QUIC_FRAME_STREAM f = {0};
  262. if (fail >= 3)
  263. /*
  264. * This case uses implicit length signalling so truncation will not
  265. * cause it to fail unless the header (which is 3 bytes) is truncated.
  266. */
  267. return 1;
  268. if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, &f), fail < 0))
  269. return 0;
  270. if (fail >= 0)
  271. return 1;
  272. if (!TEST_mem_eq(f.data, f.len,
  273. encode_case_8_data, sizeof(encode_case_8_data)))
  274. return 0;
  275. if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
  276. return 0;
  277. if (!TEST_uint64_t_eq(f.offset, 0))
  278. return 0;
  279. if (!TEST_int_eq(f.has_explicit_len, 0))
  280. return 0;
  281. if (!TEST_int_eq(f.is_fin, 0))
  282. return 0;
  283. return 1;
  284. }
  285. static const unsigned char encode_case_8_expect[] = {
  286. 0x08, /* Type (OFF=0, LEN=0, FIN=0) */
  287. 0x52, 0x34, /* Stream ID */
  288. 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
  289. };
  290. /* 9. STREAM (length, offset, fin) */
  291. static const unsigned char encode_case_9_data[] = {
  292. 0xde, 0x06, 0xcb, 0x76, 0x5d
  293. };
  294. static const OSSL_QUIC_FRAME_STREAM encode_case_9_f = {
  295. 0x1234, 0x39, 5, encode_case_9_data, 1, 1
  296. };
  297. static int encode_case_9_enc(WPACKET *pkt)
  298. {
  299. if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
  300. &encode_case_9_f)))
  301. return 0;
  302. return 1;
  303. }
  304. static int encode_case_9_dec(PACKET *pkt, ossl_ssize_t fail)
  305. {
  306. OSSL_QUIC_FRAME_STREAM f = {0};
  307. if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, &f), fail < 0))
  308. return 0;
  309. if (fail >= 0)
  310. return 1;
  311. if (!TEST_mem_eq(f.data, f.len,
  312. encode_case_9_data, sizeof(encode_case_9_data)))
  313. return 0;
  314. if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
  315. return 0;
  316. if (!TEST_uint64_t_eq(f.offset, 0x39))
  317. return 0;
  318. if (!TEST_int_eq(f.has_explicit_len, 1))
  319. return 0;
  320. if (!TEST_int_eq(f.is_fin, 1))
  321. return 0;
  322. return 1;
  323. }
  324. static const unsigned char encode_case_9_expect[] = {
  325. 0x0f, /* Type (OFF=1, LEN=1, FIN=1) */
  326. 0x52, 0x34, /* Stream ID */
  327. 0x39, /* Offset */
  328. 0x05, /* Length */
  329. 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
  330. };
  331. /* 10. MAX_DATA */
  332. static int encode_case_10_enc(WPACKET *pkt)
  333. {
  334. if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_data(pkt, 0x1234), 1))
  335. return 0;
  336. return 1;
  337. }
  338. static int encode_case_10_dec(PACKET *pkt, ossl_ssize_t fail)
  339. {
  340. uint64_t max_data = 0;
  341. if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_data(pkt, &max_data), fail < 0))
  342. return 0;
  343. if (fail >= 0)
  344. return 1;
  345. if (!TEST_uint64_t_eq(max_data, 0x1234))
  346. return 0;
  347. return 1;
  348. }
  349. static const unsigned char encode_case_10_expect[] = {
  350. 0x10, /* Type */
  351. 0x52, 0x34, /* Max Data */
  352. };
  353. /* 11. MAX_STREAM_DATA */
  354. static int encode_case_11_enc(WPACKET *pkt)
  355. {
  356. if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_stream_data(pkt,
  357. 0x1234,
  358. 0x9781), 1))
  359. return 0;
  360. return 1;
  361. }
  362. static int encode_case_11_dec(PACKET *pkt, ossl_ssize_t fail)
  363. {
  364. uint64_t stream_id = 0, max_data = 0;
  365. if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_stream_data(pkt,
  366. &stream_id,
  367. &max_data), fail < 0))
  368. return 0;
  369. if (fail >= 0)
  370. return 1;
  371. if (!TEST_uint64_t_eq(stream_id, 0x1234))
  372. return 0;
  373. if (!TEST_uint64_t_eq(max_data, 0x9781))
  374. return 0;
  375. return 1;
  376. }
  377. static const unsigned char encode_case_11_expect[] = {
  378. 0x11, /* Type */
  379. 0x52, 0x34, /* Stream ID */
  380. 0x80, 0x00, 0x97, 0x81, /* Max Data */
  381. };
  382. /* 12. MAX_STREAMS */
  383. static int encode_case_12_enc(WPACKET *pkt)
  384. {
  385. if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 0, 0x1234), 1))
  386. return 0;
  387. if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 1, 0x9781), 1))
  388. return 0;
  389. return 1;
  390. }
  391. static int encode_case_12_dec(PACKET *pkt, ossl_ssize_t fail)
  392. {
  393. uint64_t max_streams_1 = 0, max_streams_2 = 0,
  394. frame_type_1 = 0, frame_type_2 = 0;
  395. if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1),
  396. fail < 0 || fail >= 1))
  397. return 0;
  398. if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
  399. &max_streams_1),
  400. fail < 0 || fail >= 3))
  401. return 0;
  402. if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2),
  403. fail < 0 || fail >= 4))
  404. return 0;
  405. if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
  406. &max_streams_2),
  407. fail < 0))
  408. return 0;
  409. if ((fail < 0 || fail >= 3)
  410. && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI))
  411. return 0;
  412. if ((fail < 0 || fail >= 3)
  413. && !TEST_uint64_t_eq(max_streams_1, 0x1234))
  414. return 0;
  415. if ((fail < 0 || fail >= 8)
  416. && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI))
  417. return 0;
  418. if ((fail < 0 || fail >= 8)
  419. && !TEST_uint64_t_eq(max_streams_2, 0x9781))
  420. return 0;
  421. return 1;
  422. }
  423. static const unsigned char encode_case_12_expect[] = {
  424. 0x12, /* Type (MAX_STREAMS Bidirectional) */
  425. 0x52, 0x34, /* Max Streams */
  426. 0x13, /* Type (MAX_STREAMS Unidirectional) */
  427. 0x80, 0x00, 0x97, 0x81, /* Max Streams */
  428. };
  429. /* 13. DATA_BLOCKED */
  430. static int encode_case_13_enc(WPACKET *pkt)
  431. {
  432. if (!TEST_int_eq(ossl_quic_wire_encode_frame_data_blocked(pkt, 0x1234), 1))
  433. return 0;
  434. return 1;
  435. }
  436. static int encode_case_13_dec(PACKET *pkt, ossl_ssize_t fail)
  437. {
  438. uint64_t max_data = 0;
  439. if (!TEST_int_eq(ossl_quic_wire_decode_frame_data_blocked(pkt,
  440. &max_data), fail < 0))
  441. return 0;
  442. if (fail >= 0)
  443. return 1;
  444. if (!TEST_uint64_t_eq(max_data, 0x1234))
  445. return 0;
  446. return 1;
  447. }
  448. static const unsigned char encode_case_13_expect[] = {
  449. 0x14, /* Type */
  450. 0x52, 0x34, /* Max Data */
  451. };
  452. /* 14. STREAM_DATA_BLOCKED */
  453. static int encode_case_14_enc(WPACKET *pkt)
  454. {
  455. if (!TEST_int_eq(ossl_quic_wire_encode_frame_stream_data_blocked(pkt,
  456. 0x1234,
  457. 0x9781), 1))
  458. return 0;
  459. return 1;
  460. }
  461. static int encode_case_14_dec(PACKET *pkt, ossl_ssize_t fail)
  462. {
  463. uint64_t stream_id = 0, max_data = 0;
  464. if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream_data_blocked(pkt,
  465. &stream_id,
  466. &max_data), fail < 0))
  467. return 0;
  468. if (fail >= 0)
  469. return 1;
  470. if (!TEST_uint64_t_eq(stream_id, 0x1234))
  471. return 0;
  472. if (!TEST_uint64_t_eq(max_data, 0x9781))
  473. return 0;
  474. return 1;
  475. }
  476. static const unsigned char encode_case_14_expect[] = {
  477. 0x15, /* Type */
  478. 0x52, 0x34, /* Stream ID */
  479. 0x80, 0x00, 0x97, 0x81, /* Max Data */
  480. };
  481. /* 15. STREAMS_BLOCKED */
  482. static int encode_case_15_enc(WPACKET *pkt)
  483. {
  484. if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 0, 0x1234), 1))
  485. return 0;
  486. if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 1, 0x9781), 1))
  487. return 0;
  488. return 1;
  489. }
  490. static int encode_case_15_dec(PACKET *pkt, ossl_ssize_t fail)
  491. {
  492. uint64_t max_streams_1 = 0, max_streams_2 = 0,
  493. frame_type_1 = 0, frame_type_2 = 0;
  494. if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1),
  495. fail < 0 || fail >= 1))
  496. return 0;
  497. if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
  498. &max_streams_1),
  499. fail < 0 || fail >= 3))
  500. return 0;
  501. if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2),
  502. fail < 0 || fail >= 4))
  503. return 0;
  504. if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
  505. &max_streams_2),
  506. fail < 0 || fail >= 8))
  507. return 0;
  508. if ((fail < 0 || fail >= 1)
  509. && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI))
  510. return 0;
  511. if ((fail < 0 || fail >= 3)
  512. && !TEST_uint64_t_eq(max_streams_1, 0x1234))
  513. return 0;
  514. if ((fail < 0 || fail >= 4)
  515. && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI))
  516. return 0;
  517. if ((fail < 0 || fail >= 8)
  518. && !TEST_uint64_t_eq(max_streams_2, 0x9781))
  519. return 0;
  520. return 1;
  521. }
  522. static const unsigned char encode_case_15_expect[] = {
  523. 0x16, /* Type (STREAMS_BLOCKED Bidirectional) */
  524. 0x52, 0x34, /* Max Streams */
  525. 0x17, /* Type (STREAMS_BLOCKED Unidirectional) */
  526. 0x80, 0x00, 0x97, 0x81, /* Max Streams */
  527. };
  528. /* 16. NEW_CONNECTION_ID */
  529. static const unsigned char encode_case_16_conn_id[] = {
  530. 0x33, 0x44, 0x55, 0x66
  531. };
  532. static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16_f = {
  533. 0x1234,
  534. 0x9781,
  535. {
  536. 0x4,
  537. {0x33, 0x44, 0x55, 0x66}
  538. },
  539. {
  540. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
  541. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  542. }
  543. };
  544. static int encode_case_16_enc(WPACKET *pkt)
  545. {
  546. if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
  547. &encode_case_16_f), 1))
  548. return 0;
  549. return 1;
  550. }
  551. static int encode_case_16_dec(PACKET *pkt, ossl_ssize_t fail)
  552. {
  553. OSSL_QUIC_FRAME_NEW_CONN_ID f = {0};
  554. if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), fail < 0))
  555. return 0;
  556. if (fail >= 0)
  557. return 1;
  558. if (!TEST_uint64_t_eq(f.seq_num, 0x1234))
  559. return 0;
  560. if (!TEST_uint64_t_eq(f.retire_prior_to, 0x9781))
  561. return 0;
  562. if (!TEST_uint64_t_eq(f.conn_id.id_len, sizeof(encode_case_16_conn_id)))
  563. return 0;
  564. if (!TEST_mem_eq(f.conn_id.id, f.conn_id.id_len,
  565. encode_case_16_conn_id, sizeof(encode_case_16_conn_id)))
  566. return 0;
  567. if (!TEST_mem_eq(f.stateless_reset_token,
  568. sizeof(f.stateless_reset_token),
  569. encode_case_16_f.stateless_reset_token,
  570. sizeof(encode_case_16_f.stateless_reset_token)))
  571. return 0;
  572. return 1;
  573. }
  574. static const unsigned char encode_case_16_expect[] = {
  575. 0x18, /* Type */
  576. 0x52, 0x34, /* Sequence Number */
  577. 0x80, 0x00, 0x97, 0x81, /* Retire Prior To */
  578. 0x04, /* Connection ID Length */
  579. 0x33, 0x44, 0x55, 0x66, /* Connection ID */
  580. 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
  581. 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
  582. };
  583. /* 17. RETIRE_CONNECTION_ID */
  584. static int encode_case_17_enc(WPACKET *pkt)
  585. {
  586. if (!TEST_int_eq(ossl_quic_wire_encode_frame_retire_conn_id(pkt, 0x1234), 1))
  587. return 0;
  588. return 1;
  589. }
  590. static int encode_case_17_dec(PACKET *pkt, ossl_ssize_t fail)
  591. {
  592. uint64_t seq_num = 0;
  593. if (!TEST_int_eq(ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num), fail < 0))
  594. return 0;
  595. if (fail >= 0)
  596. return 1;
  597. if (!TEST_uint64_t_eq(seq_num, 0x1234))
  598. return 0;
  599. return 1;
  600. }
  601. static const unsigned char encode_case_17_expect[] = {
  602. 0x19, /* Type */
  603. 0x52, 0x34, /* Seq Num */
  604. };
  605. /* 18. PATH_CHALLENGE */
  606. static const uint64_t encode_case_18_data
  607. = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
  608. static int encode_case_18_enc(WPACKET *pkt)
  609. {
  610. if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_challenge(pkt,
  611. encode_case_18_data), 1))
  612. return 0;
  613. return 1;
  614. }
  615. static int encode_case_18_dec(PACKET *pkt, ossl_ssize_t fail)
  616. {
  617. uint64_t challenge = 0;
  618. if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_challenge(pkt, &challenge), fail < 0))
  619. return 0;
  620. if (fail >= 0)
  621. return 1;
  622. if (!TEST_uint64_t_eq(challenge, encode_case_18_data))
  623. return 0;
  624. return 1;
  625. }
  626. static const unsigned char encode_case_18_expect[] = {
  627. 0x1A, /* Type */
  628. 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
  629. };
  630. /* 19. PATH_RESPONSE */
  631. static const uint64_t encode_case_19_data
  632. = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
  633. static int encode_case_19_enc(WPACKET *pkt)
  634. {
  635. if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_response(pkt,
  636. encode_case_19_data), 1))
  637. return 0;
  638. return 1;
  639. }
  640. static int encode_case_19_dec(PACKET *pkt, ossl_ssize_t fail)
  641. {
  642. uint64_t challenge = 0;
  643. if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_response(pkt, &challenge), fail < 0))
  644. return 0;
  645. if (fail >= 0)
  646. return 1;
  647. if (!TEST_uint64_t_eq(challenge, encode_case_19_data))
  648. return 0;
  649. return 1;
  650. }
  651. static const unsigned char encode_case_19_expect[] = {
  652. 0x1B, /* Type */
  653. 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
  654. };
  655. /* 20. CONNECTION_CLOSE (transport) */
  656. static const char encode_case_20_reason[] = {
  657. /* "reason for closure" */
  658. 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f,
  659. 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
  660. };
  661. static const OSSL_QUIC_FRAME_CONN_CLOSE encode_case_20_f = {
  662. 0,
  663. 0x1234,
  664. 0x9781,
  665. encode_case_20_reason,
  666. sizeof(encode_case_20_reason)
  667. };
  668. static int encode_case_20_enc(WPACKET *pkt)
  669. {
  670. if (!TEST_int_eq(ossl_quic_wire_encode_frame_conn_close(pkt,
  671. &encode_case_20_f), 1))
  672. return 0;
  673. return 1;
  674. }
  675. static int encode_case_20_dec(PACKET *pkt, ossl_ssize_t fail)
  676. {
  677. OSSL_QUIC_FRAME_CONN_CLOSE f = {0};
  678. if (!TEST_int_eq(ossl_quic_wire_decode_frame_conn_close(pkt, &f), fail < 0))
  679. return 0;
  680. if (fail >= 0)
  681. return 1;
  682. if (!TEST_int_eq(f.is_app, 0))
  683. return 0;
  684. if (!TEST_uint64_t_eq(f.error_code, 0x1234))
  685. return 0;
  686. if (!TEST_uint64_t_eq(f.frame_type, 0x9781))
  687. return 0;
  688. if (!TEST_size_t_eq(f.reason_len, 18))
  689. return 0;
  690. if (!TEST_mem_eq(f.reason, f.reason_len,
  691. encode_case_20_f.reason, encode_case_20_f.reason_len))
  692. return 0;
  693. return 1;
  694. }
  695. static const unsigned char encode_case_20_expect[] = {
  696. 0x1C, /* Type */
  697. 0x52, 0x34, /* Sequence Number */
  698. 0x80, 0x00, 0x97, 0x81, /* Frame Type */
  699. 0x12, /* Reason Length */
  700. 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f, /* Reason */
  701. 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
  702. };
  703. /* 21. HANDSHAKE_DONE */
  704. static int encode_case_21_enc(WPACKET *pkt)
  705. {
  706. if (!TEST_int_eq(ossl_quic_wire_encode_frame_handshake_done(pkt), 1))
  707. return 0;
  708. return 1;
  709. }
  710. static int encode_case_21_dec(PACKET *pkt, ossl_ssize_t fail)
  711. {
  712. if (!TEST_int_eq(ossl_quic_wire_decode_frame_handshake_done(pkt), fail < 0))
  713. return 0;
  714. return 1;
  715. }
  716. static const unsigned char encode_case_21_expect[] = {
  717. 0x1E
  718. };
  719. /* 22. Buffer Transport Parameter */
  720. static const unsigned char encode_case_22_data[] = {0x55,0x77,0x32,0x46,0x99};
  721. static int encode_case_22_enc(WPACKET *pkt)
  722. {
  723. unsigned char *p;
  724. if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(pkt, 0x1234,
  725. encode_case_22_data,
  726. sizeof(encode_case_22_data))))
  727. return 0;
  728. if (!TEST_ptr(p = ossl_quic_wire_encode_transport_param_bytes(pkt, 0x9781,
  729. NULL, 2)))
  730. return 0;
  731. p[0] = 0x33;
  732. p[1] = 0x44;
  733. return 1;
  734. }
  735. static int encode_case_22_dec(PACKET *pkt, ossl_ssize_t fail)
  736. {
  737. uint64_t id = 0;
  738. size_t len = 0;
  739. const unsigned char *p;
  740. static const unsigned char data[] = {0x33, 0x44};
  741. if (!TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
  742. fail < 0 || fail >= 2))
  743. return 0;
  744. if ((fail < 0 || fail >= 2)
  745. && !TEST_uint64_t_eq(id, 0x1234))
  746. return 0;
  747. id = 0;
  748. p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
  749. if (fail < 0 || fail >= 8) {
  750. if (!TEST_ptr(p))
  751. return 0;
  752. } else {
  753. if (!TEST_ptr_null(p))
  754. return 0;
  755. }
  756. if ((fail < 0 || fail >= 8)
  757. && !TEST_uint64_t_eq(id, 0x1234))
  758. return 0;
  759. if ((fail < 0 || fail >= 8)
  760. && !TEST_mem_eq(p, len, encode_case_22_data, sizeof(encode_case_22_data)))
  761. return 0;
  762. if ((fail < 0 || fail >= 8)
  763. && !TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
  764. fail < 0 || fail >= 12))
  765. return 0;
  766. if ((fail < 0 || fail >= 12)
  767. && !TEST_uint64_t_eq(id, 0x9781))
  768. return 0;
  769. id = 0;
  770. p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
  771. if (fail < 0 || fail >= 15) {
  772. if (!TEST_ptr(p))
  773. return 0;
  774. } else {
  775. if (!TEST_ptr_null(p))
  776. return 0;
  777. }
  778. if ((fail < 0 || fail >= 15)
  779. && !TEST_uint64_t_eq(id, 0x9781))
  780. return 0;
  781. if ((fail < 0 || fail >= 15)
  782. && !TEST_mem_eq(p, len, data, sizeof(data)))
  783. return 0;
  784. return 1;
  785. }
  786. static const unsigned char encode_case_22_expect[] = {
  787. 0x52, 0x34, /* ID */
  788. 0x05, /* Length */
  789. 0x55, 0x77, 0x32, 0x46, 0x99, /* Data */
  790. 0x80, 0x00, 0x97, 0x81, /* ID */
  791. 0x02, /* Length */
  792. 0x33, 0x44 /* Data */
  793. };
  794. /* 23. Integer Transport Parameter */
  795. static int encode_case_23_enc(WPACKET *pkt)
  796. {
  797. if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x1234, 0x9781), 1))
  798. return 0;
  799. if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x2233, 0x4545), 1))
  800. return 0;
  801. return 1;
  802. }
  803. static int encode_case_23_dec(PACKET *pkt, ossl_ssize_t fail)
  804. {
  805. uint64_t id = 0, value = 0;
  806. if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
  807. &id, &value),
  808. fail < 0 || fail >= 7))
  809. return 0;
  810. if ((fail < 0 || fail >= 7)
  811. && !TEST_uint64_t_eq(id, 0x1234))
  812. return 0;
  813. if ((fail < 0 || fail >= 7)
  814. && !TEST_uint64_t_eq(value, 0x9781))
  815. return 0;
  816. if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
  817. &id, &value),
  818. fail < 0 || fail >= 14))
  819. return 0;
  820. if ((fail < 0 || fail >= 14)
  821. && !TEST_uint64_t_eq(id, 0x2233))
  822. return 0;
  823. if ((fail < 0 || fail >= 14)
  824. && !TEST_uint64_t_eq(value, 0x4545))
  825. return 0;
  826. return 1;
  827. }
  828. static const unsigned char encode_case_23_expect[] = {
  829. 0x52, 0x34,
  830. 0x04,
  831. 0x80, 0x00, 0x97, 0x81,
  832. 0x62, 0x33,
  833. 0x04,
  834. 0x80, 0x00, 0x45, 0x45,
  835. };
  836. #define ENCODE_CASE(n) \
  837. { \
  838. encode_case_##n##_enc, \
  839. encode_case_##n##_expect, \
  840. OSSL_NELEM(encode_case_##n##_expect), \
  841. encode_case_##n##_dec \
  842. },
  843. static const struct encode_test_case encode_cases[] = {
  844. ENCODE_CASE(1)
  845. ENCODE_CASE(2)
  846. ENCODE_CASE(3)
  847. ENCODE_CASE(4)
  848. ENCODE_CASE(5)
  849. ENCODE_CASE(6)
  850. ENCODE_CASE(7)
  851. ENCODE_CASE(8)
  852. ENCODE_CASE(9)
  853. ENCODE_CASE(10)
  854. ENCODE_CASE(11)
  855. ENCODE_CASE(12)
  856. ENCODE_CASE(13)
  857. ENCODE_CASE(14)
  858. ENCODE_CASE(15)
  859. ENCODE_CASE(16)
  860. ENCODE_CASE(17)
  861. ENCODE_CASE(18)
  862. ENCODE_CASE(19)
  863. ENCODE_CASE(20)
  864. ENCODE_CASE(21)
  865. ENCODE_CASE(22)
  866. ENCODE_CASE(23)
  867. };
  868. static int test_wire_encode(int idx)
  869. {
  870. int testresult = 0;
  871. WPACKET wpkt;
  872. PACKET pkt;
  873. BUF_MEM *buf = NULL;
  874. size_t written;
  875. const struct encode_test_case *c = &encode_cases[idx];
  876. int have_wpkt = 0;
  877. size_t i;
  878. if (!TEST_ptr(buf = BUF_MEM_new()))
  879. goto err;
  880. if (!TEST_int_eq(WPACKET_init(&wpkt, buf), 1))
  881. goto err;
  882. have_wpkt = 1;
  883. if (!TEST_int_eq(c->serializer(&wpkt), 1))
  884. goto err;
  885. if (!TEST_int_eq(WPACKET_get_total_written(&wpkt, &written), 1))
  886. goto err;
  887. if (!TEST_mem_eq(buf->data, written, c->expect_buf, c->expect_buf_len))
  888. goto err;
  889. if (!TEST_int_eq(PACKET_buf_init(&pkt, (unsigned char *)buf->data, written), 1))
  890. goto err;
  891. if (!TEST_int_eq(c->deserializer(&pkt, -1), 1))
  892. goto err;
  893. if (!TEST_false(PACKET_remaining(&pkt)))
  894. goto err;
  895. for (i = 0; i < c->expect_buf_len; ++i) {
  896. PACKET pkt2;
  897. /*
  898. * Check parsing truncated (i.e., malformed) input is handled correctly.
  899. * Generate all possible truncations of our reference encoding and
  900. * verify that they are handled correctly. The number of bytes of the
  901. * truncated encoding is passed as an argument to the deserializer to
  902. * help it determine whether decoding should fail or not.
  903. */
  904. if (!TEST_int_eq(PACKET_buf_init(&pkt2, (unsigned char *)c->expect_buf, i), 1))
  905. goto err;
  906. if (!TEST_int_eq(c->deserializer(&pkt2, i), 1))
  907. goto err;
  908. }
  909. testresult = 1;
  910. err:
  911. if (have_wpkt)
  912. WPACKET_finish(&wpkt);
  913. BUF_MEM_free(buf);
  914. return testresult;
  915. }
  916. struct ack_test_case {
  917. const unsigned char *input_buf;
  918. size_t input_buf_len;
  919. int (*deserializer)(PACKET *pkt);
  920. int expect_fail;
  921. };
  922. /* ACK Frame with Excessive First ACK Range Field */
  923. static const unsigned char ack_case_1_input[] = {
  924. 0x02, /* ACK Without ECN */
  925. 0x08, /* Largest Acknowledged */
  926. 0x01, /* ACK Delay */
  927. 0x00, /* ACK Range Count */
  928. 0x09, /* First ACK Range */
  929. };
  930. /* ACK Frame with Valid ACK Range Field */
  931. static const unsigned char ack_case_2_input[] = {
  932. 0x02, /* ACK Without ECN */
  933. 0x08, /* Largest Acknowledged */
  934. 0x01, /* ACK Delay */
  935. 0x00, /* ACK Range Count */
  936. 0x08, /* First ACK Range */
  937. };
  938. /* ACK Frame with Excessive ACK Range Gap */
  939. static const unsigned char ack_case_3_input[] = {
  940. 0x02, /* ACK Without ECN */
  941. 0x08, /* Largest Acknowledged */
  942. 0x01, /* ACK Delay */
  943. 0x01, /* ACK Range Count */
  944. 0x01, /* First ACK Range */
  945. 0x05, /* Gap */
  946. 0x01, /* ACK Range Length */
  947. };
  948. /* ACK Frame with Valid ACK Range */
  949. static const unsigned char ack_case_4_input[] = {
  950. 0x02, /* ACK Without ECN */
  951. 0x08, /* Largest Acknowledged */
  952. 0x01, /* ACK Delay */
  953. 0x01, /* ACK Range Count */
  954. 0x01, /* First ACK Range */
  955. 0x04, /* Gap */
  956. 0x01, /* ACK Range Length */
  957. };
  958. /* ACK Frame with Excessive ACK Range Length */
  959. static const unsigned char ack_case_5_input[] = {
  960. 0x02, /* ACK Without ECN */
  961. 0x08, /* Largest Acknowledged */
  962. 0x01, /* ACK Delay */
  963. 0x01, /* ACK Range Count */
  964. 0x01, /* First ACK Range */
  965. 0x04, /* Gap */
  966. 0x02, /* ACK Range Length */
  967. };
  968. /* ACK Frame with Multiple ACK Ranges, Final Having Excessive Length */
  969. static const unsigned char ack_case_6_input[] = {
  970. 0x02, /* ACK Without ECN */
  971. 0x08, /* Largest Acknowledged */
  972. 0x01, /* ACK Delay */
  973. 0x02, /* ACK Range Count */
  974. 0x01, /* First ACK Range */
  975. 0x01, /* Gap */
  976. 0x02, /* ACK Range Length */
  977. 0x00, /* Gap */
  978. 0x01, /* ACK Range Length */
  979. };
  980. /* ACK Frame with Multiple ACK Ranges, Valid */
  981. static const unsigned char ack_case_7_input[] = {
  982. 0x02, /* ACK Without ECN */
  983. 0x08, /* Largest Acknowledged */
  984. 0x01, /* ACK Delay */
  985. 0x02, /* ACK Range Count */
  986. 0x01, /* First ACK Range */
  987. 0x01, /* Gap */
  988. 0x02, /* ACK Range Length */
  989. 0x00, /* Gap */
  990. 0x00, /* ACK Range Length */
  991. };
  992. static int ack_generic_decode(PACKET *pkt)
  993. {
  994. OSSL_QUIC_ACK_RANGE ranges[8] = {0};
  995. OSSL_QUIC_FRAME_ACK f = {0};
  996. uint64_t total_ranges = 0, peek_total_ranges = 0;
  997. int r;
  998. size_t i;
  999. f.ack_ranges = ranges;
  1000. f.num_ack_ranges = OSSL_NELEM(ranges);
  1001. if (!TEST_int_eq(ossl_quic_wire_peek_frame_ack_num_ranges(pkt,
  1002. &peek_total_ranges), 1))
  1003. return 0;
  1004. r = ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges);
  1005. if (r == 0)
  1006. return 0;
  1007. if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
  1008. return 0;
  1009. for (i = 0; i < f.num_ack_ranges; ++i) {
  1010. if (!TEST_uint64_t_le(f.ack_ranges[i].start, f.ack_ranges[i].end))
  1011. return 0;
  1012. if (!TEST_uint64_t_lt(f.ack_ranges[i].end, 1000))
  1013. return 0;
  1014. }
  1015. return 1;
  1016. }
  1017. #define ACK_CASE(n, expect_fail, dec) \
  1018. { \
  1019. ack_case_##n##_input, \
  1020. sizeof(ack_case_##n##_input), \
  1021. (dec), \
  1022. (expect_fail) \
  1023. },
  1024. static const struct ack_test_case ack_cases[] = {
  1025. ACK_CASE(1, 1, ack_generic_decode)
  1026. ACK_CASE(2, 0, ack_generic_decode)
  1027. ACK_CASE(3, 1, ack_generic_decode)
  1028. ACK_CASE(4, 0, ack_generic_decode)
  1029. ACK_CASE(5, 1, ack_generic_decode)
  1030. ACK_CASE(6, 1, ack_generic_decode)
  1031. ACK_CASE(7, 0, ack_generic_decode)
  1032. };
  1033. static int test_wire_ack(int idx)
  1034. {
  1035. int testresult = 0, r;
  1036. PACKET pkt;
  1037. const struct ack_test_case *c = &ack_cases[idx];
  1038. if (!TEST_int_eq(PACKET_buf_init(&pkt,
  1039. (unsigned char *)c->input_buf,
  1040. c->input_buf_len), 1))
  1041. goto err;
  1042. r = c->deserializer(&pkt);
  1043. if (c->expect_fail) {
  1044. if (!TEST_int_eq(r, 0))
  1045. goto err;
  1046. } else {
  1047. if (!TEST_int_eq(r, 1))
  1048. goto err;
  1049. if (!TEST_false(PACKET_remaining(&pkt)))
  1050. goto err;
  1051. }
  1052. testresult = 1;
  1053. err:
  1054. return testresult;
  1055. }
  1056. int setup_tests(void)
  1057. {
  1058. ADD_ALL_TESTS(test_wire_encode, OSSL_NELEM(encode_cases));
  1059. ADD_ALL_TESTS(test_wire_ack, OSSL_NELEM(ack_cases));
  1060. return 1;
  1061. }