]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/quic_wire_test.c
Fix test cases using NEW_CONNECTION_ID frame
[thirdparty/openssl.git] / test / quic_wire_test.c
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
10 #include "internal/packet.h"
11 #include "internal/quic_wire.h"
12 #include "internal/quic_wire_pkt.h"
13 #include "testutil.h"
14
15 struct encode_test_case {
16 int (*serializer)(WPACKET *pkt);
17 const unsigned char *expect_buf;
18 size_t expect_buf_len;
19 /*
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
22 * decoding fails)
23 */
24 int (*deserializer)(PACKET *pkt, ossl_ssize_t fail);
25 };
26
27 /* 1. PADDING */
28 static int encode_case_1_enc(WPACKET *pkt)
29 {
30 if (!TEST_int_eq(ossl_quic_wire_encode_padding(pkt, 3), 1))
31 return 0;
32
33 return 1;
34 }
35
36 static int encode_case_1_dec(PACKET *pkt, ossl_ssize_t fail)
37 {
38 if (fail >= 0)
39 /* No failure modes for padding */
40 return 1;
41
42 if (!TEST_int_eq(ossl_quic_wire_decode_padding(pkt), 3))
43 return 0;
44
45 return 1;
46 }
47
48 static const unsigned char encode_case_1_expect[] = {
49 0, 0, 0
50 };
51
52 /* 2. PING */
53 static int encode_case_2_enc(WPACKET *pkt)
54 {
55
56 if (!TEST_int_eq(ossl_quic_wire_encode_frame_ping(pkt), 1))
57 return 0;
58
59 return 1;
60 }
61
62 static int encode_case_2_dec(PACKET *pkt, ossl_ssize_t fail)
63 {
64
65 if (!TEST_int_eq(ossl_quic_wire_decode_frame_ping(pkt), fail < 0))
66 return 0;
67
68 return 1;
69 }
70
71 static const unsigned char encode_case_2_expect[] = {
72 0x01
73 };
74
75 /* 3. ACK */
76 static const OSSL_QUIC_ACK_RANGE encode_case_3_ranges[] = {
77 { 20, 30 },
78 { 0, 10 }
79 };
80
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),
84 { OSSL_TIME_MS },
85 60, 70, 80, 1
86 };
87
88 static int encode_case_3_enc(WPACKET *pkt)
89 {
90 if (!TEST_int_eq(ossl_quic_wire_encode_frame_ack(pkt, 3, &encode_case_3_f), 1))
91 return 0;
92
93 return 1;
94 }
95
96 static int encode_case_3_dec(PACKET *pkt, ossl_ssize_t fail)
97 {
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;
101 int ret;
102
103 f.ack_ranges = ranges;
104 f.num_ack_ranges = OSSL_NELEM(ranges);
105
106 ret = ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &peek_total_ranges);
107 if (fail < 0 && !TEST_int_eq(ret, 1))
108 return 0;
109
110 if (!TEST_int_eq(ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges), fail < 0))
111 return 0;
112
113 if (ret == 1 && !TEST_uint64_t_eq(peek_total_ranges, 2))
114 return 0;
115
116 if (fail >= 0)
117 return 1;
118
119 if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
120 return 0;
121
122 if (!TEST_uint64_t_le(f.num_ack_ranges * sizeof(OSSL_QUIC_ACK_RANGE),
123 SIZE_MAX)
124 || !TEST_uint64_t_le(encode_case_3_f.num_ack_ranges
125 * sizeof(OSSL_QUIC_ACK_RANGE),
126 SIZE_MAX))
127 return 0;
128
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)))
133 return 0;
134
135 if (!TEST_uint64_t_eq(ossl_time2ticks(f.delay_time),
136 ossl_time2ticks(encode_case_3_f.delay_time)))
137 return 0;
138
139 if (!TEST_true(f.ecn_present))
140 return 0;
141
142 if (!TEST_uint64_t_eq(f.ect0, encode_case_3_f.ect0))
143 return 0;
144
145 if (!TEST_uint64_t_eq(f.ect1, encode_case_3_f.ect1))
146 return 0;
147
148 if (!TEST_uint64_t_eq(f.ecnce, encode_case_3_f.ecnce))
149 return 0;
150
151 return 1;
152 }
153
154 static const unsigned char encode_case_3_expect[] = {
155 0x03, /* Type */
156 0x1E, /* Largest Acknowledged */
157 0x40, 0x7d, /* ACK Delay */
158 1, /* ACK Range Count */
159 10, /* First ACK Range */
160
161 8, /* Gap */
162 10, /* Length */
163
164 0x3c, /* ECT0 Count */
165 0x40, 0x46, /* ECT1 Count */
166 0x40, 0x50, /* ECNCE Count */
167 };
168
169 /* 4. RESET_STREAM */
170 static const OSSL_QUIC_FRAME_RESET_STREAM encode_case_4_f = {
171 0x1234, 0x9781, 0x11717
172 };
173
174 static int encode_case_4_enc(WPACKET *pkt)
175 {
176 if (!TEST_int_eq(ossl_quic_wire_encode_frame_reset_stream(pkt,
177 &encode_case_4_f), 1))
178 return 0;
179
180 return 1;
181 }
182
183 static int encode_case_4_dec(PACKET *pkt, ossl_ssize_t fail)
184 {
185 OSSL_QUIC_FRAME_RESET_STREAM f = {0};
186
187 if (!TEST_int_eq(ossl_quic_wire_decode_frame_reset_stream(pkt, &f), fail < 0))
188 return 0;
189
190 if (fail >= 0)
191 return 1;
192
193 if (!TEST_mem_eq(&f, sizeof(f), &encode_case_4_f, sizeof(encode_case_4_f)))
194 return 0;
195
196 return 1;
197 }
198
199 static const unsigned char encode_case_4_expect[] = {
200 0x04, /* Type */
201 0x52, 0x34, /* Stream ID */
202 0x80, 0x00, 0x97, 0x81, /* App Error Code */
203 0x80, 0x01, 0x17, 0x17, /* Final Size */
204 };
205
206 /* 5. STOP_SENDING */
207 static const OSSL_QUIC_FRAME_STOP_SENDING encode_case_5_f = {
208 0x1234, 0x9781
209 };
210
211 static int encode_case_5_enc(WPACKET *pkt)
212 {
213 if (!TEST_int_eq(ossl_quic_wire_encode_frame_stop_sending(pkt,
214 &encode_case_5_f), 1))
215 return 0;
216
217 return 1;
218 }
219
220 static int encode_case_5_dec(PACKET *pkt, ossl_ssize_t fail)
221 {
222 OSSL_QUIC_FRAME_STOP_SENDING f = {0};
223
224 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stop_sending(pkt, &f), fail < 0))
225 return 0;
226
227 if (fail >= 0)
228 return 1;
229
230 if (!TEST_mem_eq(&f, sizeof(f), &encode_case_5_f, sizeof(encode_case_5_f)))
231 return 0;
232
233 return 1;
234 }
235
236 static const unsigned char encode_case_5_expect[] = {
237 0x05, /* Type */
238 0x52, 0x34, /* Stream ID */
239 0x80, 0x00, 0x97, 0x81 /* App Error Code */
240 };
241
242 /* 6. CRYPTO */
243 static const unsigned char encode_case_6_data[] = {
244 93, 18, 17, 102, 33
245 };
246
247 static const OSSL_QUIC_FRAME_CRYPTO encode_case_6_f = {
248 0x1234, sizeof(encode_case_6_data), encode_case_6_data
249 };
250
251 static int encode_case_6_enc(WPACKET *pkt)
252 {
253 if (!TEST_ptr(ossl_quic_wire_encode_frame_crypto(pkt,
254 &encode_case_6_f)))
255 return 0;
256
257 return 1;
258 }
259
260 static int encode_case_6_dec(PACKET *pkt, ossl_ssize_t fail)
261 {
262 OSSL_QUIC_FRAME_CRYPTO f = {0};
263
264 if (!TEST_int_eq(ossl_quic_wire_decode_frame_crypto(pkt, &f), fail < 0))
265 return 0;
266
267 if (fail >= 0)
268 return 1;
269
270 if (!TEST_uint64_t_eq(f.offset, 0x1234))
271 return 0;
272
273 if (!TEST_uint64_t_le(f.len, SIZE_MAX))
274 return 0;
275
276 if (!TEST_mem_eq(f.data, (size_t)f.len,
277 encode_case_6_data, sizeof(encode_case_6_data)))
278 return 0;
279
280 return 1;
281 }
282
283 static const unsigned char encode_case_6_expect[] = {
284 0x06, /* Type */
285 0x52, 0x34, /* Offset */
286 0x05, /* Length */
287 93, 18, 17, 102, 33 /* Data */
288 };
289
290 /* 7. NEW_TOKEN */
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
294 };
295
296 static int encode_case_7_enc(WPACKET *pkt)
297 {
298 if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_token(pkt,
299 encode_case_7_token,
300 sizeof(encode_case_7_token)), 1))
301 return 0;
302
303 return 1;
304 }
305
306 static int encode_case_7_dec(PACKET *pkt, ossl_ssize_t fail)
307 {
308 const unsigned char *token = NULL;
309 size_t token_len = 0;
310
311 if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_token(pkt,
312 &token,
313 &token_len), fail < 0))
314 return 0;
315
316 if (fail >= 0)
317 return 1;
318
319 if (!TEST_mem_eq(token, token_len,
320 encode_case_7_token, sizeof(encode_case_7_token)))
321 return 0;
322
323 return 1;
324 }
325
326 static const unsigned char encode_case_7_expect[] = {
327 0x07, /* Type */
328 0x10, /* Length */
329 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Token */
330 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
331 };
332
333 /* 8. STREAM (no length, no offset, no fin) */
334 static const unsigned char encode_case_8_data[] = {
335 0xde, 0x06, 0xcb, 0x76, 0x5d
336 };
337 static const OSSL_QUIC_FRAME_STREAM encode_case_8_f = {
338 0x1234, 0, 5, encode_case_8_data, 0, 0
339 };
340
341 static int encode_case_8_enc(WPACKET *pkt)
342 {
343 if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
344 &encode_case_8_f)))
345 return 0;
346
347 return 1;
348 }
349
350 static int encode_case_8_dec(PACKET *pkt, ossl_ssize_t fail)
351 {
352 OSSL_QUIC_FRAME_STREAM f = {0};
353
354 if (fail >= 3)
355 /*
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.
358 */
359 return 1;
360
361 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, &f), fail < 0))
362 return 0;
363
364 if (fail >= 0)
365 return 1;
366
367 if (!TEST_uint64_t_le(f.len, SIZE_MAX))
368 return 0;
369
370 if (!TEST_mem_eq(f.data, (size_t)f.len,
371 encode_case_8_data, sizeof(encode_case_8_data)))
372 return 0;
373
374 if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
375 return 0;
376
377 if (!TEST_uint64_t_eq(f.offset, 0))
378 return 0;
379
380 if (!TEST_int_eq(f.has_explicit_len, 0))
381 return 0;
382
383 if (!TEST_int_eq(f.is_fin, 0))
384 return 0;
385
386 return 1;
387 }
388
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 */
393 };
394
395 /* 9. STREAM (length, offset, fin) */
396 static const unsigned char encode_case_9_data[] = {
397 0xde, 0x06, 0xcb, 0x76, 0x5d
398 };
399 static const OSSL_QUIC_FRAME_STREAM encode_case_9_f = {
400 0x1234, 0x39, 5, encode_case_9_data, 1, 1
401 };
402
403 static int encode_case_9_enc(WPACKET *pkt)
404 {
405 if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
406 &encode_case_9_f)))
407 return 0;
408
409 return 1;
410 }
411
412 static int encode_case_9_dec(PACKET *pkt, ossl_ssize_t fail)
413 {
414 OSSL_QUIC_FRAME_STREAM f = {0};
415
416 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, &f), fail < 0))
417 return 0;
418
419 if (fail >= 0)
420 return 1;
421
422 if (!TEST_uint64_t_le(f.len, SIZE_MAX))
423 return 0;
424
425 if (!TEST_mem_eq(f.data, (size_t)f.len,
426 encode_case_9_data, sizeof(encode_case_9_data)))
427 return 0;
428
429 if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
430 return 0;
431
432 if (!TEST_uint64_t_eq(f.offset, 0x39))
433 return 0;
434
435 if (!TEST_int_eq(f.has_explicit_len, 1))
436 return 0;
437
438 if (!TEST_int_eq(f.is_fin, 1))
439 return 0;
440
441 return 1;
442 }
443
444 static const unsigned char encode_case_9_expect[] = {
445 0x0f, /* Type (OFF=1, LEN=1, FIN=1) */
446 0x52, 0x34, /* Stream ID */
447 0x39, /* Offset */
448 0x05, /* Length */
449 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
450 };
451
452 /* 10. MAX_DATA */
453 static int encode_case_10_enc(WPACKET *pkt)
454 {
455 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_data(pkt, 0x1234), 1))
456 return 0;
457
458 return 1;
459 }
460
461 static int encode_case_10_dec(PACKET *pkt, ossl_ssize_t fail)
462 {
463 uint64_t max_data = 0;
464
465 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_data(pkt, &max_data), fail < 0))
466 return 0;
467
468 if (fail >= 0)
469 return 1;
470
471 if (!TEST_uint64_t_eq(max_data, 0x1234))
472 return 0;
473
474 return 1;
475 }
476
477 static const unsigned char encode_case_10_expect[] = {
478 0x10, /* Type */
479 0x52, 0x34, /* Max Data */
480 };
481
482 /* 11. MAX_STREAM_DATA */
483 static int encode_case_11_enc(WPACKET *pkt)
484 {
485 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_stream_data(pkt,
486 0x1234,
487 0x9781), 1))
488 return 0;
489
490 return 1;
491 }
492
493 static int encode_case_11_dec(PACKET *pkt, ossl_ssize_t fail)
494 {
495 uint64_t stream_id = 0, max_data = 0;
496
497 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_stream_data(pkt,
498 &stream_id,
499 &max_data), fail < 0))
500 return 0;
501
502 if (fail >= 0)
503 return 1;
504
505 if (!TEST_uint64_t_eq(stream_id, 0x1234))
506 return 0;
507
508 if (!TEST_uint64_t_eq(max_data, 0x9781))
509 return 0;
510
511 return 1;
512 }
513
514 static const unsigned char encode_case_11_expect[] = {
515 0x11, /* Type */
516 0x52, 0x34, /* Stream ID */
517 0x80, 0x00, 0x97, 0x81, /* Max Data */
518 };
519
520 /* 12. MAX_STREAMS */
521 static int encode_case_12_enc(WPACKET *pkt)
522 {
523 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 0, 0x1234), 1))
524 return 0;
525
526 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 1, 0x9781), 1))
527 return 0;
528
529 return 1;
530 }
531
532 static int encode_case_12_dec(PACKET *pkt, ossl_ssize_t fail)
533 {
534 uint64_t max_streams_1 = 0, max_streams_2 = 0,
535 frame_type_1 = 0, frame_type_2 = 0;
536
537 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1),
538 fail < 0 || fail >= 1))
539 return 0;
540
541 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
542 &max_streams_1),
543 fail < 0 || fail >= 3))
544 return 0;
545
546 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2),
547 fail < 0 || fail >= 4))
548 return 0;
549
550 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
551 &max_streams_2),
552 fail < 0))
553 return 0;
554
555 if ((fail < 0 || fail >= 3)
556 && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI))
557 return 0;
558
559 if ((fail < 0 || fail >= 3)
560 && !TEST_uint64_t_eq(max_streams_1, 0x1234))
561 return 0;
562
563 if ((fail < 0 || fail >= 8)
564 && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI))
565 return 0;
566
567 if ((fail < 0 || fail >= 8)
568 && !TEST_uint64_t_eq(max_streams_2, 0x9781))
569 return 0;
570
571 return 1;
572 }
573
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 */
579 };
580
581 /* 13. DATA_BLOCKED */
582 static int encode_case_13_enc(WPACKET *pkt)
583 {
584 if (!TEST_int_eq(ossl_quic_wire_encode_frame_data_blocked(pkt, 0x1234), 1))
585 return 0;
586
587 return 1;
588 }
589
590 static int encode_case_13_dec(PACKET *pkt, ossl_ssize_t fail)
591 {
592 uint64_t max_data = 0;
593
594 if (!TEST_int_eq(ossl_quic_wire_decode_frame_data_blocked(pkt,
595 &max_data), fail < 0))
596 return 0;
597
598 if (fail >= 0)
599 return 1;
600
601 if (!TEST_uint64_t_eq(max_data, 0x1234))
602 return 0;
603
604 return 1;
605 }
606
607 static const unsigned char encode_case_13_expect[] = {
608 0x14, /* Type */
609 0x52, 0x34, /* Max Data */
610 };
611
612 /* 14. STREAM_DATA_BLOCKED */
613 static int encode_case_14_enc(WPACKET *pkt)
614 {
615 if (!TEST_int_eq(ossl_quic_wire_encode_frame_stream_data_blocked(pkt,
616 0x1234,
617 0x9781), 1))
618 return 0;
619
620 return 1;
621 }
622
623 static int encode_case_14_dec(PACKET *pkt, ossl_ssize_t fail)
624 {
625 uint64_t stream_id = 0, max_data = 0;
626
627 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream_data_blocked(pkt,
628 &stream_id,
629 &max_data), fail < 0))
630 return 0;
631
632 if (fail >= 0)
633 return 1;
634
635 if (!TEST_uint64_t_eq(stream_id, 0x1234))
636 return 0;
637
638 if (!TEST_uint64_t_eq(max_data, 0x9781))
639 return 0;
640
641 return 1;
642 }
643
644 static const unsigned char encode_case_14_expect[] = {
645 0x15, /* Type */
646 0x52, 0x34, /* Stream ID */
647 0x80, 0x00, 0x97, 0x81, /* Max Data */
648 };
649
650 /* 15. STREAMS_BLOCKED */
651 static int encode_case_15_enc(WPACKET *pkt)
652 {
653 if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 0, 0x1234), 1))
654 return 0;
655
656 if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 1, 0x9781), 1))
657 return 0;
658
659 return 1;
660 }
661
662 static int encode_case_15_dec(PACKET *pkt, ossl_ssize_t fail)
663 {
664 uint64_t max_streams_1 = 0, max_streams_2 = 0,
665 frame_type_1 = 0, frame_type_2 = 0;
666
667 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1),
668 fail < 0 || fail >= 1))
669 return 0;
670
671 if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
672 &max_streams_1),
673 fail < 0 || fail >= 3))
674 return 0;
675
676 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2),
677 fail < 0 || fail >= 4))
678 return 0;
679
680 if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
681 &max_streams_2),
682 fail < 0 || fail >= 8))
683 return 0;
684
685 if ((fail < 0 || fail >= 1)
686 && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI))
687 return 0;
688
689 if ((fail < 0 || fail >= 3)
690 && !TEST_uint64_t_eq(max_streams_1, 0x1234))
691 return 0;
692
693 if ((fail < 0 || fail >= 4)
694 && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI))
695 return 0;
696
697 if ((fail < 0 || fail >= 8)
698 && !TEST_uint64_t_eq(max_streams_2, 0x9781))
699 return 0;
700
701 return 1;
702 }
703
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 */
709 };
710
711 /* 16. NEW_CONNECTION_ID */
712 static const unsigned char encode_case_16_conn_id[] = {
713 0x33, 0x44, 0x55, 0x66
714 };
715
716 static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16_f = {
717 0x9781,
718 0x1234,
719 {
720 0x4,
721 {0x33, 0x44, 0x55, 0x66}
722 },
723 {
724 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
725 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
726 }
727 };
728
729 static int encode_case_16_enc(WPACKET *pkt)
730 {
731 if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
732 &encode_case_16_f), 1))
733 return 0;
734
735 return 1;
736 }
737
738 static int encode_case_16_dec(PACKET *pkt, ossl_ssize_t fail)
739 {
740 OSSL_QUIC_FRAME_NEW_CONN_ID f = {0};
741
742 if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), fail < 0))
743 return 0;
744
745 if (fail >= 0)
746 return 1;
747
748 if (!TEST_uint64_t_eq(f.seq_num, 0x9781))
749 return 0;
750
751 if (!TEST_uint64_t_eq(f.retire_prior_to, 0x1234))
752 return 0;
753
754 if (!TEST_uint64_t_eq(f.conn_id.id_len, sizeof(encode_case_16_conn_id)))
755 return 0;
756
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)))
759 return 0;
760
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)))
765 return 0;
766
767 return 1;
768 }
769
770 static const unsigned char encode_case_16_expect[] = {
771 0x18, /* Type */
772 0x80, 0x00, 0x97, 0x81, /* Sequence Number */
773 0x52, 0x34, /* 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
778 };
779
780 /* 16b. NEW_CONNECTION_ID seq_num < retire_prior_to */
781 static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16b_f = {
782 0x1234,
783 0x9781,
784 {
785 0x4,
786 {0x33, 0x44, 0x55, 0x66}
787 },
788 {
789 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
790 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
791 }
792 };
793
794 static int encode_case_16b_enc(WPACKET *pkt)
795 {
796 if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
797 &encode_case_16b_f), 1))
798 return 0;
799
800 return 1;
801 }
802
803 static int encode_case_16b_dec(PACKET *pkt, ossl_ssize_t fail)
804 {
805 OSSL_QUIC_FRAME_NEW_CONN_ID f = {0};
806
807 if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), 0))
808 return 0;
809
810 if (!TEST_true(PACKET_forward(pkt, PACKET_remaining(pkt))))
811 return 0;
812
813 return 1;
814 }
815
816 static const unsigned char encode_case_16b_expect[] = {
817 0x18, /* Type */
818 0x52, 0x34, /* Sequence Number */
819 0x80, 0x00, 0x97, 0x81, /* Retire Prior To */
820 0x04, /* Connection ID Length */
821 0x33, 0x44, 0x55, 0x66, /* Connection ID */
822 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
823 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
824 };
825
826 /* 17. RETIRE_CONNECTION_ID */
827 static int encode_case_17_enc(WPACKET *pkt)
828 {
829 if (!TEST_int_eq(ossl_quic_wire_encode_frame_retire_conn_id(pkt, 0x1234), 1))
830 return 0;
831
832 return 1;
833 }
834
835 static int encode_case_17_dec(PACKET *pkt, ossl_ssize_t fail)
836 {
837 uint64_t seq_num = 0;
838
839 if (!TEST_int_eq(ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num), fail < 0))
840 return 0;
841
842 if (fail >= 0)
843 return 1;
844
845 if (!TEST_uint64_t_eq(seq_num, 0x1234))
846 return 0;
847
848 return 1;
849 }
850
851 static const unsigned char encode_case_17_expect[] = {
852 0x19, /* Type */
853 0x52, 0x34, /* Seq Num */
854 };
855
856 /* 18. PATH_CHALLENGE */
857 static const uint64_t encode_case_18_data
858 = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
859
860 static int encode_case_18_enc(WPACKET *pkt)
861 {
862 if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_challenge(pkt,
863 encode_case_18_data), 1))
864 return 0;
865
866 return 1;
867 }
868
869 static int encode_case_18_dec(PACKET *pkt, ossl_ssize_t fail)
870 {
871 uint64_t challenge = 0;
872
873 if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_challenge(pkt, &challenge), fail < 0))
874 return 0;
875
876 if (fail >= 0)
877 return 1;
878
879 if (!TEST_uint64_t_eq(challenge, encode_case_18_data))
880 return 0;
881
882 return 1;
883 }
884
885 static const unsigned char encode_case_18_expect[] = {
886 0x1A, /* Type */
887 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
888 };
889
890 /* 19. PATH_RESPONSE */
891 static const uint64_t encode_case_19_data
892 = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
893
894 static int encode_case_19_enc(WPACKET *pkt)
895 {
896 if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_response(pkt,
897 encode_case_19_data), 1))
898 return 0;
899
900 return 1;
901 }
902
903 static int encode_case_19_dec(PACKET *pkt, ossl_ssize_t fail)
904 {
905 uint64_t challenge = 0;
906
907 if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_response(pkt, &challenge), fail < 0))
908 return 0;
909
910 if (fail >= 0)
911 return 1;
912
913 if (!TEST_uint64_t_eq(challenge, encode_case_19_data))
914 return 0;
915
916 return 1;
917 }
918
919 static const unsigned char encode_case_19_expect[] = {
920 0x1B, /* Type */
921 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
922 };
923
924 /* 20. CONNECTION_CLOSE (transport) */
925 static const char encode_case_20_reason[] = {
926 /* "reason for closure" */
927 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f,
928 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
929 };
930
931 static const OSSL_QUIC_FRAME_CONN_CLOSE encode_case_20_f = {
932 0,
933 0x1234,
934 0x9781,
935 (char *)encode_case_20_reason,
936 sizeof(encode_case_20_reason)
937 };
938
939 static int encode_case_20_enc(WPACKET *pkt)
940 {
941 if (!TEST_int_eq(ossl_quic_wire_encode_frame_conn_close(pkt,
942 &encode_case_20_f), 1))
943 return 0;
944
945 return 1;
946 }
947
948 static int encode_case_20_dec(PACKET *pkt, ossl_ssize_t fail)
949 {
950 OSSL_QUIC_FRAME_CONN_CLOSE f = {0};
951
952 if (!TEST_int_eq(ossl_quic_wire_decode_frame_conn_close(pkt, &f), fail < 0))
953 return 0;
954
955 if (fail >= 0)
956 return 1;
957
958 if (!TEST_int_eq(f.is_app, 0))
959 return 0;
960
961 if (!TEST_uint64_t_eq(f.error_code, 0x1234))
962 return 0;
963
964 if (!TEST_uint64_t_eq(f.frame_type, 0x9781))
965 return 0;
966
967 if (!TEST_size_t_eq(f.reason_len, 18))
968 return 0;
969
970 if (!TEST_mem_eq(f.reason, f.reason_len,
971 encode_case_20_f.reason, encode_case_20_f.reason_len))
972 return 0;
973
974 return 1;
975 }
976
977 static const unsigned char encode_case_20_expect[] = {
978 0x1C, /* Type */
979 0x52, 0x34, /* Sequence Number */
980 0x80, 0x00, 0x97, 0x81, /* Frame Type */
981 0x12, /* Reason Length */
982 0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f, /* Reason */
983 0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
984 };
985
986 /* 21. HANDSHAKE_DONE */
987 static int encode_case_21_enc(WPACKET *pkt)
988 {
989
990 if (!TEST_int_eq(ossl_quic_wire_encode_frame_handshake_done(pkt), 1))
991 return 0;
992
993 return 1;
994 }
995
996 static int encode_case_21_dec(PACKET *pkt, ossl_ssize_t fail)
997 {
998
999 if (!TEST_int_eq(ossl_quic_wire_decode_frame_handshake_done(pkt), fail < 0))
1000 return 0;
1001
1002 return 1;
1003 }
1004
1005 static const unsigned char encode_case_21_expect[] = {
1006 0x1E
1007 };
1008
1009 /* 22. Buffer Transport Parameter */
1010 static const unsigned char encode_case_22_data[] = {0x55,0x77,0x32,0x46,0x99};
1011
1012 static int encode_case_22_enc(WPACKET *pkt)
1013 {
1014 unsigned char *p;
1015
1016 if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(pkt, 0x1234,
1017 encode_case_22_data,
1018 sizeof(encode_case_22_data))))
1019 return 0;
1020
1021 if (!TEST_ptr(p = ossl_quic_wire_encode_transport_param_bytes(pkt, 0x9781,
1022 NULL, 2)))
1023 return 0;
1024
1025 p[0] = 0x33;
1026 p[1] = 0x44;
1027
1028 return 1;
1029 }
1030
1031 static int encode_case_22_dec(PACKET *pkt, ossl_ssize_t fail)
1032 {
1033 uint64_t id = 0;
1034 size_t len = 0;
1035 const unsigned char *p;
1036 static const unsigned char data[] = {0x33, 0x44};
1037
1038 if (!TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
1039 fail < 0 || fail >= 2))
1040 return 0;
1041
1042 if ((fail < 0 || fail >= 2)
1043 && !TEST_uint64_t_eq(id, 0x1234))
1044 return 0;
1045
1046 id = 0;
1047
1048 p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
1049 if (fail < 0 || fail >= 8) {
1050 if (!TEST_ptr(p))
1051 return 0;
1052 } else {
1053 if (!TEST_ptr_null(p))
1054 return 0;
1055 }
1056
1057 if ((fail < 0 || fail >= 8)
1058 && !TEST_uint64_t_eq(id, 0x1234))
1059 return 0;
1060
1061 if ((fail < 0 || fail >= 8)
1062 && !TEST_mem_eq(p, len, encode_case_22_data, sizeof(encode_case_22_data)))
1063 return 0;
1064
1065 if ((fail < 0 || fail >= 8)
1066 && !TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
1067 fail < 0 || fail >= 12))
1068 return 0;
1069
1070 if ((fail < 0 || fail >= 12)
1071 && !TEST_uint64_t_eq(id, 0x9781))
1072 return 0;
1073
1074 id = 0;
1075
1076 p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
1077 if (fail < 0 || fail >= 15) {
1078 if (!TEST_ptr(p))
1079 return 0;
1080 } else {
1081 if (!TEST_ptr_null(p))
1082 return 0;
1083 }
1084
1085 if ((fail < 0 || fail >= 15)
1086 && !TEST_uint64_t_eq(id, 0x9781))
1087 return 0;
1088
1089 if ((fail < 0 || fail >= 15)
1090 && !TEST_mem_eq(p, len, data, sizeof(data)))
1091 return 0;
1092
1093 return 1;
1094 }
1095
1096 static const unsigned char encode_case_22_expect[] = {
1097 0x52, 0x34, /* ID */
1098 0x05, /* Length */
1099 0x55, 0x77, 0x32, 0x46, 0x99, /* Data */
1100
1101 0x80, 0x00, 0x97, 0x81, /* ID */
1102 0x02, /* Length */
1103 0x33, 0x44 /* Data */
1104 };
1105
1106 /* 23. Integer Transport Parameter */
1107 static int encode_case_23_enc(WPACKET *pkt)
1108 {
1109 if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x1234, 0x9781), 1))
1110 return 0;
1111
1112 if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x2233, 0x4545), 1))
1113 return 0;
1114
1115 return 1;
1116 }
1117
1118 static int encode_case_23_dec(PACKET *pkt, ossl_ssize_t fail)
1119 {
1120 uint64_t id = 0, value = 0;
1121
1122 if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
1123 &id, &value),
1124 fail < 0 || fail >= 7))
1125 return 0;
1126
1127 if ((fail < 0 || fail >= 7)
1128 && !TEST_uint64_t_eq(id, 0x1234))
1129 return 0;
1130
1131 if ((fail < 0 || fail >= 7)
1132 && !TEST_uint64_t_eq(value, 0x9781))
1133 return 0;
1134
1135 if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
1136 &id, &value),
1137 fail < 0 || fail >= 14))
1138 return 0;
1139
1140 if ((fail < 0 || fail >= 14)
1141 && !TEST_uint64_t_eq(id, 0x2233))
1142 return 0;
1143
1144 if ((fail < 0 || fail >= 14)
1145 && !TEST_uint64_t_eq(value, 0x4545))
1146 return 0;
1147
1148 return 1;
1149 }
1150
1151 static const unsigned char encode_case_23_expect[] = {
1152 0x52, 0x34,
1153 0x04,
1154 0x80, 0x00, 0x97, 0x81,
1155
1156 0x62, 0x33,
1157 0x04,
1158 0x80, 0x00, 0x45, 0x45,
1159 };
1160
1161 #define ENCODE_CASE(n) \
1162 { \
1163 encode_case_##n##_enc, \
1164 encode_case_##n##_expect, \
1165 OSSL_NELEM(encode_case_##n##_expect), \
1166 encode_case_##n##_dec \
1167 },
1168
1169 static const struct encode_test_case encode_cases[] = {
1170 ENCODE_CASE(1)
1171 ENCODE_CASE(2)
1172 ENCODE_CASE(3)
1173 ENCODE_CASE(4)
1174 ENCODE_CASE(5)
1175 ENCODE_CASE(6)
1176 ENCODE_CASE(7)
1177 ENCODE_CASE(8)
1178 ENCODE_CASE(9)
1179 ENCODE_CASE(10)
1180 ENCODE_CASE(11)
1181 ENCODE_CASE(12)
1182 ENCODE_CASE(13)
1183 ENCODE_CASE(14)
1184 ENCODE_CASE(15)
1185 ENCODE_CASE(16)
1186 ENCODE_CASE(16b)
1187 ENCODE_CASE(17)
1188 ENCODE_CASE(18)
1189 ENCODE_CASE(19)
1190 ENCODE_CASE(20)
1191 ENCODE_CASE(21)
1192 ENCODE_CASE(22)
1193 ENCODE_CASE(23)
1194 };
1195
1196 static int test_wire_encode(int idx)
1197 {
1198 int testresult = 0;
1199 WPACKET wpkt;
1200 PACKET pkt;
1201 BUF_MEM *buf = NULL;
1202 size_t written;
1203 const struct encode_test_case *c = &encode_cases[idx];
1204 int have_wpkt = 0;
1205 size_t i;
1206
1207 if (!TEST_ptr(buf = BUF_MEM_new()))
1208 goto err;
1209
1210 if (!TEST_int_eq(WPACKET_init(&wpkt, buf), 1))
1211 goto err;
1212
1213 have_wpkt = 1;
1214 if (!TEST_int_eq(c->serializer(&wpkt), 1))
1215 goto err;
1216
1217 if (!TEST_int_eq(WPACKET_get_total_written(&wpkt, &written), 1))
1218 goto err;
1219
1220 if (!TEST_mem_eq(buf->data, written, c->expect_buf, c->expect_buf_len))
1221 goto err;
1222
1223 if (!TEST_int_eq(PACKET_buf_init(&pkt, (unsigned char *)buf->data, written), 1))
1224 goto err;
1225
1226 if (!TEST_int_eq(c->deserializer(&pkt, -1), 1))
1227 goto err;
1228
1229 if (!TEST_false(PACKET_remaining(&pkt)))
1230 goto err;
1231
1232 for (i = 0; i < c->expect_buf_len; ++i) {
1233 PACKET pkt2;
1234
1235 /*
1236 * Check parsing truncated (i.e., malformed) input is handled correctly.
1237 * Generate all possible truncations of our reference encoding and
1238 * verify that they are handled correctly. The number of bytes of the
1239 * truncated encoding is passed as an argument to the deserializer to
1240 * help it determine whether decoding should fail or not.
1241 */
1242 if (!TEST_int_eq(PACKET_buf_init(&pkt2, (unsigned char *)c->expect_buf, i), 1))
1243 goto err;
1244
1245 if (!TEST_int_eq(c->deserializer(&pkt2, i), 1))
1246 goto err;
1247 }
1248
1249 testresult = 1;
1250 err:
1251 if (have_wpkt)
1252 WPACKET_finish(&wpkt);
1253 BUF_MEM_free(buf);
1254 return testresult;
1255 }
1256
1257 struct ack_test_case {
1258 const unsigned char *input_buf;
1259 size_t input_buf_len;
1260 int (*deserializer)(PACKET *pkt);
1261 int expect_fail;
1262 };
1263
1264 /* ACK Frame with Excessive First ACK Range Field */
1265 static const unsigned char ack_case_1_input[] = {
1266 0x02, /* ACK Without ECN */
1267 0x08, /* Largest Acknowledged */
1268 0x01, /* ACK Delay */
1269 0x00, /* ACK Range Count */
1270 0x09, /* First ACK Range */
1271 };
1272
1273 /* ACK Frame with Valid ACK Range Field */
1274 static const unsigned char ack_case_2_input[] = {
1275 0x02, /* ACK Without ECN */
1276 0x08, /* Largest Acknowledged */
1277 0x01, /* ACK Delay */
1278 0x00, /* ACK Range Count */
1279 0x08, /* First ACK Range */
1280 };
1281
1282 /* ACK Frame with Excessive ACK Range Gap */
1283 static const unsigned char ack_case_3_input[] = {
1284 0x02, /* ACK Without ECN */
1285 0x08, /* Largest Acknowledged */
1286 0x01, /* ACK Delay */
1287 0x01, /* ACK Range Count */
1288 0x01, /* First ACK Range */
1289
1290 0x05, /* Gap */
1291 0x01, /* ACK Range Length */
1292 };
1293
1294 /* ACK Frame with Valid ACK Range */
1295 static const unsigned char ack_case_4_input[] = {
1296 0x02, /* ACK Without ECN */
1297 0x08, /* Largest Acknowledged */
1298 0x01, /* ACK Delay */
1299 0x01, /* ACK Range Count */
1300 0x01, /* First ACK Range */
1301
1302 0x04, /* Gap */
1303 0x01, /* ACK Range Length */
1304 };
1305
1306 /* ACK Frame with Excessive ACK Range Length */
1307 static const unsigned char ack_case_5_input[] = {
1308 0x02, /* ACK Without ECN */
1309 0x08, /* Largest Acknowledged */
1310 0x01, /* ACK Delay */
1311 0x01, /* ACK Range Count */
1312 0x01, /* First ACK Range */
1313
1314 0x04, /* Gap */
1315 0x02, /* ACK Range Length */
1316 };
1317
1318 /* ACK Frame with Multiple ACK Ranges, Final Having Excessive Length */
1319 static const unsigned char ack_case_6_input[] = {
1320 0x02, /* ACK Without ECN */
1321 0x08, /* Largest Acknowledged */
1322 0x01, /* ACK Delay */
1323 0x02, /* ACK Range Count */
1324 0x01, /* First ACK Range */
1325
1326 0x01, /* Gap */
1327 0x02, /* ACK Range Length */
1328
1329 0x00, /* Gap */
1330 0x01, /* ACK Range Length */
1331 };
1332
1333 /* ACK Frame with Multiple ACK Ranges, Valid */
1334 static const unsigned char ack_case_7_input[] = {
1335 0x02, /* ACK Without ECN */
1336 0x08, /* Largest Acknowledged */
1337 0x01, /* ACK Delay */
1338 0x02, /* ACK Range Count */
1339 0x01, /* First ACK Range */
1340
1341 0x01, /* Gap */
1342 0x02, /* ACK Range Length */
1343
1344 0x00, /* Gap */
1345 0x00, /* ACK Range Length */
1346 };
1347
1348 static int ack_generic_decode(PACKET *pkt)
1349 {
1350 OSSL_QUIC_ACK_RANGE ranges[8] = {0};
1351 OSSL_QUIC_FRAME_ACK f = {0};
1352 uint64_t total_ranges = 0, peek_total_ranges = 0;
1353 int r;
1354 size_t i;
1355
1356 f.ack_ranges = ranges;
1357 f.num_ack_ranges = OSSL_NELEM(ranges);
1358
1359 if (!TEST_int_eq(ossl_quic_wire_peek_frame_ack_num_ranges(pkt,
1360 &peek_total_ranges), 1))
1361 return 0;
1362
1363 r = ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges);
1364 if (r == 0)
1365 return 0;
1366
1367 if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
1368 return 0;
1369
1370 for (i = 0; i < f.num_ack_ranges; ++i) {
1371 if (!TEST_uint64_t_le(f.ack_ranges[i].start, f.ack_ranges[i].end))
1372 return 0;
1373 if (!TEST_uint64_t_lt(f.ack_ranges[i].end, 1000))
1374 return 0;
1375 }
1376
1377 return 1;
1378 }
1379
1380 #define ACK_CASE(n, expect_fail, dec) \
1381 { \
1382 ack_case_##n##_input, \
1383 sizeof(ack_case_##n##_input), \
1384 (dec), \
1385 (expect_fail) \
1386 },
1387
1388 static const struct ack_test_case ack_cases[] = {
1389 ACK_CASE(1, 1, ack_generic_decode)
1390 ACK_CASE(2, 0, ack_generic_decode)
1391 ACK_CASE(3, 1, ack_generic_decode)
1392 ACK_CASE(4, 0, ack_generic_decode)
1393 ACK_CASE(5, 1, ack_generic_decode)
1394 ACK_CASE(6, 1, ack_generic_decode)
1395 ACK_CASE(7, 0, ack_generic_decode)
1396 };
1397
1398 static int test_wire_ack(int idx)
1399 {
1400 int testresult = 0, r;
1401 PACKET pkt;
1402 const struct ack_test_case *c = &ack_cases[idx];
1403
1404 if (!TEST_int_eq(PACKET_buf_init(&pkt,
1405 (unsigned char *)c->input_buf,
1406 c->input_buf_len), 1))
1407 goto err;
1408
1409 r = c->deserializer(&pkt);
1410 if (c->expect_fail) {
1411 if (!TEST_int_eq(r, 0))
1412 goto err;
1413 } else {
1414 if (!TEST_int_eq(r, 1))
1415 goto err;
1416
1417 if (!TEST_false(PACKET_remaining(&pkt)))
1418 goto err;
1419 }
1420
1421 testresult = 1;
1422 err:
1423 return testresult;
1424 }
1425
1426 /* Packet Header PN Encoding Tests */
1427 struct pn_test {
1428 QUIC_PN pn, tx_largest_acked, rx_largest_pn;
1429 char expected_len;
1430 unsigned char expected_bytes[4];
1431 };
1432
1433 static const struct pn_test pn_tests[] = {
1434 /* RFC 9000 Section A.2 */
1435 { 0xac5c02, 0xabe8b3, 0xabe8b3, 2, {0x5c,0x02} },
1436 { 0xace8fe, 0xabe8b3, 0xabe8b3, 3, {0xac,0xe8,0xfe} },
1437 /* RFC 9000 Section A.3 */
1438 { 0xa82f9b32, 0xa82f30ea, 0xa82f30ea, 2, {0x9b,0x32} },
1439 /* Boundary Cases */
1440 { 1, 0, 0, 1, {0x01} },
1441 { 256, 255, 255, 1, {0x00} },
1442 { 257, 255, 255, 1, {0x01} },
1443 { 256, 128, 128, 1, {0x00} },
1444 { 256, 127, 127, 2, {0x01,0x00} },
1445 { 65536, 32768, 32768, 2, {0x00,0x00} },
1446 { 65537, 32769, 32769, 2, {0x00,0x01} },
1447 { 65536, 32767, 32767, 3, {0x01,0x00,0x00} },
1448 { 65537, 32768, 32768, 3, {0x01,0x00,0x01} },
1449 { 16777216, 8388608, 8388608, 3, {0x00,0x00,0x00} },
1450 { 16777217, 8388609, 8388609, 3, {0x00,0x00,0x01} },
1451 { 16777216, 8388607, 8388607, 4, {0x01,0x00,0x00,0x00} },
1452 { 16777217, 8388608, 8388608, 4, {0x01,0x00,0x00,0x01} },
1453 { 4294967296, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x00} },
1454 { 4294967297, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x01} },
1455 };
1456
1457 static int test_wire_pkt_hdr_pn(int tidx)
1458 {
1459 int testresult = 0;
1460 const struct pn_test *t = &pn_tests[tidx];
1461 unsigned char buf[4];
1462 int pn_len;
1463 QUIC_PN res_pn;
1464
1465 pn_len = ossl_quic_wire_determine_pn_len(t->pn, t->tx_largest_acked);
1466 if (!TEST_int_eq(pn_len, (int)t->expected_len))
1467 goto err;
1468
1469 if (!TEST_true(ossl_quic_wire_encode_pkt_hdr_pn(t->pn, buf, pn_len)))
1470 goto err;
1471
1472 if (!TEST_mem_eq(t->expected_bytes, t->expected_len, buf, pn_len))
1473 goto err;
1474
1475 if (!TEST_true(ossl_quic_wire_decode_pkt_hdr_pn(buf, pn_len,
1476 t->rx_largest_pn, &res_pn)))
1477 goto err;
1478
1479 if (!TEST_uint64_t_eq(res_pn, t->pn))
1480 goto err;
1481
1482 testresult = 1;
1483 err:
1484 return testresult;
1485 }
1486
1487 /* RFC 9001 s. A.4 */
1488 static const QUIC_CONN_ID retry_orig_dcid = {
1489 8, { 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08 }
1490 };
1491
1492 static const unsigned char retry_encoded[] = {
1493 0xff, /* Long Header, Retry */
1494 0x00, 0x00, 0x00, 0x01, /* Version 1 */
1495 0x00, /* DCID */
1496 0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID */
1497
1498 /* Retry Token */
1499 0x74, 0x6f, 0x6b, 0x65, 0x6e,
1500
1501 /* Retry Integrity Tag */
1502 0x04, 0xa2, 0x65, 0xba, 0x2e, 0xff, 0x4d, 0x82, 0x90, 0x58, 0xfb, 0x3f, 0x0f,
1503 0x24, 0x96, 0xba
1504 };
1505
1506 static int test_wire_retry_integrity_tag(void)
1507 {
1508 int testresult = 0;
1509 PACKET pkt = {0};
1510 QUIC_PKT_HDR hdr = {0};
1511 unsigned char got_tag[QUIC_RETRY_INTEGRITY_TAG_LEN] = {0};
1512
1513 if (!TEST_true(PACKET_buf_init(&pkt, retry_encoded, sizeof(retry_encoded))))
1514 goto err;
1515
1516 if (!TEST_true(ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, &hdr, NULL)))
1517 goto err;
1518
1519 if (!TEST_int_eq(hdr.type, QUIC_PKT_TYPE_RETRY))
1520 goto err;
1521
1522 if (!TEST_true(ossl_quic_calculate_retry_integrity_tag(NULL, NULL, &hdr,
1523 &retry_orig_dcid,
1524 got_tag)))
1525 goto err;
1526
1527 if (!TEST_mem_eq(got_tag, sizeof(got_tag),
1528 retry_encoded + sizeof(retry_encoded)
1529 - QUIC_RETRY_INTEGRITY_TAG_LEN,
1530 QUIC_RETRY_INTEGRITY_TAG_LEN))
1531 goto err;
1532
1533 if (!TEST_true(ossl_quic_validate_retry_integrity_tag(NULL, NULL, &hdr,
1534 &retry_orig_dcid)))
1535 goto err;
1536
1537 testresult = 1;
1538 err:
1539 return testresult;
1540 }
1541
1542 int setup_tests(void)
1543 {
1544 ADD_ALL_TESTS(test_wire_encode, OSSL_NELEM(encode_cases));
1545 ADD_ALL_TESTS(test_wire_ack, OSSL_NELEM(ack_cases));
1546 ADD_ALL_TESTS(test_wire_pkt_hdr_pn, OSSL_NELEM(pn_tests));
1547 ADD_TEST(test_wire_retry_integrity_tag);
1548 return 1;
1549 }