]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/quic_wire_test.c
Make OSSL_TIME a structure
[thirdparty/openssl.git] / test / quic_wire_test.c
1 /*
2 * Copyright 2015-2018 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 "testutil.h"
13
14 struct encode_test_case {
15 int (*serializer)(WPACKET *pkt);
16 const unsigned char *expect_buf;
17 size_t expect_buf_len;
18 /*
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
21 * decoding fails)
22 */
23 int (*deserializer)(PACKET *pkt, ossl_ssize_t fail);
24 };
25
26 /* 1. PADDING */
27 static int encode_case_1_enc(WPACKET *pkt)
28 {
29 if (!TEST_int_eq(ossl_quic_wire_encode_padding(pkt, 3), 1))
30 return 0;
31
32 return 1;
33 }
34
35 static int encode_case_1_dec(PACKET *pkt, ossl_ssize_t fail)
36 {
37 if (fail >= 0)
38 /* No failure modes for padding */
39 return 1;
40
41 if (!TEST_int_eq(ossl_quic_wire_decode_padding(pkt), 3))
42 return 0;
43
44 return 1;
45 }
46
47 static const unsigned char encode_case_1_expect[] = {
48 0, 0, 0
49 };
50
51 /* 2. PING */
52 static int encode_case_2_enc(WPACKET *pkt)
53 {
54
55 if (!TEST_int_eq(ossl_quic_wire_encode_frame_ping(pkt), 1))
56 return 0;
57
58 return 1;
59 }
60
61 static int encode_case_2_dec(PACKET *pkt, ossl_ssize_t fail)
62 {
63
64 if (!TEST_int_eq(ossl_quic_wire_decode_frame_ping(pkt), fail < 0))
65 return 0;
66
67 return 1;
68 }
69
70 static const unsigned char encode_case_2_expect[] = {
71 0x01
72 };
73
74 /* 3. ACK */
75 static const OSSL_QUIC_ACK_RANGE encode_case_3_ranges[] = {
76 { 20, 30 },
77 { 0, 10 }
78 };
79
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),
83 { OSSL_TIME_MS },
84 60, 70, 80, 1
85 };
86
87 static int encode_case_3_enc(WPACKET *pkt)
88 {
89 if (!TEST_int_eq(ossl_quic_wire_encode_frame_ack(pkt, 3, &encode_case_3_f), 1))
90 return 0;
91
92 return 1;
93 }
94
95 static int encode_case_3_dec(PACKET *pkt, ossl_ssize_t fail)
96 {
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;
100 int ret;
101
102 f.ack_ranges = ranges;
103 f.num_ack_ranges = OSSL_NELEM(ranges);
104
105 ret = ossl_quic_wire_peek_frame_ack_num_ranges(pkt, &peek_total_ranges);
106 if (fail < 0 && !TEST_int_eq(ret, 1))
107 return 0;
108
109 if (!TEST_int_eq(ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges), fail < 0))
110 return 0;
111
112 if (ret == 1 && !TEST_uint64_t_eq(peek_total_ranges, 2))
113 return 0;
114
115 if (fail >= 0)
116 return 1;
117
118 if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
119 return 0;
120
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)))
124 return 0;
125
126 if (!TEST_uint64_t_eq(ossl_time2ticks(f.delay_time),
127 ossl_time2ticks(encode_case_3_f.delay_time)))
128 return 0;
129
130 if (!TEST_true(f.ecn_present))
131 return 0;
132
133 if (!TEST_uint64_t_eq(f.ect0, encode_case_3_f.ect0))
134 return 0;
135
136 if (!TEST_uint64_t_eq(f.ect1, encode_case_3_f.ect1))
137 return 0;
138
139 if (!TEST_uint64_t_eq(f.ecnce, encode_case_3_f.ecnce))
140 return 0;
141
142 return 1;
143 }
144
145 static const unsigned char encode_case_3_expect[] = {
146 0x03, /* Type */
147 0x1E, /* Largest Acknowledged */
148 0x40, 0x7d, /* ACK Delay */
149 1, /* ACK Range Count */
150 10, /* First ACK Range */
151
152 8, /* Gap */
153 10, /* Length */
154
155 0x3c, /* ECT0 Count */
156 0x40, 0x46, /* ECT1 Count */
157 0x40, 0x50, /* ECNCE Count */
158 };
159
160 /* 4. RESET_STREAM */
161 static const OSSL_QUIC_FRAME_RESET_STREAM encode_case_4_f = {
162 0x1234, 0x9781, 0x11717
163 };
164
165 static int encode_case_4_enc(WPACKET *pkt)
166 {
167 if (!TEST_int_eq(ossl_quic_wire_encode_frame_reset_stream(pkt,
168 &encode_case_4_f), 1))
169 return 0;
170
171 return 1;
172 }
173
174 static int encode_case_4_dec(PACKET *pkt, ossl_ssize_t fail)
175 {
176 OSSL_QUIC_FRAME_RESET_STREAM f = {0};
177
178 if (!TEST_int_eq(ossl_quic_wire_decode_frame_reset_stream(pkt, &f), fail < 0))
179 return 0;
180
181 if (fail >= 0)
182 return 1;
183
184 if (!TEST_mem_eq(&f, sizeof(f), &encode_case_4_f, sizeof(encode_case_4_f)))
185 return 0;
186
187 return 1;
188 }
189
190 static const unsigned char encode_case_4_expect[] = {
191 0x04, /* Type */
192 0x52, 0x34, /* Stream ID */
193 0x80, 0x00, 0x97, 0x81, /* App Error Code */
194 0x80, 0x01, 0x17, 0x17, /* Final Size */
195 };
196
197 /* 5. STOP_SENDING */
198 static const OSSL_QUIC_FRAME_STOP_SENDING encode_case_5_f = {
199 0x1234, 0x9781
200 };
201
202 static int encode_case_5_enc(WPACKET *pkt)
203 {
204 if (!TEST_int_eq(ossl_quic_wire_encode_frame_stop_sending(pkt,
205 &encode_case_5_f), 1))
206 return 0;
207
208 return 1;
209 }
210
211 static int encode_case_5_dec(PACKET *pkt, ossl_ssize_t fail)
212 {
213 OSSL_QUIC_FRAME_STOP_SENDING f = {0};
214
215 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stop_sending(pkt, &f), fail < 0))
216 return 0;
217
218 if (fail >= 0)
219 return 1;
220
221 if (!TEST_mem_eq(&f, sizeof(f), &encode_case_5_f, sizeof(encode_case_5_f)))
222 return 0;
223
224 return 1;
225 }
226
227 static const unsigned char encode_case_5_expect[] = {
228 0x05, /* Type */
229 0x52, 0x34, /* Stream ID */
230 0x80, 0x00, 0x97, 0x81 /* App Error Code */
231 };
232
233 /* 6. CRYPTO */
234 static const unsigned char encode_case_6_data[] = {
235 93, 18, 17, 102, 33
236 };
237
238 static const OSSL_QUIC_FRAME_CRYPTO encode_case_6_f = {
239 0x1234, sizeof(encode_case_6_data), encode_case_6_data
240 };
241
242 static int encode_case_6_enc(WPACKET *pkt)
243 {
244 if (!TEST_ptr(ossl_quic_wire_encode_frame_crypto(pkt,
245 &encode_case_6_f)))
246 return 0;
247
248 return 1;
249 }
250
251 static int encode_case_6_dec(PACKET *pkt, ossl_ssize_t fail)
252 {
253 OSSL_QUIC_FRAME_CRYPTO f = {0};
254
255 if (!TEST_int_eq(ossl_quic_wire_decode_frame_crypto(pkt, &f), fail < 0))
256 return 0;
257
258 if (fail >= 0)
259 return 1;
260
261 if (!TEST_uint64_t_eq(f.offset, 0x1234))
262 return 0;
263
264 if (!TEST_mem_eq(f.data, f.len, encode_case_6_data, sizeof(encode_case_6_data)))
265 return 0;
266
267 return 1;
268 }
269
270 static const unsigned char encode_case_6_expect[] = {
271 0x06, /* Type */
272 0x52, 0x34, /* Offset */
273 0x05, /* Length */
274 93, 18, 17, 102, 33 /* Data */
275 };
276
277 /* 7. NEW_TOKEN */
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
281 };
282
283 static int encode_case_7_enc(WPACKET *pkt)
284 {
285 if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_token(pkt,
286 encode_case_7_token,
287 sizeof(encode_case_7_token)), 1))
288 return 0;
289
290 return 1;
291 }
292
293 static int encode_case_7_dec(PACKET *pkt, ossl_ssize_t fail)
294 {
295 const unsigned char *token = NULL;
296 size_t token_len = 0;
297
298 if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_token(pkt,
299 &token,
300 &token_len), fail < 0))
301 return 0;
302
303 if (fail >= 0)
304 return 1;
305
306 if (!TEST_mem_eq(token, token_len,
307 encode_case_7_token, sizeof(encode_case_7_token)))
308 return 0;
309
310 return 1;
311 }
312
313 static const unsigned char encode_case_7_expect[] = {
314 0x07, /* Type */
315 0x10, /* Length */
316 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71, /* Token */
317 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
318 };
319
320 /* 8. STREAM (no length, no offset, no fin) */
321 static const unsigned char encode_case_8_data[] = {
322 0xde, 0x06, 0xcb, 0x76, 0x5d
323 };
324 static const OSSL_QUIC_FRAME_STREAM encode_case_8_f = {
325 0x1234, 0, 5, encode_case_8_data, 0, 0
326 };
327
328 static int encode_case_8_enc(WPACKET *pkt)
329 {
330 if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
331 &encode_case_8_f)))
332 return 0;
333
334 return 1;
335 }
336
337 static int encode_case_8_dec(PACKET *pkt, ossl_ssize_t fail)
338 {
339 OSSL_QUIC_FRAME_STREAM f = {0};
340
341 if (fail >= 3)
342 /*
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.
345 */
346 return 1;
347
348 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, &f), fail < 0))
349 return 0;
350
351 if (fail >= 0)
352 return 1;
353
354 if (!TEST_mem_eq(f.data, f.len,
355 encode_case_8_data, sizeof(encode_case_8_data)))
356 return 0;
357
358 if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
359 return 0;
360
361 if (!TEST_uint64_t_eq(f.offset, 0))
362 return 0;
363
364 if (!TEST_int_eq(f.has_explicit_len, 0))
365 return 0;
366
367 if (!TEST_int_eq(f.is_fin, 0))
368 return 0;
369
370 return 1;
371 }
372
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 */
377 };
378
379 /* 9. STREAM (length, offset, fin) */
380 static const unsigned char encode_case_9_data[] = {
381 0xde, 0x06, 0xcb, 0x76, 0x5d
382 };
383 static const OSSL_QUIC_FRAME_STREAM encode_case_9_f = {
384 0x1234, 0x39, 5, encode_case_9_data, 1, 1
385 };
386
387 static int encode_case_9_enc(WPACKET *pkt)
388 {
389 if (!TEST_ptr(ossl_quic_wire_encode_frame_stream(pkt,
390 &encode_case_9_f)))
391 return 0;
392
393 return 1;
394 }
395
396 static int encode_case_9_dec(PACKET *pkt, ossl_ssize_t fail)
397 {
398 OSSL_QUIC_FRAME_STREAM f = {0};
399
400 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream(pkt, &f), fail < 0))
401 return 0;
402
403 if (fail >= 0)
404 return 1;
405
406 if (!TEST_mem_eq(f.data, f.len,
407 encode_case_9_data, sizeof(encode_case_9_data)))
408 return 0;
409
410 if (!TEST_uint64_t_eq(f.stream_id, 0x1234))
411 return 0;
412
413 if (!TEST_uint64_t_eq(f.offset, 0x39))
414 return 0;
415
416 if (!TEST_int_eq(f.has_explicit_len, 1))
417 return 0;
418
419 if (!TEST_int_eq(f.is_fin, 1))
420 return 0;
421
422 return 1;
423 }
424
425 static const unsigned char encode_case_9_expect[] = {
426 0x0f, /* Type (OFF=1, LEN=1, FIN=1) */
427 0x52, 0x34, /* Stream ID */
428 0x39, /* Offset */
429 0x05, /* Length */
430 0xde, 0x06, 0xcb, 0x76, 0x5d /* Data */
431 };
432
433 /* 10. MAX_DATA */
434 static int encode_case_10_enc(WPACKET *pkt)
435 {
436 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_data(pkt, 0x1234), 1))
437 return 0;
438
439 return 1;
440 }
441
442 static int encode_case_10_dec(PACKET *pkt, ossl_ssize_t fail)
443 {
444 uint64_t max_data = 0;
445
446 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_data(pkt, &max_data), fail < 0))
447 return 0;
448
449 if (fail >= 0)
450 return 1;
451
452 if (!TEST_uint64_t_eq(max_data, 0x1234))
453 return 0;
454
455 return 1;
456 }
457
458 static const unsigned char encode_case_10_expect[] = {
459 0x10, /* Type */
460 0x52, 0x34, /* Max Data */
461 };
462
463 /* 11. MAX_STREAM_DATA */
464 static int encode_case_11_enc(WPACKET *pkt)
465 {
466 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_stream_data(pkt,
467 0x1234,
468 0x9781), 1))
469 return 0;
470
471 return 1;
472 }
473
474 static int encode_case_11_dec(PACKET *pkt, ossl_ssize_t fail)
475 {
476 uint64_t stream_id = 0, max_data = 0;
477
478 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_stream_data(pkt,
479 &stream_id,
480 &max_data), fail < 0))
481 return 0;
482
483 if (fail >= 0)
484 return 1;
485
486 if (!TEST_uint64_t_eq(stream_id, 0x1234))
487 return 0;
488
489 if (!TEST_uint64_t_eq(max_data, 0x9781))
490 return 0;
491
492 return 1;
493 }
494
495 static const unsigned char encode_case_11_expect[] = {
496 0x11, /* Type */
497 0x52, 0x34, /* Stream ID */
498 0x80, 0x00, 0x97, 0x81, /* Max Data */
499 };
500
501 /* 12. MAX_STREAMS */
502 static int encode_case_12_enc(WPACKET *pkt)
503 {
504 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 0, 0x1234), 1))
505 return 0;
506
507 if (!TEST_int_eq(ossl_quic_wire_encode_frame_max_streams(pkt, 1, 0x9781), 1))
508 return 0;
509
510 return 1;
511 }
512
513 static int encode_case_12_dec(PACKET *pkt, ossl_ssize_t fail)
514 {
515 uint64_t max_streams_1 = 0, max_streams_2 = 0,
516 frame_type_1 = 0, frame_type_2 = 0;
517
518 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1),
519 fail < 0 || fail >= 1))
520 return 0;
521
522 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
523 &max_streams_1),
524 fail < 0 || fail >= 3))
525 return 0;
526
527 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2),
528 fail < 0 || fail >= 4))
529 return 0;
530
531 if (!TEST_int_eq(ossl_quic_wire_decode_frame_max_streams(pkt,
532 &max_streams_2),
533 fail < 0))
534 return 0;
535
536 if ((fail < 0 || fail >= 3)
537 && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_BIDI))
538 return 0;
539
540 if ((fail < 0 || fail >= 3)
541 && !TEST_uint64_t_eq(max_streams_1, 0x1234))
542 return 0;
543
544 if ((fail < 0 || fail >= 8)
545 && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_MAX_STREAMS_UNI))
546 return 0;
547
548 if ((fail < 0 || fail >= 8)
549 && !TEST_uint64_t_eq(max_streams_2, 0x9781))
550 return 0;
551
552 return 1;
553 }
554
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 */
560 };
561
562 /* 13. DATA_BLOCKED */
563 static int encode_case_13_enc(WPACKET *pkt)
564 {
565 if (!TEST_int_eq(ossl_quic_wire_encode_frame_data_blocked(pkt, 0x1234), 1))
566 return 0;
567
568 return 1;
569 }
570
571 static int encode_case_13_dec(PACKET *pkt, ossl_ssize_t fail)
572 {
573 uint64_t max_data = 0;
574
575 if (!TEST_int_eq(ossl_quic_wire_decode_frame_data_blocked(pkt,
576 &max_data), fail < 0))
577 return 0;
578
579 if (fail >= 0)
580 return 1;
581
582 if (!TEST_uint64_t_eq(max_data, 0x1234))
583 return 0;
584
585 return 1;
586 }
587
588 static const unsigned char encode_case_13_expect[] = {
589 0x14, /* Type */
590 0x52, 0x34, /* Max Data */
591 };
592
593 /* 14. STREAM_DATA_BLOCKED */
594 static int encode_case_14_enc(WPACKET *pkt)
595 {
596 if (!TEST_int_eq(ossl_quic_wire_encode_frame_stream_data_blocked(pkt,
597 0x1234,
598 0x9781), 1))
599 return 0;
600
601 return 1;
602 }
603
604 static int encode_case_14_dec(PACKET *pkt, ossl_ssize_t fail)
605 {
606 uint64_t stream_id = 0, max_data = 0;
607
608 if (!TEST_int_eq(ossl_quic_wire_decode_frame_stream_data_blocked(pkt,
609 &stream_id,
610 &max_data), fail < 0))
611 return 0;
612
613 if (fail >= 0)
614 return 1;
615
616 if (!TEST_uint64_t_eq(stream_id, 0x1234))
617 return 0;
618
619 if (!TEST_uint64_t_eq(max_data, 0x9781))
620 return 0;
621
622 return 1;
623 }
624
625 static const unsigned char encode_case_14_expect[] = {
626 0x15, /* Type */
627 0x52, 0x34, /* Stream ID */
628 0x80, 0x00, 0x97, 0x81, /* Max Data */
629 };
630
631 /* 15. STREAMS_BLOCKED */
632 static int encode_case_15_enc(WPACKET *pkt)
633 {
634 if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 0, 0x1234), 1))
635 return 0;
636
637 if (!TEST_int_eq(ossl_quic_wire_encode_frame_streams_blocked(pkt, 1, 0x9781), 1))
638 return 0;
639
640 return 1;
641 }
642
643 static int encode_case_15_dec(PACKET *pkt, ossl_ssize_t fail)
644 {
645 uint64_t max_streams_1 = 0, max_streams_2 = 0,
646 frame_type_1 = 0, frame_type_2 = 0;
647
648 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_1),
649 fail < 0 || fail >= 1))
650 return 0;
651
652 if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
653 &max_streams_1),
654 fail < 0 || fail >= 3))
655 return 0;
656
657 if (!TEST_int_eq(ossl_quic_wire_peek_frame_header(pkt, &frame_type_2),
658 fail < 0 || fail >= 4))
659 return 0;
660
661 if (!TEST_int_eq(ossl_quic_wire_decode_frame_streams_blocked(pkt,
662 &max_streams_2),
663 fail < 0 || fail >= 8))
664 return 0;
665
666 if ((fail < 0 || fail >= 1)
667 && !TEST_uint64_t_eq(frame_type_1, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_BIDI))
668 return 0;
669
670 if ((fail < 0 || fail >= 3)
671 && !TEST_uint64_t_eq(max_streams_1, 0x1234))
672 return 0;
673
674 if ((fail < 0 || fail >= 4)
675 && !TEST_uint64_t_eq(frame_type_2, OSSL_QUIC_FRAME_TYPE_STREAMS_BLOCKED_UNI))
676 return 0;
677
678 if ((fail < 0 || fail >= 8)
679 && !TEST_uint64_t_eq(max_streams_2, 0x9781))
680 return 0;
681
682 return 1;
683 }
684
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 */
690 };
691
692 /* 16. NEW_CONNECTION_ID */
693 static const unsigned char encode_case_16_conn_id[] = {
694 0x33, 0x44, 0x55, 0x66
695 };
696
697 static const OSSL_QUIC_FRAME_NEW_CONN_ID encode_case_16_f = {
698 0x1234,
699 0x9781,
700 {
701 0x4,
702 {0x33, 0x44, 0x55, 0x66}
703 },
704 {
705 0xde, 0x06, 0xcb, 0x76, 0x5d, 0xb1, 0xa7, 0x71,
706 0x78, 0x09, 0xbb, 0xe8, 0x50, 0x19, 0x12, 0x9a
707 }
708 };
709
710 static int encode_case_16_enc(WPACKET *pkt)
711 {
712 if (!TEST_int_eq(ossl_quic_wire_encode_frame_new_conn_id(pkt,
713 &encode_case_16_f), 1))
714 return 0;
715
716 return 1;
717 }
718
719 static int encode_case_16_dec(PACKET *pkt, ossl_ssize_t fail)
720 {
721 OSSL_QUIC_FRAME_NEW_CONN_ID f = {0};
722
723 if (!TEST_int_eq(ossl_quic_wire_decode_frame_new_conn_id(pkt, &f), fail < 0))
724 return 0;
725
726 if (fail >= 0)
727 return 1;
728
729 if (!TEST_uint64_t_eq(f.seq_num, 0x1234))
730 return 0;
731
732 if (!TEST_uint64_t_eq(f.retire_prior_to, 0x9781))
733 return 0;
734
735 if (!TEST_uint64_t_eq(f.conn_id.id_len, sizeof(encode_case_16_conn_id)))
736 return 0;
737
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)))
740 return 0;
741
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)))
746 return 0;
747
748 return 1;
749 }
750
751 static const unsigned char encode_case_16_expect[] = {
752 0x18, /* Type */
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
759 };
760
761 /* 17. RETIRE_CONNECTION_ID */
762 static int encode_case_17_enc(WPACKET *pkt)
763 {
764 if (!TEST_int_eq(ossl_quic_wire_encode_frame_retire_conn_id(pkt, 0x1234), 1))
765 return 0;
766
767 return 1;
768 }
769
770 static int encode_case_17_dec(PACKET *pkt, ossl_ssize_t fail)
771 {
772 uint64_t seq_num = 0;
773
774 if (!TEST_int_eq(ossl_quic_wire_decode_frame_retire_conn_id(pkt, &seq_num), fail < 0))
775 return 0;
776
777 if (fail >= 0)
778 return 1;
779
780 if (!TEST_uint64_t_eq(seq_num, 0x1234))
781 return 0;
782
783 return 1;
784 }
785
786 static const unsigned char encode_case_17_expect[] = {
787 0x19, /* Type */
788 0x52, 0x34, /* Seq Num */
789 };
790
791 /* 18. PATH_CHALLENGE */
792 static const uint64_t encode_case_18_data
793 = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
794
795 static int encode_case_18_enc(WPACKET *pkt)
796 {
797 if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_challenge(pkt,
798 encode_case_18_data), 1))
799 return 0;
800
801 return 1;
802 }
803
804 static int encode_case_18_dec(PACKET *pkt, ossl_ssize_t fail)
805 {
806 uint64_t challenge = 0;
807
808 if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_challenge(pkt, &challenge), fail < 0))
809 return 0;
810
811 if (fail >= 0)
812 return 1;
813
814 if (!TEST_uint64_t_eq(challenge, encode_case_18_data))
815 return 0;
816
817 return 1;
818 }
819
820 static const unsigned char encode_case_18_expect[] = {
821 0x1A, /* Type */
822 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
823 };
824
825 /* 19. PATH_RESPONSE */
826 static const uint64_t encode_case_19_data
827 = (((uint64_t)0x5f4b12)<<40) | (uint64_t)0x731834UL;
828
829 static int encode_case_19_enc(WPACKET *pkt)
830 {
831 if (!TEST_int_eq(ossl_quic_wire_encode_frame_path_response(pkt,
832 encode_case_19_data), 1))
833 return 0;
834
835 return 1;
836 }
837
838 static int encode_case_19_dec(PACKET *pkt, ossl_ssize_t fail)
839 {
840 uint64_t challenge = 0;
841
842 if (!TEST_int_eq(ossl_quic_wire_decode_frame_path_response(pkt, &challenge), fail < 0))
843 return 0;
844
845 if (fail >= 0)
846 return 1;
847
848 if (!TEST_uint64_t_eq(challenge, encode_case_19_data))
849 return 0;
850
851 return 1;
852 }
853
854 static const unsigned char encode_case_19_expect[] = {
855 0x1B, /* Type */
856 0x5f, 0x4b, 0x12, 0x00, 0x00, 0x73, 0x18, 0x34, /* Data */
857 };
858
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
864 };
865
866 static const OSSL_QUIC_FRAME_CONN_CLOSE encode_case_20_f = {
867 0,
868 0x1234,
869 0x9781,
870 encode_case_20_reason,
871 sizeof(encode_case_20_reason)
872 };
873
874 static int encode_case_20_enc(WPACKET *pkt)
875 {
876 if (!TEST_int_eq(ossl_quic_wire_encode_frame_conn_close(pkt,
877 &encode_case_20_f), 1))
878 return 0;
879
880 return 1;
881 }
882
883 static int encode_case_20_dec(PACKET *pkt, ossl_ssize_t fail)
884 {
885 OSSL_QUIC_FRAME_CONN_CLOSE f = {0};
886
887 if (!TEST_int_eq(ossl_quic_wire_decode_frame_conn_close(pkt, &f), fail < 0))
888 return 0;
889
890 if (fail >= 0)
891 return 1;
892
893 if (!TEST_int_eq(f.is_app, 0))
894 return 0;
895
896 if (!TEST_uint64_t_eq(f.error_code, 0x1234))
897 return 0;
898
899 if (!TEST_uint64_t_eq(f.frame_type, 0x9781))
900 return 0;
901
902 if (!TEST_size_t_eq(f.reason_len, 18))
903 return 0;
904
905 if (!TEST_mem_eq(f.reason, f.reason_len,
906 encode_case_20_f.reason, encode_case_20_f.reason_len))
907 return 0;
908
909 return 1;
910 }
911
912 static const unsigned char encode_case_20_expect[] = {
913 0x1C, /* Type */
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
919 };
920
921 /* 21. HANDSHAKE_DONE */
922 static int encode_case_21_enc(WPACKET *pkt)
923 {
924
925 if (!TEST_int_eq(ossl_quic_wire_encode_frame_handshake_done(pkt), 1))
926 return 0;
927
928 return 1;
929 }
930
931 static int encode_case_21_dec(PACKET *pkt, ossl_ssize_t fail)
932 {
933
934 if (!TEST_int_eq(ossl_quic_wire_decode_frame_handshake_done(pkt), fail < 0))
935 return 0;
936
937 return 1;
938 }
939
940 static const unsigned char encode_case_21_expect[] = {
941 0x1E
942 };
943
944 /* 22. Buffer Transport Parameter */
945 static const unsigned char encode_case_22_data[] = {0x55,0x77,0x32,0x46,0x99};
946
947 static int encode_case_22_enc(WPACKET *pkt)
948 {
949 unsigned char *p;
950
951 if (!TEST_ptr(ossl_quic_wire_encode_transport_param_bytes(pkt, 0x1234,
952 encode_case_22_data,
953 sizeof(encode_case_22_data))))
954 return 0;
955
956 if (!TEST_ptr(p = ossl_quic_wire_encode_transport_param_bytes(pkt, 0x9781,
957 NULL, 2)))
958 return 0;
959
960 p[0] = 0x33;
961 p[1] = 0x44;
962
963 return 1;
964 }
965
966 static int encode_case_22_dec(PACKET *pkt, ossl_ssize_t fail)
967 {
968 uint64_t id = 0;
969 size_t len = 0;
970 const unsigned char *p;
971 static const unsigned char data[] = {0x33, 0x44};
972
973 if (!TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
974 fail < 0 || fail >= 2))
975 return 0;
976
977 if ((fail < 0 || fail >= 2)
978 && !TEST_uint64_t_eq(id, 0x1234))
979 return 0;
980
981 id = 0;
982
983 p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
984 if (fail < 0 || fail >= 8) {
985 if (!TEST_ptr(p))
986 return 0;
987 } else {
988 if (!TEST_ptr_null(p))
989 return 0;
990 }
991
992 if ((fail < 0 || fail >= 8)
993 && !TEST_uint64_t_eq(id, 0x1234))
994 return 0;
995
996 if ((fail < 0 || fail >= 8)
997 && !TEST_mem_eq(p, len, encode_case_22_data, sizeof(encode_case_22_data)))
998 return 0;
999
1000 if ((fail < 0 || fail >= 8)
1001 && !TEST_int_eq(ossl_quic_wire_peek_transport_param(pkt, &id),
1002 fail < 0 || fail >= 12))
1003 return 0;
1004
1005 if ((fail < 0 || fail >= 12)
1006 && !TEST_uint64_t_eq(id, 0x9781))
1007 return 0;
1008
1009 id = 0;
1010
1011 p = ossl_quic_wire_decode_transport_param_bytes(pkt, &id, &len);
1012 if (fail < 0 || fail >= 15) {
1013 if (!TEST_ptr(p))
1014 return 0;
1015 } else {
1016 if (!TEST_ptr_null(p))
1017 return 0;
1018 }
1019
1020 if ((fail < 0 || fail >= 15)
1021 && !TEST_uint64_t_eq(id, 0x9781))
1022 return 0;
1023
1024 if ((fail < 0 || fail >= 15)
1025 && !TEST_mem_eq(p, len, data, sizeof(data)))
1026 return 0;
1027
1028 return 1;
1029 }
1030
1031 static const unsigned char encode_case_22_expect[] = {
1032 0x52, 0x34, /* ID */
1033 0x05, /* Length */
1034 0x55, 0x77, 0x32, 0x46, 0x99, /* Data */
1035
1036 0x80, 0x00, 0x97, 0x81, /* ID */
1037 0x02, /* Length */
1038 0x33, 0x44 /* Data */
1039 };
1040
1041 /* 23. Integer Transport Parameter */
1042 static int encode_case_23_enc(WPACKET *pkt)
1043 {
1044 if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x1234, 0x9781), 1))
1045 return 0;
1046
1047 if (!TEST_int_eq(ossl_quic_wire_encode_transport_param_int(pkt, 0x2233, 0x4545), 1))
1048 return 0;
1049
1050 return 1;
1051 }
1052
1053 static int encode_case_23_dec(PACKET *pkt, ossl_ssize_t fail)
1054 {
1055 uint64_t id = 0, value = 0;
1056
1057 if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
1058 &id, &value),
1059 fail < 0 || fail >= 7))
1060 return 0;
1061
1062 if ((fail < 0 || fail >= 7)
1063 && !TEST_uint64_t_eq(id, 0x1234))
1064 return 0;
1065
1066 if ((fail < 0 || fail >= 7)
1067 && !TEST_uint64_t_eq(value, 0x9781))
1068 return 0;
1069
1070 if (!TEST_int_eq(ossl_quic_wire_decode_transport_param_int(pkt,
1071 &id, &value),
1072 fail < 0 || fail >= 14))
1073 return 0;
1074
1075 if ((fail < 0 || fail >= 14)
1076 && !TEST_uint64_t_eq(id, 0x2233))
1077 return 0;
1078
1079 if ((fail < 0 || fail >= 14)
1080 && !TEST_uint64_t_eq(value, 0x4545))
1081 return 0;
1082
1083 return 1;
1084 }
1085
1086 static const unsigned char encode_case_23_expect[] = {
1087 0x52, 0x34,
1088 0x04,
1089 0x80, 0x00, 0x97, 0x81,
1090
1091 0x62, 0x33,
1092 0x04,
1093 0x80, 0x00, 0x45, 0x45,
1094 };
1095
1096 #define ENCODE_CASE(n) \
1097 { \
1098 encode_case_##n##_enc, \
1099 encode_case_##n##_expect, \
1100 OSSL_NELEM(encode_case_##n##_expect), \
1101 encode_case_##n##_dec \
1102 },
1103
1104 static const struct encode_test_case encode_cases[] = {
1105 ENCODE_CASE(1)
1106 ENCODE_CASE(2)
1107 ENCODE_CASE(3)
1108 ENCODE_CASE(4)
1109 ENCODE_CASE(5)
1110 ENCODE_CASE(6)
1111 ENCODE_CASE(7)
1112 ENCODE_CASE(8)
1113 ENCODE_CASE(9)
1114 ENCODE_CASE(10)
1115 ENCODE_CASE(11)
1116 ENCODE_CASE(12)
1117 ENCODE_CASE(13)
1118 ENCODE_CASE(14)
1119 ENCODE_CASE(15)
1120 ENCODE_CASE(16)
1121 ENCODE_CASE(17)
1122 ENCODE_CASE(18)
1123 ENCODE_CASE(19)
1124 ENCODE_CASE(20)
1125 ENCODE_CASE(21)
1126 ENCODE_CASE(22)
1127 ENCODE_CASE(23)
1128 };
1129
1130 static int test_wire_encode(int idx)
1131 {
1132 int testresult = 0;
1133 WPACKET wpkt;
1134 PACKET pkt;
1135 BUF_MEM *buf = NULL;
1136 size_t written;
1137 const struct encode_test_case *c = &encode_cases[idx];
1138 int have_wpkt = 0;
1139 size_t i;
1140
1141 if (!TEST_ptr(buf = BUF_MEM_new()))
1142 goto err;
1143
1144 if (!TEST_int_eq(WPACKET_init(&wpkt, buf), 1))
1145 goto err;
1146
1147 have_wpkt = 1;
1148 if (!TEST_int_eq(c->serializer(&wpkt), 1))
1149 goto err;
1150
1151 if (!TEST_int_eq(WPACKET_get_total_written(&wpkt, &written), 1))
1152 goto err;
1153
1154 if (!TEST_mem_eq(buf->data, written, c->expect_buf, c->expect_buf_len))
1155 goto err;
1156
1157 if (!TEST_int_eq(PACKET_buf_init(&pkt, (unsigned char *)buf->data, written), 1))
1158 goto err;
1159
1160 if (!TEST_int_eq(c->deserializer(&pkt, -1), 1))
1161 goto err;
1162
1163 if (!TEST_false(PACKET_remaining(&pkt)))
1164 goto err;
1165
1166 for (i = 0; i < c->expect_buf_len; ++i) {
1167 PACKET pkt2;
1168
1169 /*
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.
1175 */
1176 if (!TEST_int_eq(PACKET_buf_init(&pkt2, (unsigned char *)c->expect_buf, i), 1))
1177 goto err;
1178
1179 if (!TEST_int_eq(c->deserializer(&pkt2, i), 1))
1180 goto err;
1181 }
1182
1183 testresult = 1;
1184 err:
1185 if (have_wpkt)
1186 WPACKET_finish(&wpkt);
1187 BUF_MEM_free(buf);
1188 return testresult;
1189 }
1190
1191 struct ack_test_case {
1192 const unsigned char *input_buf;
1193 size_t input_buf_len;
1194 int (*deserializer)(PACKET *pkt);
1195 int expect_fail;
1196 };
1197
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 */
1205 };
1206
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 */
1214 };
1215
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 */
1223
1224 0x05, /* Gap */
1225 0x01, /* ACK Range Length */
1226 };
1227
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 */
1235
1236 0x04, /* Gap */
1237 0x01, /* ACK Range Length */
1238 };
1239
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 */
1247
1248 0x04, /* Gap */
1249 0x02, /* ACK Range Length */
1250 };
1251
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 */
1259
1260 0x01, /* Gap */
1261 0x02, /* ACK Range Length */
1262
1263 0x00, /* Gap */
1264 0x01, /* ACK Range Length */
1265 };
1266
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 */
1274
1275 0x01, /* Gap */
1276 0x02, /* ACK Range Length */
1277
1278 0x00, /* Gap */
1279 0x00, /* ACK Range Length */
1280 };
1281
1282 static int ack_generic_decode(PACKET *pkt)
1283 {
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;
1287 int r;
1288 size_t i;
1289
1290 f.ack_ranges = ranges;
1291 f.num_ack_ranges = OSSL_NELEM(ranges);
1292
1293 if (!TEST_int_eq(ossl_quic_wire_peek_frame_ack_num_ranges(pkt,
1294 &peek_total_ranges), 1))
1295 return 0;
1296
1297 r = ossl_quic_wire_decode_frame_ack(pkt, 3, &f, &total_ranges);
1298 if (r == 0)
1299 return 0;
1300
1301 if (!TEST_uint64_t_eq(total_ranges, peek_total_ranges))
1302 return 0;
1303
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))
1306 return 0;
1307 if (!TEST_uint64_t_lt(f.ack_ranges[i].end, 1000))
1308 return 0;
1309 }
1310
1311 return 1;
1312 }
1313
1314 #define ACK_CASE(n, expect_fail, dec) \
1315 { \
1316 ack_case_##n##_input, \
1317 sizeof(ack_case_##n##_input), \
1318 (dec), \
1319 (expect_fail) \
1320 },
1321
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)
1330 };
1331
1332 static int test_wire_ack(int idx)
1333 {
1334 int testresult = 0, r;
1335 PACKET pkt;
1336 const struct ack_test_case *c = &ack_cases[idx];
1337
1338 if (!TEST_int_eq(PACKET_buf_init(&pkt,
1339 (unsigned char *)c->input_buf,
1340 c->input_buf_len), 1))
1341 goto err;
1342
1343 r = c->deserializer(&pkt);
1344 if (c->expect_fail) {
1345 if (!TEST_int_eq(r, 0))
1346 goto err;
1347 } else {
1348 if (!TEST_int_eq(r, 1))
1349 goto err;
1350
1351 if (!TEST_false(PACKET_remaining(&pkt)))
1352 goto err;
1353 }
1354
1355 testresult = 1;
1356 err:
1357 return testresult;
1358 }
1359
1360 int setup_tests(void)
1361 {
1362 ADD_ALL_TESTS(test_wire_encode, OSSL_NELEM(encode_cases));
1363 ADD_ALL_TESTS(test_wire_ack, OSSL_NELEM(ack_cases));
1364 return 1;
1365 }