2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/buffer.h>
12 #include <openssl/rand.h>
13 #include "internal/packet.h"
16 static const unsigned char simple1
[] = { 0xff };
17 static const unsigned char simple2
[] = { 0x01, 0xff };
18 static const unsigned char simple3
[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
19 static const unsigned char nestedsub
[] = { 0x03, 0xff, 0x01, 0xff };
20 static const unsigned char seqsub
[] = { 0x01, 0xff, 0x01, 0xff };
21 static const unsigned char empty
[] = { 0x00 };
22 static const unsigned char alloc
[] = { 0x02, 0xfe, 0xff };
23 static const unsigned char submem
[] = { 0x03, 0x02, 0xfe, 0xff };
24 static const unsigned char fixed
[] = { 0xff, 0xff, 0xff };
25 static const unsigned char simpleder
[] = {
26 0xfc, 0x04, 0x00, 0x01, 0x02, 0x03, 0xff, 0xfe, 0xfd
29 #ifndef OPENSSL_NO_QUIC
31 /* QUIC sub-packet with 4-byte length prefix, containing a 1-byte vlint */
32 static const unsigned char quic1
[] = { 0x80, 0x00, 0x00, 0x01, 0x09 };
33 /* QUIC sub-packet with 1-byte length prefix, containing a 1-byte vlint */
34 static const unsigned char quic2
[] = { 0x01, 0x09 };
35 /* QUIC sub-packet with 2-byte length prefix, containing a 2-byte vlint */
36 static const unsigned char quic3
[] = { 0x40, 0x02, 0x40, 0x41 };
37 /* QUIC sub-packet with 8-byte length prefix, containing a 4-byte vlint */
38 static const unsigned char quic4
[] = {
39 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
40 0x80, 0x01, 0x3c, 0x6a
42 /* QUIC sub-packet with 8-byte length prefix, containing a 8-byte vlint */
43 static const unsigned char quic5
[] = {
44 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
45 0xef, 0x77, 0x21, 0x3f, 0x3f, 0x50, 0x5b, 0xa5
47 /* QUIC sub-packet, length known up-front */
48 static const unsigned char quic6
[] = { 0x03, 0x55, 0x66, 0x77 };
49 /* Nested and sequential sub-packets with length prefixes */
50 static const unsigned char quic7
[] = {
51 0x07, 0x80, 0x00, 0x00, 0x08, 0x65, 0x14, 0x40, 0x01, 0x05,
52 0x40, 0x01, 0x11, 0x40, 0x01, 0x12, 0x40, 0x01, 0x13
59 static int cleanup(WPACKET
*pkt
)
65 static int test_WPACKET_init(void)
70 unsigned char sbuf
[3];
72 if (!TEST_true(WPACKET_init(&pkt
, buf
))
73 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
74 /* Closing a top level WPACKET should fail */
75 || !TEST_false(WPACKET_close(&pkt
))
76 /* Finishing a top level WPACKET should succeed */
77 || !TEST_true(WPACKET_finish(&pkt
))
79 * Can't call close or finish on a WPACKET that's already
82 || !TEST_false(WPACKET_close(&pkt
))
83 || !TEST_false(WPACKET_finish(&pkt
))
84 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
85 || !TEST_mem_eq(buf
->data
, written
, simple1
, sizeof(simple1
)))
88 /* Now try with a one byte length prefix */
89 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 1))
90 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
91 || !TEST_true(WPACKET_finish(&pkt
))
92 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
93 || !TEST_mem_eq(buf
->data
, written
, simple2
, sizeof(simple2
)))
96 /* And a longer length prefix */
97 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 4))
98 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
99 || !TEST_true(WPACKET_finish(&pkt
))
100 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
101 || !TEST_mem_eq(buf
->data
, written
, simple3
, sizeof(simple3
)))
102 return cleanup(&pkt
);
104 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 1)))
105 return cleanup(&pkt
);
106 for (i
= 1; i
< 257; i
++) {
108 * Putting more bytes in than fit for the size of the length prefix
111 if (!TEST_int_eq(WPACKET_put_bytes_u8(&pkt
, 0xff), i
< 256))
112 return cleanup(&pkt
);
114 if (!TEST_true(WPACKET_finish(&pkt
)))
115 return cleanup(&pkt
);
117 /* Test initialising from a fixed size buffer */
118 if (!TEST_true(WPACKET_init_static_len(&pkt
, sbuf
, sizeof(sbuf
), 0))
119 /* Adding 3 bytes should succeed */
120 || !TEST_true(WPACKET_put_bytes_u24(&pkt
, 0xffffff))
121 /* Adding 1 more byte should fail */
122 || !TEST_false(WPACKET_put_bytes_u8(&pkt
, 0xff))
123 /* Finishing the top level WPACKET should succeed */
124 || !TEST_true(WPACKET_finish(&pkt
))
125 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
126 || !TEST_mem_eq(sbuf
, written
, fixed
, sizeof(sbuf
))
127 /* Initialise with 1 len byte */
128 || !TEST_true(WPACKET_init_static_len(&pkt
, sbuf
, sizeof(sbuf
), 1))
129 /* Adding 2 bytes should succeed */
130 || !TEST_true(WPACKET_put_bytes_u16(&pkt
, 0xfeff))
131 /* Adding 1 more byte should fail */
132 || !TEST_false(WPACKET_put_bytes_u8(&pkt
, 0xff))
133 || !TEST_true(WPACKET_finish(&pkt
))
134 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
135 || !TEST_mem_eq(sbuf
, written
, alloc
, sizeof(alloc
)))
136 return cleanup(&pkt
);
141 static int test_WPACKET_set_max_size(void)
146 if (!TEST_true(WPACKET_init(&pkt
, buf
))
148 * No previous lenbytes set so we should be ok to set the max
151 || !TEST_true(WPACKET_set_max_size(&pkt
, SIZE_MAX
))
152 /* We should be able to set it smaller too */
153 || !TEST_true(WPACKET_set_max_size(&pkt
, SIZE_MAX
-1))
154 /* And setting it bigger again should be ok */
155 || !TEST_true(WPACKET_set_max_size(&pkt
, SIZE_MAX
))
156 || !TEST_true(WPACKET_finish(&pkt
)))
157 return cleanup(&pkt
);
159 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 1))
161 * Should fail because we already consumed 1 byte with the
164 || !TEST_false(WPACKET_set_max_size(&pkt
, 0))
166 * Max size can't be bigger than biggest that will fit in
169 || !TEST_false(WPACKET_set_max_size(&pkt
, 0x0101))
170 /* It can be the same as the maximum possible size */
171 || !TEST_true(WPACKET_set_max_size(&pkt
, 0x0100))
172 /* Or it can be less */
173 || !TEST_true(WPACKET_set_max_size(&pkt
, 0x01))
174 /* Should fail because packet is already filled */
175 || !TEST_false(WPACKET_put_bytes_u8(&pkt
, 0xff))
176 /* You can't put in more bytes than max size */
177 || !TEST_true(WPACKET_set_max_size(&pkt
, 0x02))
178 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
179 || !TEST_false(WPACKET_put_bytes_u8(&pkt
, 0xff))
180 || !TEST_true(WPACKET_finish(&pkt
))
181 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
182 || !TEST_mem_eq(buf
->data
, written
, simple2
, sizeof(simple2
)))
183 return cleanup(&pkt
);
188 static int test_WPACKET_start_sub_packet(void)
194 if (!TEST_true(WPACKET_init(&pkt
, buf
))
195 || !TEST_true(WPACKET_start_sub_packet(&pkt
))
196 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
197 /* Can't finish because we have a sub packet */
198 || !TEST_false(WPACKET_finish(&pkt
))
199 || !TEST_true(WPACKET_close(&pkt
))
200 /* Sub packet is closed so can't close again */
201 || !TEST_false(WPACKET_close(&pkt
))
202 /* Now a top level so finish should succeed */
203 || !TEST_true(WPACKET_finish(&pkt
))
204 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
205 || !TEST_mem_eq(buf
->data
, written
, simple1
, sizeof(simple1
)))
206 return cleanup(&pkt
);
208 /* Single sub-packet with length prefix */
209 if (!TEST_true(WPACKET_init(&pkt
, buf
))
210 || !TEST_true(WPACKET_start_sub_packet_u8(&pkt
))
211 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
212 || !TEST_true(WPACKET_close(&pkt
))
213 || !TEST_true(WPACKET_finish(&pkt
))
214 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
215 || !TEST_mem_eq(buf
->data
, written
, simple2
, sizeof(simple2
)))
216 return cleanup(&pkt
);
218 /* Nested sub-packets with length prefixes */
219 if (!TEST_true(WPACKET_init(&pkt
, buf
))
220 || !TEST_true(WPACKET_start_sub_packet_u8(&pkt
))
221 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
222 || !TEST_true(WPACKET_start_sub_packet_u8(&pkt
))
223 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
224 || !TEST_true(WPACKET_get_length(&pkt
, &len
))
225 || !TEST_size_t_eq(len
, 1)
226 || !TEST_true(WPACKET_close(&pkt
))
227 || !TEST_true(WPACKET_get_length(&pkt
, &len
))
228 || !TEST_size_t_eq(len
, 3)
229 || !TEST_true(WPACKET_close(&pkt
))
230 || !TEST_true(WPACKET_finish(&pkt
))
231 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
232 || !TEST_mem_eq(buf
->data
, written
, nestedsub
, sizeof(nestedsub
)))
233 return cleanup(&pkt
);
235 /* Sequential sub-packets with length prefixes */
236 if (!TEST_true(WPACKET_init(&pkt
, buf
))
237 || !TEST_true(WPACKET_start_sub_packet_u8(&pkt
))
238 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
239 || !TEST_true(WPACKET_close(&pkt
))
240 || !TEST_true(WPACKET_start_sub_packet_u8(&pkt
))
241 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
242 || !TEST_true(WPACKET_close(&pkt
))
243 || !TEST_true(WPACKET_finish(&pkt
))
244 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
245 || !TEST_mem_eq(buf
->data
, written
, seqsub
, sizeof(seqsub
)))
246 return cleanup(&pkt
);
248 /* Nested sub-packets with lengths filled before finish */
249 if (!TEST_true(WPACKET_init(&pkt
, buf
))
250 || !TEST_true(WPACKET_start_sub_packet_u8(&pkt
))
251 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
252 || !TEST_true(WPACKET_start_sub_packet_u8(&pkt
))
253 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
254 || !TEST_true(WPACKET_get_length(&pkt
, &len
))
255 || !TEST_size_t_eq(len
, 1)
256 || !TEST_true(WPACKET_close(&pkt
))
257 || !TEST_true(WPACKET_get_length(&pkt
, &len
))
258 || !TEST_size_t_eq(len
, 3)
259 || !TEST_true(WPACKET_close(&pkt
))
260 || !TEST_true(WPACKET_fill_lengths(&pkt
))
261 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
262 || !TEST_mem_eq(buf
->data
, written
, nestedsub
, sizeof(nestedsub
))
263 || !TEST_true(WPACKET_finish(&pkt
)))
264 return cleanup(&pkt
);
270 static int test_WPACKET_set_flags(void)
275 /* Set packet to be non-zero length */
276 if (!TEST_true(WPACKET_init(&pkt
, buf
))
277 || !TEST_true(WPACKET_set_flags(&pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
))
278 /* Should fail because of zero length */
279 || !TEST_false(WPACKET_finish(&pkt
))
280 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
281 || !TEST_true(WPACKET_finish(&pkt
))
282 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
283 || !TEST_mem_eq(buf
->data
, written
, simple1
, sizeof(simple1
)))
284 return cleanup(&pkt
);
286 /* Repeat above test in a sub-packet */
287 if (!TEST_true(WPACKET_init(&pkt
, buf
))
288 || !TEST_true(WPACKET_start_sub_packet(&pkt
))
289 || !TEST_true(WPACKET_set_flags(&pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
))
290 /* Should fail because of zero length */
291 || !TEST_false(WPACKET_close(&pkt
))
292 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
293 || !TEST_true(WPACKET_close(&pkt
))
294 || !TEST_true(WPACKET_finish(&pkt
))
295 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
296 || !TEST_mem_eq(buf
->data
, written
, simple1
, sizeof(simple1
)))
297 return cleanup(&pkt
);
299 /* Set packet to abandon non-zero length */
300 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 1))
301 || !TEST_true(WPACKET_set_flags(&pkt
, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
))
302 || !TEST_true(WPACKET_finish(&pkt
))
303 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
304 || !TEST_size_t_eq(written
, 0))
305 return cleanup(&pkt
);
307 /* Repeat above test but only abandon a sub-packet */
308 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 1))
309 || !TEST_true(WPACKET_start_sub_packet_u8(&pkt
))
310 || !TEST_true(WPACKET_set_flags(&pkt
, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
))
311 || !TEST_true(WPACKET_close(&pkt
))
312 || !TEST_true(WPACKET_finish(&pkt
))
313 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
314 || !TEST_mem_eq(buf
->data
, written
, empty
, sizeof(empty
)))
315 return cleanup(&pkt
);
317 /* And repeat with a non empty sub-packet */
318 if (!TEST_true(WPACKET_init(&pkt
, buf
))
319 || !TEST_true(WPACKET_start_sub_packet_u8(&pkt
))
320 || !TEST_true(WPACKET_set_flags(&pkt
, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
))
321 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xff))
322 || !TEST_true(WPACKET_close(&pkt
))
323 || !TEST_true(WPACKET_finish(&pkt
))
324 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
325 || !TEST_mem_eq(buf
->data
, written
, simple2
, sizeof(simple2
)))
326 return cleanup(&pkt
);
330 static int test_WPACKET_allocate_bytes(void)
334 unsigned char *bytes
;
336 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 1))
337 || !TEST_true(WPACKET_allocate_bytes(&pkt
, 2, &bytes
)))
338 return cleanup(&pkt
);
341 if (!TEST_true(WPACKET_finish(&pkt
))
342 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
343 || !TEST_mem_eq(buf
->data
, written
, alloc
, sizeof(alloc
)))
344 return cleanup(&pkt
);
346 /* Repeat with WPACKET_sub_allocate_bytes */
347 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 1))
348 || !TEST_true(WPACKET_sub_allocate_bytes_u8(&pkt
, 2, &bytes
)))
349 return cleanup(&pkt
);
352 if (!TEST_true(WPACKET_finish(&pkt
))
353 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
354 || !TEST_mem_eq(buf
->data
, written
, submem
, sizeof(submem
)))
355 return cleanup(&pkt
);
360 static int test_WPACKET_memcpy(void)
364 const unsigned char bytes
[] = { 0xfe, 0xff };
366 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 1))
367 || !TEST_true(WPACKET_memcpy(&pkt
, bytes
, sizeof(bytes
)))
368 || !TEST_true(WPACKET_finish(&pkt
))
369 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
370 || !TEST_mem_eq(buf
->data
, written
, alloc
, sizeof(alloc
)))
371 return cleanup(&pkt
);
373 /* Repeat with WPACKET_sub_memcpy() */
374 if (!TEST_true(WPACKET_init_len(&pkt
, buf
, 1))
375 || !TEST_true(WPACKET_sub_memcpy_u8(&pkt
, bytes
, sizeof(bytes
)))
376 || !TEST_true(WPACKET_finish(&pkt
))
377 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
378 || !TEST_mem_eq(buf
->data
, written
, submem
, sizeof(submem
)))
379 return cleanup(&pkt
);
384 static int test_WPACKET_init_der(void)
387 unsigned char sbuf
[1024];
388 unsigned char testdata
[] = { 0x00, 0x01, 0x02, 0x03 };
389 unsigned char testdata2
[259] = { 0x82, 0x01, 0x00 };
392 int flags
= WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
;
395 /* Test initialising for writing DER */
396 if (!TEST_true(WPACKET_init_der(&pkt
, sbuf
, sizeof(sbuf
)))
397 || !TEST_true(WPACKET_put_bytes_u24(&pkt
, 0xfffefd))
398 /* Test writing data in a length prefixed sub-packet */
399 || !TEST_true(WPACKET_start_sub_packet(&pkt
))
400 || !TEST_true(WPACKET_memcpy(&pkt
, testdata
, sizeof(testdata
)))
401 || !TEST_true(WPACKET_close(&pkt
))
402 || !TEST_true(WPACKET_put_bytes_u8(&pkt
, 0xfc))
403 /* this sub-packet is empty, and should render zero bytes */
404 || (!TEST_true(WPACKET_start_sub_packet(&pkt
))
405 || !TEST_true(WPACKET_set_flags(&pkt
, flags
))
406 || !TEST_true(WPACKET_get_total_written(&pkt
, &size1
))
407 || !TEST_true(WPACKET_close(&pkt
))
408 || !TEST_true(WPACKET_get_total_written(&pkt
, &size2
))
409 || !TEST_size_t_eq(size1
, size2
))
410 || !TEST_true(WPACKET_finish(&pkt
))
411 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
[0]))
412 || !TEST_mem_eq(WPACKET_get_curr(&pkt
), written
[0], simpleder
,
414 return cleanup(&pkt
);
416 /* Generate random packet data for test */
417 if (!TEST_int_gt(RAND_bytes(&testdata2
[3], sizeof(testdata2
) - 3), 0))
421 * Test with a sub-packet that has 2 length bytes. We do 2 passes - first
422 * with a NULL buffer, just to calculate lengths, and a second pass with a
423 * real buffer to actually generate a packet
425 for (i
= 0; i
< 2; i
++) {
427 if (!TEST_true(WPACKET_init_null_der(&pkt
)))
430 if (!TEST_true(WPACKET_init_der(&pkt
, sbuf
, sizeof(sbuf
))))
433 if (!TEST_true(WPACKET_start_sub_packet(&pkt
))
434 || !TEST_true(WPACKET_memcpy(&pkt
, &testdata2
[3],
435 sizeof(testdata2
) - 3))
436 || !TEST_true(WPACKET_close(&pkt
))
437 || !TEST_true(WPACKET_finish(&pkt
))
438 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
[i
])))
439 return cleanup(&pkt
);
443 * Check that the size calculated in the first pass equals the size of the
444 * packet actually generated in the second pass. Also check the generated
445 * packet looks as we expect it to.
447 if (!TEST_size_t_eq(written
[0], written
[1])
448 || !TEST_mem_eq(WPACKET_get_curr(&pkt
), written
[1], testdata2
,
455 #ifndef OPENSSL_NO_QUIC
457 static int test_WPACKET_quic(void)
461 unsigned char *bytes
;
463 /* QUIC sub-packet with 4-byte length prefix, containing a 1-byte vlint */
464 if (!TEST_true(WPACKET_init(&pkt
, buf
))
465 || !TEST_true(WPACKET_start_quic_sub_packet(&pkt
))
466 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x09))
467 /* Can't finish because we have a sub packet */
468 || !TEST_false(WPACKET_finish(&pkt
))
469 || !TEST_true(WPACKET_close(&pkt
))
470 /* Sub packet is closed so can't close again */
471 || !TEST_false(WPACKET_close(&pkt
))
472 /* Now a top level so finish should succeed */
473 || !TEST_true(WPACKET_finish(&pkt
))
474 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
475 || !TEST_mem_eq(buf
->data
, written
, quic1
, sizeof(quic1
)))
476 return cleanup(&pkt
);
478 /* QUIC sub-packet with 1-byte length prefix, containing a 1-byte vlint */
479 if (!TEST_true(WPACKET_init(&pkt
, buf
))
480 || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt
, OSSL_QUIC_VLINT_1B_MAX
))
481 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x09))
482 || !TEST_false(WPACKET_finish(&pkt
))
483 || !TEST_true(WPACKET_close(&pkt
))
484 || !TEST_false(WPACKET_close(&pkt
))
485 || !TEST_true(WPACKET_finish(&pkt
))
486 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
487 || !TEST_mem_eq(buf
->data
, written
, quic2
, sizeof(quic2
)))
488 return cleanup(&pkt
);
490 /* QUIC sub-packet with 2-byte length prefix, containing a 2-byte vlint */
491 if (!TEST_true(WPACKET_init(&pkt
, buf
))
492 || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt
, OSSL_QUIC_VLINT_2B_MIN
))
493 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x41))
494 || !TEST_false(WPACKET_finish(&pkt
))
495 || !TEST_true(WPACKET_close(&pkt
))
496 || !TEST_false(WPACKET_close(&pkt
))
497 || !TEST_true(WPACKET_finish(&pkt
))
498 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
499 || !TEST_mem_eq(buf
->data
, written
, quic3
, sizeof(quic3
)))
500 return cleanup(&pkt
);
502 /* QUIC sub-packet with 8-byte length prefix, containing a 4-byte vlint */
503 if (!TEST_true(WPACKET_init(&pkt
, buf
))
504 || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt
, OSSL_QUIC_VLINT_8B_MIN
))
505 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x13c6a))
506 || !TEST_false(WPACKET_finish(&pkt
))
507 || !TEST_true(WPACKET_close(&pkt
))
508 || !TEST_false(WPACKET_close(&pkt
))
509 || !TEST_true(WPACKET_finish(&pkt
))
510 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
511 || !TEST_mem_eq(buf
->data
, written
, quic4
, sizeof(quic4
)))
512 return cleanup(&pkt
);
514 /* QUIC sub-packet with 8-byte length prefix, containing a 8-byte vlint */
515 if (!TEST_true(WPACKET_init(&pkt
, buf
))
516 || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt
, OSSL_QUIC_VLINT_8B_MIN
))
517 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x2f77213f3f505ba5ULL
))
518 || !TEST_false(WPACKET_finish(&pkt
))
519 || !TEST_true(WPACKET_close(&pkt
))
520 || !TEST_false(WPACKET_close(&pkt
))
521 || !TEST_true(WPACKET_finish(&pkt
))
522 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
523 || !TEST_mem_eq(buf
->data
, written
, quic5
, sizeof(quic5
)))
524 return cleanup(&pkt
);
526 /* QUIC sub-packet, length known up-front */
527 if (!TEST_true(WPACKET_init(&pkt
, buf
))
528 || !TEST_true(WPACKET_quic_sub_allocate_bytes(&pkt
, 3, &bytes
)))
529 return cleanup(&pkt
);
535 if (!TEST_true(WPACKET_finish(&pkt
))
536 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
537 || !TEST_mem_eq(buf
->data
, written
, quic6
, sizeof(quic6
)))
538 return cleanup(&pkt
);
540 /* Nested and sequential sub-packets with length prefixes */
541 if (!TEST_true(WPACKET_init(&pkt
, buf
))
542 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x07))
543 || !TEST_true(WPACKET_get_length(&pkt
, &len
))
544 || !TEST_size_t_eq(len
, 1)
545 || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt
, OSSL_QUIC_VLINT_4B_MIN
))
546 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x2514))
547 || !TEST_true(WPACKET_get_length(&pkt
, &len
))
548 || !TEST_size_t_eq(len
, 2)
549 || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt
, OSSL_QUIC_VLINT_2B_MIN
))
550 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x05))
551 || !TEST_true(WPACKET_get_length(&pkt
, &len
))
552 || !TEST_size_t_eq(len
, 1)
553 || !TEST_true(WPACKET_close(&pkt
))
554 || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt
, OSSL_QUIC_VLINT_2B_MIN
))
555 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x11))
556 || !TEST_true(WPACKET_close(&pkt
))
557 || !TEST_true(WPACKET_get_length(&pkt
, &len
))
558 || !TEST_size_t_eq(len
, 8)
559 || !TEST_true(WPACKET_close(&pkt
))
560 || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt
, OSSL_QUIC_VLINT_2B_MIN
))
561 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x12))
562 || !TEST_true(WPACKET_close(&pkt
))
563 || !TEST_true(WPACKET_start_quic_sub_packet_bound(&pkt
, OSSL_QUIC_VLINT_2B_MIN
))
564 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, 0x13))
565 || !TEST_true(WPACKET_close(&pkt
))
566 || !TEST_true(WPACKET_finish(&pkt
))
567 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
))
568 || !TEST_mem_eq(buf
->data
, written
, quic7
, sizeof(quic7
)))
569 return cleanup(&pkt
);
571 /* Trying to encode a value above OSSL_QUIC_VLINT_MAX should fail */
572 if (!TEST_true(WPACKET_init(&pkt
, buf
))
573 || !TEST_false(WPACKET_quic_write_vlint(&pkt
, OSSL_QUIC_VLINT_MAX
+1))
574 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, OSSL_QUIC_VLINT_MAX
)))
575 return cleanup(&pkt
);
577 WPACKET_cleanup(&pkt
);
581 static int test_WPACKET_quic_vlint_random(void)
584 uint64_t expected
, actual
= 0;
585 unsigned char rand_data
[9];
587 PACKET read_pkt
= {0};
589 for (i
= 0; i
< 10000; ++i
) {
590 if (!TEST_int_gt(RAND_bytes(rand_data
, sizeof(rand_data
)), 0))
591 return cleanup(&pkt
);
593 expected
= *(uint64_t*)rand_data
;
596 * Ensure that all size classes get tested with equal probability.
598 switch (rand_data
[8] & 3) {
600 expected
&= OSSL_QUIC_VLINT_1B_MAX
;
603 expected
&= OSSL_QUIC_VLINT_2B_MAX
;
606 expected
&= OSSL_QUIC_VLINT_4B_MAX
;
609 expected
&= OSSL_QUIC_VLINT_8B_MAX
;
613 if (!TEST_true(WPACKET_init(&pkt
, buf
))
614 || !TEST_true(WPACKET_quic_write_vlint(&pkt
, expected
))
615 || !TEST_true(WPACKET_get_total_written(&pkt
, &written
)))
616 return cleanup(&pkt
);
618 if (!TEST_true(PACKET_buf_init(&read_pkt
, (unsigned char *)buf
->data
, written
))
619 || !TEST_true(PACKET_get_quic_vlint(&read_pkt
, &actual
))
620 || !TEST_uint64_t_eq(expected
, actual
))
621 return cleanup(&pkt
);
623 WPACKET_cleanup(&pkt
);
626 WPACKET_cleanup(&pkt
);
632 int setup_tests(void)
634 if (!TEST_ptr(buf
= BUF_MEM_new()))
637 ADD_TEST(test_WPACKET_init
);
638 ADD_TEST(test_WPACKET_set_max_size
);
639 ADD_TEST(test_WPACKET_start_sub_packet
);
640 ADD_TEST(test_WPACKET_set_flags
);
641 ADD_TEST(test_WPACKET_allocate_bytes
);
642 ADD_TEST(test_WPACKET_memcpy
);
643 ADD_TEST(test_WPACKET_init_der
);
644 #ifndef OPENSSL_NO_QUIC
645 ADD_TEST(test_WPACKET_quic
);
646 ADD_TEST(test_WPACKET_quic_vlint_random
);
651 void cleanup_tests(void)