Add a test for the public core bio API

Check that reading/writing to a core bio via BIO_new_from_core_bio()
works as expected.

Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/15072)
This commit is contained in:
Matt Caswell 2021-04-28 13:57:43 +01:00
parent b0ee1de9ab
commit 93954ab050
3 changed files with 124 additions and 1 deletions

107
test/bio_core_test.c Normal file
View File

@ -0,0 +1,107 @@
/*
* Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (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/bio.h>
#include "testutil.h"
struct ossl_core_bio_st {
int dummy;
BIO *bio;
};
static int tst_bio_core_read_ex(OSSL_CORE_BIO *bio, char *data, size_t data_len,
size_t *bytes_read)
{
return BIO_read_ex(bio->bio, data, data_len, bytes_read);
}
static int tst_bio_core_write_ex(OSSL_CORE_BIO *bio, const char *data,
size_t data_len, size_t *written)
{
return BIO_write_ex(bio->bio, data, data_len, written);
}
static int tst_bio_core_gets(OSSL_CORE_BIO *bio, char *buf, int size)
{
return BIO_gets(bio->bio, buf, size);
}
static int tst_bio_core_puts(OSSL_CORE_BIO *bio, const char *str)
{
return BIO_puts(bio->bio, str);
}
static long tst_bio_core_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)
{
return BIO_ctrl(bio->bio, cmd, num, ptr);
}
static const OSSL_DISPATCH biocbs[] = {
{ OSSL_FUNC_BIO_READ_EX, (void (*)(void))tst_bio_core_read_ex },
{ OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))tst_bio_core_write_ex },
{ OSSL_FUNC_BIO_GETS, (void (*)(void))tst_bio_core_gets },
{ OSSL_FUNC_BIO_PUTS, (void (*)(void))tst_bio_core_puts },
{ OSSL_FUNC_BIO_CTRL, (void (*)(void))tst_bio_core_ctrl },
{ 0, NULL }
};
static int test_bio_core(void)
{
BIO *cbio = NULL, *cbiobad = NULL;
OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(biocbs);
int testresult = 0;
OSSL_CORE_BIO corebio;
const char *msg = "Hello world";
char buf[80];
corebio.bio = BIO_new(BIO_s_mem());
if (!TEST_ptr(corebio.bio)
|| !TEST_ptr(libctx)
/*
* Attempting to create a corebio in a libctx that was not
* created via OSSL_LIB_CTX_new_from_dispatch() should fail.
*/
|| !TEST_ptr_null((cbiobad = BIO_new_from_core_bio(NULL, &corebio)))
|| !TEST_ptr((cbio = BIO_new_from_core_bio(libctx, &corebio))))
goto err;
if (!TEST_int_gt(BIO_puts(corebio.bio, msg), 0)
/* Test a ctrl via BIO_eof */
|| !TEST_false(BIO_eof(cbio))
|| !TEST_int_gt(BIO_gets(cbio, buf, sizeof(buf)), 0)
|| !TEST_true(BIO_eof(cbio))
|| !TEST_str_eq(buf, msg))
goto err;
buf[0] = '\0';
if (!TEST_int_gt(BIO_write(cbio, msg, strlen(msg) + 1), 0)
|| !TEST_int_gt(BIO_read(cbio, buf, sizeof(buf)), 0)
|| !TEST_str_eq(buf, msg))
goto err;
testresult = 1;
err:
BIO_free(cbiobad);
BIO_free(cbio);
BIO_free(corebio.bio);
OSSL_LIB_CTX_free(libctx);
return testresult;
}
int setup_tests(void)
{
if (!test_skip_common_options()) {
TEST_error("Error parsing test options\n");
return 0;
}
ADD_TEST(test_bio_core);
return 1;
}

View File

@ -44,7 +44,7 @@ IF[{- !$disabled{tests} -}]
packettest asynctest secmemtest srptest memleaktest stack_test \
dtlsv1listentest ct_test threadstest afalgtest d2i_test \
ssl_test_ctx_test ssl_test x509aux cipherlist_test asynciotest \
bio_callback_test bio_memleak_test param_build_test \
bio_callback_test bio_memleak_test bio_core_test param_build_test \
bioprinttest sslapitest dtlstest sslcorrupttest \
bio_enc_test pkey_meth_test pkey_meth_kdf_test evp_kdf_test uitest \
cipherbytes_test \
@ -320,6 +320,10 @@ IF[{- !$disabled{tests} -}]
INCLUDE[bioprinttest]=../include ../apps/include
DEPEND[bioprinttest]=../libcrypto libtestutil.a
SOURCE[bio_core_test]=bio_core_test.c
INCLUDE[bio_core_test]=../include ../apps/include
DEPEND[bio_core_test]=../libcrypto libtestutil.a
SOURCE[params_api_test]=params_api_test.c
INCLUDE[params_api_test]=../include ../apps/include
DEPEND[params_api_test]=../libcrypto libtestutil.a

View File

@ -0,0 +1,12 @@
#! /usr/bin/env perl
# Copyright 2016 The OpenSSL Project Authors. All Rights Reserved.
#
# Licensed under the Apache License 2.0 (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
use OpenSSL::Test::Simple;
simple_test("test_bio_core", "bio_core_test");