xref: /openssl/test/bio_core_test.c (revision da1c088f)
1 /*
2  * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9 
10 #include <string.h>
11 #include <openssl/bio.h>
12 #include "testutil.h"
13 
14 struct ossl_core_bio_st {
15     int dummy;
16     BIO *bio;
17 };
18 
tst_bio_core_read_ex(OSSL_CORE_BIO * bio,char * data,size_t data_len,size_t * bytes_read)19 static int tst_bio_core_read_ex(OSSL_CORE_BIO *bio, char *data, size_t data_len,
20                                 size_t *bytes_read)
21 {
22     return BIO_read_ex(bio->bio, data, data_len, bytes_read);
23 }
24 
tst_bio_core_write_ex(OSSL_CORE_BIO * bio,const char * data,size_t data_len,size_t * written)25 static int tst_bio_core_write_ex(OSSL_CORE_BIO *bio, const char *data,
26                                  size_t data_len, size_t *written)
27 {
28     return BIO_write_ex(bio->bio, data, data_len, written);
29 }
30 
tst_bio_core_gets(OSSL_CORE_BIO * bio,char * buf,int size)31 static int tst_bio_core_gets(OSSL_CORE_BIO *bio, char *buf, int size)
32 {
33     return BIO_gets(bio->bio, buf, size);
34 }
35 
tst_bio_core_puts(OSSL_CORE_BIO * bio,const char * str)36 static int tst_bio_core_puts(OSSL_CORE_BIO *bio, const char *str)
37 {
38     return BIO_puts(bio->bio, str);
39 }
40 
tst_bio_core_ctrl(OSSL_CORE_BIO * bio,int cmd,long num,void * ptr)41 static long tst_bio_core_ctrl(OSSL_CORE_BIO *bio, int cmd, long num, void *ptr)
42 {
43     return BIO_ctrl(bio->bio, cmd, num, ptr);
44 }
45 
tst_bio_core_up_ref(OSSL_CORE_BIO * bio)46 static int tst_bio_core_up_ref(OSSL_CORE_BIO *bio)
47 {
48     return BIO_up_ref(bio->bio);
49 }
50 
tst_bio_core_free(OSSL_CORE_BIO * bio)51 static int tst_bio_core_free(OSSL_CORE_BIO *bio)
52 {
53     return BIO_free(bio->bio);
54 }
55 
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 },
64     OSSL_DISPATCH_END
65 };
66 
test_bio_core(void)67 static int test_bio_core(void)
68 {
69     BIO *cbio = NULL, *cbiobad = NULL;
70     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(NULL, biocbs);
71     int testresult = 0;
72     OSSL_CORE_BIO corebio;
73     const char *msg = "Hello world";
74     char buf[80];
75 
76     corebio.bio = BIO_new(BIO_s_mem());
77     if (!TEST_ptr(corebio.bio)
78             || !TEST_ptr(libctx)
79                /*
80                 * Attempting to create a corebio in a libctx that was not
81                 * created via OSSL_LIB_CTX_new_from_dispatch() should fail.
82                 */
83             || !TEST_ptr_null((cbiobad = BIO_new_from_core_bio(NULL, &corebio)))
84             || !TEST_ptr((cbio = BIO_new_from_core_bio(libctx, &corebio))))
85         goto err;
86 
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))
93         goto err;
94 
95     buf[0] = '\0';
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))
99         goto err;
100 
101     testresult = 1;
102  err:
103     BIO_free(cbiobad);
104     BIO_free(cbio);
105     BIO_free(corebio.bio);
106     OSSL_LIB_CTX_free(libctx);
107     return testresult;
108 }
109 
setup_tests(void)110 int setup_tests(void)
111 {
112     if (!test_skip_common_options()) {
113         TEST_error("Error parsing test options\n");
114         return 0;
115     }
116 
117     ADD_TEST(test_bio_core);
118     return 1;
119 }
120