]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/wpackettest.c
Add support for initialising WPACKETs from a static buffer
[thirdparty/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 const static unsigned char fixed[] = { 0xff, 0xff, 0xff };
24
25 static BUF_MEM *buf;
26
27 static void testfail(const char *msg, WPACKET *pkt)
28 {
29 fprintf(stderr, "%s", msg);
30 WPACKET_cleanup(pkt);
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 (!WPACKET_init(&pkt, buf)
41 || !WPACKET_put_bytes_u8(&pkt, 0xff)
42 /* Closing a top level WPACKET should fail */
43 || WPACKET_close(&pkt)
44 /* Finishing a top level WPACKET should succeed */
45 || !WPACKET_finish(&pkt)
46 /*
47 * Can't call close or finish on a WPACKET that's already
48 * finished.
49 */
50 || WPACKET_close(&pkt)
51 || WPACKET_finish(&pkt)
52 || !WPACKET_get_total_written(&pkt, &written)
53 || written != sizeof(simple1)
54 || memcmp(buf->data, &simple1, written) != 0) {
55 testfail("test_WPACKET_init():1 failed\n", &pkt);
56 return 0;
57 }
58
59 /* Now try with a one byte length prefix */
60 if (!WPACKET_init_len(&pkt, buf, 1)
61 || !WPACKET_put_bytes_u8(&pkt, 0xff)
62 || !WPACKET_finish(&pkt)
63 || !WPACKET_get_total_written(&pkt, &written)
64 || written != sizeof(simple2)
65 || memcmp(buf->data, &simple2, written) != 0) {
66 testfail("test_WPACKET_init():2 failed\n", &pkt);
67 return 0;
68 }
69
70 /* And a longer length prefix */
71 if (!WPACKET_init_len(&pkt, buf, 4)
72 || !WPACKET_put_bytes_u8(&pkt, 0xff)
73 || !WPACKET_finish(&pkt)
74 || !WPACKET_get_total_written(&pkt, &written)
75 || written != sizeof(simple3)
76 || memcmp(buf->data, &simple3, written) != 0) {
77 testfail("test_WPACKET_init():3 failed\n", &pkt);
78 return 0;
79 }
80
81 if (!WPACKET_init_len(&pkt, buf, 1)) {
82 testfail("test_WPACKET_init():4 failed\n", &pkt);
83 return 0;
84 }
85 for (i = 1; i < 257; i++) {
86 /*
87 * Putting more bytes in than fit for the size of the length prefix
88 * should fail
89 */
90 if ((!WPACKET_put_bytes_u8(&pkt, 0xff)) == (i != 256)) {
91 testfail("test_WPACKET_init():4 failed\n", &pkt);
92 return 0;
93 }
94 }
95 if (!WPACKET_finish(&pkt)) {
96 testfail("test_WPACKET_init():4 failed\n", &pkt);
97 return 0;
98 }
99
100 /* Test initialising from a fixed size buffer */
101 if (!WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0)
102 /* Adding 3 bytes should succeed */
103 || !WPACKET_put_bytes_u24(&pkt, 0xffffff)
104 /* Adding 1 more byte should fail */
105 || WPACKET_put_bytes_u8(&pkt, 0xff)
106 /* Finishing the top level WPACKET should succeed */
107 || !WPACKET_finish(&pkt)
108 || !WPACKET_get_total_written(&pkt, &written)
109 || written != sizeof(fixed)
110 || memcmp(sbuf, fixed, sizeof(sbuf)) != 0
111 /* Initialise with 1 len byte */
112 || !WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1)
113 /* Adding 2 bytes should succeed */
114 || !WPACKET_put_bytes_u16(&pkt, 0xfeff)
115 /* Adding 1 more byte should fail */
116 || WPACKET_put_bytes_u8(&pkt, 0xff)
117 || !WPACKET_finish(&pkt)
118 || !WPACKET_get_total_written(&pkt, &written)
119 || written != sizeof(alloc)
120 || memcmp(sbuf, alloc, written) != 0) {
121 testfail("test_WPACKET_init():5 failed\n", &pkt);
122 return 0;
123 }
124
125 return 1;
126 }
127
128 static int test_WPACKET_set_max_size(void)
129 {
130 WPACKET pkt;
131 size_t written;
132
133 if (!WPACKET_init(&pkt, buf)
134 /*
135 * No previous lenbytes set so we should be ok to set the max
136 * possible max size
137 */
138 || !WPACKET_set_max_size(&pkt, SIZE_MAX)
139 /* We should be able to set it smaller too */
140 || !WPACKET_set_max_size(&pkt, SIZE_MAX -1)
141 /* And setting it bigger again should be ok */
142 || !WPACKET_set_max_size(&pkt, SIZE_MAX)
143 || !WPACKET_finish(&pkt)) {
144 testfail("test_WPACKET_set_max_size():1 failed\n", &pkt);
145 return 0;
146 }
147
148 if (!WPACKET_init_len(&pkt, buf, 1)
149 /*
150 * Should fail because we already consumed 1 byte with the
151 * length
152 */
153 || WPACKET_set_max_size(&pkt, 0)
154 /*
155 * Max size can't be bigger than biggest that will fit in
156 * lenbytes
157 */
158 || WPACKET_set_max_size(&pkt, 0x0101)
159 /* It can be the same as the maximum possible size */
160 || !WPACKET_set_max_size(&pkt, 0x0100)
161 /* Or it can be less */
162 || !WPACKET_set_max_size(&pkt, 0x01)
163 /*
164 * Should fail because packet is already filled
165 */
166 || WPACKET_put_bytes_u8(&pkt, 0xff)
167 /*
168 * You can't put in more bytes than max size
169 */
170 || !WPACKET_set_max_size(&pkt, 0x02)
171 || !WPACKET_put_bytes_u8(&pkt, 0xff)
172 || WPACKET_put_bytes_u8(&pkt, 0xff)
173 || !WPACKET_finish(&pkt)
174 || !WPACKET_get_total_written(&pkt, &written)
175 || written != sizeof(simple2)
176 || memcmp(buf->data, &simple2, written) != 0) {
177 testfail("test_WPACKET_set_max_size():2 failed\n", &pkt);
178 return 0;
179 }
180
181 return 1;
182 }
183
184 static int test_WPACKET_start_sub_packet(void)
185 {
186 WPACKET pkt;
187 size_t written;
188 size_t len;
189
190 if (!WPACKET_init(&pkt, buf)
191 || !WPACKET_start_sub_packet(&pkt)
192 || !WPACKET_put_bytes_u8(&pkt, 0xff)
193 /* Can't finish because we have a sub packet */
194 || WPACKET_finish(&pkt)
195 || !WPACKET_close(&pkt)
196 /* Sub packet is closed so can't close again */
197 || WPACKET_close(&pkt)
198 /* Now a top level so finish should succeed */
199 || !WPACKET_finish(&pkt)
200 || !WPACKET_get_total_written(&pkt, &written)
201 || written != sizeof(simple1)
202 || memcmp(buf->data, &simple1, written) != 0) {
203 testfail("test_WPACKET_start_sub_packet():1 failed\n", &pkt);
204 return 0;
205 }
206
207 /* Single sub-packet with length prefix */
208 if (!WPACKET_init(&pkt, buf)
209 || !WPACKET_start_sub_packet_u8(&pkt)
210 || !WPACKET_put_bytes_u8(&pkt, 0xff)
211 || !WPACKET_close(&pkt)
212 || !WPACKET_finish(&pkt)
213 || !WPACKET_get_total_written(&pkt, &written)
214 || written != sizeof(simple2)
215 || memcmp(buf->data, &simple2, written) != 0) {
216 testfail("test_WPACKET_start_sub_packet():2 failed\n", &pkt);
217 return 0;
218 }
219
220 /* Nested sub-packets with length prefixes */
221 if (!WPACKET_init(&pkt, buf)
222 || !WPACKET_start_sub_packet_u8(&pkt)
223 || !WPACKET_put_bytes_u8(&pkt, 0xff)
224 || !WPACKET_start_sub_packet_u8(&pkt)
225 || !WPACKET_put_bytes_u8(&pkt, 0xff)
226 || !WPACKET_get_length(&pkt, &len)
227 || len != 1
228 || !WPACKET_close(&pkt)
229 || !WPACKET_get_length(&pkt, &len)
230 || len != 3
231 || !WPACKET_close(&pkt)
232 || !WPACKET_finish(&pkt)
233 || !WPACKET_get_total_written(&pkt, &written)
234 || written != sizeof(nestedsub)
235 || memcmp(buf->data, &nestedsub, written) != 0) {
236 testfail("test_WPACKET_start_sub_packet():3 failed\n", &pkt);
237 return 0;
238 }
239
240 /* Sequential sub-packets with length prefixes */
241 if (!WPACKET_init(&pkt, buf)
242 || !WPACKET_start_sub_packet_u8(&pkt)
243 || !WPACKET_put_bytes_u8(&pkt, 0xff)
244 || !WPACKET_close(&pkt)
245 || !WPACKET_start_sub_packet_u8(&pkt)
246 || !WPACKET_put_bytes_u8(&pkt, 0xff)
247 || !WPACKET_close(&pkt)
248 || !WPACKET_finish(&pkt)
249 || !WPACKET_get_total_written(&pkt, &written)
250 || written != sizeof(seqsub)
251 || memcmp(buf->data, &seqsub, written) != 0) {
252 testfail("test_WPACKET_start_sub_packet():4 failed\n", &pkt);
253 return 0;
254 }
255
256 return 1;
257 }
258
259
260 static int test_WPACKET_set_flags(void)
261 {
262 WPACKET pkt;
263 size_t written;
264
265 /* Set packet to be non-zero length */
266 if (!WPACKET_init(&pkt, buf)
267 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
268 /* Should fail because of zero length */
269 || WPACKET_finish(&pkt)
270 || !WPACKET_put_bytes_u8(&pkt, 0xff)
271 || !WPACKET_finish(&pkt)
272 || !WPACKET_get_total_written(&pkt, &written)
273 || written != sizeof(simple1)
274 || memcmp(buf->data, &simple1, written) != 0) {
275 testfail("test_WPACKET_set_flags():1 failed\n", &pkt);
276 return 0;
277 }
278
279 /* Repeat above test in a sub-packet */
280 if (!WPACKET_init(&pkt, buf)
281 || !WPACKET_start_sub_packet(&pkt)
282 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH)
283 /* Should fail because of zero length */
284 || WPACKET_close(&pkt)
285 || !WPACKET_put_bytes_u8(&pkt, 0xff)
286 || !WPACKET_close(&pkt)
287 || !WPACKET_finish(&pkt)
288 || !WPACKET_get_total_written(&pkt, &written)
289 || written != sizeof(simple1)
290 || memcmp(buf->data, &simple1, written) != 0) {
291 testfail("test_WPACKET_set_flags():2 failed\n", &pkt);
292 return 0;
293 }
294
295 /* Set packet to abandon non-zero length */
296 if (!WPACKET_init_len(&pkt, buf, 1)
297 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
298 || !WPACKET_finish(&pkt)
299 || !WPACKET_get_total_written(&pkt, &written)
300 || written != 0) {
301 testfail("test_WPACKET_set_flags():3 failed\n", &pkt);
302 return 0;
303 }
304
305 /* Repeat above test but only abandon a sub-packet */
306 if (!WPACKET_init_len(&pkt, buf, 1)
307 || !WPACKET_start_sub_packet_u8(&pkt)
308 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
309 || !WPACKET_close(&pkt)
310 || !WPACKET_finish(&pkt)
311 || !WPACKET_get_total_written(&pkt, &written)
312 || written != sizeof(empty)
313 || memcmp(buf->data, &empty, written) != 0) {
314 testfail("test_WPACKET_set_flags():4 failed\n", &pkt);
315 return 0;
316 }
317
318 /* And repeat with a non empty sub-packet */
319 if (!WPACKET_init(&pkt, buf)
320 || !WPACKET_start_sub_packet_u8(&pkt)
321 || !WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH)
322 || !WPACKET_put_bytes_u8(&pkt, 0xff)
323 || !WPACKET_close(&pkt)
324 || !WPACKET_finish(&pkt)
325 || !WPACKET_get_total_written(&pkt, &written)
326 || written != sizeof(simple2)
327 || memcmp(buf->data, &simple2, written) != 0) {
328 testfail("test_WPACKET_set_flags():5 failed\n", &pkt);
329 return 0;
330 }
331 return 1;
332 }
333
334 static int test_WPACKET_allocate_bytes(void)
335 {
336 WPACKET pkt;
337 size_t written;
338 unsigned char *bytes;
339
340 if (!WPACKET_init_len(&pkt, buf, 1)
341 || !WPACKET_allocate_bytes(&pkt, 2, &bytes)) {
342 testfail("test_WPACKET_allocate_bytes():1 failed\n", &pkt);
343 return 0;
344 }
345 bytes[0] = 0xfe;
346 bytes[1] = 0xff;
347 if (!WPACKET_finish(&pkt)
348 || !WPACKET_get_total_written(&pkt, &written)
349 || written != sizeof(alloc)
350 || memcmp(buf->data, &alloc, written) != 0) {
351 testfail("test_WPACKET_allocate_bytes():2 failed\n", &pkt);
352 return 0;
353 }
354
355 /* Repeat with WPACKET_sub_allocate_bytes */
356 if (!WPACKET_init_len(&pkt, buf, 1)
357 || !WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)) {
358 testfail("test_WPACKET_allocate_bytes():3 failed\n", &pkt);
359 return 0;
360 }
361 bytes[0] = 0xfe;
362 bytes[1] = 0xff;
363 if (!WPACKET_finish(&pkt)
364 || !WPACKET_get_total_written(&pkt, &written)
365 || written != sizeof(submem)
366 || memcmp(buf->data, &submem, written) != 0) {
367 testfail("test_WPACKET_allocate_bytes():4 failed\n", &pkt);
368 return 0;
369 }
370
371 return 1;
372 }
373
374 static int test_WPACKET_memcpy(void)
375 {
376 WPACKET pkt;
377 size_t written;
378 const unsigned char bytes[] = { 0xfe, 0xff };
379
380 if (!WPACKET_init_len(&pkt, buf, 1)
381 || !WPACKET_memcpy(&pkt, bytes, sizeof(bytes))
382 || !WPACKET_finish(&pkt)
383 || !WPACKET_get_total_written(&pkt, &written)
384 || written != sizeof(alloc)
385 || memcmp(buf->data, &alloc, written) != 0) {
386 testfail("test_WPACKET_memcpy():1 failed\n", &pkt);
387 return 0;
388 }
389
390 /* Repeat with WPACKET_sub_memcpy() */
391 if (!WPACKET_init_len(&pkt, buf, 1)
392 || !WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes))
393 || !WPACKET_finish(&pkt)
394 || !WPACKET_get_total_written(&pkt, &written)
395 || written != sizeof(submem)
396 || memcmp(buf->data, &submem, written) != 0) {
397 testfail("test_WPACKET_memcpy():2 failed\n", &pkt);
398 return 0;
399 }
400
401 return 1;
402 }
403
404 int main(int argc, char *argv[])
405 {
406 BIO *err = NULL;
407 int testresult = 0;
408
409 err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
410
411 CRYPTO_set_mem_debug(1);
412 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
413
414 buf = BUF_MEM_new();
415 if (buf != NULL) {
416 ADD_TEST(test_WPACKET_init);
417 ADD_TEST(test_WPACKET_set_max_size);
418 ADD_TEST(test_WPACKET_start_sub_packet);
419 ADD_TEST(test_WPACKET_set_flags);
420 ADD_TEST(test_WPACKET_allocate_bytes);
421 ADD_TEST(test_WPACKET_memcpy);
422
423 testresult = run_tests(argv[0]);
424
425 BUF_MEM_free(buf);
426 }
427
428 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
429 if (CRYPTO_mem_leaks(err) <= 0)
430 testresult = 1;
431 #endif
432 BIO_free(err);
433
434 if (!testresult)
435 printf("PASS\n");
436
437 return testresult;
438 }
439