quic_wire_test.c 39 KB

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