xref: /third_party/openssl/test/wpackettest.c (revision e1051a39)
1e1051a39Sopenharmony_ci/*
2e1051a39Sopenharmony_ci * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci *
4e1051a39Sopenharmony_ci * Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci * this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci * in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci * https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci */
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ci#include <string.h>
11e1051a39Sopenharmony_ci#include <openssl/buffer.h>
12e1051a39Sopenharmony_ci#include <openssl/rand.h>
13e1051a39Sopenharmony_ci#include "internal/packet.h"
14e1051a39Sopenharmony_ci#include "testutil.h"
15e1051a39Sopenharmony_ci
16e1051a39Sopenharmony_cistatic const unsigned char simple1[] = { 0xff };
17e1051a39Sopenharmony_cistatic const unsigned char simple2[] = { 0x01, 0xff };
18e1051a39Sopenharmony_cistatic const unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
19e1051a39Sopenharmony_cistatic const unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff };
20e1051a39Sopenharmony_cistatic const unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff };
21e1051a39Sopenharmony_cistatic const unsigned char empty[] = { 0x00 };
22e1051a39Sopenharmony_cistatic const unsigned char alloc[] = { 0x02, 0xfe, 0xff };
23e1051a39Sopenharmony_cistatic const unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff };
24e1051a39Sopenharmony_cistatic const unsigned char fixed[] = { 0xff, 0xff, 0xff };
25e1051a39Sopenharmony_cistatic const unsigned char simpleder[] = {
26e1051a39Sopenharmony_ci    0xfc, 0x04, 0x00, 0x01, 0x02, 0x03, 0xff, 0xfe, 0xfd
27e1051a39Sopenharmony_ci};
28e1051a39Sopenharmony_ci
29e1051a39Sopenharmony_cistatic BUF_MEM *buf;
30e1051a39Sopenharmony_ci
31e1051a39Sopenharmony_cistatic int cleanup(WPACKET *pkt)
32e1051a39Sopenharmony_ci{
33e1051a39Sopenharmony_ci    WPACKET_cleanup(pkt);
34e1051a39Sopenharmony_ci    return 0;
35e1051a39Sopenharmony_ci}
36e1051a39Sopenharmony_ci
37e1051a39Sopenharmony_cistatic int test_WPACKET_init(void)
38e1051a39Sopenharmony_ci{
39e1051a39Sopenharmony_ci    WPACKET pkt;
40e1051a39Sopenharmony_ci    int i;
41e1051a39Sopenharmony_ci    size_t written;
42e1051a39Sopenharmony_ci    unsigned char sbuf[3];
43e1051a39Sopenharmony_ci
44e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
45e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
46e1051a39Sopenharmony_ci                /* Closing a top level WPACKET should fail */
47e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_close(&pkt))
48e1051a39Sopenharmony_ci                /* Finishing a top level WPACKET should succeed */
49e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
50e1051a39Sopenharmony_ci                /*
51e1051a39Sopenharmony_ci                 * Can't call close or finish on a WPACKET that's already
52e1051a39Sopenharmony_ci                 * finished.
53e1051a39Sopenharmony_ci                 */
54e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_close(&pkt))
55e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_finish(&pkt))
56e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
57e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
58e1051a39Sopenharmony_ci        return cleanup(&pkt);
59e1051a39Sopenharmony_ci
60e1051a39Sopenharmony_ci    /* Now try with a one byte length prefix */
61e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
62e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
63e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
64e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
65e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
66e1051a39Sopenharmony_ci        return cleanup(&pkt);
67e1051a39Sopenharmony_ci
68e1051a39Sopenharmony_ci    /* And a longer length prefix */
69e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 4))
70e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
71e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
72e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
73e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, simple3, sizeof(simple3)))
74e1051a39Sopenharmony_ci        return cleanup(&pkt);
75e1051a39Sopenharmony_ci
76e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)))
77e1051a39Sopenharmony_ci        return cleanup(&pkt);
78e1051a39Sopenharmony_ci    for (i = 1; i < 257; i++) {
79e1051a39Sopenharmony_ci        /*
80e1051a39Sopenharmony_ci         * Putting more bytes in than fit for the size of the length prefix
81e1051a39Sopenharmony_ci         * should fail
82e1051a39Sopenharmony_ci         */
83e1051a39Sopenharmony_ci        if (!TEST_int_eq(WPACKET_put_bytes_u8(&pkt, 0xff), i < 256))
84e1051a39Sopenharmony_ci            return cleanup(&pkt);
85e1051a39Sopenharmony_ci    }
86e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_finish(&pkt)))
87e1051a39Sopenharmony_ci        return cleanup(&pkt);
88e1051a39Sopenharmony_ci
89e1051a39Sopenharmony_ci    /* Test initialising from a fixed size buffer */
90e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0))
91e1051a39Sopenharmony_ci                /* Adding 3 bytes should succeed */
92e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xffffff))
93e1051a39Sopenharmony_ci                /* Adding 1 more byte should fail */
94e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
95e1051a39Sopenharmony_ci                /* Finishing the top level WPACKET should succeed */
96e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
97e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
98e1051a39Sopenharmony_ci            || !TEST_mem_eq(sbuf, written, fixed, sizeof(sbuf))
99e1051a39Sopenharmony_ci                /* Initialise with 1 len byte */
100e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1))
101e1051a39Sopenharmony_ci                /* Adding 2 bytes should succeed */
102e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u16(&pkt, 0xfeff))
103e1051a39Sopenharmony_ci                /* Adding 1 more byte should fail */
104e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
105e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
106e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
107e1051a39Sopenharmony_ci            || !TEST_mem_eq(sbuf, written, alloc, sizeof(alloc)))
108e1051a39Sopenharmony_ci        return cleanup(&pkt);
109e1051a39Sopenharmony_ci
110e1051a39Sopenharmony_ci    return 1;
111e1051a39Sopenharmony_ci}
112e1051a39Sopenharmony_ci
113e1051a39Sopenharmony_cistatic int test_WPACKET_set_max_size(void)
114e1051a39Sopenharmony_ci{
115e1051a39Sopenharmony_ci    WPACKET pkt;
116e1051a39Sopenharmony_ci    size_t written;
117e1051a39Sopenharmony_ci
118e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
119e1051a39Sopenharmony_ci                /*
120e1051a39Sopenharmony_ci                 * No previous lenbytes set so we should be ok to set the max
121e1051a39Sopenharmony_ci                 * possible max size
122e1051a39Sopenharmony_ci                 */
123e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
124e1051a39Sopenharmony_ci                /* We should be able to set it smaller too */
125e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX -1))
126e1051a39Sopenharmony_ci                /* And setting it bigger again should be ok */
127e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
128e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt)))
129e1051a39Sopenharmony_ci        return cleanup(&pkt);
130e1051a39Sopenharmony_ci
131e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
132e1051a39Sopenharmony_ci                /*
133e1051a39Sopenharmony_ci                 * Should fail because we already consumed 1 byte with the
134e1051a39Sopenharmony_ci                 * length
135e1051a39Sopenharmony_ci                 */
136e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_set_max_size(&pkt, 0))
137e1051a39Sopenharmony_ci                /*
138e1051a39Sopenharmony_ci                 * Max size can't be bigger than biggest that will fit in
139e1051a39Sopenharmony_ci                 * lenbytes
140e1051a39Sopenharmony_ci                 */
141e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_set_max_size(&pkt, 0x0101))
142e1051a39Sopenharmony_ci                /* It can be the same as the maximum possible size */
143e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_max_size(&pkt, 0x0100))
144e1051a39Sopenharmony_ci                /* Or it can be less */
145e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_max_size(&pkt, 0x01))
146e1051a39Sopenharmony_ci                /* Should fail because packet is already filled */
147e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
148e1051a39Sopenharmony_ci                /* You can't put in more bytes than max size */
149e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_max_size(&pkt, 0x02))
150e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
151e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
152e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
153e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
154e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
155e1051a39Sopenharmony_ci        return cleanup(&pkt);
156e1051a39Sopenharmony_ci
157e1051a39Sopenharmony_ci    return 1;
158e1051a39Sopenharmony_ci}
159e1051a39Sopenharmony_ci
160e1051a39Sopenharmony_cistatic int test_WPACKET_start_sub_packet(void)
161e1051a39Sopenharmony_ci{
162e1051a39Sopenharmony_ci    WPACKET pkt;
163e1051a39Sopenharmony_ci    size_t written;
164e1051a39Sopenharmony_ci    size_t len;
165e1051a39Sopenharmony_ci
166e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
167e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet(&pkt))
168e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
169e1051a39Sopenharmony_ci                /* Can't finish because we have a sub packet */
170e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_finish(&pkt))
171e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
172e1051a39Sopenharmony_ci                /* Sub packet is closed so can't close again */
173e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_close(&pkt))
174e1051a39Sopenharmony_ci                /* Now a top level so finish should succeed */
175e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
176e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
177e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
178e1051a39Sopenharmony_ci        return cleanup(&pkt);
179e1051a39Sopenharmony_ci
180e1051a39Sopenharmony_ci   /* Single sub-packet with length prefix */
181e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
182e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
183e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
184e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
185e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
186e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
187e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
188e1051a39Sopenharmony_ci        return cleanup(&pkt);
189e1051a39Sopenharmony_ci
190e1051a39Sopenharmony_ci    /* Nested sub-packets with length prefixes */
191e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
192e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
193e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
194e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
195e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
196e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_length(&pkt, &len))
197e1051a39Sopenharmony_ci            || !TEST_size_t_eq(len, 1)
198e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
199e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_length(&pkt, &len))
200e1051a39Sopenharmony_ci            || !TEST_size_t_eq(len, 3)
201e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
202e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
203e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
204e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub)))
205e1051a39Sopenharmony_ci        return cleanup(&pkt);
206e1051a39Sopenharmony_ci
207e1051a39Sopenharmony_ci    /* Sequential sub-packets with length prefixes */
208e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
209e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
210e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
211e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
212e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
213e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
214e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
215e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
216e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
217e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, seqsub, sizeof(seqsub)))
218e1051a39Sopenharmony_ci        return cleanup(&pkt);
219e1051a39Sopenharmony_ci
220e1051a39Sopenharmony_ci    /* Nested sub-packets with lengths filled before finish */
221e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
222e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
223e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
224e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
225e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
226e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_length(&pkt, &len))
227e1051a39Sopenharmony_ci            || !TEST_size_t_eq(len, 1)
228e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
229e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_length(&pkt, &len))
230e1051a39Sopenharmony_ci            || !TEST_size_t_eq(len, 3)
231e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
232e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_fill_lengths(&pkt))
233e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
234e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub))
235e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt)))
236e1051a39Sopenharmony_ci        return cleanup(&pkt);
237e1051a39Sopenharmony_ci
238e1051a39Sopenharmony_ci    return 1;
239e1051a39Sopenharmony_ci}
240e1051a39Sopenharmony_ci
241e1051a39Sopenharmony_ci
242e1051a39Sopenharmony_cistatic int test_WPACKET_set_flags(void)
243e1051a39Sopenharmony_ci{
244e1051a39Sopenharmony_ci    WPACKET pkt;
245e1051a39Sopenharmony_ci    size_t written;
246e1051a39Sopenharmony_ci
247e1051a39Sopenharmony_ci    /* Set packet to be non-zero length */
248e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
249e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
250e1051a39Sopenharmony_ci                /* Should fail because of zero length */
251e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_finish(&pkt))
252e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
253e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
254e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
255e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
256e1051a39Sopenharmony_ci        return cleanup(&pkt);
257e1051a39Sopenharmony_ci
258e1051a39Sopenharmony_ci    /* Repeat above test in a sub-packet */
259e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
260e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet(&pkt))
261e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
262e1051a39Sopenharmony_ci                /* Should fail because of zero length */
263e1051a39Sopenharmony_ci            || !TEST_false(WPACKET_close(&pkt))
264e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
265e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
266e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
267e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
268e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
269e1051a39Sopenharmony_ci        return cleanup(&pkt);
270e1051a39Sopenharmony_ci
271e1051a39Sopenharmony_ci    /* Set packet to abandon non-zero length */
272e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
273e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
274e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
275e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
276e1051a39Sopenharmony_ci            || !TEST_size_t_eq(written, 0))
277e1051a39Sopenharmony_ci        return cleanup(&pkt);
278e1051a39Sopenharmony_ci
279e1051a39Sopenharmony_ci    /* Repeat above test but only abandon a sub-packet */
280e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
281e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
282e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
283e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
284e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
285e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
286e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, empty, sizeof(empty)))
287e1051a39Sopenharmony_ci        return cleanup(&pkt);
288e1051a39Sopenharmony_ci
289e1051a39Sopenharmony_ci    /* And repeat with a non empty sub-packet */
290e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init(&pkt, buf))
291e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
292e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
293e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
294e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
295e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
296e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
297e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
298e1051a39Sopenharmony_ci        return cleanup(&pkt);
299e1051a39Sopenharmony_ci    return 1;
300e1051a39Sopenharmony_ci}
301e1051a39Sopenharmony_ci
302e1051a39Sopenharmony_cistatic int test_WPACKET_allocate_bytes(void)
303e1051a39Sopenharmony_ci{
304e1051a39Sopenharmony_ci    WPACKET pkt;
305e1051a39Sopenharmony_ci    size_t written;
306e1051a39Sopenharmony_ci    unsigned char *bytes;
307e1051a39Sopenharmony_ci
308e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
309e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_allocate_bytes(&pkt, 2, &bytes)))
310e1051a39Sopenharmony_ci        return cleanup(&pkt);
311e1051a39Sopenharmony_ci    bytes[0] = 0xfe;
312e1051a39Sopenharmony_ci    bytes[1] = 0xff;
313e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_finish(&pkt))
314e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
315e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
316e1051a39Sopenharmony_ci        return cleanup(&pkt);
317e1051a39Sopenharmony_ci
318e1051a39Sopenharmony_ci    /* Repeat with WPACKET_sub_allocate_bytes */
319e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
320e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)))
321e1051a39Sopenharmony_ci        return cleanup(&pkt);
322e1051a39Sopenharmony_ci    bytes[0] = 0xfe;
323e1051a39Sopenharmony_ci    bytes[1] = 0xff;
324e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_finish(&pkt))
325e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
326e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
327e1051a39Sopenharmony_ci        return cleanup(&pkt);
328e1051a39Sopenharmony_ci
329e1051a39Sopenharmony_ci    return 1;
330e1051a39Sopenharmony_ci}
331e1051a39Sopenharmony_ci
332e1051a39Sopenharmony_cistatic int test_WPACKET_memcpy(void)
333e1051a39Sopenharmony_ci{
334e1051a39Sopenharmony_ci    WPACKET pkt;
335e1051a39Sopenharmony_ci    size_t written;
336e1051a39Sopenharmony_ci    const unsigned char bytes[] = { 0xfe, 0xff };
337e1051a39Sopenharmony_ci
338e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
339e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_memcpy(&pkt, bytes, sizeof(bytes)))
340e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
341e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
342e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
343e1051a39Sopenharmony_ci        return cleanup(&pkt);
344e1051a39Sopenharmony_ci
345e1051a39Sopenharmony_ci    /* Repeat with WPACKET_sub_memcpy() */
346e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
347e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes)))
348e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
349e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written))
350e1051a39Sopenharmony_ci            || !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
351e1051a39Sopenharmony_ci        return cleanup(&pkt);
352e1051a39Sopenharmony_ci
353e1051a39Sopenharmony_ci    return 1;
354e1051a39Sopenharmony_ci}
355e1051a39Sopenharmony_ci
356e1051a39Sopenharmony_cistatic int test_WPACKET_init_der(void)
357e1051a39Sopenharmony_ci{
358e1051a39Sopenharmony_ci    WPACKET pkt;
359e1051a39Sopenharmony_ci    unsigned char sbuf[1024];
360e1051a39Sopenharmony_ci    unsigned char testdata[] = { 0x00, 0x01, 0x02, 0x03 };
361e1051a39Sopenharmony_ci    unsigned char testdata2[259]  = { 0x82, 0x01, 0x00 };
362e1051a39Sopenharmony_ci    size_t written[2];
363e1051a39Sopenharmony_ci    size_t size1, size2;
364e1051a39Sopenharmony_ci    int flags = WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH;
365e1051a39Sopenharmony_ci    int i;
366e1051a39Sopenharmony_ci
367e1051a39Sopenharmony_ci    /* Test initialising for writing DER */
368e1051a39Sopenharmony_ci    if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf)))
369e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xfffefd))
370e1051a39Sopenharmony_ci               /* Test writing data in a length prefixed sub-packet */
371e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_start_sub_packet(&pkt))
372e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_memcpy(&pkt, testdata, sizeof(testdata)))
373e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
374e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xfc))
375e1051a39Sopenharmony_ci            /* this sub-packet is empty, and should render zero bytes */
376e1051a39Sopenharmony_ci            || (!TEST_true(WPACKET_start_sub_packet(&pkt))
377e1051a39Sopenharmony_ci                || !TEST_true(WPACKET_set_flags(&pkt, flags))
378e1051a39Sopenharmony_ci                || !TEST_true(WPACKET_get_total_written(&pkt, &size1))
379e1051a39Sopenharmony_ci                || !TEST_true(WPACKET_close(&pkt))
380e1051a39Sopenharmony_ci                || !TEST_true(WPACKET_get_total_written(&pkt, &size2))
381e1051a39Sopenharmony_ci                || !TEST_size_t_eq(size1, size2))
382e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
383e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written[0]))
384e1051a39Sopenharmony_ci            || !TEST_mem_eq(WPACKET_get_curr(&pkt), written[0], simpleder,
385e1051a39Sopenharmony_ci                            sizeof(simpleder)))
386e1051a39Sopenharmony_ci        return cleanup(&pkt);
387e1051a39Sopenharmony_ci
388e1051a39Sopenharmony_ci    /* Generate random packet data for test */
389e1051a39Sopenharmony_ci    if (!TEST_int_gt(RAND_bytes(&testdata2[3], sizeof(testdata2) - 3), 0))
390e1051a39Sopenharmony_ci        return 0;
391e1051a39Sopenharmony_ci
392e1051a39Sopenharmony_ci    /*
393e1051a39Sopenharmony_ci     * Test with a sub-packet that has 2 length bytes. We do 2 passes - first
394e1051a39Sopenharmony_ci     * with a NULL buffer, just to calculate lengths, and a second pass with a
395e1051a39Sopenharmony_ci     * real buffer to actually generate a packet
396e1051a39Sopenharmony_ci     */
397e1051a39Sopenharmony_ci    for (i = 0; i < 2; i++) {
398e1051a39Sopenharmony_ci        if (i == 0) {
399e1051a39Sopenharmony_ci            if (!TEST_true(WPACKET_init_null_der(&pkt)))
400e1051a39Sopenharmony_ci                return 0;
401e1051a39Sopenharmony_ci        } else {
402e1051a39Sopenharmony_ci            if (!TEST_true(WPACKET_init_der(&pkt, sbuf, sizeof(sbuf))))
403e1051a39Sopenharmony_ci                return 0;
404e1051a39Sopenharmony_ci        }
405e1051a39Sopenharmony_ci        if (!TEST_true(WPACKET_start_sub_packet(&pkt))
406e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_memcpy(&pkt, &testdata2[3],
407e1051a39Sopenharmony_ci                                         sizeof(testdata2) - 3))
408e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_close(&pkt))
409e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_finish(&pkt))
410e1051a39Sopenharmony_ci            || !TEST_true(WPACKET_get_total_written(&pkt, &written[i])))
411e1051a39Sopenharmony_ci        return cleanup(&pkt);
412e1051a39Sopenharmony_ci    }
413e1051a39Sopenharmony_ci
414e1051a39Sopenharmony_ci    /*
415e1051a39Sopenharmony_ci     * Check that the size calculated in the first pass equals the size of the
416e1051a39Sopenharmony_ci     * packet actually generated in the second pass. Also check the generated
417e1051a39Sopenharmony_ci     * packet looks as we expect it to.
418e1051a39Sopenharmony_ci     */
419e1051a39Sopenharmony_ci    if (!TEST_size_t_eq(written[0], written[1])
420e1051a39Sopenharmony_ci            || !TEST_mem_eq(WPACKET_get_curr(&pkt), written[1], testdata2,
421e1051a39Sopenharmony_ci                            sizeof(testdata2)))
422e1051a39Sopenharmony_ci        return 0;
423e1051a39Sopenharmony_ci
424e1051a39Sopenharmony_ci    return 1;
425e1051a39Sopenharmony_ci}
426e1051a39Sopenharmony_ci
427e1051a39Sopenharmony_ciint setup_tests(void)
428e1051a39Sopenharmony_ci{
429e1051a39Sopenharmony_ci    if (!TEST_ptr(buf = BUF_MEM_new()))
430e1051a39Sopenharmony_ci            return 0;
431e1051a39Sopenharmony_ci
432e1051a39Sopenharmony_ci    ADD_TEST(test_WPACKET_init);
433e1051a39Sopenharmony_ci    ADD_TEST(test_WPACKET_set_max_size);
434e1051a39Sopenharmony_ci    ADD_TEST(test_WPACKET_start_sub_packet);
435e1051a39Sopenharmony_ci    ADD_TEST(test_WPACKET_set_flags);
436e1051a39Sopenharmony_ci    ADD_TEST(test_WPACKET_allocate_bytes);
437e1051a39Sopenharmony_ci    ADD_TEST(test_WPACKET_memcpy);
438e1051a39Sopenharmony_ci    ADD_TEST(test_WPACKET_init_der);
439e1051a39Sopenharmony_ci    return 1;
440e1051a39Sopenharmony_ci}
441e1051a39Sopenharmony_ci
442e1051a39Sopenharmony_civoid cleanup_tests(void)
443e1051a39Sopenharmony_ci{
444e1051a39Sopenharmony_ci    BUF_MEM_free(buf);
445e1051a39Sopenharmony_ci}
446