Integrate BoringSSL shim
[openssl.git] / test / wpackettest.c
1 /*
2  * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
10 #include <string.h>
11 #include <openssl/buffer.h>
12 #include "../ssl/packet_locl.h"
13 #include "testutil.h"
14
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 };
23
24 static BUF_MEM *buf;
25
26 static void testfail(const char *msg, WPACKET *pkt)
27 {
28     fprintf(stderr, "%s", msg);
29     WPACKET_cleanup(pkt);
30 }
31
32 static int test_WPACKET_init(void)
33 {
34     WPACKET pkt;
35     int i;
36     size_t written;
37
38     if (!WPACKET_init(&pkt, buf)
39             || !WPACKET_put_bytes_u8(&pkt, 0xff)
40                 /* Closing a top level WPACKET should fail */
41             ||  WPACKET_close(&pkt)
42                 /* Finishing a top level WPACKET should succeed */
43             || !WPACKET_finish(&pkt)
44                 /*
45                  * Can't call close or finish on a WPACKET that's already
46                  * finished.
47                  */
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);
54         return 0;
55     }
56
57     /* Now try with a one byte length prefix */
58     if (!WPACKET_init_len(&pkt, buf, 1)
59             || !WPACKET_put_bytes_u8(&pkt, 0xff)
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);
65         return 0;
66     }
67
68     /* And a longer length prefix */
69     if (!WPACKET_init_len(&pkt, buf, 4)
70             || !WPACKET_put_bytes_u8(&pkt, 0xff)
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);
76         return 0;
77     }
78
79     if (!WPACKET_init_len(&pkt, buf, 1)) {
80         testfail("test_WPACKET_init():4 failed\n", &pkt);
81         return 0;
82     }
83     for (i = 1; i < 257; i++) {
84         /*
85          * Putting more bytes in than fit for the size of the length prefix
86          * should fail
87          */
88         if ((!WPACKET_put_bytes_u8(&pkt, 0xff)) == (i != 256)) {
89             testfail("test_WPACKET_init():4 failed\n", &pkt);
90             return 0;
91         }
92     }
93     if (!WPACKET_finish(&pkt)) {
94         testfail("test_WPACKET_init():4 failed\n", &pkt);
95         return 0;
96     }
97
98     return 1;
99 }
100
101 static int test_WPACKET_set_max_size(void)
102 {
103     WPACKET pkt;
104     size_t written;
105
106     if (!WPACKET_init(&pkt, buf)
107                 /*
108                  * No previous lenbytes set so we should be ok to set the max
109                  * possible max size
110                  */
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);
118         return 0;
119     }
120
121     if (!WPACKET_init_len(&pkt, buf, 1)
122                 /*
123                  * Should fail because we already consumed 1 byte with the
124                  * length
125                  */
126             ||  WPACKET_set_max_size(&pkt, 0)
127                 /*
128                  * Max size can't be bigger than biggest that will fit in
129                  * lenbytes
130                  */
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)
136                 /*
137                  * Should fail because packet is already filled
138                  */
139             ||  WPACKET_put_bytes_u8(&pkt, 0xff)
140                 /*
141                  * You can't put in more bytes than max size
142                  */
143             || !WPACKET_set_max_size(&pkt, 0x02)
144             || !WPACKET_put_bytes_u8(&pkt, 0xff)
145             ||  WPACKET_put_bytes_u8(&pkt, 0xff)
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);
151         return 0;
152     }
153
154     return 1;
155 }
156
157 static int test_WPACKET_start_sub_packet(void)
158 {
159     WPACKET pkt;
160     size_t written;
161     size_t len;
162
163     if (!WPACKET_init(&pkt, buf)
164             || !WPACKET_start_sub_packet(&pkt)
165             || !WPACKET_put_bytes_u8(&pkt, 0xff)
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);
177         return 0;
178     }
179
180    /* Single sub-packet with length prefix */
181     if (!WPACKET_init(&pkt, buf)
182             || !WPACKET_start_sub_packet_u8(&pkt)
183             || !WPACKET_put_bytes_u8(&pkt, 0xff)
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);
190         return 0;
191     }
192
193     /* Nested sub-packets with length prefixes */
194     if (!WPACKET_init(&pkt, buf)
195             || !WPACKET_start_sub_packet_u8(&pkt)
196             || !WPACKET_put_bytes_u8(&pkt, 0xff)
197             || !WPACKET_start_sub_packet_u8(&pkt)
198             || !WPACKET_put_bytes_u8(&pkt, 0xff)
199             || !WPACKET_get_length(&pkt, &len)
200             || len != 1
201             || !WPACKET_close(&pkt)
202             || !WPACKET_get_length(&pkt, &len)
203             || len != 3
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);
210         return 0;
211     }
212
213     /* Sequential sub-packets with length prefixes */
214     if (!WPACKET_init(&pkt, buf)
215             || !WPACKET_start_sub_packet_u8(&pkt)
216             || !WPACKET_put_bytes_u8(&pkt, 0xff)
217             || !WPACKET_close(&pkt)
218             || !WPACKET_start_sub_packet_u8(&pkt)
219             || !WPACKET_put_bytes_u8(&pkt, 0xff)
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);
226         return 0;
227     }
228
229     return 1;
230 }
231
232
233 static int test_WPACKET_set_flags(void)
234 {
235     WPACKET pkt;
236     size_t written;
237
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_u8(&pkt, 0xff)
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);
249         return 0;
250     }
251
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_u8(&pkt, 0xff)
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);
265         return 0;
266     }
267
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)
273             ||  written != 0) {
274         testfail("test_WPACKET_set_flags():3 failed\n", &pkt);
275         return 0;
276     }
277
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);
288         return 0;
289     }
290
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_u8(&pkt, 0xff)
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);
302         return 0;
303     }
304     return 1;
305 }
306
307 static int test_WPACKET_allocate_bytes(void)
308 {
309     WPACKET pkt;
310     size_t written;
311     unsigned char *bytes;
312
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);
316         return 0;
317     }
318     bytes[0] = 0xfe;
319     bytes[1] = 0xff;
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);
325         return 0;
326     }
327
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);
332         return 0;
333     }
334     bytes[0] = 0xfe;
335     bytes[1] = 0xff;
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);
341         return 0;
342     }
343
344     return 1;
345 }
346
347 static int test_WPACKET_memcpy(void)
348 {
349     WPACKET pkt;
350     size_t written;
351     const unsigned char bytes[] = { 0xfe, 0xff };
352
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);
360         return 0;
361     }
362
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);
371         return 0;
372     }
373
374     return 1;
375 }
376
377 int main(int argc, char *argv[])
378 {
379     BIO *err = NULL;
380     int testresult = 0;
381
382     err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
383
384     CRYPTO_set_mem_debug(1);
385     CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
386
387     buf = BUF_MEM_new();
388     if (buf != NULL) {
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);
395
396         testresult = run_tests(argv[0]);
397
398         BUF_MEM_free(buf);
399     }
400
401 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
402     if (CRYPTO_mem_leaks(err) <= 0)
403         testresult = 1;
404 #endif
405     BIO_free(err);
406
407     if (!testresult)
408         printf("PASS\n");
409
410     return testresult;
411 }
412