2 * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include "internal/packet.h"
11 #include "internal/quic_wire.h"
12 #include "internal/quic_wire_pkt.h"
15 struct encode_test_case
{
16 int (*serializer
)(WPACKET
*pkt
);
17 const unsigned char *expect_buf
;
18 size_t expect_buf_len
;
20 * fail: -1 if not truncated (function should test for success), else number
21 * of bytes to which the input has been truncated (function should test that
24 int (*deserializer
)(PACKET
*pkt
, ossl_ssize_t fail
);
28 static int encode_case_1_enc(WPACKET
*pkt
)
30 if (!TEST_int_eq(ossl_quic_wire_encode_padding(pkt
, 3), 1))
36 static int encode_case_1_dec(PACKET
*pkt
, ossl_ssize_t fail
)
39 /* No failure modes for padding */
42 if (!TEST_int_eq(ossl_quic_wire_decode_padding(pkt
), 3))
48 static const unsigned char encode_case_1_expect
[] = {
53 static int encode_case_2_enc(WPACKET
*pkt
)
56 if (!TEST_int_eq(ossl_quic_wire_encode_frame_ping(pkt
), 1))
62 static int encode_case_2_dec(PACKET
*pkt
, ossl_ssize_t fail
)
65 if (!TEST_int_eq(ossl_quic_wire_decode_frame_ping(pkt
), fail
< 0))
71 static const unsigned char encode_case_2_expect
[] = {
76 static const OSSL_QUIC_ACK_RANGE encode_case_3_ranges
[] = {
81 static const OSSL_QUIC_FRAME_ACK encode_case_3_f
= {
82 (OSSL_QUIC_ACK_RANGE
*)encode_case_3_ranges
,
83 OSSL_NELEM(encode_case_3_ranges
),
88 static int encode_case_3_enc(WPACKET
*pkt
)
90 if (!TEST_int_eq(ossl_quic_wire_encode_frame_ack(pkt
, 3, &encode_case_3_f
), 1))
96 static int encode_case_3_dec(PACKET
*pkt
, ossl_ssize_t fail
)
98 OSSL_QUIC_ACK_RANGE ranges
[4] = {0};
99 OSSL_QUIC_FRAME_ACK f
= {0};
100 uint64_t total_ranges
= 0, peek_total_ranges
= 0;
103 f
.ack_ranges
= ranges
;
104 f
.num_ack_ranges
= OSSL_NELEM(ranges
);
106 ret
= ossl_quic_wire_peek_frame_ack_num_ranges(pkt
, &peek_total_ranges
);
107 if (fail
< 0 && !TEST_int_eq(ret
, 1))
110 if (!TEST_int_eq(ossl_quic_wire_decode_frame_ack(pkt
, 3, &f
, &total_ranges
), fail
< 0))
113 if (ret
== 1 && !TEST_uint64_t_eq(peek_total_ranges
, 2))
119 if (!TEST_uint64_t_eq(total_ranges
, peek_total_ranges
))
122 if (!TEST_uint64_t_le(f
.num_ack_ranges
* sizeof(OSSL_QUIC_ACK_RANGE
),
124 || !TEST_uint64_t_le(encode_case_3_f
.num_ack_ranges
125 * sizeof(OSSL_QUIC_ACK_RANGE
),
129 if (!TEST_mem_eq(f
.ack_ranges
,
130 (size_t)f
.num_ack_ranges
* sizeof(OSSL_QUIC_ACK_RANGE
),
131 encode_case_3_f
.ack_ranges
,
132 (size_t)encode_case_3_f
.num_ack_ranges
* sizeof(OSSL_QUIC_ACK_RANGE
)))
135 if (!TEST_uint64_t_eq(ossl_time2ticks(f
.delay_time
),
136 ossl_time2ticks(encode_case_3_f
.delay_time
)))
139 if (!TEST_true(f
.ecn_present
))
142 if (!TEST_uint64_t_eq(f
.ect0
, encode_case_3_f
.ect0
))
145 if (!TEST_uint64_t_eq(f
.ect1
, encode_case_3_f
.ect1
))
148 if (!TEST_uint64_t_eq(f
.ecnce
, encode_case_3_f
.ecnce
))
154 static const unsigned char encode_case_3_expect
[] = {
156 0x1E, /* Largest Acknowledged */
157 0x40, 0x7d, /* ACK Delay */
158 1, /* ACK Range Count */
159 10, /* First ACK Range */
164 0x3c, /* ECT0 Count */
165 0x40, 0x46, /* ECT1 Count */
166 0x40, 0x50, /* ECNCE Count */
169 /* 4. RESET_STREAM */
170 static const OSSL_QUIC_FRAME_RESET_STREAM encode_case_4_f
= {
171 0x1234, 0x9781, 0x11717
174 static int encode_case_4_enc(WPACKET
*pkt
)
176 if (!TEST_int_eq(ossl_quic_wire_encode_frame_reset_stream(pkt
,
177 &encode_case_4_f
), 1))
183 static int encode_case_4_dec(PACKET
*pkt
, ossl_ssize_t fail
)
185 OSSL_QUIC_FRAME_RESET_STREAM f
= {0};
187 if (!TEST_int_eq(ossl_quic_wire_decode_frame_reset_stream(pkt
, &f
), fail
< 0))
193 if (!TEST_mem_eq(&f
, sizeof(f
), &encode_case_4_f
, sizeof(encode_case_4_f
)))
199 static const unsigned char encode_case_4_expect
[] = {
201 0x52, 0x34, /* Stream ID */
202 0x80, 0x00, 0x97, 0x81, /* App Error Code */
203 0x80, 0x01, 0x17, 0x17, /* Final Size */
206 /* 5. STOP_SENDING */
207 static const OSSL_QUIC_FRAME_STOP_SENDING encode_case_5_f
= {
211 static int encode_case_5_enc(WPACKET
*pkt
)
213 if (!TEST_int_eq(ossl_quic_wire_encode_frame_stop_sending(pkt
,
214 &encode_case_5_f
), 1))
220 static int encode_case_5_dec(PACKET
*pkt
, ossl_ssize_t fail
)
222 OSSL_QUIC_FRAME_STOP_SENDING f
= {0};
224 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stop_sending(pkt
, &f
), fail
< 0))
230 if (!TEST_mem_eq(&f
, sizeof(f
), &encode_case_5_f
, sizeof(encode_case_5_f
)))
236 static const unsigned char encode_case_5_expect
[] = {
238 0x52, 0x34, /* Stream ID */
239 0x80, 0x00, 0x97, 0x81 /* App Error Code */
243 static const unsigned char encode_case_6_data
[] = {
247 static const OSSL_QUIC_FRAME_CRYPTO encode_case_6_f
= {
248 0x1234, sizeof(encode_case_6_data
), encode_case_6_data
251 static int encode_case_6_enc(WPACKET
*pkt
)
253 if (!TEST_ptr(ossl_quic_wire_encode_frame_crypto(pkt
,
260 static int encode_case_6_dec(PACKET
*pkt
, ossl_ssize_t fail
)
262 OSSL_QUIC_FRAME_CRYPTO f
= {0};
264 if (!TEST_int_eq(ossl_quic_wire_decode_frame_crypto(pkt
, &f
), fail
< 0))
270 if (!TEST_uint64_t_eq(f
.offset
, 0x1234))
273 if (!TEST_uint64_t_le(f
.len
, SIZE_MAX
))
276 if (!TEST_mem_eq(f
.data
, (size_t)f
.len
,
277 encode_case_6_data
, sizeof(encode_case_6_data
)))
283 static const unsigned char encode_case_6_expect
[] = {
285 0x52, 0x34, /* Offset */
287 93, 18, 17, 102, 33 /* Data */
291 static const unsigned char encode_case_7_token
[] = {
292 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
293 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
296 static int encode_case_7_enc(WPACKET
*pkt
)
298 if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_token(pkt
,
300 sizeof(encode_case_7_token
)), 1))
306 static int encode_case_7_dec(PACKET
*pkt
, ossl_ssize_t fail
)
308 const unsigned char *token
= NULL
;
309 size_t token_len
= 0;
311 if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_token(pkt
,
313 &token_len
), fail
< 0))
319 if (!TEST_mem_eq(token
, token_len
,
320 encode_case_7_token
, sizeof(encode_case_7_token
)))
326 static const unsigned char encode_case_7_expect
[] = {
329 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Token */
330 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
333 /* 8. STREAM (no length, no offset, no fin) */
334 static const unsigned char encode_case_8_data
[] = {
335 0xde, 0x06, 0xcb, 0x76, 0x5d
337 static const OSSL_QUIC_FRAME_STREAM encode_case_8_f
= {
338 0x1234, 0, 5, encode_case_8_data
, 0, 0
341 static int encode_case_8_enc(WPACKET
*pkt
)
343 if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt
,
350 static int encode_case_8_dec(PACKET
*pkt
, ossl_ssize_t fail
)
352 OSSL_QUIC_FRAME_STREAM f
= {0};
356 * This case uses implicit length signalling so truncation will not
357 * cause it to fail unless the header (which is 3 bytes) is truncated.
361 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt
, &f
), fail
< 0))
367 if (!TEST_uint64_t_le(f
.len
, SIZE_MAX
))
370 if (!TEST_mem_eq(f
.data
, (size_t)f
.len
,
371 encode_case_8_data
, sizeof(encode_case_8_data
)))
374 if (!TEST_uint64_t_eq(f
.stream_id
, 0x1234))
377 if (!TEST_uint64_t_eq(f
.offset
, 0))
380 if (!TEST_int_eq(f
.has_explicit_len
, 0))
383 if (!TEST_int_eq(f
.is_fin
, 0))
389 static const unsigned char encode_case_8_expect
[] = {
390 0x08, /* Type (OFF=0, LEN=0, FIN=0) */
391 0x52, 0x34, /* Stream ID */
392 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
395 /* 9. STREAM (length, offset, fin) */
396 static const unsigned char encode_case_9_data
[] = {
397 0xde, 0x06, 0xcb, 0x76, 0x5d
399 static const OSSL_QUIC_FRAME_STREAM encode_case_9_f
= {
400 0x1234, 0x39, 5, encode_case_9_data
, 1, 1
403 static int encode_case_9_enc(WPACKET
*pkt
)
405 if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt
,
412 static int encode_case_9_dec(PACKET
*pkt
, ossl_ssize_t fail
)
414 OSSL_QUIC_FRAME_STREAM f
= {0};
416 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt
, &f
), fail
< 0))
422 if (!TEST_uint64_t_le(f
.len
, SIZE_MAX
))
425 if (!TEST_mem_eq(f
.data
, (size_t)f
.len
,
426 encode_case_9_data
, sizeof(encode_case_9_data
)))
429 if (!TEST_uint64_t_eq(f
.stream_id
, 0x1234))
432 if (!TEST_uint64_t_eq(f
.offset
, 0x39))
435 if (!TEST_int_eq(f
.has_explicit_len
, 1))
438 if (!TEST_int_eq(f
.is_fin
, 1))
444 static const unsigned char encode_case_9_expect
[] = {
445 0x0f, /* Type (OFF=1, LEN=1, FIN=1) */
446 0x52, 0x34, /* Stream ID */
449 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
453 static int encode_case_10_enc(WPACKET
*pkt
)
455 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_data(pkt
, 0x1234), 1))
461 static int encode_case_10_dec(PACKET
*pkt
, ossl_ssize_t fail
)
463 uint64_t max_data
= 0;
465 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_data(pkt
, &max_data
), fail
< 0))
471 if (!TEST_uint64_t_eq(max_data
, 0x1234))
477 static const unsigned char encode_case_10_expect
[] = {
479 0x52, 0x34, /* Max Data */
482 /* 11. MAX_STREAM_DATA */
483 static int encode_case_11_enc(WPACKET
*pkt
)
485 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_stream_data(pkt
,
493 static int encode_case_11_dec(PACKET
*pkt
, ossl_ssize_t fail
)
495 uint64_t stream_id
= 0, max_data
= 0;
497 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_stream_data(pkt
,
499 &max_data
), fail
< 0))
505 if (!TEST_uint64_t_eq(stream_id
, 0x1234))
508 if (!TEST_uint64_t_eq(max_data
, 0x9781))
514 static const unsigned char encode_case_11_expect
[] = {
516 0x52, 0x34, /* Stream ID */
517 0x80, 0x00, 0x97, 0x81, /* Max Data */
520 /* 12. MAX_STREAMS */
521 static int encode_case_12_enc(WPACKET
*pkt
)
523 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt
, 0, 0x1234), 1))
526 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt
, 1, 0x9781), 1))
532 static int encode_case_12_dec(PACKET
*pkt
, ossl_ssize_t fail
)
534 uint64_t max_streams_1
= 0, max_streams_2
= 0,
535 frame_type_1
= 0, frame_type_2
= 0;
537 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt
, &frame_type_1
),
538 fail
< 0 || fail
>= 1))
541 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt
,
543 fail
< 0 || fail
>= 3))
546 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt
, &frame_type_2
),
547 fail
< 0 || fail
>= 4))
550 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt
,
555 if ((fail
< 0 || fail
>= 3)
556 && !TEST_uint64_t_eq(frame_type_1
, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI
))
559 if ((fail
< 0 || fail
>= 3)
560 && !TEST_uint64_t_eq(max_streams_1
, 0x1234))
563 if ((fail
< 0 || fail
>= 8)
564 && !TEST_uint64_t_eq(frame_type_2
, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI
))
567 if ((fail
< 0 || fail
>= 8)
568 && !TEST_uint64_t_eq(max_streams_2
, 0x9781))
574 static const unsigned char encode_case_12_expect
[] = {
575 0x12, /* Type (MAX_STREAMS Bidirectional) */
576 0x52, 0x34, /* Max Streams */
577 0x13, /* Type (MAX_STREAMS Unidirectional) */
578 0x80, 0x00, 0x97, 0x81, /* Max Streams */
581 /* 13. DATA_BLOCKED */
582 static int encode_case_13_enc(WPACKET
*pkt
)
584 if (!TEST_int_eq(ossl_quic_wire_encode_frame_data_blocked(pkt
, 0x1234), 1))
590 static int encode_case_13_dec(PACKET
*pkt
, ossl_ssize_t fail
)
592 uint64_t max_data
= 0;
594 if (!TEST_int_eq(ossl_quic_wire_decode_frame_data_blocked(pkt
,
595 &max_data
), fail
< 0))
601 if (!TEST_uint64_t_eq(max_data
, 0x1234))
607 static const unsigned char encode_case_13_expect
[] = {
609 0x52, 0x34, /* Max Data */
612 /* 14. STREAM_DATA_BLOCKED */
613 static int encode_case_14_enc(WPACKET
*pkt
)
615 if (!TEST_int_eq(ossl_quic_wire_encode_frame_stream_data_blocked(pkt
,
623 static int encode_case_14_dec(PACKET
*pkt
, ossl_ssize_t fail
)
625 uint64_t stream_id
= 0, max_data
= 0;
627 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream_data_blocked(pkt
,
629 &max_data
), fail
< 0))
635 if (!TEST_uint64_t_eq(stream_id
, 0x1234))
638 if (!TEST_uint64_t_eq(max_data
, 0x9781))
644 static const unsigned char encode_case_14_expect
[] = {
646 0x52, 0x34, /* Stream ID */
647 0x80, 0x00, 0x97, 0x81, /* Max Data */
650 /* 15. STREAMS_BLOCKED */
651 static int encode_case_15_enc(WPACKET
*pkt
)
653 if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt
, 0, 0x1234), 1))
656 if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt
, 1, 0x9781), 1))
662 static int encode_case_15_dec(PACKET
*pkt
, ossl_ssize_t fail
)
664 uint64_t max_streams_1
= 0, max_streams_2
= 0,
665 frame_type_1
= 0, frame_type_2
= 0;
667 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt
, &frame_type_1
),
668 fail
< 0 || fail
>= 1))
671 if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt
,
673 fail
< 0 || fail
>= 3))
676 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt
, &frame_type_2
),
677 fail
< 0 || fail
>= 4))
680 if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt
,
682 fail
< 0 || fail
>= 8))
685 if ((fail
< 0 || fail
>= 1)
686 && !TEST_uint64_t_eq(frame_type_1
, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI
))
689 if ((fail
< 0 || fail
>= 3)
690 && !TEST_uint64_t_eq(max_streams_1
, 0x1234))
693 if ((fail
< 0 || fail
>= 4)
694 && !TEST_uint64_t_eq(frame_type_2
, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI
))
697 if ((fail
< 0 || fail
>= 8)
698 && !TEST_uint64_t_eq(max_streams_2
, 0x9781))
704 static const unsigned char encode_case_15_expect
[] = {
705 0x16, /* Type (STREAMS_BLOCKED Bidirectional) */
706 0x52, 0x34, /* Max Streams */
707 0x17, /* Type (STREAMS_BLOCKED Unidirectional) */
708 0x80, 0x00, 0x97, 0x81, /* Max Streams */
711 /* 16. NEW_CONNECTION_ID */
712 static const unsigned char encode_case_16_conn_id
[] = {
713 0x33, 0x44, 0x55, 0x66
716 static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16_f
= {
721 {0x33, 0x44, 0x55, 0x66}
724 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
725 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
729 static int encode_case_16_enc(WPACKET
*pkt
)
731 if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt
,
732 &encode_case_16_f
), 1))
738 static int encode_case_16_dec(PACKET
*pkt
, ossl_ssize_t fail
)
740 OSSL_QUIC_FRAME_NEW_CONN_ID f
= {0};
742 if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt
, &f
), fail
< 0))
748 if (!TEST_uint64_t_eq(f
.seq_num
, 0x1234))
751 if (!TEST_uint64_t_eq(f
.retire_prior_to
, 0x9781))
754 if (!TEST_uint64_t_eq(f
.conn_id
.id_len
, sizeof(encode_case_16_conn_id
)))
757 if (!TEST_mem_eq(f
.conn_id
.id
, f
.conn_id
.id_len
,
758 encode_case_16_conn_id
, sizeof(encode_case_16_conn_id
)))
761 if (!TEST_mem_eq(f
.stateless_reset_token
,
762 sizeof(f
.stateless_reset_token
),
763 encode_case_16_f
.stateless_reset_token
,
764 sizeof(encode_case_16_f
.stateless_reset_token
)))
770 static const unsigned char encode_case_16_expect
[] = {
772 0x52, 0x34, /* Sequence Number */
773 0x80, 0x00, 0x97, 0x81, /* Retire Prior To */
774 0x04, /* Connection ID Length */
775 0x33, 0x44, 0x55, 0x66, /* Connection ID */
776 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
777 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
780 /* 17. RETIRE_CONNECTION_ID */
781 static int encode_case_17_enc(WPACKET
*pkt
)
783 if (!TEST_int_eq(ossl_quic_wire_encode_frame_retire_conn_id(pkt
, 0x1234), 1))
789 static int encode_case_17_dec(PACKET
*pkt
, ossl_ssize_t fail
)
791 uint64_t seq_num
= 0;
793 if (!TEST_int_eq(ossl_quic_wire_decode_frame_retire_conn_id(pkt
, &seq_num
), fail
< 0))
799 if (!TEST_uint64_t_eq(seq_num
, 0x1234))
805 static const unsigned char encode_case_17_expect
[] = {
807 0x52, 0x34, /* Seq Num */
810 /* 18. PATH_CHALLENGE */
811 static const uint64_t encode_case_18_data
812 = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL
;
814 static int encode_case_18_enc(WPACKET
*pkt
)
816 if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_challenge(pkt
,
817 encode_case_18_data
), 1))
823 static int encode_case_18_dec(PACKET
*pkt
, ossl_ssize_t fail
)
825 uint64_t challenge
= 0;
827 if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_challenge(pkt
, &challenge
), fail
< 0))
833 if (!TEST_uint64_t_eq(challenge
, encode_case_18_data
))
839 static const unsigned char encode_case_18_expect
[] = {
841 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
844 /* 19. PATH_RESPONSE */
845 static const uint64_t encode_case_19_data
846 = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL
;
848 static int encode_case_19_enc(WPACKET
*pkt
)
850 if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_response(pkt
,
851 encode_case_19_data
), 1))
857 static int encode_case_19_dec(PACKET
*pkt
, ossl_ssize_t fail
)
859 uint64_t challenge
= 0;
861 if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_response(pkt
, &challenge
), fail
< 0))
867 if (!TEST_uint64_t_eq(challenge
, encode_case_19_data
))
873 static const unsigned char encode_case_19_expect
[] = {
875 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
878 /* 20. CONNECTION_CLOSE (transport) */
879 static const char encode_case_20_reason
[] = {
880 /* "reason for closure" */
881 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f,
882 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
885 static const OSSL_QUIC_FRAME_CONN_CLOSE encode_case_20_f
= {
889 encode_case_20_reason
,
890 sizeof(encode_case_20_reason
)
893 static int encode_case_20_enc(WPACKET
*pkt
)
895 if (!TEST_int_eq(ossl_quic_wire_encode_frame_conn_close(pkt
,
896 &encode_case_20_f
), 1))
902 static int encode_case_20_dec(PACKET
*pkt
, ossl_ssize_t fail
)
904 OSSL_QUIC_FRAME_CONN_CLOSE f
= {0};
906 if (!TEST_int_eq(ossl_quic_wire_decode_frame_conn_close(pkt
, &f
), fail
< 0))
912 if (!TEST_int_eq(f
.is_app
, 0))
915 if (!TEST_uint64_t_eq(f
.error_code
, 0x1234))
918 if (!TEST_uint64_t_eq(f
.frame_type
, 0x9781))
921 if (!TEST_size_t_eq(f
.reason_len
, 18))
924 if (!TEST_mem_eq(f
.reason
, f
.reason_len
,
925 encode_case_20_f
.reason
, encode_case_20_f
.reason_len
))
931 static const unsigned char encode_case_20_expect
[] = {
933 0x52, 0x34, /* Sequence Number */
934 0x80, 0x00, 0x97, 0x81, /* Frame Type */
935 0x12, /* Reason Length */
936 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f, /* Reason */
937 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
940 /* 21. HANDSHAKE_DONE */
941 static int encode_case_21_enc(WPACKET
*pkt
)
944 if (!TEST_int_eq(ossl_quic_wire_encode_frame_handshake_done(pkt
), 1))
950 static int encode_case_21_dec(PACKET
*pkt
, ossl_ssize_t fail
)
953 if (!TEST_int_eq(ossl_quic_wire_decode_frame_handshake_done(pkt
), fail
< 0))
959 static const unsigned char encode_case_21_expect
[] = {
963 /* 22. Buffer Transport Parameter */
964 static const unsigned char encode_case_22_data
[] = {0x55,0x77,0x32,0x46,0x99};
966 static int encode_case_22_enc(WPACKET
*pkt
)
970 if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(pkt
, 0x1234,
972 sizeof(encode_case_22_data
))))
975 if (!TEST_ptr(p
= ossl_quic_wire_encode_transport_param_bytes(pkt
, 0x9781,
985 static int encode_case_22_dec(PACKET
*pkt
, ossl_ssize_t fail
)
989 const unsigned char *p
;
990 static const unsigned char data
[] = {0x33, 0x44};
992 if (!TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt
, &id
),
993 fail
< 0 || fail
>= 2))
996 if ((fail
< 0 || fail
>= 2)
997 && !TEST_uint64_t_eq(id
, 0x1234))
1002 p
= ossl_quic_wire_decode_transport_param_bytes(pkt
, &id
, &len
);
1003 if (fail
< 0 || fail
>= 8) {
1007 if (!TEST_ptr_null(p
))
1011 if ((fail
< 0 || fail
>= 8)
1012 && !TEST_uint64_t_eq(id
, 0x1234))
1015 if ((fail
< 0 || fail
>= 8)
1016 && !TEST_mem_eq(p
, len
, encode_case_22_data
, sizeof(encode_case_22_data
)))
1019 if ((fail
< 0 || fail
>= 8)
1020 && !TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt
, &id
),
1021 fail
< 0 || fail
>= 12))
1024 if ((fail
< 0 || fail
>= 12)
1025 && !TEST_uint64_t_eq(id
, 0x9781))
1030 p
= ossl_quic_wire_decode_transport_param_bytes(pkt
, &id
, &len
);
1031 if (fail
< 0 || fail
>= 15) {
1035 if (!TEST_ptr_null(p
))
1039 if ((fail
< 0 || fail
>= 15)
1040 && !TEST_uint64_t_eq(id
, 0x9781))
1043 if ((fail
< 0 || fail
>= 15)
1044 && !TEST_mem_eq(p
, len
, data
, sizeof(data
)))
1050 static const unsigned char encode_case_22_expect
[] = {
1051 0x52, 0x34, /* ID */
1053 0x55, 0x77, 0x32, 0x46, 0x99, /* Data */
1055 0x80, 0x00, 0x97, 0x81, /* ID */
1057 0x33, 0x44 /* Data */
1060 /* 23. Integer Transport Parameter */
1061 static int encode_case_23_enc(WPACKET
*pkt
)
1063 if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt
, 0x1234, 0x9781), 1))
1066 if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt
, 0x2233, 0x4545), 1))
1072 static int encode_case_23_dec(PACKET
*pkt
, ossl_ssize_t fail
)
1074 uint64_t id
= 0, value
= 0;
1076 if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt
,
1078 fail
< 0 || fail
>= 7))
1081 if ((fail
< 0 || fail
>= 7)
1082 && !TEST_uint64_t_eq(id
, 0x1234))
1085 if ((fail
< 0 || fail
>= 7)
1086 && !TEST_uint64_t_eq(value
, 0x9781))
1089 if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt
,
1091 fail
< 0 || fail
>= 14))
1094 if ((fail
< 0 || fail
>= 14)
1095 && !TEST_uint64_t_eq(id
, 0x2233))
1098 if ((fail
< 0 || fail
>= 14)
1099 && !TEST_uint64_t_eq(value
, 0x4545))
1105 static const unsigned char encode_case_23_expect
[] = {
1108 0x80, 0x00, 0x97, 0x81,
1112 0x80, 0x00, 0x45, 0x45,
1115 #define ENCODE_CASE(n) \
1117 encode_case_##n##_enc, \
1118 encode_case_##n##_expect, \
1119 OSSL_NELEM(encode_case_##n##_expect), \
1120 encode_case_##n##_dec \
1123 static const struct encode_test_case encode_cases
[] = {
1149 static int test_wire_encode(int idx
)
1154 BUF_MEM
*buf
= NULL
;
1156 const struct encode_test_case
*c
= &encode_cases
[idx
];
1160 if (!TEST_ptr(buf
= BUF_MEM_new()))
1163 if (!TEST_int_eq(WPACKET_init(&wpkt
, buf
), 1))
1167 if (!TEST_int_eq(c
->serializer(&wpkt
), 1))
1170 if (!TEST_int_eq(WPACKET_get_total_written(&wpkt
, &written
), 1))
1173 if (!TEST_mem_eq(buf
->data
, written
, c
->expect_buf
, c
->expect_buf_len
))
1176 if (!TEST_int_eq(PACKET_buf_init(&pkt
, (unsigned char *)buf
->data
, written
), 1))
1179 if (!TEST_int_eq(c
->deserializer(&pkt
, -1), 1))
1182 if (!TEST_false(PACKET_remaining(&pkt
)))
1185 for (i
= 0; i
< c
->expect_buf_len
; ++i
) {
1189 * Check parsing truncated (i.e., malformed) input is handled correctly.
1190 * Generate all possible truncations of our reference encoding and
1191 * verify that they are handled correctly. The number of bytes of the
1192 * truncated encoding is passed as an argument to the deserializer to
1193 * help it determine whether decoding should fail or not.
1195 if (!TEST_int_eq(PACKET_buf_init(&pkt2
, (unsigned char *)c
->expect_buf
, i
), 1))
1198 if (!TEST_int_eq(c
->deserializer(&pkt2
, i
), 1))
1205 WPACKET_finish(&wpkt
);
1210 struct ack_test_case
{
1211 const unsigned char *input_buf
;
1212 size_t input_buf_len
;
1213 int (*deserializer
)(PACKET
*pkt
);
1217 /* ACK Frame with Excessive First ACK Range Field */
1218 static const unsigned char ack_case_1_input
[] = {
1219 0x02, /* ACK Without ECN */
1220 0x08, /* Largest Acknowledged */
1221 0x01, /* ACK Delay */
1222 0x00, /* ACK Range Count */
1223 0x09, /* First ACK Range */
1226 /* ACK Frame with Valid ACK Range Field */
1227 static const unsigned char ack_case_2_input
[] = {
1228 0x02, /* ACK Without ECN */
1229 0x08, /* Largest Acknowledged */
1230 0x01, /* ACK Delay */
1231 0x00, /* ACK Range Count */
1232 0x08, /* First ACK Range */
1235 /* ACK Frame with Excessive ACK Range Gap */
1236 static const unsigned char ack_case_3_input
[] = {
1237 0x02, /* ACK Without ECN */
1238 0x08, /* Largest Acknowledged */
1239 0x01, /* ACK Delay */
1240 0x01, /* ACK Range Count */
1241 0x01, /* First ACK Range */
1244 0x01, /* ACK Range Length */
1247 /* ACK Frame with Valid ACK Range */
1248 static const unsigned char ack_case_4_input
[] = {
1249 0x02, /* ACK Without ECN */
1250 0x08, /* Largest Acknowledged */
1251 0x01, /* ACK Delay */
1252 0x01, /* ACK Range Count */
1253 0x01, /* First ACK Range */
1256 0x01, /* ACK Range Length */
1259 /* ACK Frame with Excessive ACK Range Length */
1260 static const unsigned char ack_case_5_input
[] = {
1261 0x02, /* ACK Without ECN */
1262 0x08, /* Largest Acknowledged */
1263 0x01, /* ACK Delay */
1264 0x01, /* ACK Range Count */
1265 0x01, /* First ACK Range */
1268 0x02, /* ACK Range Length */
1271 /* ACK Frame with Multiple ACK Ranges, Final Having Excessive Length */
1272 static const unsigned char ack_case_6_input
[] = {
1273 0x02, /* ACK Without ECN */
1274 0x08, /* Largest Acknowledged */
1275 0x01, /* ACK Delay */
1276 0x02, /* ACK Range Count */
1277 0x01, /* First ACK Range */
1280 0x02, /* ACK Range Length */
1283 0x01, /* ACK Range Length */
1286 /* ACK Frame with Multiple ACK Ranges, Valid */
1287 static const unsigned char ack_case_7_input
[] = {
1288 0x02, /* ACK Without ECN */
1289 0x08, /* Largest Acknowledged */
1290 0x01, /* ACK Delay */
1291 0x02, /* ACK Range Count */
1292 0x01, /* First ACK Range */
1295 0x02, /* ACK Range Length */
1298 0x00, /* ACK Range Length */
1301 static int ack_generic_decode(PACKET
*pkt
)
1303 OSSL_QUIC_ACK_RANGE ranges
[8] = {0};
1304 OSSL_QUIC_FRAME_ACK f
= {0};
1305 uint64_t total_ranges
= 0, peek_total_ranges
= 0;
1309 f
.ack_ranges
= ranges
;
1310 f
.num_ack_ranges
= OSSL_NELEM(ranges
);
1312 if (!TEST_int_eq(ossl_quic_wire_peek_frame_ack_num_ranges(pkt
,
1313 &peek_total_ranges
), 1))
1316 r
= ossl_quic_wire_decode_frame_ack(pkt
, 3, &f
, &total_ranges
);
1320 if (!TEST_uint64_t_eq(total_ranges
, peek_total_ranges
))
1323 for (i
= 0; i
< f
.num_ack_ranges
; ++i
) {
1324 if (!TEST_uint64_t_le(f
.ack_ranges
[i
].start
, f
.ack_ranges
[i
].end
))
1326 if (!TEST_uint64_t_lt(f
.ack_ranges
[i
].end
, 1000))
1333 #define ACK_CASE(n, expect_fail, dec) \
1335 ack_case_##n##_input, \
1336 sizeof(ack_case_##n##_input), \
1341 static const struct ack_test_case ack_cases
[] = {
1342 ACK_CASE(1, 1, ack_generic_decode
)
1343 ACK_CASE(2, 0, ack_generic_decode
)
1344 ACK_CASE(3, 1, ack_generic_decode
)
1345 ACK_CASE(4, 0, ack_generic_decode
)
1346 ACK_CASE(5, 1, ack_generic_decode
)
1347 ACK_CASE(6, 1, ack_generic_decode
)
1348 ACK_CASE(7, 0, ack_generic_decode
)
1351 static int test_wire_ack(int idx
)
1353 int testresult
= 0, r
;
1355 const struct ack_test_case
*c
= &ack_cases
[idx
];
1357 if (!TEST_int_eq(PACKET_buf_init(&pkt
,
1358 (unsigned char *)c
->input_buf
,
1359 c
->input_buf_len
), 1))
1362 r
= c
->deserializer(&pkt
);
1363 if (c
->expect_fail
) {
1364 if (!TEST_int_eq(r
, 0))
1367 if (!TEST_int_eq(r
, 1))
1370 if (!TEST_false(PACKET_remaining(&pkt
)))
1379 /* Packet Header PN Encoding Tests */
1381 QUIC_PN pn
, tx_largest_acked
, rx_largest_pn
;
1383 unsigned char expected_bytes
[4];
1386 static const struct pn_test pn_tests
[] = {
1387 /* RFC 9000 Section A.2 */
1388 { 0xac5c02, 0xabe8b3, 0xabe8b3, 2, {0x5c,0x02} },
1389 { 0xace8fe, 0xabe8b3, 0xabe8b3, 3, {0xac,0xe8,0xfe} },
1390 /* RFC 9000 Section A.3 */
1391 { 0xa82f9b32, 0xa82f30ea, 0xa82f30ea, 2, {0x9b,0x32} },
1392 /* Boundary Cases */
1393 { 1, 0, 0, 1, {0x01} },
1394 { 256, 255, 255, 1, {0x00} },
1395 { 257, 255, 255, 1, {0x01} },
1396 { 256, 128, 128, 1, {0x00} },
1397 { 256, 127, 127, 2, {0x01,0x00} },
1398 { 65536, 32768, 32768, 2, {0x00,0x00} },
1399 { 65537, 32769, 32769, 2, {0x00,0x01} },
1400 { 65536, 32767, 32767, 3, {0x01,0x00,0x00} },
1401 { 65537, 32768, 32768, 3, {0x01,0x00,0x01} },
1402 { 16777216, 8388608, 8388608, 3, {0x00,0x00,0x00} },
1403 { 16777217, 8388609, 8388609, 3, {0x00,0x00,0x01} },
1404 { 16777216, 8388607, 8388607, 4, {0x01,0x00,0x00,0x00} },
1405 { 16777217, 8388608, 8388608, 4, {0x01,0x00,0x00,0x01} },
1406 { 4294967296, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x00} },
1407 { 4294967297, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x01} },
1410 static int test_wire_pkt_hdr_pn(int tidx
)
1413 const struct pn_test
*t
= &pn_tests
[tidx
];
1414 unsigned char buf
[4];
1418 pn_len
= ossl_quic_wire_determine_pn_len(t
->pn
, t
->tx_largest_acked
);
1419 if (!TEST_int_eq(pn_len
, (int)t
->expected_len
))
1422 if (!TEST_true(ossl_quic_wire_encode_pkt_hdr_pn(t
->pn
, buf
, pn_len
)))
1425 if (!TEST_mem_eq(t
->expected_bytes
, t
->expected_len
, buf
, pn_len
))
1428 if (!TEST_true(ossl_quic_wire_decode_pkt_hdr_pn(buf
, pn_len
,
1429 t
->rx_largest_pn
, &res_pn
)))
1432 if (!TEST_uint64_t_eq(res_pn
, t
->pn
))
1440 int setup_tests(void)
1442 ADD_ALL_TESTS(test_wire_encode
, OSSL_NELEM(encode_cases
));
1443 ADD_ALL_TESTS(test_wire_ack
, OSSL_NELEM(ack_cases
));
1444 ADD_ALL_TESTS(test_wire_pkt_hdr_pn
, OSSL_NELEM(pn_tests
));