util/mknum.pl: Really allow unset ordinals in development
[openssl.git] / test / bio_core_test.c
1 /*
2  * Copyright 2021 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
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
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
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
36 static int tst_bio_core_puts(OSSL_CORE_BIO *bio, const char *str)
37 {
38     return BIO_puts(bio->bio, str);
39 }
40
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
46 static const OSSL_DISPATCH biocbs[] = {
47     { OSSL_FUNC_BIO_READ_EX, (void (*)(void))tst_bio_core_read_ex },
48     { OSSL_FUNC_BIO_WRITE_EX, (void (*)(void))tst_bio_core_write_ex },
49     { OSSL_FUNC_BIO_GETS, (void (*)(void))tst_bio_core_gets },
50     { OSSL_FUNC_BIO_PUTS, (void (*)(void))tst_bio_core_puts },
51     { OSSL_FUNC_BIO_CTRL, (void (*)(void))tst_bio_core_ctrl },
52     { 0, NULL }
53 };
54
55 static int test_bio_core(void)
56 {
57     BIO *cbio = NULL, *cbiobad = NULL;
58     OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(NULL, biocbs);
59     int testresult = 0;
60     OSSL_CORE_BIO corebio;
61     const char *msg = "Hello world";
62     char buf[80];
63
64     corebio.bio = BIO_new(BIO_s_mem());
65     if (!TEST_ptr(corebio.bio)
66             || !TEST_ptr(libctx)
67                /*
68                 * Attempting to create a corebio in a libctx that was not
69                 * created via OSSL_LIB_CTX_new_from_dispatch() should fail.
70                 */
71             || !TEST_ptr_null((cbiobad = BIO_new_from_core_bio(NULL, &corebio)))
72             || !TEST_ptr((cbio = BIO_new_from_core_bio(libctx, &corebio))))
73         goto err;
74
75     if (!TEST_int_gt(BIO_puts(corebio.bio, msg), 0)
76                /* Test a ctrl via BIO_eof */
77             || !TEST_false(BIO_eof(cbio))
78             || !TEST_int_gt(BIO_gets(cbio, buf, sizeof(buf)), 0)
79             || !TEST_true(BIO_eof(cbio))
80             || !TEST_str_eq(buf, msg))
81         goto err;
82
83     buf[0] = '\0';
84     if (!TEST_int_gt(BIO_write(cbio, msg, strlen(msg) + 1), 0)
85             || !TEST_int_gt(BIO_read(cbio, buf, sizeof(buf)), 0)
86             || !TEST_str_eq(buf, msg))
87         goto err;
88
89     testresult = 1;
90  err:
91     BIO_free(cbiobad);
92     BIO_free(cbio);
93     BIO_free(corebio.bio);
94     OSSL_LIB_CTX_free(libctx);
95     return testresult;
96 }
97
98 int setup_tests(void)
99 {
100     if (!test_skip_common_options()) {
101         TEST_error("Error parsing test options\n");
102         return 0;
103     }
104
105     ADD_TEST(test_bio_core);
106     return 1;
107 }