xref: /freebsd/crypto/openssl/test/bio_meth_test.c (revision e7be843b4a162e68651d3911f0357ed464915629)
1*e7be843bSPierre Pronchery /*
2*e7be843bSPierre Pronchery  * Copyright 2024 The OpenSSL Project Authors. All Rights Reserved.
3*e7be843bSPierre Pronchery  *
4*e7be843bSPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*e7be843bSPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*e7be843bSPierre Pronchery  * in the file LICENSE in the source distribution or at
7*e7be843bSPierre Pronchery  * https://www.openssl.org/source/license.html
8*e7be843bSPierre Pronchery  */
9*e7be843bSPierre Pronchery 
10*e7be843bSPierre Pronchery #include <openssl/bio.h>
11*e7be843bSPierre Pronchery #include "testutil.h"
12*e7be843bSPierre Pronchery 
test_bio_meth(void)13*e7be843bSPierre Pronchery static int test_bio_meth(void)
14*e7be843bSPierre Pronchery {
15*e7be843bSPierre Pronchery     int i, ret = 0, id;
16*e7be843bSPierre Pronchery     BIO_METHOD *meth1 = NULL, *meth2 = NULL, *meth3 = NULL;
17*e7be843bSPierre Pronchery     BIO *membio = NULL, *bio1 = NULL, *bio2 = NULL, *bio3 = NULL;
18*e7be843bSPierre Pronchery 
19*e7be843bSPierre Pronchery     id = BIO_get_new_index();
20*e7be843bSPierre Pronchery     if (!TEST_int_eq(id, BIO_TYPE_START + 1))
21*e7be843bSPierre Pronchery         goto err;
22*e7be843bSPierre Pronchery 
23*e7be843bSPierre Pronchery     if (!TEST_ptr(meth1 = BIO_meth_new(id, "Method1"))
24*e7be843bSPierre Pronchery         || !TEST_ptr(meth2 = BIO_meth_new(BIO_TYPE_NONE, "Method2"))
25*e7be843bSPierre Pronchery         || !TEST_ptr(meth3 = BIO_meth_new(BIO_TYPE_NONE|BIO_TYPE_FILTER, "Method3"))
26*e7be843bSPierre Pronchery         || !TEST_ptr(bio1 = BIO_new(meth1))
27*e7be843bSPierre Pronchery         || !TEST_ptr(bio2 = BIO_new(meth2))
28*e7be843bSPierre Pronchery         || !TEST_ptr(bio3 = BIO_new(meth3))
29*e7be843bSPierre Pronchery         || !TEST_ptr(membio = BIO_new(BIO_s_mem())))
30*e7be843bSPierre Pronchery         goto err;
31*e7be843bSPierre Pronchery 
32*e7be843bSPierre Pronchery     BIO_set_next(bio3, bio2);
33*e7be843bSPierre Pronchery     BIO_set_next(bio2, bio1);
34*e7be843bSPierre Pronchery     BIO_set_next(bio1, membio);
35*e7be843bSPierre Pronchery 
36*e7be843bSPierre Pronchery     /* Check that we get an error if we exhaust BIO_get_new_index() */
37*e7be843bSPierre Pronchery     for (i = id + 1; i <= BIO_TYPE_MASK; ++i) {
38*e7be843bSPierre Pronchery         if (!TEST_int_eq(BIO_get_new_index(), i))
39*e7be843bSPierre Pronchery             goto err;
40*e7be843bSPierre Pronchery     }
41*e7be843bSPierre Pronchery     if (!TEST_int_eq(BIO_get_new_index(), -1))
42*e7be843bSPierre Pronchery         goto err;
43*e7be843bSPierre Pronchery 
44*e7be843bSPierre Pronchery     /* test searching works */
45*e7be843bSPierre Pronchery     if (!TEST_ptr_eq(BIO_find_type(bio3, BIO_TYPE_MEM), membio)
46*e7be843bSPierre Pronchery         || !TEST_ptr_eq(BIO_find_type(bio3, id), bio1))
47*e7be843bSPierre Pronchery         goto err;
48*e7be843bSPierre Pronchery 
49*e7be843bSPierre Pronchery     /* Check searching for BIO_TYPE_NONE returns NULL */
50*e7be843bSPierre Pronchery     if (!TEST_ptr_null(BIO_find_type(bio3, BIO_TYPE_NONE)))
51*e7be843bSPierre Pronchery         goto err;
52*e7be843bSPierre Pronchery     /* Check searching for BIO_TYPE_NONE + BIO_TYPE_FILTER works */
53*e7be843bSPierre Pronchery     if (!TEST_ptr_eq(BIO_find_type(bio3, BIO_TYPE_FILTER), bio3))
54*e7be843bSPierre Pronchery         goto err;
55*e7be843bSPierre Pronchery     ret = 1;
56*e7be843bSPierre Pronchery err:
57*e7be843bSPierre Pronchery     BIO_free(membio);
58*e7be843bSPierre Pronchery     BIO_free(bio3);
59*e7be843bSPierre Pronchery     BIO_free(bio2);
60*e7be843bSPierre Pronchery     BIO_free(bio1);
61*e7be843bSPierre Pronchery     BIO_meth_free(meth3);
62*e7be843bSPierre Pronchery     BIO_meth_free(meth2);
63*e7be843bSPierre Pronchery     BIO_meth_free(meth1);
64*e7be843bSPierre Pronchery     return ret;
65*e7be843bSPierre Pronchery }
66*e7be843bSPierre Pronchery 
setup_tests(void)67*e7be843bSPierre Pronchery int setup_tests(void)
68*e7be843bSPierre Pronchery {
69*e7be843bSPierre Pronchery     ADD_TEST(test_bio_meth);
70*e7be843bSPierre Pronchery     return 1;
71*e7be843bSPierre Pronchery }
72