2 * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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 "../ssl/packet_locl.h"
15 const static unsigned char simple1
= 0xff;
16 const static unsigned char simple2
[] = { 0x01, 0xff };
17 const static unsigned char simple3
[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
18 const static unsigned char nestedsub
[] = { 0x03, 0xff, 0x01, 0xff };
19 const static unsigned char seqsub
[] = { 0x01, 0xff, 0x01, 0xff };
20 const static unsigned char empty
= 0x00;
21 const static unsigned char alloc
[] = { 0x02, 0xfe, 0xff };
22 const static unsigned char submem
[] = { 0x03, 0x02, 0xfe, 0xff };
26 static void testfail(const char *msg
, WPACKET
*pkt
)
28 fprintf(stderr
, "%s", msg
);
32 static int test_WPACKET_init(void)
38 if (!WPACKET_init(&pkt
, buf
)
39 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
40 /* Closing a top level WPACKET should fail */
41 || WPACKET_close(&pkt
)
42 /* Finishing a top level WPACKET should succeed */
43 || !WPACKET_finish(&pkt
)
45 * Can't call close or finish on a WPACKET that's already
48 || WPACKET_close(&pkt
)
49 || WPACKET_finish(&pkt
)
50 || !WPACKET_get_total_written(&pkt
, &written
)
51 || written
!= sizeof(simple1
)
52 || memcmp(buf
->data
, &simple1
, written
) != 0) {
53 testfail("test_WPACKET_init():1 failed\n", &pkt
);
57 /* Now try with a one byte length prefix */
58 if (!WPACKET_init_len(&pkt
, buf
, 1)
59 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
60 || !WPACKET_finish(&pkt
)
61 || !WPACKET_get_total_written(&pkt
, &written
)
62 || written
!= sizeof(simple2
)
63 || memcmp(buf
->data
, &simple2
, written
) != 0) {
64 testfail("test_WPACKET_init():2 failed\n", &pkt
);
68 /* And a longer length prefix */
69 if (!WPACKET_init_len(&pkt
, buf
, 4)
70 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
71 || !WPACKET_finish(&pkt
)
72 || !WPACKET_get_total_written(&pkt
, &written
)
73 || written
!= sizeof(simple3
)
74 || memcmp(buf
->data
, &simple3
, written
) != 0) {
75 testfail("test_WPACKET_init():3 failed\n", &pkt
);
79 if (!WPACKET_init_len(&pkt
, buf
, 1)) {
80 testfail("test_WPACKET_init():4 failed\n", &pkt
);
83 for (i
= 1; i
< 257; i
++) {
85 * Putting more bytes in than fit for the size of the length prefix
88 if ((!WPACKET_put_bytes(&pkt
, 0xff, 1)) == (i
!= 256)) {
89 testfail("test_WPACKET_init():4 failed\n", &pkt
);
93 if (!WPACKET_finish(&pkt
)) {
94 testfail("test_WPACKET_init():4 failed\n", &pkt
);
101 static int test_WPACKET_set_max_size(void)
106 if (!WPACKET_init(&pkt
, buf
)
108 * No previous lenbytes set so we should be ok to set the max
111 || !WPACKET_set_max_size(&pkt
, SIZE_MAX
)
112 /* We should be able to set it smaller too */
113 || !WPACKET_set_max_size(&pkt
, SIZE_MAX
-1)
114 /* And setting it bigger again should be ok */
115 || !WPACKET_set_max_size(&pkt
, SIZE_MAX
)
116 || !WPACKET_finish(&pkt
)) {
117 testfail("test_WPACKET_set_max_size():1 failed\n", &pkt
);
121 if (!WPACKET_init_len(&pkt
, buf
, 1)
123 * Should fail because we already consumed 1 byte with the
126 || WPACKET_set_max_size(&pkt
, 0)
128 * Max size can't be bigger than biggest that will fit in
131 || WPACKET_set_max_size(&pkt
, 0x0101)
132 /* It can be the same as the maximum possible size */
133 || !WPACKET_set_max_size(&pkt
, 0x0100)
134 /* Or it can be less */
135 || !WPACKET_set_max_size(&pkt
, 0x01)
137 * Should fail because packet is already filled
139 || WPACKET_put_bytes(&pkt
, 0xff, 1)
141 * You can't put in more bytes than max size
143 || !WPACKET_set_max_size(&pkt
, 0x02)
144 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
145 || WPACKET_put_bytes(&pkt
, 0xff, 1)
146 || !WPACKET_finish(&pkt
)
147 || !WPACKET_get_total_written(&pkt
, &written
)
148 || written
!= sizeof(simple2
)
149 || memcmp(buf
->data
, &simple2
, written
) != 0) {
150 testfail("test_WPACKET_set_max_size():2 failed\n", &pkt
);
157 static int test_WPACKET_start_sub_packet(void)
163 if (!WPACKET_init(&pkt
, buf
)
164 || !WPACKET_start_sub_packet(&pkt
)
165 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
166 /* Can't finish because we have a sub packet */
167 || WPACKET_finish(&pkt
)
168 || !WPACKET_close(&pkt
)
169 /* Sub packet is closed so can't close again */
170 || WPACKET_close(&pkt
)
171 /* Now a top level so finish should succeed */
172 || !WPACKET_finish(&pkt
)
173 || !WPACKET_get_total_written(&pkt
, &written
)
174 || written
!= sizeof(simple1
)
175 || memcmp(buf
->data
, &simple1
, written
) != 0) {
176 testfail("test_WPACKET_start_sub_packet():1 failed\n", &pkt
);
180 /* Single sub-packet with length prefix */
181 if (!WPACKET_init(&pkt
, buf
)
182 || !WPACKET_start_sub_packet_u8(&pkt
)
183 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
184 || !WPACKET_close(&pkt
)
185 || !WPACKET_finish(&pkt
)
186 || !WPACKET_get_total_written(&pkt
, &written
)
187 || written
!= sizeof(simple2
)
188 || memcmp(buf
->data
, &simple2
, written
) != 0) {
189 testfail("test_WPACKET_start_sub_packet():2 failed\n", &pkt
);
193 /* Nested sub-packets with length prefixes */
194 if (!WPACKET_init(&pkt
, buf
)
195 || !WPACKET_start_sub_packet_u8(&pkt
)
196 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
197 || !WPACKET_start_sub_packet_u8(&pkt
)
198 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
199 || !WPACKET_get_length(&pkt
, &len
)
201 || !WPACKET_close(&pkt
)
202 || !WPACKET_get_length(&pkt
, &len
)
204 || !WPACKET_close(&pkt
)
205 || !WPACKET_finish(&pkt
)
206 || !WPACKET_get_total_written(&pkt
, &written
)
207 || written
!= sizeof(nestedsub
)
208 || memcmp(buf
->data
, &nestedsub
, written
) != 0) {
209 testfail("test_WPACKET_start_sub_packet():3 failed\n", &pkt
);
213 /* Sequential sub-packets with length prefixes */
214 if (!WPACKET_init(&pkt
, buf
)
215 || !WPACKET_start_sub_packet_u8(&pkt
)
216 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
217 || !WPACKET_close(&pkt
)
218 || !WPACKET_start_sub_packet_u8(&pkt
)
219 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
220 || !WPACKET_close(&pkt
)
221 || !WPACKET_finish(&pkt
)
222 || !WPACKET_get_total_written(&pkt
, &written
)
223 || written
!= sizeof(seqsub
)
224 || memcmp(buf
->data
, &seqsub
, written
) != 0) {
225 testfail("test_WPACKET_start_sub_packet():4 failed\n", &pkt
);
233 static int test_WPACKET_set_flags(void)
238 /* Set packet to be non-zero length */
239 if (!WPACKET_init(&pkt
, buf
)
240 || !WPACKET_set_flags(&pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
241 /* Should fail because of zero length */
242 || WPACKET_finish(&pkt
)
243 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
244 || !WPACKET_finish(&pkt
)
245 || !WPACKET_get_total_written(&pkt
, &written
)
246 || written
!= sizeof(simple1
)
247 || memcmp(buf
->data
, &simple1
, written
) != 0) {
248 testfail("test_WPACKET_set_flags():1 failed\n", &pkt
);
252 /* Repeat above test in a sub-packet */
253 if (!WPACKET_init(&pkt
, buf
)
254 || !WPACKET_start_sub_packet(&pkt
)
255 || !WPACKET_set_flags(&pkt
, WPACKET_FLAGS_NON_ZERO_LENGTH
)
256 /* Should fail because of zero length */
257 || WPACKET_close(&pkt
)
258 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
259 || !WPACKET_close(&pkt
)
260 || !WPACKET_finish(&pkt
)
261 || !WPACKET_get_total_written(&pkt
, &written
)
262 || written
!= sizeof(simple1
)
263 || memcmp(buf
->data
, &simple1
, written
) != 0) {
264 testfail("test_WPACKET_set_flags():2 failed\n", &pkt
);
268 /* Set packet to abandon non-zero length */
269 if (!WPACKET_init_len(&pkt
, buf
, 1)
270 || !WPACKET_set_flags(&pkt
, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
)
271 || !WPACKET_finish(&pkt
)
272 || !WPACKET_get_total_written(&pkt
, &written
)
274 testfail("test_WPACKET_set_flags():3 failed\n", &pkt
);
278 /* Repeat above test but only abandon a sub-packet */
279 if (!WPACKET_init_len(&pkt
, buf
, 1)
280 || !WPACKET_start_sub_packet_u8(&pkt
)
281 || !WPACKET_set_flags(&pkt
, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
)
282 || !WPACKET_close(&pkt
)
283 || !WPACKET_finish(&pkt
)
284 || !WPACKET_get_total_written(&pkt
, &written
)
285 || written
!= sizeof(empty
)
286 || memcmp(buf
->data
, &empty
, written
) != 0) {
287 testfail("test_WPACKET_set_flags():4 failed\n", &pkt
);
291 /* And repeat with a non empty sub-packet */
292 if (!WPACKET_init(&pkt
, buf
)
293 || !WPACKET_start_sub_packet_u8(&pkt
)
294 || !WPACKET_set_flags(&pkt
, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH
)
295 || !WPACKET_put_bytes(&pkt
, 0xff, 1)
296 || !WPACKET_close(&pkt
)
297 || !WPACKET_finish(&pkt
)
298 || !WPACKET_get_total_written(&pkt
, &written
)
299 || written
!= sizeof(simple2
)
300 || memcmp(buf
->data
, &simple2
, written
) != 0) {
301 testfail("test_WPACKET_set_flags():5 failed\n", &pkt
);
307 static int test_WPACKET_allocate_bytes(void)
311 unsigned char *bytes
;
313 if (!WPACKET_init_len(&pkt
, buf
, 1)
314 || !WPACKET_allocate_bytes(&pkt
, 2, &bytes
)) {
315 testfail("test_WPACKET_allocate_bytes():1 failed\n", &pkt
);
320 if (!WPACKET_finish(&pkt
)
321 || !WPACKET_get_total_written(&pkt
, &written
)
322 || written
!= sizeof(alloc
)
323 || memcmp(buf
->data
, &alloc
, written
) != 0) {
324 testfail("test_WPACKET_allocate_bytes():2 failed\n", &pkt
);
328 /* Repeat with WPACKET_sub_allocate_bytes */
329 if (!WPACKET_init_len(&pkt
, buf
, 1)
330 || !WPACKET_sub_allocate_bytes_u8(&pkt
, 2, &bytes
)) {
331 testfail("test_WPACKET_allocate_bytes():3 failed\n", &pkt
);
336 if (!WPACKET_finish(&pkt
)
337 || !WPACKET_get_total_written(&pkt
, &written
)
338 || written
!= sizeof(submem
)
339 || memcmp(buf
->data
, &submem
, written
) != 0) {
340 testfail("test_WPACKET_allocate_bytes():4 failed\n", &pkt
);
347 static int test_WPACKET_memcpy(void)
351 const unsigned char bytes
[] = { 0xfe, 0xff };
353 if (!WPACKET_init_len(&pkt
, buf
, 1)
354 || !WPACKET_memcpy(&pkt
, bytes
, sizeof(bytes
))
355 || !WPACKET_finish(&pkt
)
356 || !WPACKET_get_total_written(&pkt
, &written
)
357 || written
!= sizeof(alloc
)
358 || memcmp(buf
->data
, &alloc
, written
) != 0) {
359 testfail("test_WPACKET_memcpy():1 failed\n", &pkt
);
363 /* Repeat with WPACKET_sub_memcpy() */
364 if (!WPACKET_init_len(&pkt
, buf
, 1)
365 || !WPACKET_sub_memcpy_u8(&pkt
, bytes
, sizeof(bytes
))
366 || !WPACKET_finish(&pkt
)
367 || !WPACKET_get_total_written(&pkt
, &written
)
368 || written
!= sizeof(submem
)
369 || memcmp(buf
->data
, &submem
, written
) != 0) {
370 testfail("test_WPACKET_memcpy():2 failed\n", &pkt
);
377 int main(int argc
, char *argv
[])
382 err
= BIO_new_fp(stderr
, BIO_NOCLOSE
| BIO_FP_TEXT
);
384 CRYPTO_set_mem_debug(1);
385 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
389 ADD_TEST(test_WPACKET_init
);
390 ADD_TEST(test_WPACKET_set_max_size
);
391 ADD_TEST(test_WPACKET_start_sub_packet
);
392 ADD_TEST(test_WPACKET_set_flags
);
393 ADD_TEST(test_WPACKET_allocate_bytes
);
394 ADD_TEST(test_WPACKET_memcpy
);
396 testresult
= run_tests(argv
[0]);
401 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
402 if (CRYPTO_mem_leaks(err
) <= 0)