1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
3*e0c4386eSCy Schubert *
4*e0c4386eSCy Schubert * Licensed under the Apache License 2.0 (the "License"). You may not use
5*e0c4386eSCy Schubert * this file except in compliance with the License. You can obtain a copy
6*e0c4386eSCy Schubert * in the file LICENSE in the source distribution or at
7*e0c4386eSCy Schubert * https://www.openssl.org/source/license.html
8*e0c4386eSCy Schubert */
9*e0c4386eSCy Schubert
10*e0c4386eSCy Schubert #include <string.h>
11*e0c4386eSCy Schubert #include <openssl/bio.h>
12*e0c4386eSCy Schubert #include "testutil.h"
13*e0c4386eSCy Schubert
14*e0c4386eSCy Schubert struct ossl_core_bio_st {
15*e0c4386eSCy Schubert int dummy;
16*e0c4386eSCy Schubert BIO *bio;
17*e0c4386eSCy Schubert };
18*e0c4386eSCy Schubert
tst_bio_core_read_ex(OSSL_CORE_BIO * bio,char * data,size_t data_len,size_t * bytes_read)19*e0c4386eSCy Schubert static int tst_bio_core_read_ex(OSSL_CORE_BIO *bio, char *data, size_t data_len,
20*e0c4386eSCy Schubert size_t *bytes_read)
21*e0c4386eSCy Schubert {
22*e0c4386eSCy Schubert return BIO_read_ex(bio->bio, data, data_len, bytes_read);
23*e0c4386eSCy Schubert }
24*e0c4386eSCy Schubert
tst_bio_core_write_ex(OSSL_CORE_BIO * bio,const char * data,size_t data_len,size_t * written)25*e0c4386eSCy Schubert static int tst_bio_core_write_ex(OSSL_CORE_BIO *bio, const char *data,
26*e0c4386eSCy Schubert size_t data_len, size_t *written)
27*e0c4386eSCy Schubert {
28*e0c4386eSCy Schubert return BIO_write_ex(bio->bio, data, data_len, written);
29*e0c4386eSCy Schubert }
30*e0c4386eSCy Schubert
tst_bio_core_gets(OSSL_CORE_BIO * bio,char * buf,int size)31*e0c4386eSCy Schubert static int tst_bio_core_gets(OSSL_CORE_BIO *bio, char *buf, int size)
32*e0c4386eSCy Schubert {
33*e0c4386eSCy Schubert return BIO_gets(bio->bio, buf, size);
34*e0c4386eSCy Schubert }
35*e0c4386eSCy Schubert
tst_bio_core_puts(OSSL_CORE_BIO * bio,const char * str)36*e0c4386eSCy Schubert static int tst_bio_core_puts(OSSL_CORE_BIO *bio, const char *str)
37*e0c4386eSCy Schubert {
38*e0c4386eSCy Schubert return BIO_puts(bio->bio, str);
39*e0c4386eSCy Schubert }
40*e0c4386eSCy Schubert
tst_bio_core_ctrl(OSSL_CORE_BIO * bio,int cmd,long num,void * ptr)41*e0c4386eSCy Schubert static long tst_bio_core_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)
42*e0c4386eSCy Schubert {
43*e0c4386eSCy Schubert return BIO_ctrl(bio->bio, cmd, num, ptr);
44*e0c4386eSCy Schubert }
45*e0c4386eSCy Schubert
tst_bio_core_up_ref(OSSL_CORE_BIO * bio)46*e0c4386eSCy Schubert static int tst_bio_core_up_ref(OSSL_CORE_BIO *bio)
47*e0c4386eSCy Schubert {
48*e0c4386eSCy Schubert return BIO_up_ref(bio->bio);
49*e0c4386eSCy Schubert }
50*e0c4386eSCy Schubert
tst_bio_core_free(OSSL_CORE_BIO * bio)51*e0c4386eSCy Schubert static int tst_bio_core_free(OSSL_CORE_BIO *bio)
52*e0c4386eSCy Schubert {
53*e0c4386eSCy Schubert return BIO_free(bio->bio);
54*e0c4386eSCy Schubert }
55*e0c4386eSCy Schubert
56*e0c4386eSCy Schubert static const OSSL_DISPATCH biocbs[] = {
57*e0c4386eSCy Schubert { OSSL_FUNC_BIO_READ_EX, (void (*)(void))tst_bio_core_read_ex },
58*e0c4386eSCy Schubert { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))tst_bio_core_write_ex },
59*e0c4386eSCy Schubert { OSSL_FUNC_BIO_GETS, (void (*)(void))tst_bio_core_gets },
60*e0c4386eSCy Schubert { OSSL_FUNC_BIO_PUTS, (void (*)(void))tst_bio_core_puts },
61*e0c4386eSCy Schubert { OSSL_FUNC_BIO_CTRL, (void (*)(void))tst_bio_core_ctrl },
62*e0c4386eSCy Schubert { OSSL_FUNC_BIO_UP_REF, (void (*)(void))tst_bio_core_up_ref },
63*e0c4386eSCy Schubert { OSSL_FUNC_BIO_FREE, (void (*)(void))tst_bio_core_free },
64*e0c4386eSCy Schubert { 0, NULL }
65*e0c4386eSCy Schubert };
66*e0c4386eSCy Schubert
test_bio_core(void)67*e0c4386eSCy Schubert static int test_bio_core(void)
68*e0c4386eSCy Schubert {
69*e0c4386eSCy Schubert BIO *cbio = NULL, *cbiobad = NULL;
70*e0c4386eSCy Schubert OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(NULL, biocbs);
71*e0c4386eSCy Schubert int testresult = 0;
72*e0c4386eSCy Schubert OSSL_CORE_BIO corebio;
73*e0c4386eSCy Schubert const char *msg = "Hello world";
74*e0c4386eSCy Schubert char buf[80];
75*e0c4386eSCy Schubert
76*e0c4386eSCy Schubert corebio.bio = BIO_new(BIO_s_mem());
77*e0c4386eSCy Schubert if (!TEST_ptr(corebio.bio)
78*e0c4386eSCy Schubert || !TEST_ptr(libctx)
79*e0c4386eSCy Schubert /*
80*e0c4386eSCy Schubert * Attempting to create a corebio in a libctx that was not
81*e0c4386eSCy Schubert * created via OSSL_LIB_CTX_new_from_dispatch() should fail.
82*e0c4386eSCy Schubert */
83*e0c4386eSCy Schubert || !TEST_ptr_null((cbiobad = BIO_new_from_core_bio(NULL, &corebio)))
84*e0c4386eSCy Schubert || !TEST_ptr((cbio = BIO_new_from_core_bio(libctx, &corebio))))
85*e0c4386eSCy Schubert goto err;
86*e0c4386eSCy Schubert
87*e0c4386eSCy Schubert if (!TEST_int_gt(BIO_puts(corebio.bio, msg), 0)
88*e0c4386eSCy Schubert /* Test a ctrl via BIO_eof */
89*e0c4386eSCy Schubert || !TEST_false(BIO_eof(cbio))
90*e0c4386eSCy Schubert || !TEST_int_gt(BIO_gets(cbio, buf, sizeof(buf)), 0)
91*e0c4386eSCy Schubert || !TEST_true(BIO_eof(cbio))
92*e0c4386eSCy Schubert || !TEST_str_eq(buf, msg))
93*e0c4386eSCy Schubert goto err;
94*e0c4386eSCy Schubert
95*e0c4386eSCy Schubert buf[0] = '\0';
96*e0c4386eSCy Schubert if (!TEST_int_gt(BIO_write(cbio, msg, strlen(msg) + 1), 0)
97*e0c4386eSCy Schubert || !TEST_int_gt(BIO_read(cbio, buf, sizeof(buf)), 0)
98*e0c4386eSCy Schubert || !TEST_str_eq(buf, msg))
99*e0c4386eSCy Schubert goto err;
100*e0c4386eSCy Schubert
101*e0c4386eSCy Schubert testresult = 1;
102*e0c4386eSCy Schubert err:
103*e0c4386eSCy Schubert BIO_free(cbiobad);
104*e0c4386eSCy Schubert BIO_free(cbio);
105*e0c4386eSCy Schubert BIO_free(corebio.bio);
106*e0c4386eSCy Schubert OSSL_LIB_CTX_free(libctx);
107*e0c4386eSCy Schubert return testresult;
108*e0c4386eSCy Schubert }
109*e0c4386eSCy Schubert
setup_tests(void)110*e0c4386eSCy Schubert int setup_tests(void)
111*e0c4386eSCy Schubert {
112*e0c4386eSCy Schubert if (!test_skip_common_options()) {
113*e0c4386eSCy Schubert TEST_error("Error parsing test options\n");
114*e0c4386eSCy Schubert return 0;
115*e0c4386eSCy Schubert }
116*e0c4386eSCy Schubert
117*e0c4386eSCy Schubert ADD_TEST(test_bio_core);
118*e0c4386eSCy Schubert return 1;
119*e0c4386eSCy Schubert }
120