From 6d41fc80e6152a6bf9d062b2a8e835a388ed0062 Mon Sep 17 00:00:00 2001 From: Emilia Kasper Date: Tue, 1 Sep 2015 18:19:14 +0200 Subject: PACKET: add PACKET_memdup and PACKET_strndup Use each once in s3_srvr.c to show how they work. Also fix a bug introduced in c3fc7eeab884b6876a1b4006163f190d325aa047 and made apparent by this change: ssl3_get_next_proto wasn't updating next_proto_negotiated_len Reviewed-by: Matt Caswell --- test/packettest.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) (limited to 'test/packettest.c') diff --git a/test/packettest.c b/test/packettest.c index b3f7bbb19e..23b60857f1 100644 --- a/test/packettest.c +++ b/test/packettest.c @@ -230,6 +230,57 @@ static int test_PACKET_copy_bytes(PACKET *pkt, size_t start) return 1; } +static int test_PACKET_memdup(PACKET *pkt, size_t start) +{ + unsigned char *data = NULL; + size_t len; + if ( !PACKET_goto_bookmark(pkt, start) + || !PACKET_memdup(pkt, &data, &len) + || len != BUF_LEN + || memcmp(data, PACKET_data(pkt), len) + || !PACKET_forward(pkt, 10) + || !PACKET_memdup(pkt, &data, &len) + || len != BUF_LEN - 10 + || memcmp(data, PACKET_data(pkt), len) + || !PACKET_back(pkt, 1) + || !PACKET_memdup(pkt, &data, &len) + || len != BUF_LEN - 9 + || memcmp(data, PACKET_data(pkt), len)) { + fprintf(stderr, "test_PACKET_memdup() failed\n"); + OPENSSL_free(data); + return 0; + } + + OPENSSL_free(data); + return 1; +} + +static int test_PACKET_strndup() +{ + char buf[10], buf2[10]; + memset(buf, 'x', 10); + memset(buf2, 'y', 10); + buf2[5] = '\0'; + char *data = NULL; + PACKET pkt; + + if ( !PACKET_buf_init(&pkt, (unsigned char*)buf, 10) + || !PACKET_strndup(&pkt, &data) + || strlen(data) != 10 + || strncmp(data, buf, 10) + || !PACKET_buf_init(&pkt, (unsigned char*)buf2, 10) + || !PACKET_strndup(&pkt, &data) + || strlen(data) != 5 + || strcmp(data, buf2)) { + fprintf(stderr, "test_PACKET_strndup failed\n"); + OPENSSL_free(data); + return 0; + } + + OPENSSL_free(data); + return 1; +} + static int test_PACKET_move_funcs(PACKET *pkt, size_t start) { unsigned char *byte; @@ -388,6 +439,8 @@ int main(int argc, char **argv) || !test_PACKET_get_sub_packet(&pkt, start) || !test_PACKET_get_bytes(&pkt, start) || !test_PACKET_copy_bytes(&pkt, start) + || !test_PACKET_memdup(&pkt, start) + || !test_PACKET_strndup() || !test_PACKET_move_funcs(&pkt, start) || !test_PACKET_get_length_prefixed_1() || !test_PACKET_get_length_prefixed_2() -- cgit v1.2.1