abb001ea14228aaa48e66d81148875b79a4d96ad
[openssl.git] / test / wpackettest.c
1 /*
2  * Copyright 2016-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 <string.h>
11 #include <openssl/buffer.h>
12 #include "internal/packet.h"
13 #include "testutil.h"
14
15 static const unsigned char simple1[] = { 0xff };
16 static const unsigned char simple2[] = { 0x01, 0xff };
17 static const unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
18 static const unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff };
19 static const unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff };
20 static const unsigned char empty[] = { 0x00 };
21 static const unsigned char alloc[] = { 0x02, 0xfe, 0xff };
22 static const unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff };
23 static const unsigned char fixed[] = { 0xff, 0xff, 0xff };
24
25 static BUF_MEM *buf;
26
27 static int cleanup(WPACKET *pkt)
28 {
29     WPACKET_cleanup(pkt);
30     return 0;
31 }
32
33 static int test_WPACKET_init(void)
34 {
35     WPACKET pkt;
36     int i;
37     size_t written;
38     unsigned char sbuf[3];
39
40     if (!TEST_true(WPACKET_init(&pkt, buf))
41             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
42                 /* Closing a top level WPACKET should fail */
43             || !TEST_false(WPACKET_close(&pkt))
44                 /* Finishing a top level WPACKET should succeed */
45             || !TEST_true(WPACKET_finish(&pkt))
46                 /*
47                  * Can't call close or finish on a WPACKET that's already
48                  * finished.
49                  */
50             || !TEST_false(WPACKET_close(&pkt))
51             || !TEST_false(WPACKET_finish(&pkt))
52             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
53             || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
54         return cleanup(&pkt);
55
56     /* Now try with a one byte length prefix */
57     if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
58             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
59             || !TEST_true(WPACKET_finish(&pkt))
60             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
61             || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
62         return cleanup(&pkt);
63
64     /* And a longer length prefix */
65     if (!TEST_true(WPACKET_init_len(&pkt, buf, 4))
66             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
67             || !TEST_true(WPACKET_finish(&pkt))
68             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
69             || !TEST_mem_eq(buf->data, written, simple3, sizeof(simple3)))
70         return cleanup(&pkt);
71
72     if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)))
73         return cleanup(&pkt);
74     for (i = 1; i < 257; i++) {
75         /*
76          * Putting more bytes in than fit for the size of the length prefix
77          * should fail
78          */
79         if (!TEST_int_eq(WPACKET_put_bytes_u8(&pkt, 0xff), i < 256))
80             return cleanup(&pkt);
81     }
82     if (!TEST_true(WPACKET_finish(&pkt)))
83         return cleanup(&pkt);
84
85     /* Test initialising from a fixed size buffer */
86     if (!TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0))
87                 /* Adding 3 bytes should succeed */
88             || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xffffff))
89                 /* Adding 1 more byte should fail */
90             || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
91                 /* Finishing the top level WPACKET should succeed */
92             || !TEST_true(WPACKET_finish(&pkt))
93             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
94             || !TEST_mem_eq(sbuf, written, fixed, sizeof(sbuf))
95                 /* Initialise with 1 len byte */
96             || !TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1))
97                 /* Adding 2 bytes should succeed */
98             || !TEST_true(WPACKET_put_bytes_u16(&pkt, 0xfeff))
99                 /* Adding 1 more byte should fail */
100             || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
101             || !TEST_true(WPACKET_finish(&pkt))
102             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
103             || !TEST_mem_eq(sbuf, written, alloc, sizeof(alloc)))
104         return cleanup(&pkt);
105
106     return 1;
107 }
108
109 static int test_WPACKET_set_max_size(void)
110 {
111     WPACKET pkt;
112     size_t written;
113
114     if (!TEST_true(WPACKET_init(&pkt, buf))
115                 /*
116                  * No previous lenbytes set so we should be ok to set the max
117                  * possible max size
118                  */
119             || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
120                 /* We should be able to set it smaller too */
121             || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX -1))
122                 /* And setting it bigger again should be ok */
123             || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
124             || !TEST_true(WPACKET_finish(&pkt)))
125         return cleanup(&pkt);
126
127     if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
128                 /*
129                  * Should fail because we already consumed 1 byte with the
130                  * length
131                  */
132             || !TEST_false(WPACKET_set_max_size(&pkt, 0))
133                 /*
134                  * Max size can't be bigger than biggest that will fit in
135                  * lenbytes
136                  */
137             || !TEST_false(WPACKET_set_max_size(&pkt, 0x0101))
138                 /* It can be the same as the maximum possible size */
139             || !TEST_true(WPACKET_set_max_size(&pkt, 0x0100))
140                 /* Or it can be less */
141             || !TEST_true(WPACKET_set_max_size(&pkt, 0x01))
142                 /* Should fail because packet is already filled */
143             || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
144                 /* You can't put in more bytes than max size */
145             || !TEST_true(WPACKET_set_max_size(&pkt, 0x02))
146             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
147             || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
148             || !TEST_true(WPACKET_finish(&pkt))
149             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
150             || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
151         return cleanup(&pkt);
152
153     return 1;
154 }
155
156 static int test_WPACKET_start_sub_packet(void)
157 {
158     WPACKET pkt;
159     size_t written;
160     size_t len;
161
162     if (!TEST_true(WPACKET_init(&pkt, buf))
163             || !TEST_true(WPACKET_start_sub_packet(&pkt))
164             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
165                 /* Can't finish because we have a sub packet */
166             || !TEST_false(WPACKET_finish(&pkt))
167             || !TEST_true(WPACKET_close(&pkt))
168                 /* Sub packet is closed so can't close again */
169             || !TEST_false(WPACKET_close(&pkt))
170                 /* Now a top level so finish should succeed */
171             || !TEST_true(WPACKET_finish(&pkt))
172             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
173             || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
174         return cleanup(&pkt);
175
176    /* Single sub-packet with length prefix */
177     if (!TEST_true(WPACKET_init(&pkt, buf))
178             || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
179             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
180             || !TEST_true(WPACKET_close(&pkt))
181             || !TEST_true(WPACKET_finish(&pkt))
182             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
183             || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
184         return cleanup(&pkt);
185
186     /* Nested sub-packets with length prefixes */
187     if (!TEST_true(WPACKET_init(&pkt, buf))
188             || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
189             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
190             || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
191             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
192             || !TEST_true(WPACKET_get_length(&pkt, &len))
193             || !TEST_size_t_eq(len, 1)
194             || !TEST_true(WPACKET_close(&pkt))
195             || !TEST_true(WPACKET_get_length(&pkt, &len))
196             || !TEST_size_t_eq(len, 3)
197             || !TEST_true(WPACKET_close(&pkt))
198             || !TEST_true(WPACKET_finish(&pkt))
199             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
200             || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub)))
201         return cleanup(&pkt);
202
203     /* Sequential sub-packets with length prefixes */
204     if (!TEST_true(WPACKET_init(&pkt, buf))
205             || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
206             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
207             || !TEST_true(WPACKET_close(&pkt))
208             || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
209             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
210             || !TEST_true(WPACKET_close(&pkt))
211             || !TEST_true(WPACKET_finish(&pkt))
212             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
213             || !TEST_mem_eq(buf->data, written, seqsub, sizeof(seqsub)))
214         return cleanup(&pkt);
215
216     /* Nested sub-packets with lengths filled before finish */
217     if (!TEST_true(WPACKET_init(&pkt, buf))
218             || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
219             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
220             || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
221             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
222             || !TEST_true(WPACKET_get_length(&pkt, &len))
223             || !TEST_size_t_eq(len, 1)
224             || !TEST_true(WPACKET_close(&pkt))
225             || !TEST_true(WPACKET_get_length(&pkt, &len))
226             || !TEST_size_t_eq(len, 3)
227             || !TEST_true(WPACKET_close(&pkt))
228             || !TEST_true(WPACKET_fill_lengths(&pkt))
229             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
230             || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub))
231             || !TEST_true(WPACKET_finish(&pkt)))
232         return cleanup(&pkt);
233
234     return 1;
235 }
236
237
238 static int test_WPACKET_set_flags(void)
239 {
240     WPACKET pkt;
241     size_t written;
242
243     /* Set packet to be non-zero length */
244     if (!TEST_true(WPACKET_init(&pkt, buf))
245             || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
246                 /* Should fail because of zero length */
247             || !TEST_false(WPACKET_finish(&pkt))
248             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
249             || !TEST_true(WPACKET_finish(&pkt))
250             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
251             || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
252         return cleanup(&pkt);
253
254     /* Repeat above test in a sub-packet */
255     if (!TEST_true(WPACKET_init(&pkt, buf))
256             || !TEST_true(WPACKET_start_sub_packet(&pkt))
257             || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
258                 /* Should fail because of zero length */
259             || !TEST_false(WPACKET_close(&pkt))
260             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
261             || !TEST_true(WPACKET_close(&pkt))
262             || !TEST_true(WPACKET_finish(&pkt))
263             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
264             || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
265         return cleanup(&pkt);
266
267     /* Set packet to abandon non-zero length */
268     if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
269             || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
270             || !TEST_true(WPACKET_finish(&pkt))
271             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
272             || !TEST_size_t_eq(written, 0))
273         return cleanup(&pkt);
274
275     /* Repeat above test but only abandon a sub-packet */
276     if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
277             || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
278             || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
279             || !TEST_true(WPACKET_close(&pkt))
280             || !TEST_true(WPACKET_finish(&pkt))
281             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
282             || !TEST_mem_eq(buf->data, written, empty, sizeof(empty)))
283         return cleanup(&pkt);
284
285     /* And repeat with a non empty sub-packet */
286     if (!TEST_true(WPACKET_init(&pkt, buf))
287             || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
288             || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
289             || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
290             || !TEST_true(WPACKET_close(&pkt))
291             || !TEST_true(WPACKET_finish(&pkt))
292             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
293             || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
294         return cleanup(&pkt);
295     return 1;
296 }
297
298 static int test_WPACKET_allocate_bytes(void)
299 {
300     WPACKET pkt;
301     size_t written;
302     unsigned char *bytes;
303
304     if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
305             || !TEST_true(WPACKET_allocate_bytes(&pkt, 2, &bytes)))
306         return cleanup(&pkt);
307     bytes[0] = 0xfe;
308     bytes[1] = 0xff;
309     if (!TEST_true(WPACKET_finish(&pkt))
310             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
311             || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
312         return cleanup(&pkt);
313
314     /* Repeat with WPACKET_sub_allocate_bytes */
315     if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
316             || !TEST_true(WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)))
317         return cleanup(&pkt);
318     bytes[0] = 0xfe;
319     bytes[1] = 0xff;
320     if (!TEST_true(WPACKET_finish(&pkt))
321             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
322             || !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
323         return cleanup(&pkt);
324
325     return 1;
326 }
327
328 static int test_WPACKET_memcpy(void)
329 {
330     WPACKET pkt;
331     size_t written;
332     const unsigned char bytes[] = { 0xfe, 0xff };
333
334     if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
335             || !TEST_true(WPACKET_memcpy(&pkt, bytes, sizeof(bytes)))
336             || !TEST_true(WPACKET_finish(&pkt))
337             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
338             || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
339         return cleanup(&pkt);
340
341     /* Repeat with WPACKET_sub_memcpy() */
342     if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
343             || !TEST_true(WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes)))
344             || !TEST_true(WPACKET_finish(&pkt))
345             || !TEST_true(WPACKET_get_total_written(&pkt, &written))
346             || !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
347         return cleanup(&pkt);
348
349     return 1;
350 }
351
352 int setup_tests(void)
353 {
354     if (!TEST_ptr(buf = BUF_MEM_new()))
355             return 0;
356
357     ADD_TEST(test_WPACKET_init);
358     ADD_TEST(test_WPACKET_set_max_size);
359     ADD_TEST(test_WPACKET_start_sub_packet);
360     ADD_TEST(test_WPACKET_set_flags);
361     ADD_TEST(test_WPACKET_allocate_bytes);
362     ADD_TEST(test_WPACKET_memcpy);
363     return 1;
364 }
365
366 void cleanup_tests(void)
367 {
368     BUF_MEM_free(buf);
369 }