2016-09-08 17:01:24 +08:00
|
|
|
/*
|
2017-07-18 09:48:27 +08:00
|
|
|
* Copyright 2016-2017 The OpenSSL Project Authors. All Rights Reserved.
|
2016-09-08 17:01:24 +08:00
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include "../ssl/packet_locl.h"
|
|
|
|
#include "testutil.h"
|
|
|
|
|
2017-04-15 00:53:04 +08:00
|
|
|
static const unsigned char simple1[] = { 0xff };
|
|
|
|
static const unsigned char simple2[] = { 0x01, 0xff };
|
|
|
|
static const unsigned char simple3[] = { 0x00, 0x00, 0x00, 0x01, 0xff };
|
|
|
|
static const unsigned char nestedsub[] = { 0x03, 0xff, 0x01, 0xff };
|
|
|
|
static const unsigned char seqsub[] = { 0x01, 0xff, 0x01, 0xff };
|
|
|
|
static const unsigned char empty[] = { 0x00 };
|
|
|
|
static const unsigned char alloc[] = { 0x02, 0xfe, 0xff };
|
|
|
|
static const unsigned char submem[] = { 0x03, 0x02, 0xfe, 0xff };
|
|
|
|
static const unsigned char fixed[] = { 0xff, 0xff, 0xff };
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
static BUF_MEM *buf;
|
|
|
|
|
2017-04-19 04:33:15 +08:00
|
|
|
static int cleanup(WPACKET *pkt)
|
2016-09-08 17:01:24 +08:00
|
|
|
{
|
|
|
|
WPACKET_cleanup(pkt);
|
2017-04-19 04:33:15 +08:00
|
|
|
return 0;
|
2016-09-08 17:01:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int test_WPACKET_init(void)
|
|
|
|
{
|
|
|
|
WPACKET pkt;
|
|
|
|
int i;
|
|
|
|
size_t written;
|
2016-11-08 18:33:35 +08:00
|
|
|
unsigned char sbuf[3];
|
2016-09-08 17:01:24 +08:00
|
|
|
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* Closing a top level WPACKET should fail */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_close(&pkt))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* Finishing a top level WPACKET should succeed */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
2016-09-08 17:01:24 +08:00
|
|
|
/*
|
|
|
|
* Can't call close or finish on a WPACKET that's already
|
|
|
|
* finished.
|
|
|
|
*/
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_false(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* Now try with a one byte length prefix */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* And a longer length prefix */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 4))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, simple3, sizeof(simple3)))
|
|
|
|
return cleanup(&pkt);
|
|
|
|
|
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 1)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
for (i = 1; i < 257; i++) {
|
|
|
|
/*
|
|
|
|
* Putting more bytes in than fit for the size of the length prefix
|
|
|
|
* should fail
|
|
|
|
*/
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_int_eq(WPACKET_put_bytes_u8(&pkt, 0xff), i < 256))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
}
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_finish(&pkt)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
2016-11-08 18:33:35 +08:00
|
|
|
/* Test initialising from a fixed size buffer */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 0))
|
2016-11-08 18:33:35 +08:00
|
|
|
/* Adding 3 bytes should succeed */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_put_bytes_u24(&pkt, 0xffffff))
|
2016-11-08 18:33:35 +08:00
|
|
|
/* Adding 1 more byte should fail */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
|
2016-11-08 18:33:35 +08:00
|
|
|
/* Finishing the top level WPACKET should succeed */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(sbuf, written, fixed, sizeof(sbuf))
|
2016-11-08 18:33:35 +08:00
|
|
|
/* Initialise with 1 len byte */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_init_static_len(&pkt, sbuf, sizeof(sbuf), 1))
|
2016-11-08 18:33:35 +08:00
|
|
|
/* Adding 2 bytes should succeed */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_put_bytes_u16(&pkt, 0xfeff))
|
2016-11-08 18:33:35 +08:00
|
|
|
/* Adding 1 more byte should fail */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(sbuf, written, alloc, sizeof(alloc)))
|
|
|
|
return cleanup(&pkt);
|
2016-11-08 18:33:35 +08:00
|
|
|
|
2016-09-08 17:01:24 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_WPACKET_set_max_size(void)
|
|
|
|
{
|
|
|
|
WPACKET pkt;
|
|
|
|
size_t written;
|
|
|
|
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
2016-09-08 17:01:24 +08:00
|
|
|
/*
|
|
|
|
* No previous lenbytes set so we should be ok to set the max
|
|
|
|
* possible max size
|
|
|
|
*/
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* We should be able to set it smaller too */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX -1))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* And setting it bigger again should be ok */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_set_max_size(&pkt, SIZE_MAX))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-09 07:13:41 +08:00
|
|
|
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
|
2016-09-09 07:13:41 +08:00
|
|
|
/*
|
|
|
|
* Should fail because we already consumed 1 byte with the
|
|
|
|
* length
|
|
|
|
*/
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_set_max_size(&pkt, 0))
|
2016-09-08 17:01:24 +08:00
|
|
|
/*
|
|
|
|
* Max size can't be bigger than biggest that will fit in
|
|
|
|
* lenbytes
|
|
|
|
*/
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_set_max_size(&pkt, 0x0101))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* It can be the same as the maximum possible size */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_set_max_size(&pkt, 0x0100))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* Or it can be less */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_set_max_size(&pkt, 0x01))
|
|
|
|
/* Should fail because packet is already filled */
|
|
|
|
|| !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
/* You can't put in more bytes than max size */
|
|
|
|
|| !TEST_true(WPACKET_set_max_size(&pkt, 0x02))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_false(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_WPACKET_start_sub_packet(void)
|
|
|
|
{
|
|
|
|
WPACKET pkt;
|
|
|
|
size_t written;
|
|
|
|
size_t len;
|
|
|
|
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* Can't finish because we have a sub packet */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* Sub packet is closed so can't close again */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_close(&pkt))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* Now a top level so finish should succeed */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* Single sub-packet with length prefix */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* Nested sub-packets with length prefixes */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_get_length(&pkt, &len))
|
|
|
|
|| !TEST_size_t_eq(len, 1)
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_length(&pkt, &len))
|
|
|
|
|| !TEST_size_t_eq(len, 3)
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* Sequential sub-packets with length prefixes */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, seqsub, sizeof(seqsub)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
2017-01-23 23:09:25 +08:00
|
|
|
/* Nested sub-packets with lengths filled before finish */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_get_length(&pkt, &len))
|
|
|
|
|| !TEST_size_t_eq(len, 1)
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_length(&pkt, &len))
|
|
|
|
|| !TEST_size_t_eq(len, 3)
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_fill_lengths(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, nestedsub, sizeof(nestedsub))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt)))
|
|
|
|
return cleanup(&pkt);
|
2017-01-23 23:09:25 +08:00
|
|
|
|
2016-09-08 17:01:24 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int test_WPACKET_set_flags(void)
|
|
|
|
{
|
|
|
|
WPACKET pkt;
|
|
|
|
size_t written;
|
|
|
|
|
|
|
|
/* Set packet to be non-zero length */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
|
|
|
|| !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* Should fail because of zero length */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* Repeat above test in a sub-packet */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_NON_ZERO_LENGTH))
|
2016-09-08 17:01:24 +08:00
|
|
|
/* Should fail because of zero length */
|
2017-04-19 04:33:15 +08:00
|
|
|
|| !TEST_false(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, simple1, sizeof(simple1)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* Set packet to abandon non-zero length */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
|
|
|
|
|| !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_size_t_eq(written, 0))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* Repeat above test but only abandon a sub-packet */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, empty, sizeof(empty)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* And repeat with a non empty sub-packet */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init(&pkt, buf))
|
|
|
|
|| !TEST_true(WPACKET_start_sub_packet_u8(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_set_flags(&pkt, WPACKET_FLAGS_ABANDON_ON_ZERO_LENGTH))
|
|
|
|
|| !TEST_true(WPACKET_put_bytes_u8(&pkt, 0xff))
|
|
|
|
|| !TEST_true(WPACKET_close(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, simple2, sizeof(simple2)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_WPACKET_allocate_bytes(void)
|
|
|
|
{
|
|
|
|
WPACKET pkt;
|
|
|
|
size_t written;
|
|
|
|
unsigned char *bytes;
|
|
|
|
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
|
|
|
|
|| !TEST_true(WPACKET_allocate_bytes(&pkt, 2, &bytes)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
bytes[0] = 0xfe;
|
|
|
|
bytes[1] = 0xff;
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
2016-09-13 18:32:52 +08:00
|
|
|
/* Repeat with WPACKET_sub_allocate_bytes */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
|
|
|
|
|| !TEST_true(WPACKET_sub_allocate_bytes_u8(&pkt, 2, &bytes)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-13 18:32:52 +08:00
|
|
|
bytes[0] = 0xfe;
|
|
|
|
bytes[1] = 0xff;
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-13 18:32:52 +08:00
|
|
|
|
2016-09-08 17:01:24 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_WPACKET_memcpy(void)
|
|
|
|
{
|
|
|
|
WPACKET pkt;
|
|
|
|
size_t written;
|
|
|
|
const unsigned char bytes[] = { 0xfe, 0xff };
|
|
|
|
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
|
|
|
|
|| !TEST_true(WPACKET_memcpy(&pkt, bytes, sizeof(bytes)))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, alloc, sizeof(alloc)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
/* Repeat with WPACKET_sub_memcpy() */
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_true(WPACKET_init_len(&pkt, buf, 1))
|
|
|
|
|| !TEST_true(WPACKET_sub_memcpy_u8(&pkt, bytes, sizeof(bytes)))
|
|
|
|
|| !TEST_true(WPACKET_finish(&pkt))
|
|
|
|
|| !TEST_true(WPACKET_get_total_written(&pkt, &written))
|
|
|
|
|| !TEST_mem_eq(buf->data, written, submem, sizeof(submem)))
|
|
|
|
return cleanup(&pkt);
|
2016-09-08 17:01:24 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-18 09:48:27 +08:00
|
|
|
int setup_tests(void)
|
2016-09-08 17:01:24 +08:00
|
|
|
{
|
2017-04-19 04:33:15 +08:00
|
|
|
if (!TEST_ptr(buf = BUF_MEM_new()))
|
|
|
|
return 0;
|
2016-09-08 17:01:24 +08:00
|
|
|
|
2017-04-19 04:33:15 +08:00
|
|
|
ADD_TEST(test_WPACKET_init);
|
|
|
|
ADD_TEST(test_WPACKET_set_max_size);
|
|
|
|
ADD_TEST(test_WPACKET_start_sub_packet);
|
|
|
|
ADD_TEST(test_WPACKET_set_flags);
|
|
|
|
ADD_TEST(test_WPACKET_allocate_bytes);
|
|
|
|
ADD_TEST(test_WPACKET_memcpy);
|
2017-07-18 09:48:27 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2016-09-08 17:01:24 +08:00
|
|
|
|
2017-07-18 09:48:27 +08:00
|
|
|
void cleanup_tests(void)
|
|
|
|
{
|
2017-04-19 04:33:15 +08:00
|
|
|
BUF_MEM_free(buf);
|
2016-09-08 17:01:24 +08:00
|
|
|
}
|