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"
14 #include "test_main_custom.h"
16 const static unsigned char simple1 = 0xff;
17 const static unsigned char simple2[] = { 0x01, 0xff };
18 const static unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
19 const static unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff };
20 const static unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff };
21 const static unsigned char empty = 0x00;
22 const static unsigned char alloc[] = { 0x02, 0xfe, 0xff };
23 const static unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff };
24 const static unsigned char fixed[] = { 0xff, 0xff, 0xff };
28 static void testfail(const char *msg, WPACKET *pkt)
30 fprintf(stderr, "%s", msg);
34 static int test_WPACKET_init(void)
39 unsigned char sbuf[3];
41 if (!WPACKET_init(&pkt, buf)
42 || !WPACKET_put_bytes_u8(&pkt, 0xff)
43 /* Closing a top level WPACKET should fail */
44 || WPACKET_close(&pkt)
45 /* Finishing a top level WPACKET should succeed */
46 || !WPACKET_finish(&pkt)
48 * Can't call close or finish on a WPACKET that's already
51 || WPACKET_close(&pkt)
52 || WPACKET_finish(&pkt)
53 || !WPACKET_get_total_written(&pkt, &written)
54 || written != sizeof(simple1)
55 || memcmp(buf->data, &simple1, written) != 0) {
56 testfail("test_WPACKET_init():1 failed\n", &pkt);
60 /* Now try with a one byte length prefix */
61 if (!WPACKET_init_len(&pkt, buf, 1)
62 || !WPACKET_put_bytes_u8(&pkt, 0xff)
63 || !WPACKET_finish(&pkt)
64 || !WPACKET_get_total_written(&pkt, &written)
65 || written != sizeof(simple2)
66 || memcmp(buf->data, &simple2, written) != 0) {
67 testfail("test_WPACKET_init():2 failed\n", &pkt);
71 /* And a longer length prefix */
72 if (!WPACKET_init_len(&pkt, buf, 4)
73 || !WPACKET_put_bytes_u8(&pkt, 0xff)
74 || !WPACKET_finish(&pkt)
75 || !WPACKET_get_total_written(&pkt, &written)
76 || written != sizeof(simple3)
77 || memcmp(buf->data, &simple3, written) != 0) {
78 testfail("test_WPACKET_init():3 failed\n", &pkt);
82 if (!WPACKET_init_len(&pkt, buf, 1)) {
83 testfail("test_WPACKET_init():4 failed\n", &pkt);
86 for (i = 1; i < 257; i++) {
88 * Putting more bytes in than fit for the size of the length prefix
91 if ((!WPACKET_put_bytes_u8(&pkt, 0xff)) == (i != 256)) {
92 testfail("test_WPACKET_init():4 failed\n", &pkt);
96 if (!WPACKET_finish(&pkt)) {
97 testfail("test_WPACKET_init():4 failed\n", &pkt);
101 /* Test initialising from a fixed size buffer */
102 if (!WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0)
103 /* Adding 3 bytes should succeed */
104 || !WPACKET_put_bytes_u24(&pkt, 0xffffff)
105 /* Adding 1 more byte should fail */
106 || WPACKET_put_bytes_u8(&pkt, 0xff)
107 /* Finishing the top level WPACKET should succeed */
108 || !WPACKET_finish(&pkt)
109 || !WPACKET_get_total_written(&pkt, &written)
110 || written != sizeof(fixed)
111 || memcmp(sbuf, fixed, sizeof(sbuf)) != 0
112 /* Initialise with 1 len byte */
113 || !WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1)
114 /* Adding 2 bytes should succeed */
115 || !WPACKET_put_bytes_u16(&pkt, 0xfeff)
116 /* Adding 1 more byte should fail */
117 || WPACKET_put_bytes_u8(&pkt, 0xff)
118 || !WPACKET_finish(&pkt)
119 || !WPACKET_get_total_written(&pkt, &written)
120 || written != sizeof(alloc)
121 || memcmp(sbuf, alloc, written) != 0) {
122 testfail("test_WPACKET_init():5 failed\n", &pkt);
129 static int test_WPACKET_set_max_size(void)
134 if (!WPACKET_init(&pkt, buf)
136 * No previous lenbytes set so we should be ok to set the max
139 || !WPACKET_set_max_size(&pkt, SIZE_MAX)
140 /* We should be able to set it smaller too */
141 || !WPACKET_set_max_size(&pkt, SIZE_MAX -1)
142 /* And setting it bigger again should be ok */
143 || !WPACKET_set_max_size(&pkt, SIZE_MAX)
144 || !WPACKET_finish(&pkt)) {
145 testfail("test_WPACKET_set_max_size():1 failed\n", &pkt);
149 if (!WPACKET_init_len(&pkt, buf, 1)
151 * Should fail because we already consumed 1 byte with the
154 || WPACKET_set_max_size(&pkt, 0)
156 * Max size can't be bigger than biggest that will fit in
159 || WPACKET_set_max_size(&pkt, 0x0101)
160 /* It can be the same as the maximum possible size */
161 || !WPACKET_set_max_size(&pkt, 0x0100)
162 /* Or it can be less */
163 || !WPACKET_set_max_size(&pkt, 0x01)
165 * Should fail because packet is already filled
167 || WPACKET_put_bytes_u8(&pkt, 0xff)
169 * You can't put in more bytes than max size
171 || !WPACKET_set_max_size(&pkt, 0x02)
172 || !WPACKET_put_bytes_u8(&pkt, 0xff)
173 || WPACKET_put_bytes_u8(&pkt, 0xff)
174 || !WPACKET_finish(&pkt)
175 || !WPACKET_get_total_written(&pkt, &written)
176 || written != sizeof(simple2)
177 || memcmp(buf->data, &simple2, written) != 0) {
178 testfail("test_WPACKET_set_max_size():2 failed\n", &pkt);
185 static int test_WPACKET_start_sub_packet(void)
191 if (!WPACKET_init(&pkt, buf)
192 || !WPACKET_start_sub_packet(&pkt)
193 || !WPACKET_put_bytes_u8(&pkt, 0xff)
194 /* Can't finish because we have a sub packet */
195 || WPACKET_finish(&pkt)
196 || !WPACKET_close(&pkt)
197 /* Sub packet is closed so can't close again */
198 || WPACKET_close(&pkt)
199 /* Now a top level so finish should succeed */
200 || !WPACKET_finish(&pkt)
201 || !WPACKET_get_total_written(&pkt, &written)
202 || written != sizeof(simple1)
203 || memcmp(buf->data, &simple1, written) != 0) {
204 testfail("test_WPACKET_start_sub_packet():1 failed\n", &pkt);
208 /* Single sub-packet with length prefix */
209 if (!WPACKET_init(&pkt, buf)
210 || !WPACKET_start_sub_packet_u8(&pkt)
211 || !WPACKET_put_bytes_u8(&pkt, 0xff)
212 || !WPACKET_close(&pkt)
213 || !WPACKET_finish(&pkt)
214 || !WPACKET_get_total_written(&pkt, &written)
215 || written != sizeof(simple2)
216 || memcmp(buf->data, &simple2, written) != 0) {
217 testfail("test_WPACKET_start_sub_packet():2 failed\n", &pkt);
221 /* Nested sub-packets with length prefixes */
222 if (!WPACKET_init(&pkt, buf)
223 || !WPACKET_start_sub_packet_u8(&pkt)
224 || !WPACKET_put_bytes_u8(&pkt, 0xff)
225 || !WPACKET_start_sub_packet_u8(&pkt)
226 || !WPACKET_put_bytes_u8(&pkt, 0xff)
227 || !WPACKET_get_length(&pkt, &len)
229 || !WPACKET_close(&pkt)
230 || !WPACKET_get_length(&pkt, &len)
232 || !WPACKET_close(&pkt)
233 || !WPACKET_finish(&pkt)
234 || !WPACKET_get_total_written(&pkt, &written)
235 || written != sizeof(nestedsub)
236 || memcmp(buf->data, &nestedsub, written) != 0) {
237 testfail("test_WPACKET_start_sub_packet():3 failed\n", &pkt);
241 /* Sequential sub-packets with length prefixes */
242 if (!WPACKET_init(&pkt, buf)
243 || !WPACKET_start_sub_packet_u8(&pkt)
244 || !WPACKET_put_bytes_u8(&pkt, 0xff)
245 || !WPACKET_close(&pkt)
246 || !WPACKET_start_sub_packet_u8(&pkt)
247 || !WPACKET_put_bytes_u8(&pkt, 0xff)
248 || !WPACKET_close(&pkt)
249 || !WPACKET_finish(&pkt)
250 || !WPACKET_get_total_written(&pkt, &written)
251 || written != sizeof(seqsub)
252 || memcmp(buf->data, &seqsub, written) != 0) {
253 testfail("test_WPACKET_start_sub_packet():4 failed\n", &pkt);
257 /* Nested sub-packets with lengths filled before finish */
258 if (!WPACKET_init(&pkt, buf)
259 || !WPACKET_start_sub_packet_u8(&pkt)
260 || !WPACKET_put_bytes_u8(&pkt, 0xff)
261 || !WPACKET_start_sub_packet_u8(&pkt)
262 || !WPACKET_put_bytes_u8(&pkt, 0xff)
263 || !WPACKET_get_length(&pkt, &len)
265 || !WPACKET_close(&pkt)
266 || !WPACKET_get_length(&pkt, &len)
268 || !WPACKET_close(&pkt)
269 || !WPACKET_fill_lengths(&pkt)
270 || !WPACKET_get_total_written(&pkt, &written)
271 || written != sizeof(nestedsub)
272 || memcmp(buf->data, &nestedsub, written) != 0
273 || !WPACKET_finish(&pkt)) {
274 testfail("test_WPACKET_start_sub_packet():5 failed\n", &pkt);
282 static int test_WPACKET_set_flags(void)
287 /* Set packet to be non-zero length */
288 if (!WPACKET_init(&pkt, buf)
289 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
290 /* Should fail because of zero length */
291 || WPACKET_finish(&pkt)
292 || !WPACKET_put_bytes_u8(&pkt, 0xff)
293 || !WPACKET_finish(&pkt)
294 || !WPACKET_get_total_written(&pkt, &written)
295 || written != sizeof(simple1)
296 || memcmp(buf->data, &simple1, written) != 0) {
297 testfail("test_WPACKET_set_flags():1 failed\n", &pkt);
301 /* Repeat above test in a sub-packet */
302 if (!WPACKET_init(&pkt, buf)
303 || !WPACKET_start_sub_packet(&pkt)
304 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
305 /* Should fail because of zero length */
306 || WPACKET_close(&pkt)
307 || !WPACKET_put_bytes_u8(&pkt, 0xff)
308 || !WPACKET_close(&pkt)
309 || !WPACKET_finish(&pkt)
310 || !WPACKET_get_total_written(&pkt, &written)
311 || written != sizeof(simple1)
312 || memcmp(buf->data, &simple1, written) != 0) {
313 testfail("test_WPACKET_set_flags():2 failed\n", &pkt);
317 /* Set packet to abandon non-zero length */
318 if (!WPACKET_init_len(&pkt, buf, 1)
319 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
320 || !WPACKET_finish(&pkt)
321 || !WPACKET_get_total_written(&pkt, &written)
323 testfail("test_WPACKET_set_flags():3 failed\n", &pkt);
327 /* Repeat above test but only abandon a sub-packet */
328 if (!WPACKET_init_len(&pkt, buf, 1)
329 || !WPACKET_start_sub_packet_u8(&pkt)
330 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
331 || !WPACKET_close(&pkt)
332 || !WPACKET_finish(&pkt)
333 || !WPACKET_get_total_written(&pkt, &written)
334 || written != sizeof(empty)
335 || memcmp(buf->data, &empty, written) != 0) {
336 testfail("test_WPACKET_set_flags():4 failed\n", &pkt);
340 /* And repeat with a non empty sub-packet */
341 if (!WPACKET_init(&pkt, buf)
342 || !WPACKET_start_sub_packet_u8(&pkt)
343 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
344 || !WPACKET_put_bytes_u8(&pkt, 0xff)
345 || !WPACKET_close(&pkt)
346 || !WPACKET_finish(&pkt)
347 || !WPACKET_get_total_written(&pkt, &written)
348 || written != sizeof(simple2)
349 || memcmp(buf->data, &simple2, written) != 0) {
350 testfail("test_WPACKET_set_flags():5 failed\n", &pkt);
356 static int test_WPACKET_allocate_bytes(void)
360 unsigned char *bytes;
362 if (!WPACKET_init_len(&pkt, buf, 1)
363 || !WPACKET_allocate_bytes(&pkt, 2, &bytes)) {
364 testfail("test_WPACKET_allocate_bytes():1 failed\n", &pkt);
369 if (!WPACKET_finish(&pkt)
370 || !WPACKET_get_total_written(&pkt, &written)
371 || written != sizeof(alloc)
372 || memcmp(buf->data, &alloc, written) != 0) {
373 testfail("test_WPACKET_allocate_bytes():2 failed\n", &pkt);
377 /* Repeat with WPACKET_sub_allocate_bytes */
378 if (!WPACKET_init_len(&pkt, buf, 1)
379 || !WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)) {
380 testfail("test_WPACKET_allocate_bytes():3 failed\n", &pkt);
385 if (!WPACKET_finish(&pkt)
386 || !WPACKET_get_total_written(&pkt, &written)
387 || written != sizeof(submem)
388 || memcmp(buf->data, &submem, written) != 0) {
389 testfail("test_WPACKET_allocate_bytes():4 failed\n", &pkt);
396 static int test_WPACKET_memcpy(void)
400 const unsigned char bytes[] = { 0xfe, 0xff };
402 if (!WPACKET_init_len(&pkt, buf, 1)
403 || !WPACKET_memcpy(&pkt, bytes, sizeof(bytes))
404 || !WPACKET_finish(&pkt)
405 || !WPACKET_get_total_written(&pkt, &written)
406 || written != sizeof(alloc)
407 || memcmp(buf->data, &alloc, written) != 0) {
408 testfail("test_WPACKET_memcpy():1 failed\n", &pkt);
412 /* Repeat with WPACKET_sub_memcpy() */
413 if (!WPACKET_init_len(&pkt, buf, 1)
414 || !WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes))
415 || !WPACKET_finish(&pkt)
416 || !WPACKET_get_total_written(&pkt, &written)
417 || written != sizeof(submem)
418 || memcmp(buf->data, &submem, written) != 0) {
419 testfail("test_WPACKET_memcpy():2 failed\n", &pkt);
426 int test_main(int argc, char *argv[])
432 ADD_TEST(test_WPACKET_init);
433 ADD_TEST(test_WPACKET_set_max_size);
434 ADD_TEST(test_WPACKET_start_sub_packet);
435 ADD_TEST(test_WPACKET_set_flags);
436 ADD_TEST(test_WPACKET_allocate_bytes);
437 ADD_TEST(test_WPACKET_memcpy);
439 testresult = run_tests(argv[0]);