2 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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
11 #include <openssl/bio.h>
14 struct ossl_core_bio_st {
19 static int tst_bio_core_read_ex(OSSL_CORE_BIO *bio, char *data, size_t data_len,
22 return BIO_read_ex(bio->bio, data, data_len, bytes_read);
25 static int tst_bio_core_write_ex(OSSL_CORE_BIO *bio, const char *data,
26 size_t data_len, size_t *written)
28 return BIO_write_ex(bio->bio, data, data_len, written);
31 static int tst_bio_core_gets(OSSL_CORE_BIO *bio, char *buf, int size)
33 return BIO_gets(bio->bio, buf, size);
36 static int tst_bio_core_puts(OSSL_CORE_BIO *bio, const char *str)
38 return BIO_puts(bio->bio, str);
41 static long tst_bio_core_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)
43 return BIO_ctrl(bio->bio, cmd, num, ptr);
46 static int tst_bio_core_up_ref(OSSL_CORE_BIO *bio)
48 return BIO_up_ref(bio->bio);
51 static int tst_bio_core_free(OSSL_CORE_BIO *bio)
53 return BIO_free(bio->bio);
56 static const OSSL_DISPATCH biocbs[] = {
57 { OSSL_FUNC_BIO_READ_EX, (void (*)(void))tst_bio_core_read_ex },
58 { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))tst_bio_core_write_ex },
59 { OSSL_FUNC_BIO_GETS, (void (*)(void))tst_bio_core_gets },
60 { OSSL_FUNC_BIO_PUTS, (void (*)(void))tst_bio_core_puts },
61 { OSSL_FUNC_BIO_CTRL, (void (*)(void))tst_bio_core_ctrl },
62 { OSSL_FUNC_BIO_UP_REF, (void (*)(void))tst_bio_core_up_ref },
63 { OSSL_FUNC_BIO_FREE, (void (*)(void))tst_bio_core_free },
67 static int test_bio_core(void)
69 BIO *cbio = NULL, *cbiobad = NULL;
70 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(NULL, biocbs);
72 OSSL_CORE_BIO corebio;
73 const char *msg = "Hello world";
76 corebio.bio = BIO_new(BIO_s_mem());
77 if (!TEST_ptr(corebio.bio)
80 * Attempting to create a corebio in a libctx that was not
81 * created via OSSL_LIB_CTX_new_from_dispatch() should fail.
83 || !TEST_ptr_null((cbiobad = BIO_new_from_core_bio(NULL, &corebio)))
84 || !TEST_ptr((cbio = BIO_new_from_core_bio(libctx, &corebio))))
87 if (!TEST_int_gt(BIO_puts(corebio.bio, msg), 0)
88 /* Test a ctrl via BIO_eof */
89 || !TEST_false(BIO_eof(cbio))
90 || !TEST_int_gt(BIO_gets(cbio, buf, sizeof(buf)), 0)
91 || !TEST_true(BIO_eof(cbio))
92 || !TEST_str_eq(buf, msg))
96 if (!TEST_int_gt(BIO_write(cbio, msg, strlen(msg) + 1), 0)
97 || !TEST_int_gt(BIO_read(cbio, buf, sizeof(buf)), 0)
98 || !TEST_str_eq(buf, msg))
105 BIO_free(corebio.bio);
106 OSSL_LIB_CTX_free(libctx);
110 int setup_tests(void)
112 if (!test_skip_common_options()) {
113 TEST_error("Error parsing test options\n");
117 ADD_TEST(test_bio_core);