2019-01-21 07:47:02 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* 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 <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <openssl/buffer.h>
|
|
|
|
#include <openssl/bio.h>
|
|
|
|
|
|
|
|
#include "testutil.h"
|
|
|
|
|
|
|
|
static int test_bio_memleak(void)
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
BIO *bio;
|
|
|
|
BUF_MEM bufmem;
|
2019-03-01 00:55:38 +00:00
|
|
|
static const char str[] = "BIO test\n";
|
2019-01-21 07:47:02 +00:00
|
|
|
char buf[100];
|
|
|
|
|
|
|
|
bio = BIO_new(BIO_s_mem());
|
2019-03-01 00:55:38 +00:00
|
|
|
if (!TEST_ptr(bio))
|
2019-01-21 07:47:02 +00:00
|
|
|
goto finish;
|
2019-03-01 00:55:38 +00:00
|
|
|
bufmem.length = sizeof(str);
|
2019-01-21 07:47:02 +00:00
|
|
|
bufmem.data = (char *) str;
|
|
|
|
bufmem.max = bufmem.length;
|
|
|
|
BIO_set_mem_buf(bio, &bufmem, BIO_NOCLOSE);
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
|
2019-03-01 00:55:38 +00:00
|
|
|
if (!TEST_int_eq(BIO_read(bio, buf, sizeof(buf)), sizeof(str)))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(buf, sizeof(str), str, sizeof(str)))
|
|
|
|
goto finish;
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
BIO_free(bio);
|
|
|
|
return ok;
|
|
|
|
}
|
2019-01-21 07:47:02 +00:00
|
|
|
|
2019-03-01 00:55:38 +00:00
|
|
|
static int test_bio_get_mem(void)
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
BIO *bio = NULL;
|
|
|
|
BUF_MEM *bufmem = NULL;
|
2019-01-21 07:47:02 +00:00
|
|
|
|
2019-03-01 00:55:38 +00:00
|
|
|
bio = BIO_new(BIO_s_mem());
|
|
|
|
if (!TEST_ptr(bio))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
|
|
|
|
goto finish;
|
|
|
|
BIO_get_mem_ptr(bio, &bufmem);
|
|
|
|
if (!TEST_ptr(bufmem))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_gt(BIO_set_close(bio, BIO_NOCLOSE), 0))
|
|
|
|
goto finish;
|
|
|
|
BIO_free(bio);
|
|
|
|
bio = NULL;
|
|
|
|
if (!TEST_mem_eq(bufmem->data, bufmem->length, "Hello World\n", 12))
|
|
|
|
goto finish;
|
|
|
|
ok = 1;
|
2019-01-21 07:47:02 +00:00
|
|
|
|
|
|
|
finish:
|
|
|
|
BIO_free(bio);
|
2019-03-01 00:55:38 +00:00
|
|
|
BUF_MEM_free(bufmem);
|
2019-01-21 07:47:02 +00:00
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2019-04-03 17:07:00 +00:00
|
|
|
static int test_bio_new_mem_buf(void)
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
BIO *bio;
|
|
|
|
BUF_MEM *bufmem;
|
|
|
|
char data[16];
|
|
|
|
|
|
|
|
bio = BIO_new_mem_buf("Hello World\n", 12);
|
|
|
|
if (!TEST_ptr(bio))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(data, 5, "Hello", 5))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_lt(BIO_write(bio, "test", 4), 0))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_eq(BIO_read(bio, data, 16), 7))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(data, 7, " World\n", 7))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_gt(BIO_reset(bio), 0))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
|
|
|
|
goto finish;
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
BIO_free(bio);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int test_bio_rdonly_mem_buf(void)
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
BIO *bio, *bio2 = NULL;
|
|
|
|
BUF_MEM *bufmem;
|
|
|
|
char data[16];
|
|
|
|
|
|
|
|
bio = BIO_new_mem_buf("Hello World\n", 12);
|
|
|
|
if (!TEST_ptr(bio))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_eq(BIO_read(bio, data, 5), 5))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(data, 5, "Hello", 5))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_gt(BIO_get_mem_ptr(bio, &bufmem), 0))
|
|
|
|
goto finish;
|
|
|
|
(void)BIO_set_close(bio, BIO_NOCLOSE);
|
|
|
|
|
|
|
|
bio2 = BIO_new(BIO_s_mem());
|
|
|
|
if (!TEST_ptr(bio2))
|
|
|
|
goto finish;
|
|
|
|
BIO_set_mem_buf(bio2, bufmem, BIO_CLOSE);
|
|
|
|
BIO_set_flags(bio2, BIO_FLAGS_MEM_RDONLY);
|
|
|
|
|
|
|
|
if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(data, 7, " World\n", 7))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_gt(BIO_reset(bio2), 0))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_eq(BIO_read(bio2, data, 16), 7))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(data, 7, " World\n", 7))
|
|
|
|
goto finish;
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
BIO_free(bio);
|
|
|
|
BIO_free(bio2);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2019-04-04 07:49:36 +00:00
|
|
|
static int test_bio_rdwr_rdonly(void)
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
BIO *bio = NULL;
|
|
|
|
char data[16];
|
|
|
|
|
|
|
|
bio = BIO_new(BIO_s_mem());
|
|
|
|
if (!TEST_ptr(bio))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_MEM_RDONLY);
|
|
|
|
if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_gt(BIO_reset(bio), 0))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_MEM_RDONLY);
|
|
|
|
if (!TEST_int_eq(BIO_puts(bio, "Hi!\n"), 4))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_eq(BIO_read(bio, data, 16), 16))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
if (!TEST_mem_eq(data, 16, "Hello World\nHi!\n", 16))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
BIO_free(bio);
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2019-06-18 14:41:48 +00:00
|
|
|
static int test_bio_nonclear_rst(void)
|
|
|
|
{
|
|
|
|
int ok = 0;
|
|
|
|
BIO *bio = NULL;
|
|
|
|
char data[16];
|
|
|
|
|
|
|
|
bio = BIO_new(BIO_s_mem());
|
|
|
|
if (!TEST_ptr(bio))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_eq(BIO_puts(bio, "Hello World\n"), 12))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
BIO_set_flags(bio, BIO_FLAGS_NONCLEAR_RST);
|
|
|
|
|
|
|
|
if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_int_gt(BIO_reset(bio), 0))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
if (!TEST_int_eq(BIO_read(bio, data, 16), 12))
|
|
|
|
goto finish;
|
|
|
|
if (!TEST_mem_eq(data, 12, "Hello World\n", 12))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
BIO_clear_flags(bio, BIO_FLAGS_NONCLEAR_RST);
|
|
|
|
if (!TEST_int_gt(BIO_reset(bio), 0))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
if (!TEST_int_lt(BIO_read(bio, data, 16), 1))
|
|
|
|
goto finish;
|
|
|
|
|
|
|
|
ok = 1;
|
|
|
|
|
|
|
|
finish:
|
|
|
|
BIO_free(bio);
|
|
|
|
return ok;
|
|
|
|
}
|
2019-04-04 07:49:36 +00:00
|
|
|
|
2019-01-21 07:47:02 +00:00
|
|
|
int global_init(void)
|
|
|
|
{
|
|
|
|
CRYPTO_set_mem_debug(1);
|
|
|
|
CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int setup_tests(void)
|
|
|
|
{
|
|
|
|
ADD_TEST(test_bio_memleak);
|
2019-03-01 00:55:38 +00:00
|
|
|
ADD_TEST(test_bio_get_mem);
|
2019-04-03 17:07:00 +00:00
|
|
|
ADD_TEST(test_bio_new_mem_buf);
|
|
|
|
ADD_TEST(test_bio_rdonly_mem_buf);
|
2019-04-04 07:49:36 +00:00
|
|
|
ADD_TEST(test_bio_rdwr_rdonly);
|
2019-06-18 14:41:48 +00:00
|
|
|
ADD_TEST(test_bio_nonclear_rst);
|
2019-01-21 07:47:02 +00:00
|
|
|
return 1;
|
|
|
|
}
|