f9c6688d99be97ccacef74d6bcb0a69092f25906
[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, 0, &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, 0, &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, 0, &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     int is_minimal = 1, success_if;
537
538     success_if = (fail < 0 || fail >= 1);
539     if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1,
540                                                       &is_minimal),
541                      success_if))
542         return 0;
543
544     if (!TEST_true(!success_if || is_minimal))
545         return 0;
546
547     success_if = (fail < 0 || fail >= 3);
548     if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
549                                                              &max_streams_1),
550                      success_if))
551         return 0;
552
553     success_if = (fail < 0 || fail >= 4);
554     if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2,
555                                                       &is_minimal),
556                      success_if))
557         return 0;
558
559     if (!TEST_true(!success_if || is_minimal))
560         return 0;
561
562     success_if = (fail < 0);
563     if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
564                                                              &max_streams_2),
565                      success_if))
566         return 0;
567
568     if ((fail < 0 || fail >= 3)
569         && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI))
570         return 0;
571
572     if ((fail < 0 || fail >= 3)
573         && !TEST_uint64_t_eq(max_streams_1, 0x1234))
574         return 0;
575
576     if ((fail < 0 || fail >= 8)
577         && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI))
578         return 0;
579
580     if ((fail < 0 || fail >= 8)
581         && !TEST_uint64_t_eq(max_streams_2, 0x9781))
582         return 0;
583
584     return 1;
585 }
586
587 static const unsigned char encode_case_12_expect[] = {
588     0x12,                           /* Type (MAX_STREAMS Bidirectional) */
589     0x52, 0x34,                     /* Max Streams */
590     0x13,                           /* Type (MAX_STREAMS Unidirectional) */
591     0x80, 0x00, 0x97, 0x81,         /* Max Streams */
592 };
593
594 /* 13. DATA_BLOCKED */
595 static int encode_case_13_enc(WPACKET *pkt)
596 {
597     if (!TEST_int_eq(ossl_quic_wire_encode_frame_data_blocked(pkt, 0x1234), 1))
598         return 0;
599
600     return 1;
601 }
602
603 static int encode_case_13_dec(PACKET *pkt, ossl_ssize_t fail)
604 {
605     uint64_t max_data = 0;
606
607     if (!TEST_int_eq(ossl_quic_wire_decode_frame_data_blocked(pkt,
608                                                               &max_data), fail < 0))
609         return 0;
610
611     if (fail >= 0)
612         return 1;
613
614     if (!TEST_uint64_t_eq(max_data, 0x1234))
615         return 0;
616
617     return 1;
618 }
619
620 static const unsigned char encode_case_13_expect[] = {
621     0x14,                           /* Type */
622     0x52, 0x34,                     /* Max Data */
623 };
624
625 /* 14. STREAM_DATA_BLOCKED */
626 static int encode_case_14_enc(WPACKET *pkt)
627 {
628     if (!TEST_int_eq(ossl_quic_wire_encode_frame_stream_data_blocked(pkt,
629                                                                      0x1234,
630                                                                      0x9781), 1))
631         return 0;
632
633     return 1;
634 }
635
636 static int encode_case_14_dec(PACKET *pkt, ossl_ssize_t fail)
637 {
638     uint64_t stream_id = 0, max_data = 0;
639
640     if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream_data_blocked(pkt,
641                                                                      &stream_id,
642                                                                      &max_data), fail < 0))
643         return 0;
644
645     if (fail >= 0)
646         return 1;
647
648     if (!TEST_uint64_t_eq(stream_id, 0x1234))
649         return 0;
650
651     if (!TEST_uint64_t_eq(max_data, 0x9781))
652         return 0;
653
654     return 1;
655 }
656
657 static const unsigned char encode_case_14_expect[] = {
658     0x15,                           /* Type */
659     0x52, 0x34,                     /* Stream ID */
660     0x80, 0x00, 0x97, 0x81,         /* Max Data */
661 };
662
663 /* 15. STREAMS_BLOCKED */
664 static int encode_case_15_enc(WPACKET *pkt)
665 {
666     if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 0, 0x1234), 1))
667         return 0;
668
669     if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 1, 0x9781), 1))
670         return 0;
671
672     return 1;
673 }
674
675 static int encode_case_15_dec(PACKET *pkt, ossl_ssize_t fail)
676 {
677     uint64_t max_streams_1 = 0, max_streams_2 = 0,
678             frame_type_1 = 0, frame_type_2 = 0;
679     int is_minimal = 1, success_if;
680
681     success_if = (fail < 0 || fail >= 1);
682     if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1,
683                                                       &is_minimal),
684                      success_if))
685         return 0;
686
687     if (!TEST_true(!success_if || is_minimal))
688         return 0;
689
690     success_if = (fail < 0 || fail >= 3);
691     if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
692                                                                  &max_streams_1),
693                      success_if))
694         return 0;
695
696     success_if = (fail < 0 || fail >= 4);
697     if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2,
698                                                       &is_minimal),
699                      success_if))
700         return 0;
701
702     if (!TEST_true(!success_if || is_minimal))
703         return 0;
704
705     if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
706                                                                  &max_streams_2),
707                      fail < 0 || fail >= 8))
708         return 0;
709
710     if ((fail < 0 || fail >= 1)
711         && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI))
712         return 0;
713
714     if ((fail < 0 || fail >= 3)
715         && !TEST_uint64_t_eq(max_streams_1, 0x1234))
716         return 0;
717
718     if ((fail < 0 || fail >= 4)
719         && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI))
720         return 0;
721
722     if ((fail < 0 || fail >= 8)
723         && !TEST_uint64_t_eq(max_streams_2, 0x9781))
724         return 0;
725
726     return 1;
727 }
728
729 static const unsigned char encode_case_15_expect[] = {
730     0x16,                           /* Type (STREAMS_BLOCKED Bidirectional) */
731     0x52, 0x34,                     /* Max Streams */
732     0x17,                           /* Type (STREAMS_BLOCKED Unidirectional) */
733     0x80, 0x00, 0x97, 0x81,         /* Max Streams */
734 };
735
736 /* 16. NEW_CONNECTION_ID */
737 static const unsigned char encode_case_16_conn_id[] = {
738     0x33, 0x44, 0x55, 0x66
739 };
740
741 static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16_f = {
742     0x9781,
743     0x1234,
744     {
745         0x4,
746         {0x33, 0x44, 0x55, 0x66}
747     },
748     {
749         0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
750         0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
751     }
752 };
753
754 static int encode_case_16_enc(WPACKET *pkt)
755 {
756     if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
757                                                              &encode_case_16_f), 1))
758         return 0;
759
760     return 1;
761 }
762
763 static int encode_case_16_dec(PACKET *pkt, ossl_ssize_t fail)
764 {
765     OSSL_QUIC_FRAME_NEW_CONN_ID f = {0};
766
767     if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), fail < 0))
768         return 0;
769
770     if (fail >= 0)
771         return 1;
772
773     if (!TEST_uint64_t_eq(f.seq_num, 0x9781))
774         return 0;
775
776     if (!TEST_uint64_t_eq(f.retire_prior_to, 0x1234))
777         return 0;
778
779     if (!TEST_uint64_t_eq(f.conn_id.id_len, sizeof(encode_case_16_conn_id)))
780         return 0;
781
782     if (!TEST_mem_eq(f.conn_id.id, f.conn_id.id_len,
783                      encode_case_16_conn_id, sizeof(encode_case_16_conn_id)))
784         return 0;
785
786     if (!TEST_mem_eq(f.stateless_reset_token,
787                      sizeof(f.stateless_reset_token),
788                      encode_case_16_f.stateless_reset_token,
789                      sizeof(encode_case_16_f.stateless_reset_token)))
790         return 0;
791
792     return 1;
793 }
794
795 static const unsigned char encode_case_16_expect[] = {
796     0x18,                           /* Type */
797     0x80, 0x00, 0x97, 0x81,         /* Sequence Number */
798     0x52, 0x34,                     /* Retire Prior To */
799     0x04,                           /* Connection ID Length */
800     0x33, 0x44, 0x55, 0x66,         /* Connection ID */
801     0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
802     0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
803 };
804
805 /* 16b. NEW_CONNECTION_ID seq_num < retire_prior_to */
806 static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16b_f = {
807     0x1234,
808     0x9781,
809     {
810         0x4,
811         {0x33, 0x44, 0x55, 0x66}
812     },
813     {
814         0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
815         0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
816     }
817 };
818
819 static int encode_case_16b_enc(WPACKET *pkt)
820 {
821     if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
822                                                              &encode_case_16b_f), 1))
823         return 0;
824
825     return 1;
826 }
827
828 static int encode_case_16b_dec(PACKET *pkt, ossl_ssize_t fail)
829 {
830     OSSL_QUIC_FRAME_NEW_CONN_ID f = {0};
831
832     if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), 0))
833         return 0;
834
835     if (!TEST_true(PACKET_forward(pkt, PACKET_remaining(pkt))))
836         return 0;
837
838     return 1;
839 }
840
841 static const unsigned char encode_case_16b_expect[] = {
842     0x18,                           /* Type */
843     0x52, 0x34,                     /* Sequence Number */
844     0x80, 0x00, 0x97, 0x81,         /* Retire Prior To */
845     0x04,                           /* Connection ID Length */
846     0x33, 0x44, 0x55, 0x66,         /* Connection ID */
847     0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Stateless Reset Token */
848     0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
849 };
850
851 /* 17. RETIRE_CONNECTION_ID */
852 static int encode_case_17_enc(WPACKET *pkt)
853 {
854     if (!TEST_int_eq(ossl_quic_wire_encode_frame_retire_conn_id(pkt, 0x1234), 1))
855         return 0;
856
857     return 1;
858 }
859
860 static int encode_case_17_dec(PACKET *pkt, ossl_ssize_t fail)
861 {
862     uint64_t seq_num = 0;
863
864     if (!TEST_int_eq(ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num), fail < 0))
865         return 0;
866
867     if (fail >= 0)
868         return 1;
869
870     if (!TEST_uint64_t_eq(seq_num, 0x1234))
871         return 0;
872
873     return 1;
874 }
875
876 static const unsigned char encode_case_17_expect[] = {
877     0x19,                           /* Type */
878     0x52, 0x34,                     /* Seq Num */
879 };
880
881 /* 18. PATH_CHALLENGE */
882 static const uint64_t encode_case_18_data
883     = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
884
885 static int encode_case_18_enc(WPACKET *pkt)
886 {
887     if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_challenge(pkt,
888                                                                 encode_case_18_data), 1))
889         return 0;
890
891     return 1;
892 }
893
894 static int encode_case_18_dec(PACKET *pkt, ossl_ssize_t fail)
895 {
896     uint64_t challenge = 0;
897
898     if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_challenge(pkt, &challenge), fail < 0))
899         return 0;
900
901     if (fail >= 0)
902         return 1;
903
904     if (!TEST_uint64_t_eq(challenge, encode_case_18_data))
905         return 0;
906
907     return 1;
908 }
909
910 static const unsigned char encode_case_18_expect[] = {
911     0x1A,                                           /* Type */
912     0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
913 };
914
915 /* 19. PATH_RESPONSE */
916 static const uint64_t encode_case_19_data
917     = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
918
919 static int encode_case_19_enc(WPACKET *pkt)
920 {
921     if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_response(pkt,
922                                                                encode_case_19_data), 1))
923         return 0;
924
925     return 1;
926 }
927
928 static int encode_case_19_dec(PACKET *pkt, ossl_ssize_t fail)
929 {
930     uint64_t challenge = 0;
931
932     if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_response(pkt, &challenge), fail < 0))
933         return 0;
934
935     if (fail >= 0)
936         return 1;
937
938     if (!TEST_uint64_t_eq(challenge, encode_case_19_data))
939         return 0;
940
941     return 1;
942 }
943
944 static const unsigned char encode_case_19_expect[] = {
945     0x1B,                                           /* Type */
946     0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
947 };
948
949 /* 20. CONNECTION_CLOSE (transport) */
950 static const char encode_case_20_reason[] = {
951     /* "reason for closure" */
952     0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f,
953     0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
954 };
955
956 static const OSSL_QUIC_FRAME_CONN_CLOSE encode_case_20_f = {
957     0,
958     0x1234,
959     0x9781,
960     (char *)encode_case_20_reason,
961     sizeof(encode_case_20_reason)
962 };
963
964 static int encode_case_20_enc(WPACKET *pkt)
965 {
966     if (!TEST_int_eq(ossl_quic_wire_encode_frame_conn_close(pkt,
967                                                             &encode_case_20_f), 1))
968         return 0;
969
970     return 1;
971 }
972
973 static int encode_case_20_dec(PACKET *pkt, ossl_ssize_t fail)
974 {
975     OSSL_QUIC_FRAME_CONN_CLOSE f = {0};
976
977     if (!TEST_int_eq(ossl_quic_wire_decode_frame_conn_close(pkt, &f), fail < 0))
978         return 0;
979
980     if (fail >= 0)
981         return 1;
982
983     if (!TEST_int_eq(f.is_app, 0))
984         return 0;
985
986     if (!TEST_uint64_t_eq(f.error_code, 0x1234))
987         return 0;
988
989     if (!TEST_uint64_t_eq(f.frame_type, 0x9781))
990         return 0;
991
992     if (!TEST_size_t_eq(f.reason_len, 18))
993         return 0;
994
995     if (!TEST_mem_eq(f.reason, f.reason_len,
996                      encode_case_20_f.reason, encode_case_20_f.reason_len))
997         return 0;
998
999     return 1;
1000 }
1001
1002 static const unsigned char encode_case_20_expect[] = {
1003     0x1C,                           /* Type */
1004     0x52, 0x34,                     /* Sequence Number */
1005     0x80, 0x00, 0x97, 0x81,         /* Frame Type */
1006     0x12,                           /* Reason Length */
1007     0x72, 0x65, 0x61, 0x73, 0x6f, 0x6e, 0x20, 0x66, 0x6f, /* Reason */
1008     0x72, 0x20, 0x63, 0x6c, 0x6f, 0x73, 0x75, 0x72, 0x65
1009 };
1010
1011 /* 21. HANDSHAKE_DONE */
1012 static int encode_case_21_enc(WPACKET *pkt)
1013 {
1014
1015     if (!TEST_int_eq(ossl_quic_wire_encode_frame_handshake_done(pkt), 1))
1016         return 0;
1017
1018     return 1;
1019 }
1020
1021 static int encode_case_21_dec(PACKET *pkt, ossl_ssize_t fail)
1022 {
1023
1024     if (!TEST_int_eq(ossl_quic_wire_decode_frame_handshake_done(pkt), fail < 0))
1025         return 0;
1026
1027     return 1;
1028 }
1029
1030 static const unsigned char encode_case_21_expect[] = {
1031     0x1E
1032 };
1033
1034 /* 22. Buffer Transport Parameter */
1035 static const unsigned char encode_case_22_data[] = {0x55,0x77,0x32,0x46,0x99};
1036
1037 static int encode_case_22_enc(WPACKET *pkt)
1038 {
1039     unsigned char *p;
1040
1041     if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(pkt, 0x1234,
1042                                                               encode_case_22_data,
1043                                                               sizeof(encode_case_22_data))))
1044         return 0;
1045
1046     if (!TEST_ptr(p = ossl_quic_wire_encode_transport_param_bytes(pkt, 0x9781,
1047                                                                   NULL, 2)))
1048         return 0;
1049
1050     p[0] = 0x33;
1051     p[1] = 0x44;
1052
1053     return 1;
1054 }
1055
1056 static int encode_case_22_dec(PACKET *pkt, ossl_ssize_t fail)
1057 {
1058     uint64_t id = 0;
1059     size_t len = 0;
1060     const unsigned char *p;
1061     static const unsigned char data[] = {0x33, 0x44};
1062
1063     if (!TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
1064                      fail < 0 || fail >= 2))
1065         return 0;
1066
1067     if ((fail < 0 || fail >= 2)
1068         && !TEST_uint64_t_eq(id, 0x1234))
1069         return 0;
1070
1071     id = 0;
1072
1073     p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
1074     if (fail < 0 || fail >= 8) {
1075         if (!TEST_ptr(p))
1076             return 0;
1077     } else {
1078         if (!TEST_ptr_null(p))
1079             return 0;
1080     }
1081
1082     if ((fail < 0 || fail >= 8)
1083         && !TEST_uint64_t_eq(id, 0x1234))
1084         return 0;
1085
1086     if ((fail < 0 || fail >= 8)
1087         && !TEST_mem_eq(p, len, encode_case_22_data, sizeof(encode_case_22_data)))
1088         return 0;
1089
1090     if ((fail < 0 || fail >= 8)
1091         && !TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
1092                         fail < 0 || fail >= 12))
1093         return 0;
1094
1095     if ((fail < 0 || fail >= 12)
1096         && !TEST_uint64_t_eq(id, 0x9781))
1097         return 0;
1098
1099     id = 0;
1100
1101     p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
1102     if (fail < 0 || fail >= 15) {
1103         if (!TEST_ptr(p))
1104             return 0;
1105     } else {
1106         if (!TEST_ptr_null(p))
1107             return 0;
1108     }
1109
1110     if ((fail < 0 || fail >= 15)
1111         && !TEST_uint64_t_eq(id, 0x9781))
1112         return 0;
1113
1114     if ((fail < 0 || fail >= 15)
1115         && !TEST_mem_eq(p, len, data, sizeof(data)))
1116         return 0;
1117
1118     return 1;
1119 }
1120
1121 static const unsigned char encode_case_22_expect[] = {
1122     0x52, 0x34,                         /* ID */
1123     0x05,                               /* Length */
1124     0x55, 0x77, 0x32, 0x46, 0x99,       /* Data */
1125
1126     0x80, 0x00, 0x97, 0x81,             /* ID */
1127     0x02,                               /* Length */
1128     0x33, 0x44                          /* Data */
1129 };
1130
1131 /* 23. Integer Transport Parameter */
1132 static int encode_case_23_enc(WPACKET *pkt)
1133 {
1134     if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x1234, 0x9781), 1))
1135         return 0;
1136
1137     if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x2233, 0x4545), 1))
1138         return 0;
1139
1140     return 1;
1141 }
1142
1143 static int encode_case_23_dec(PACKET *pkt, ossl_ssize_t fail)
1144 {
1145     uint64_t id = 0, value = 0;
1146
1147     if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
1148                                                                &id, &value),
1149                      fail < 0 || fail >= 7))
1150         return 0;
1151
1152     if ((fail < 0 || fail >= 7)
1153         && !TEST_uint64_t_eq(id, 0x1234))
1154         return 0;
1155
1156     if ((fail < 0 || fail >= 7)
1157         && !TEST_uint64_t_eq(value, 0x9781))
1158         return 0;
1159
1160     if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
1161                                                                &id, &value),
1162                      fail < 0 || fail >= 14))
1163         return 0;
1164
1165     if ((fail < 0 || fail >= 14)
1166         && !TEST_uint64_t_eq(id, 0x2233))
1167         return 0;
1168
1169     if ((fail < 0 || fail >= 14)
1170         && !TEST_uint64_t_eq(value, 0x4545))
1171         return 0;
1172
1173     return 1;
1174 }
1175
1176 static const unsigned char encode_case_23_expect[] = {
1177     0x52, 0x34,
1178     0x04,
1179     0x80, 0x00, 0x97, 0x81,
1180
1181     0x62, 0x33,
1182     0x04,
1183     0x80, 0x00, 0x45, 0x45,
1184 };
1185
1186 #define ENCODE_CASE(n)                          \
1187     {                                           \
1188       encode_case_##n##_enc,                    \
1189       encode_case_##n##_expect,                 \
1190       OSSL_NELEM(encode_case_##n##_expect),     \
1191       encode_case_##n##_dec                     \
1192     },
1193
1194 static const struct encode_test_case encode_cases[] = {
1195     ENCODE_CASE(1)
1196     ENCODE_CASE(2)
1197     ENCODE_CASE(3)
1198     ENCODE_CASE(4)
1199     ENCODE_CASE(5)
1200     ENCODE_CASE(6)
1201     ENCODE_CASE(7)
1202     ENCODE_CASE(8)
1203     ENCODE_CASE(9)
1204     ENCODE_CASE(10)
1205     ENCODE_CASE(11)
1206     ENCODE_CASE(12)
1207     ENCODE_CASE(13)
1208     ENCODE_CASE(14)
1209     ENCODE_CASE(15)
1210     ENCODE_CASE(16)
1211     ENCODE_CASE(16b)
1212     ENCODE_CASE(17)
1213     ENCODE_CASE(18)
1214     ENCODE_CASE(19)
1215     ENCODE_CASE(20)
1216     ENCODE_CASE(21)
1217     ENCODE_CASE(22)
1218     ENCODE_CASE(23)
1219 };
1220
1221 static int test_wire_encode(int idx)
1222 {
1223     int testresult = 0;
1224     WPACKET wpkt;
1225     PACKET pkt;
1226     BUF_MEM *buf = NULL;
1227     size_t written;
1228     const struct encode_test_case *c = &encode_cases[idx];
1229     int have_wpkt = 0;
1230     size_t i;
1231
1232     if (!TEST_ptr(buf = BUF_MEM_new()))
1233         goto err;
1234
1235     if (!TEST_int_eq(WPACKET_init(&wpkt, buf), 1))
1236         goto err;
1237
1238     have_wpkt = 1;
1239     if (!TEST_int_eq(c->serializer(&wpkt), 1))
1240         goto err;
1241
1242     if (!TEST_int_eq(WPACKET_get_total_written(&wpkt, &written), 1))
1243         goto err;
1244
1245     if (!TEST_mem_eq(buf->data, written, c->expect_buf, c->expect_buf_len))
1246         goto err;
1247
1248     if (!TEST_int_eq(PACKET_buf_init(&pkt, (unsigned char *)buf->data, written), 1))
1249         goto err;
1250
1251     if (!TEST_int_eq(c->deserializer(&pkt, -1), 1))
1252         goto err;
1253
1254     if (!TEST_false(PACKET_remaining(&pkt)))
1255         goto err;
1256
1257     for (i = 0; i < c->expect_buf_len; ++i) {
1258         PACKET pkt2;
1259
1260         /*
1261          * Check parsing truncated (i.e., malformed) input is handled correctly.
1262          * Generate all possible truncations of our reference encoding and
1263          * verify that they are handled correctly. The number of bytes of the
1264          * truncated encoding is passed as an argument to the deserializer to
1265          * help it determine whether decoding should fail or not.
1266          */
1267         if (!TEST_int_eq(PACKET_buf_init(&pkt2, (unsigned char *)c->expect_buf, i), 1))
1268             goto err;
1269
1270         if (!TEST_int_eq(c->deserializer(&pkt2, i), 1))
1271             goto err;
1272     }
1273
1274     testresult = 1;
1275 err:
1276     if (have_wpkt)
1277         WPACKET_finish(&wpkt);
1278     BUF_MEM_free(buf);
1279     return testresult;
1280 }
1281
1282 struct ack_test_case {
1283     const unsigned char    *input_buf;
1284     size_t                  input_buf_len;
1285     int                   (*deserializer)(PACKET *pkt);
1286     int                     expect_fail;
1287 };
1288
1289 /* ACK Frame with Excessive First ACK Range Field */
1290 static const unsigned char ack_case_1_input[] = {
1291     0x02,           /* ACK Without ECN */
1292     0x08,           /* Largest Acknowledged */
1293     0x01,           /* ACK Delay */
1294     0x00,           /* ACK Range Count */
1295     0x09,           /* First ACK Range */
1296 };
1297
1298 /* ACK Frame with Valid ACK Range Field */
1299 static const unsigned char ack_case_2_input[] = {
1300     0x02,           /* ACK Without ECN */
1301     0x08,           /* Largest Acknowledged */
1302     0x01,           /* ACK Delay */
1303     0x00,           /* ACK Range Count */
1304     0x08,           /* First ACK Range */
1305 };
1306
1307 /* ACK Frame with Excessive ACK Range Gap */
1308 static const unsigned char ack_case_3_input[] = {
1309     0x02,           /* ACK Without ECN */
1310     0x08,           /* Largest Acknowledged */
1311     0x01,           /* ACK Delay */
1312     0x01,           /* ACK Range Count */
1313     0x01,           /* First ACK Range */
1314
1315     0x05,           /* Gap */
1316     0x01,           /* ACK Range Length */
1317 };
1318
1319 /* ACK Frame with Valid ACK Range */
1320 static const unsigned char ack_case_4_input[] = {
1321     0x02,           /* ACK Without ECN */
1322     0x08,           /* Largest Acknowledged */
1323     0x01,           /* ACK Delay */
1324     0x01,           /* ACK Range Count */
1325     0x01,           /* First ACK Range */
1326
1327     0x04,           /* Gap */
1328     0x01,           /* ACK Range Length */
1329 };
1330
1331 /* ACK Frame with Excessive ACK Range Length */
1332 static const unsigned char ack_case_5_input[] = {
1333     0x02,           /* ACK Without ECN */
1334     0x08,           /* Largest Acknowledged */
1335     0x01,           /* ACK Delay */
1336     0x01,           /* ACK Range Count */
1337     0x01,           /* First ACK Range */
1338
1339     0x04,           /* Gap */
1340     0x02,           /* ACK Range Length */
1341 };
1342
1343 /* ACK Frame with Multiple ACK Ranges, Final Having Excessive Length */
1344 static const unsigned char ack_case_6_input[] = {
1345     0x02,           /* ACK Without ECN */
1346     0x08,           /* Largest Acknowledged */
1347     0x01,           /* ACK Delay */
1348     0x02,           /* ACK Range Count */
1349     0x01,           /* First ACK Range */
1350
1351     0x01,           /* Gap */
1352     0x02,           /* ACK Range Length */
1353
1354     0x00,           /* Gap */
1355     0x01,           /* ACK Range Length */
1356 };
1357
1358 /* ACK Frame with Multiple ACK Ranges, Valid */
1359 static const unsigned char ack_case_7_input[] = {
1360     0x02,           /* ACK Without ECN */
1361     0x08,           /* Largest Acknowledged */
1362     0x01,           /* ACK Delay */
1363     0x02,           /* ACK Range Count */
1364     0x01,           /* First ACK Range */
1365
1366     0x01,           /* Gap */
1367     0x02,           /* ACK Range Length */
1368
1369     0x00,           /* Gap */
1370     0x00,           /* ACK Range Length */
1371 };
1372
1373 static int ack_generic_decode(PACKET *pkt)
1374 {
1375     OSSL_QUIC_ACK_RANGE ranges[8] = {0};
1376     OSSL_QUIC_FRAME_ACK f = {0};
1377     uint64_t total_ranges = 0, peek_total_ranges = 0;
1378     int r;
1379     size_t i;
1380
1381     f.ack_ranges        = ranges;
1382     f.num_ack_ranges    = OSSL_NELEM(ranges);
1383
1384     if (!TEST_int_eq(ossl_quic_wire_peek_frame_ack_num_ranges(pkt,
1385                                                               &peek_total_ranges), 1))
1386         return 0;
1387
1388     r = ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges);
1389     if (r == 0)
1390         return 0;
1391
1392     if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
1393         return 0;
1394
1395     for (i = 0; i < f.num_ack_ranges; ++i) {
1396         if (!TEST_uint64_t_le(f.ack_ranges[i].start, f.ack_ranges[i].end))
1397             return 0;
1398         if (!TEST_uint64_t_lt(f.ack_ranges[i].end, 1000))
1399             return 0;
1400     }
1401
1402     return 1;
1403 }
1404
1405 #define ACK_CASE(n, expect_fail, dec)   \
1406     {                                   \
1407         ack_case_##n##_input,           \
1408         sizeof(ack_case_##n##_input),   \
1409         (dec),                          \
1410         (expect_fail)                   \
1411     },
1412
1413 static const struct ack_test_case ack_cases[] = {
1414     ACK_CASE(1, 1, ack_generic_decode)
1415     ACK_CASE(2, 0, ack_generic_decode)
1416     ACK_CASE(3, 1, ack_generic_decode)
1417     ACK_CASE(4, 0, ack_generic_decode)
1418     ACK_CASE(5, 1, ack_generic_decode)
1419     ACK_CASE(6, 1, ack_generic_decode)
1420     ACK_CASE(7, 0, ack_generic_decode)
1421 };
1422
1423 static int test_wire_ack(int idx)
1424 {
1425     int testresult = 0, r;
1426     PACKET pkt;
1427     const struct ack_test_case *c = &ack_cases[idx];
1428
1429     if (!TEST_int_eq(PACKET_buf_init(&pkt,
1430                                      (unsigned char *)c->input_buf,
1431                                      c->input_buf_len), 1))
1432         goto err;
1433
1434     r = c->deserializer(&pkt);
1435     if (c->expect_fail) {
1436         if (!TEST_int_eq(r, 0))
1437             goto err;
1438     } else {
1439         if (!TEST_int_eq(r, 1))
1440             goto err;
1441
1442         if (!TEST_false(PACKET_remaining(&pkt)))
1443             goto err;
1444     }
1445
1446     testresult = 1;
1447 err:
1448     return testresult;
1449 }
1450
1451 /* Packet Header PN Encoding Tests */
1452 struct pn_test {
1453     QUIC_PN         pn, tx_largest_acked, rx_largest_pn;
1454     char            expected_len;
1455     unsigned char   expected_bytes[4];
1456 };
1457
1458 static const struct pn_test pn_tests[] = {
1459     /* RFC 9000 Section A.2 */
1460     { 0xac5c02, 0xabe8b3, 0xabe8b3, 2, {0x5c,0x02} },
1461     { 0xace8fe, 0xabe8b3, 0xabe8b3, 3, {0xac,0xe8,0xfe} },
1462     /* RFC 9000 Section A.3 */
1463     { 0xa82f9b32, 0xa82f30ea, 0xa82f30ea, 2, {0x9b,0x32} },
1464     /* Boundary Cases */
1465     { 1, 0, 0, 1, {0x01} },
1466     { 256, 255, 255, 1, {0x00} },
1467     { 257, 255, 255, 1, {0x01} },
1468     { 256, 128, 128, 1, {0x00} },
1469     { 256, 127, 127, 2, {0x01,0x00} },
1470     { 65536, 32768, 32768, 2, {0x00,0x00} },
1471     { 65537, 32769, 32769, 2, {0x00,0x01} },
1472     { 65536, 32767, 32767, 3, {0x01,0x00,0x00} },
1473     { 65537, 32768, 32768, 3, {0x01,0x00,0x01} },
1474     { 16777216, 8388608, 8388608, 3, {0x00,0x00,0x00} },
1475     { 16777217, 8388609, 8388609, 3, {0x00,0x00,0x01} },
1476     { 16777216, 8388607, 8388607, 4, {0x01,0x00,0x00,0x00} },
1477     { 16777217, 8388608, 8388608, 4, {0x01,0x00,0x00,0x01} },
1478     { 4294967296, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x00} },
1479     { 4294967297, 2147483648, 2147483648, 4, {0x00,0x00,0x00,0x01} },
1480 };
1481
1482 static int test_wire_pkt_hdr_pn(int tidx)
1483 {
1484     int testresult = 0;
1485     const struct pn_test *t = &pn_tests[tidx];
1486     unsigned char buf[4];
1487     int pn_len;
1488     QUIC_PN res_pn;
1489
1490     pn_len = ossl_quic_wire_determine_pn_len(t->pn, t->tx_largest_acked);
1491     if (!TEST_int_eq(pn_len, (int)t->expected_len))
1492         goto err;
1493
1494     if (!TEST_true(ossl_quic_wire_encode_pkt_hdr_pn(t->pn, buf, pn_len)))
1495         goto err;
1496
1497     if (!TEST_mem_eq(t->expected_bytes, t->expected_len, buf, pn_len))
1498         goto err;
1499
1500     if (!TEST_true(ossl_quic_wire_decode_pkt_hdr_pn(buf, pn_len,
1501                                                     t->rx_largest_pn, &res_pn)))
1502         goto err;
1503
1504     if (!TEST_uint64_t_eq(res_pn, t->pn))
1505         goto err;
1506
1507     testresult = 1;
1508 err:
1509     return testresult;
1510 }
1511
1512 /* RFC 9001 s. A.4 */
1513 static const QUIC_CONN_ID retry_orig_dcid = {
1514     8, { 0x83, 0x94, 0xc8, 0xf0, 0x3e, 0x51, 0x57, 0x08 }
1515 };
1516
1517 static const unsigned char retry_encoded[] = {
1518   0xff,                                                 /* Long Header, Retry */
1519   0x00, 0x00, 0x00, 0x01,                               /* Version 1 */
1520   0x00,                                                 /* DCID */
1521   0x08, 0xf0, 0x67, 0xa5, 0x50, 0x2a, 0x42, 0x62, 0xb5, /* SCID */
1522
1523   /* Retry Token */
1524   0x74, 0x6f, 0x6b, 0x65, 0x6e,
1525
1526   /* Retry Integrity Tag */
1527   0x04, 0xa2, 0x65, 0xba, 0x2e, 0xff, 0x4d, 0x82, 0x90, 0x58, 0xfb, 0x3f, 0x0f,
1528   0x24, 0x96, 0xba
1529 };
1530
1531 static int test_wire_retry_integrity_tag(void)
1532 {
1533     int testresult = 0;
1534     PACKET pkt = {0};
1535     QUIC_PKT_HDR hdr = {0};
1536     unsigned char got_tag[QUIC_RETRY_INTEGRITY_TAG_LEN] = {0};
1537
1538     if (!TEST_true(PACKET_buf_init(&pkt, retry_encoded, sizeof(retry_encoded))))
1539         goto err;
1540
1541     if (!TEST_true(ossl_quic_wire_decode_pkt_hdr(&pkt, 0, 0, 0, &hdr, NULL)))
1542         goto err;
1543
1544     if (!TEST_int_eq(hdr.type, QUIC_PKT_TYPE_RETRY))
1545         goto err;
1546
1547     if (!TEST_true(ossl_quic_calculate_retry_integrity_tag(NULL, NULL, &hdr,
1548                                                            &retry_orig_dcid,
1549                                                            got_tag)))
1550         goto err;
1551
1552     if (!TEST_mem_eq(got_tag, sizeof(got_tag),
1553                      retry_encoded + sizeof(retry_encoded)
1554                         - QUIC_RETRY_INTEGRITY_TAG_LEN,
1555                      QUIC_RETRY_INTEGRITY_TAG_LEN))
1556         goto err;
1557
1558     if (!TEST_true(ossl_quic_validate_retry_integrity_tag(NULL, NULL, &hdr,
1559                                                           &retry_orig_dcid)))
1560         goto err;
1561
1562     testresult = 1;
1563 err:
1564     return testresult;
1565 }
1566
1567 /* is_minimal=0 test */
1568 static const unsigned char non_minimal_1[] = {
1569     0x40, 0x00,
1570 };
1571
1572 static const unsigned char non_minimal_2[] = {
1573     0x40, 0x3F,
1574 };
1575
1576 static const unsigned char non_minimal_3[] = {
1577     0x80, 0x00, 0x00, 0x00,
1578 };
1579
1580 static const unsigned char non_minimal_4[] = {
1581     0x80, 0x00, 0x3F, 0xFF,
1582 };
1583
1584 static const unsigned char non_minimal_5[] = {
1585     0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1586 };
1587
1588 static const unsigned char non_minimal_6[] = {
1589     0xC0, 0x00, 0x00, 0x00, 0x3F, 0xFF, 0xFF, 0xFF
1590 };
1591
1592 static const unsigned char *const non_minimal[] = {
1593     non_minimal_1,
1594     non_minimal_2,
1595     non_minimal_3,
1596     non_minimal_4,
1597     non_minimal_5,
1598     non_minimal_6,
1599 };
1600
1601 static const size_t non_minimal_len[] = {
1602     OSSL_NELEM(non_minimal_1),
1603     OSSL_NELEM(non_minimal_2),
1604     OSSL_NELEM(non_minimal_3),
1605     OSSL_NELEM(non_minimal_4),
1606     OSSL_NELEM(non_minimal_5),
1607     OSSL_NELEM(non_minimal_6),
1608 };
1609
1610 static int test_wire_minimal(int idx)
1611 {
1612     int testresult = 0;
1613     int is_minimal;
1614     uint64_t frame_type;
1615     PACKET pkt;
1616
1617     if (!TEST_true(PACKET_buf_init(&pkt, non_minimal[idx],
1618                                    non_minimal_len[idx])))
1619         goto err;
1620
1621     if (!TEST_true(ossl_quic_wire_peek_frame_header(&pkt, &frame_type,
1622                                                     &is_minimal)))
1623         goto err;
1624
1625     if (!TEST_false(is_minimal))
1626         goto err;
1627
1628     testresult = 1;
1629 err:
1630     return testresult;
1631 }
1632
1633 int setup_tests(void)
1634 {
1635     ADD_ALL_TESTS(test_wire_encode,     OSSL_NELEM(encode_cases));
1636     ADD_ALL_TESTS(test_wire_ack,        OSSL_NELEM(ack_cases));
1637     ADD_ALL_TESTS(test_wire_pkt_hdr_pn, OSSL_NELEM(pn_tests));
1638     ADD_TEST(test_wire_retry_integrity_tag);
1639     ADD_ALL_TESTS(test_wire_minimal,    OSSL_NELEM(non_minimal_len));
1640     return 1;
1641 }