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