2 * Copyright 2015-2018 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"
14 struct encode_test_case
{
15 int (*serializer
)(WPACKET
*pkt
);
16 const unsigned char *expect_buf
;
17 size_t expect_buf_len
;
19 * fail: -1 if not truncated (function should test for success), else number
20 * of bytes to which the input has been truncated (function should test that
23 int (*deserializer
)(PACKET
*pkt
, ossl_ssize_t fail
);
27 static int encode_case_1_enc(WPACKET
*pkt
)
29 if (!TEST_int_eq(ossl_quic_wire_encode_padding(pkt
, 3), 1))
35 static int encode_case_1_dec(PACKET
*pkt
, ossl_ssize_t fail
)
38 /* No failure modes for padding */
41 if (!TEST_int_eq(ossl_quic_wire_decode_padding(pkt
), 3))
47 static const unsigned char encode_case_1_expect
[] = {
52 static int encode_case_2_enc(WPACKET
*pkt
)
55 if (!TEST_int_eq(ossl_quic_wire_encode_frame_ping(pkt
), 1))
61 static int encode_case_2_dec(PACKET
*pkt
, ossl_ssize_t fail
)
64 if (!TEST_int_eq(ossl_quic_wire_decode_frame_ping(pkt
), fail
< 0))
70 static const unsigned char encode_case_2_expect
[] = {
75 static const OSSL_QUIC_ACK_RANGE encode_case_3_ranges
[] = {
80 static const OSSL_QUIC_FRAME_ACK encode_case_3_f
= {
81 (OSSL_QUIC_ACK_RANGE
*)encode_case_3_ranges
,
82 OSSL_NELEM(encode_case_3_ranges
),
87 static int encode_case_3_enc(WPACKET
*pkt
)
89 if (!TEST_int_eq(ossl_quic_wire_encode_frame_ack(pkt
, 3, &encode_case_3_f
), 1))
95 static int encode_case_3_dec(PACKET
*pkt
, ossl_ssize_t fail
)
97 OSSL_QUIC_ACK_RANGE ranges
[4] = {0};
98 OSSL_QUIC_FRAME_ACK f
= {0};
99 uint64_t total_ranges
= 0, peek_total_ranges
= 0;
102 f
.ack_ranges
= ranges
;
103 f
.num_ack_ranges
= OSSL_NELEM(ranges
);
105 ret
= ossl_quic_wire_peek_frame_ack_num_ranges(pkt
, &peek_total_ranges
);
106 if (fail
< 0 && !TEST_int_eq(ret
, 1))
109 if (!TEST_int_eq(ossl_quic_wire_decode_frame_ack(pkt
, 3, &f
, &total_ranges
), fail
< 0))
112 if (ret
== 1 && !TEST_uint64_t_eq(peek_total_ranges
, 2))
118 if (!TEST_uint64_t_eq(total_ranges
, peek_total_ranges
))
121 if (!TEST_mem_eq(f
.ack_ranges
, f
.num_ack_ranges
* sizeof(OSSL_QUIC_ACK_RANGE
),
122 encode_case_3_f
.ack_ranges
,
123 encode_case_3_f
.num_ack_ranges
* sizeof(OSSL_QUIC_ACK_RANGE
)))
126 if (!TEST_uint64_t_eq(ossl_time2ticks(f
.delay_time
),
127 ossl_time2ticks(encode_case_3_f
.delay_time
)))
130 if (!TEST_true(f
.ecn_present
))
133 if (!TEST_uint64_t_eq(f
.ect0
, encode_case_3_f
.ect0
))
136 if (!TEST_uint64_t_eq(f
.ect1
, encode_case_3_f
.ect1
))
139 if (!TEST_uint64_t_eq(f
.ecnce
, encode_case_3_f
.ecnce
))
145 static const unsigned char encode_case_3_expect
[] = {
147 0x1E, /* Largest Acknowledged */
148 0x40, 0x7d, /* ACK Delay */
149 1, /* ACK Range Count */
150 10, /* First ACK Range */
155 0x3c, /* ECT0 Count */
156 0x40, 0x46, /* ECT1 Count */
157 0x40, 0x50, /* ECNCE Count */
160 /* 4. RESET_STREAM */
161 static const OSSL_QUIC_FRAME_RESET_STREAM encode_case_4_f
= {
162 0x1234, 0x9781, 0x11717
165 static int encode_case_4_enc(WPACKET
*pkt
)
167 if (!TEST_int_eq(ossl_quic_wire_encode_frame_reset_stream(pkt
,
168 &encode_case_4_f
), 1))
174 static int encode_case_4_dec(PACKET
*pkt
, ossl_ssize_t fail
)
176 OSSL_QUIC_FRAME_RESET_STREAM f
= {0};
178 if (!TEST_int_eq(ossl_quic_wire_decode_frame_reset_stream(pkt
, &f
), fail
< 0))
184 if (!TEST_mem_eq(&f
, sizeof(f
), &encode_case_4_f
, sizeof(encode_case_4_f
)))
190 static const unsigned char encode_case_4_expect
[] = {
192 0x52, 0x34, /* Stream ID */
193 0x80, 0x00, 0x97, 0x81, /* App Error Code */
194 0x80, 0x01, 0x17, 0x17, /* Final Size */
197 /* 5. STOP_SENDING */
198 static const OSSL_QUIC_FRAME_STOP_SENDING encode_case_5_f
= {
202 static int encode_case_5_enc(WPACKET
*pkt
)
204 if (!TEST_int_eq(ossl_quic_wire_encode_frame_stop_sending(pkt
,
205 &encode_case_5_f
), 1))
211 static int encode_case_5_dec(PACKET
*pkt
, ossl_ssize_t fail
)
213 OSSL_QUIC_FRAME_STOP_SENDING f
= {0};
215 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stop_sending(pkt
, &f
), fail
< 0))
221 if (!TEST_mem_eq(&f
, sizeof(f
), &encode_case_5_f
, sizeof(encode_case_5_f
)))
227 static const unsigned char encode_case_5_expect
[] = {
229 0x52, 0x34, /* Stream ID */
230 0x80, 0x00, 0x97, 0x81 /* App Error Code */
234 static const unsigned char encode_case_6_data
[] = {
238 static const OSSL_QUIC_FRAME_CRYPTO encode_case_6_f
= {
239 0x1234, sizeof(encode_case_6_data
), encode_case_6_data
242 static int encode_case_6_enc(WPACKET
*pkt
)
244 if (!TEST_ptr(ossl_quic_wire_encode_frame_crypto(pkt
,
251 static int encode_case_6_dec(PACKET
*pkt
, ossl_ssize_t fail
)
253 OSSL_QUIC_FRAME_CRYPTO f
= {0};
255 if (!TEST_int_eq(ossl_quic_wire_decode_frame_crypto(pkt
, &f
), fail
< 0))
261 if (!TEST_uint64_t_eq(f
.offset
, 0x1234))
264 if (!TEST_mem_eq(f
.data
, f
.len
, encode_case_6_data
, sizeof(encode_case_6_data
)))
270 static const unsigned char encode_case_6_expect
[] = {
272 0x52, 0x34, /* Offset */
274 93, 18, 17, 102, 33 /* Data */
278 static const unsigned char encode_case_7_token
[] = {
279 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
280 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
283 static int encode_case_7_enc(WPACKET
*pkt
)
285 if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_token(pkt
,
287 sizeof(encode_case_7_token
)), 1))
293 static int encode_case_7_dec(PACKET
*pkt
, ossl_ssize_t fail
)
295 const unsigned char *token
= NULL
;
296 size_t token_len
= 0;
298 if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_token(pkt
,
300 &token_len
), fail
< 0))
306 if (!TEST_mem_eq(token
, token_len
,
307 encode_case_7_token
, sizeof(encode_case_7_token
)))
313 static const unsigned char encode_case_7_expect
[] = {
316 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Token */
317 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
320 /* 8. STREAM (no length, no offset, no fin) */
321 static const unsigned char encode_case_8_data
[] = {
322 0xde, 0x06, 0xcb, 0x76, 0x5d
324 static const OSSL_QUIC_FRAME_STREAM encode_case_8_f
= {
325 0x1234, 0, 5, encode_case_8_data
, 0, 0
328 static int encode_case_8_enc(WPACKET
*pkt
)
330 if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt
,
337 static int encode_case_8_dec(PACKET
*pkt
, ossl_ssize_t fail
)
339 OSSL_QUIC_FRAME_STREAM f
= {0};
343 * This case uses implicit length signalling so truncation will not
344 * cause it to fail unless the header (which is 3 bytes) is truncated.
348 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt
, &f
), fail
< 0))
354 if (!TEST_mem_eq(f
.data
, f
.len
,
355 encode_case_8_data
, sizeof(encode_case_8_data
)))
358 if (!TEST_uint64_t_eq(f
.stream_id
, 0x1234))
361 if (!TEST_uint64_t_eq(f
.offset
, 0))
364 if (!TEST_int_eq(f
.has_explicit_len
, 0))
367 if (!TEST_int_eq(f
.is_fin
, 0))
373 static const unsigned char encode_case_8_expect
[] = {
374 0x08, /* Type (OFF=0, LEN=0, FIN=0) */
375 0x52, 0x34, /* Stream ID */
376 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
379 /* 9. STREAM (length, offset, fin) */
380 static const unsigned char encode_case_9_data
[] = {
381 0xde, 0x06, 0xcb, 0x76, 0x5d
383 static const OSSL_QUIC_FRAME_STREAM encode_case_9_f
= {
384 0x1234, 0x39, 5, encode_case_9_data
, 1, 1
387 static int encode_case_9_enc(WPACKET
*pkt
)
389 if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt
,
396 static int encode_case_9_dec(PACKET
*pkt
, ossl_ssize_t fail
)
398 OSSL_QUIC_FRAME_STREAM f
= {0};
400 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt
, &f
), fail
< 0))
406 if (!TEST_mem_eq(f
.data
, f
.len
,
407 encode_case_9_data
, sizeof(encode_case_9_data
)))
410 if (!TEST_uint64_t_eq(f
.stream_id
, 0x1234))
413 if (!TEST_uint64_t_eq(f
.offset
, 0x39))
416 if (!TEST_int_eq(f
.has_explicit_len
, 1))
419 if (!TEST_int_eq(f
.is_fin
, 1))
425 static const unsigned char encode_case_9_expect
[] = {
426 0x0f, /* Type (OFF=1, LEN=1, FIN=1) */
427 0x52, 0x34, /* Stream ID */
430 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
434 static int encode_case_10_enc(WPACKET
*pkt
)
436 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_data(pkt
, 0x1234), 1))
442 static int encode_case_10_dec(PACKET
*pkt
, ossl_ssize_t fail
)
444 uint64_t max_data
= 0;
446 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_data(pkt
, &max_data
), fail
< 0))
452 if (!TEST_uint64_t_eq(max_data
, 0x1234))
458 static const unsigned char encode_case_10_expect
[] = {
460 0x52, 0x34, /* Max Data */
463 /* 11. MAX_STREAM_DATA */
464 static int encode_case_11_enc(WPACKET
*pkt
)
466 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_stream_data(pkt
,
474 static int encode_case_11_dec(PACKET
*pkt
, ossl_ssize_t fail
)
476 uint64_t stream_id
= 0, max_data
= 0;
478 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_stream_data(pkt
,
480 &max_data
), fail
< 0))
486 if (!TEST_uint64_t_eq(stream_id
, 0x1234))
489 if (!TEST_uint64_t_eq(max_data
, 0x9781))
495 static const unsigned char encode_case_11_expect
[] = {
497 0x52, 0x34, /* Stream ID */
498 0x80, 0x00, 0x97, 0x81, /* Max Data */
501 /* 12. MAX_STREAMS */
502 static int encode_case_12_enc(WPACKET
*pkt
)
504 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt
, 0, 0x1234), 1))
507 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt
, 1, 0x9781), 1))
513 static int encode_case_12_dec(PACKET
*pkt
, ossl_ssize_t fail
)
515 uint64_t max_streams_1
= 0, max_streams_2
= 0,
516 frame_type_1
= 0, frame_type_2
= 0;
518 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt
, &frame_type_1
),
519 fail
< 0 || fail
>= 1))
522 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt
,
524 fail
< 0 || fail
>= 3))
527 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt
, &frame_type_2
),
528 fail
< 0 || fail
>= 4))
531 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt
,
536 if ((fail
< 0 || fail
>= 3)
537 && !TEST_uint64_t_eq(frame_type_1
, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI
))
540 if ((fail
< 0 || fail
>= 3)
541 && !TEST_uint64_t_eq(max_streams_1
, 0x1234))
544 if ((fail
< 0 || fail
>= 8)
545 && !TEST_uint64_t_eq(frame_type_2
, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI
))
548 if ((fail
< 0 || fail
>= 8)
549 && !TEST_uint64_t_eq(max_streams_2
, 0x9781))
555 static const unsigned char encode_case_12_expect
[] = {
556 0x12, /* Type (MAX_STREAMS Bidirectional) */
557 0x52, 0x34, /* Max Streams */
558 0x13, /* Type (MAX_STREAMS Unidirectional) */
559 0x80, 0x00, 0x97, 0x81, /* Max Streams */
562 /* 13. DATA_BLOCKED */
563 static int encode_case_13_enc(WPACKET
*pkt
)
565 if (!TEST_int_eq(ossl_quic_wire_encode_frame_data_blocked(pkt
, 0x1234), 1))
571 static int encode_case_13_dec(PACKET
*pkt
, ossl_ssize_t fail
)
573 uint64_t max_data
= 0;
575 if (!TEST_int_eq(ossl_quic_wire_decode_frame_data_blocked(pkt
,
576 &max_data
), fail
< 0))
582 if (!TEST_uint64_t_eq(max_data
, 0x1234))
588 static const unsigned char encode_case_13_expect
[] = {
590 0x52, 0x34, /* Max Data */
593 /* 14. STREAM_DATA_BLOCKED */
594 static int encode_case_14_enc(WPACKET
*pkt
)
596 if (!TEST_int_eq(ossl_quic_wire_encode_frame_stream_data_blocked(pkt
,
604 static int encode_case_14_dec(PACKET
*pkt
, ossl_ssize_t fail
)
606 uint64_t stream_id
= 0, max_data
= 0;
608 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream_data_blocked(pkt
,
610 &max_data
), fail
< 0))
616 if (!TEST_uint64_t_eq(stream_id
, 0x1234))
619 if (!TEST_uint64_t_eq(max_data
, 0x9781))
625 static const unsigned char encode_case_14_expect
[] = {
627 0x52, 0x34, /* Stream ID */
628 0x80, 0x00, 0x97, 0x81, /* Max Data */
631 /* 15. STREAMS_BLOCKED */
632 static int encode_case_15_enc(WPACKET
*pkt
)
634 if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt
, 0, 0x1234), 1))
637 if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt
, 1, 0x9781), 1))
643 static int encode_case_15_dec(PACKET
*pkt
, ossl_ssize_t fail
)
645 uint64_t max_streams_1
= 0, max_streams_2
= 0,
646 frame_type_1
= 0, frame_type_2
= 0;
648 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt
, &frame_type_1
),
649 fail
< 0 || fail
>= 1))
652 if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt
,
654 fail
< 0 || fail
>= 3))
657 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt
, &frame_type_2
),
658 fail
< 0 || fail
>= 4))
661 if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt
,
663 fail
< 0 || fail
>= 8))
666 if ((fail
< 0 || fail
>= 1)
667 && !TEST_uint64_t_eq(frame_type_1
, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI
))
670 if ((fail
< 0 || fail
>= 3)
671 && !TEST_uint64_t_eq(max_streams_1
, 0x1234))
674 if ((fail
< 0 || fail
>= 4)
675 && !TEST_uint64_t_eq(frame_type_2
, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI
))
678 if ((fail
< 0 || fail
>= 8)
679 && !TEST_uint64_t_eq(max_streams_2
, 0x9781))
685 static const unsigned char encode_case_15_expect
[] = {
686 0x16, /* Type (STREAMS_BLOCKED Bidirectional) */
687 0x52, 0x34, /* Max Streams */
688 0x17, /* Type (STREAMS_BLOCKED Unidirectional) */
689 0x80, 0x00, 0x97, 0x81, /* Max Streams */
692 /* 16. NEW_CONNECTION_ID */
693 static const unsigned char encode_case_16_conn_id
[] = {
694 0x33, 0x44, 0x55, 0x66
697 static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16_f
= {
702 {0x33, 0x44, 0x55, 0x66}
705 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
706 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
710 static int encode_case_16_enc(WPACKET
*pkt
)
712 if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt
,
713 &encode_case_16_f
), 1))
719 static int encode_case_16_dec(PACKET
*pkt
, ossl_ssize_t fail
)
721 OSSL_QUIC_FRAME_NEW_CONN_ID f
= {0};
723 if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt
, &f
), fail
< 0))
729 if (!TEST_uint64_t_eq(f
.seq_num
, 0x1234))
732 if (!TEST_uint64_t_eq(f
.retire_prior_to
, 0x9781))
735 if (!TEST_uint64_t_eq(f
.conn_id
.id_len
, sizeof(encode_case_16_conn_id
)))
738 if (!TEST_mem_eq(f
.conn_id
.id
, f
.conn_id
.id_len
,
739 encode_case_16_conn_id
, sizeof(encode_case_16_conn_id
)))
742 if (!TEST_mem_eq(f
.stateless_reset_token
,
743 sizeof(f
.stateless_reset_token
),
744 encode_case_16_f
.stateless_reset_token
,
745 sizeof(encode_case_16_f
.stateless_reset_token
)))
751 static const unsigned char encode_case_16_expect
[] = {
753 0x52, 0x34, /* Sequence Number */
754 0x80, 0x00, 0x97, 0x81, /* Retire Prior To */
755 0x04, /* Connection ID Length */
756 0x33, 0x44, 0x55, 0x66, /* Connection ID */
757 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
758 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
761 /* 17. RETIRE_CONNECTION_ID */
762 static int encode_case_17_enc(WPACKET
*pkt
)
764 if (!TEST_int_eq(ossl_quic_wire_encode_frame_retire_conn_id(pkt
, 0x1234), 1))
770 static int encode_case_17_dec(PACKET
*pkt
, ossl_ssize_t fail
)
772 uint64_t seq_num
= 0;
774 if (!TEST_int_eq(ossl_quic_wire_decode_frame_retire_conn_id(pkt
, &seq_num
), fail
< 0))
780 if (!TEST_uint64_t_eq(seq_num
, 0x1234))
786 static const unsigned char encode_case_17_expect
[] = {
788 0x52, 0x34, /* Seq Num */
791 /* 18. PATH_CHALLENGE */
792 static const uint64_t encode_case_18_data
793 = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL
;
795 static int encode_case_18_enc(WPACKET
*pkt
)
797 if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_challenge(pkt
,
798 encode_case_18_data
), 1))
804 static int encode_case_18_dec(PACKET
*pkt
, ossl_ssize_t fail
)
806 uint64_t challenge
= 0;
808 if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_challenge(pkt
, &challenge
), fail
< 0))
814 if (!TEST_uint64_t_eq(challenge
, encode_case_18_data
))
820 static const unsigned char encode_case_18_expect
[] = {
822 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
825 /* 19. PATH_RESPONSE */
826 static const uint64_t encode_case_19_data
827 = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL
;
829 static int encode_case_19_enc(WPACKET
*pkt
)
831 if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_response(pkt
,
832 encode_case_19_data
), 1))
838 static int encode_case_19_dec(PACKET
*pkt
, ossl_ssize_t fail
)
840 uint64_t challenge
= 0;
842 if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_response(pkt
, &challenge
), fail
< 0))
848 if (!TEST_uint64_t_eq(challenge
, encode_case_19_data
))
854 static const unsigned char encode_case_19_expect
[] = {
856 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
859 /* 20. CONNECTION_CLOSE (transport) */
860 static const char encode_case_20_reason
[] = {
861 /* "reason for closure" */
862 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f,
863 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
866 static const OSSL_QUIC_FRAME_CONN_CLOSE encode_case_20_f
= {
870 encode_case_20_reason
,
871 sizeof(encode_case_20_reason
)
874 static int encode_case_20_enc(WPACKET
*pkt
)
876 if (!TEST_int_eq(ossl_quic_wire_encode_frame_conn_close(pkt
,
877 &encode_case_20_f
), 1))
883 static int encode_case_20_dec(PACKET
*pkt
, ossl_ssize_t fail
)
885 OSSL_QUIC_FRAME_CONN_CLOSE f
= {0};
887 if (!TEST_int_eq(ossl_quic_wire_decode_frame_conn_close(pkt
, &f
), fail
< 0))
893 if (!TEST_int_eq(f
.is_app
, 0))
896 if (!TEST_uint64_t_eq(f
.error_code
, 0x1234))
899 if (!TEST_uint64_t_eq(f
.frame_type
, 0x9781))
902 if (!TEST_size_t_eq(f
.reason_len
, 18))
905 if (!TEST_mem_eq(f
.reason
, f
.reason_len
,
906 encode_case_20_f
.reason
, encode_case_20_f
.reason_len
))
912 static const unsigned char encode_case_20_expect
[] = {
914 0x52, 0x34, /* Sequence Number */
915 0x80, 0x00, 0x97, 0x81, /* Frame Type */
916 0x12, /* Reason Length */
917 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f, /* Reason */
918 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
921 /* 21. HANDSHAKE_DONE */
922 static int encode_case_21_enc(WPACKET
*pkt
)
925 if (!TEST_int_eq(ossl_quic_wire_encode_frame_handshake_done(pkt
), 1))
931 static int encode_case_21_dec(PACKET
*pkt
, ossl_ssize_t fail
)
934 if (!TEST_int_eq(ossl_quic_wire_decode_frame_handshake_done(pkt
), fail
< 0))
940 static const unsigned char encode_case_21_expect
[] = {
944 /* 22. Buffer Transport Parameter */
945 static const unsigned char encode_case_22_data
[] = {0x55,0x77,0x32,0x46,0x99};
947 static int encode_case_22_enc(WPACKET
*pkt
)
951 if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(pkt
, 0x1234,
953 sizeof(encode_case_22_data
))))
956 if (!TEST_ptr(p
= ossl_quic_wire_encode_transport_param_bytes(pkt
, 0x9781,
966 static int encode_case_22_dec(PACKET
*pkt
, ossl_ssize_t fail
)
970 const unsigned char *p
;
971 static const unsigned char data
[] = {0x33, 0x44};
973 if (!TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt
, &id
),
974 fail
< 0 || fail
>= 2))
977 if ((fail
< 0 || fail
>= 2)
978 && !TEST_uint64_t_eq(id
, 0x1234))
983 p
= ossl_quic_wire_decode_transport_param_bytes(pkt
, &id
, &len
);
984 if (fail
< 0 || fail
>= 8) {
988 if (!TEST_ptr_null(p
))
992 if ((fail
< 0 || fail
>= 8)
993 && !TEST_uint64_t_eq(id
, 0x1234))
996 if ((fail
< 0 || fail
>= 8)
997 && !TEST_mem_eq(p
, len
, encode_case_22_data
, sizeof(encode_case_22_data
)))
1000 if ((fail
< 0 || fail
>= 8)
1001 && !TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt
, &id
),
1002 fail
< 0 || fail
>= 12))
1005 if ((fail
< 0 || fail
>= 12)
1006 && !TEST_uint64_t_eq(id
, 0x9781))
1011 p
= ossl_quic_wire_decode_transport_param_bytes(pkt
, &id
, &len
);
1012 if (fail
< 0 || fail
>= 15) {
1016 if (!TEST_ptr_null(p
))
1020 if ((fail
< 0 || fail
>= 15)
1021 && !TEST_uint64_t_eq(id
, 0x9781))
1024 if ((fail
< 0 || fail
>= 15)
1025 && !TEST_mem_eq(p
, len
, data
, sizeof(data
)))
1031 static const unsigned char encode_case_22_expect
[] = {
1032 0x52, 0x34, /* ID */
1034 0x55, 0x77, 0x32, 0x46, 0x99, /* Data */
1036 0x80, 0x00, 0x97, 0x81, /* ID */
1038 0x33, 0x44 /* Data */
1041 /* 23. Integer Transport Parameter */
1042 static int encode_case_23_enc(WPACKET
*pkt
)
1044 if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt
, 0x1234, 0x9781), 1))
1047 if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt
, 0x2233, 0x4545), 1))
1053 static int encode_case_23_dec(PACKET
*pkt
, ossl_ssize_t fail
)
1055 uint64_t id
= 0, value
= 0;
1057 if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt
,
1059 fail
< 0 || fail
>= 7))
1062 if ((fail
< 0 || fail
>= 7)
1063 && !TEST_uint64_t_eq(id
, 0x1234))
1066 if ((fail
< 0 || fail
>= 7)
1067 && !TEST_uint64_t_eq(value
, 0x9781))
1070 if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt
,
1072 fail
< 0 || fail
>= 14))
1075 if ((fail
< 0 || fail
>= 14)
1076 && !TEST_uint64_t_eq(id
, 0x2233))
1079 if ((fail
< 0 || fail
>= 14)
1080 && !TEST_uint64_t_eq(value
, 0x4545))
1086 static const unsigned char encode_case_23_expect
[] = {
1089 0x80, 0x00, 0x97, 0x81,
1093 0x80, 0x00, 0x45, 0x45,
1096 #define ENCODE_CASE(n) \
1098 encode_case_##n##_enc, \
1099 encode_case_##n##_expect, \
1100 OSSL_NELEM(encode_case_##n##_expect), \
1101 encode_case_##n##_dec \
1104 static const struct encode_test_case encode_cases
[] = {
1130 static int test_wire_encode(int idx
)
1135 BUF_MEM
*buf
= NULL
;
1137 const struct encode_test_case
*c
= &encode_cases
[idx
];
1141 if (!TEST_ptr(buf
= BUF_MEM_new()))
1144 if (!TEST_int_eq(WPACKET_init(&wpkt
, buf
), 1))
1148 if (!TEST_int_eq(c
->serializer(&wpkt
), 1))
1151 if (!TEST_int_eq(WPACKET_get_total_written(&wpkt
, &written
), 1))
1154 if (!TEST_mem_eq(buf
->data
, written
, c
->expect_buf
, c
->expect_buf_len
))
1157 if (!TEST_int_eq(PACKET_buf_init(&pkt
, (unsigned char *)buf
->data
, written
), 1))
1160 if (!TEST_int_eq(c
->deserializer(&pkt
, -1), 1))
1163 if (!TEST_false(PACKET_remaining(&pkt
)))
1166 for (i
= 0; i
< c
->expect_buf_len
; ++i
) {
1170 * Check parsing truncated (i.e., malformed) input is handled correctly.
1171 * Generate all possible truncations of our reference encoding and
1172 * verify that they are handled correctly. The number of bytes of the
1173 * truncated encoding is passed as an argument to the deserializer to
1174 * help it determine whether decoding should fail or not.
1176 if (!TEST_int_eq(PACKET_buf_init(&pkt2
, (unsigned char *)c
->expect_buf
, i
), 1))
1179 if (!TEST_int_eq(c
->deserializer(&pkt2
, i
), 1))
1186 WPACKET_finish(&wpkt
);
1191 struct ack_test_case
{
1192 const unsigned char *input_buf
;
1193 size_t input_buf_len
;
1194 int (*deserializer
)(PACKET
*pkt
);
1198 /* ACK Frame with Excessive First ACK Range Field */
1199 static const unsigned char ack_case_1_input
[] = {
1200 0x02, /* ACK Without ECN */
1201 0x08, /* Largest Acknowledged */
1202 0x01, /* ACK Delay */
1203 0x00, /* ACK Range Count */
1204 0x09, /* First ACK Range */
1207 /* ACK Frame with Valid ACK Range Field */
1208 static const unsigned char ack_case_2_input
[] = {
1209 0x02, /* ACK Without ECN */
1210 0x08, /* Largest Acknowledged */
1211 0x01, /* ACK Delay */
1212 0x00, /* ACK Range Count */
1213 0x08, /* First ACK Range */
1216 /* ACK Frame with Excessive ACK Range Gap */
1217 static const unsigned char ack_case_3_input
[] = {
1218 0x02, /* ACK Without ECN */
1219 0x08, /* Largest Acknowledged */
1220 0x01, /* ACK Delay */
1221 0x01, /* ACK Range Count */
1222 0x01, /* First ACK Range */
1225 0x01, /* ACK Range Length */
1228 /* ACK Frame with Valid ACK Range */
1229 static const unsigned char ack_case_4_input
[] = {
1230 0x02, /* ACK Without ECN */
1231 0x08, /* Largest Acknowledged */
1232 0x01, /* ACK Delay */
1233 0x01, /* ACK Range Count */
1234 0x01, /* First ACK Range */
1237 0x01, /* ACK Range Length */
1240 /* ACK Frame with Excessive ACK Range Length */
1241 static const unsigned char ack_case_5_input
[] = {
1242 0x02, /* ACK Without ECN */
1243 0x08, /* Largest Acknowledged */
1244 0x01, /* ACK Delay */
1245 0x01, /* ACK Range Count */
1246 0x01, /* First ACK Range */
1249 0x02, /* ACK Range Length */
1252 /* ACK Frame with Multiple ACK Ranges, Final Having Excessive Length */
1253 static const unsigned char ack_case_6_input
[] = {
1254 0x02, /* ACK Without ECN */
1255 0x08, /* Largest Acknowledged */
1256 0x01, /* ACK Delay */
1257 0x02, /* ACK Range Count */
1258 0x01, /* First ACK Range */
1261 0x02, /* ACK Range Length */
1264 0x01, /* ACK Range Length */
1267 /* ACK Frame with Multiple ACK Ranges, Valid */
1268 static const unsigned char ack_case_7_input
[] = {
1269 0x02, /* ACK Without ECN */
1270 0x08, /* Largest Acknowledged */
1271 0x01, /* ACK Delay */
1272 0x02, /* ACK Range Count */
1273 0x01, /* First ACK Range */
1276 0x02, /* ACK Range Length */
1279 0x00, /* ACK Range Length */
1282 static int ack_generic_decode(PACKET
*pkt
)
1284 OSSL_QUIC_ACK_RANGE ranges
[8] = {0};
1285 OSSL_QUIC_FRAME_ACK f
= {0};
1286 uint64_t total_ranges
= 0, peek_total_ranges
= 0;
1290 f
.ack_ranges
= ranges
;
1291 f
.num_ack_ranges
= OSSL_NELEM(ranges
);
1293 if (!TEST_int_eq(ossl_quic_wire_peek_frame_ack_num_ranges(pkt
,
1294 &peek_total_ranges
), 1))
1297 r
= ossl_quic_wire_decode_frame_ack(pkt
, 3, &f
, &total_ranges
);
1301 if (!TEST_uint64_t_eq(total_ranges
, peek_total_ranges
))
1304 for (i
= 0; i
< f
.num_ack_ranges
; ++i
) {
1305 if (!TEST_uint64_t_le(f
.ack_ranges
[i
].start
, f
.ack_ranges
[i
].end
))
1307 if (!TEST_uint64_t_lt(f
.ack_ranges
[i
].end
, 1000))
1314 #define ACK_CASE(n, expect_fail, dec) \
1316 ack_case_##n##_input, \
1317 sizeof(ack_case_##n##_input), \
1322 static const struct ack_test_case ack_cases
[] = {
1323 ACK_CASE(1, 1, ack_generic_decode
)
1324 ACK_CASE(2, 0, ack_generic_decode
)
1325 ACK_CASE(3, 1, ack_generic_decode
)
1326 ACK_CASE(4, 0, ack_generic_decode
)
1327 ACK_CASE(5, 1, ack_generic_decode
)
1328 ACK_CASE(6, 1, ack_generic_decode
)
1329 ACK_CASE(7, 0, ack_generic_decode
)
1332 static int test_wire_ack(int idx
)
1334 int testresult
= 0, r
;
1336 const struct ack_test_case
*c
= &ack_cases
[idx
];
1338 if (!TEST_int_eq(PACKET_buf_init(&pkt
,
1339 (unsigned char *)c
->input_buf
,
1340 c
->input_buf_len
), 1))
1343 r
= c
->deserializer(&pkt
);
1344 if (c
->expect_fail
) {
1345 if (!TEST_int_eq(r
, 0))
1348 if (!TEST_int_eq(r
, 1))
1351 if (!TEST_false(PACKET_remaining(&pkt
)))
1360 int setup_tests(void)
1362 ADD_ALL_TESTS(test_wire_encode
, OSSL_NELEM(encode_cases
));
1363 ADD_ALL_TESTS(test_wire_ack
, OSSL_NELEM(ack_cases
));