1 /* 2 * Copyright 2024 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 <openssl/bio.h> 11 #include "testutil.h" 12 13 static int test_bio_meth(void) 14 { 15 int i, ret = 0, id; 16 BIO_METHOD *meth1 = NULL, *meth2 = NULL, *meth3 = NULL; 17 BIO *membio = NULL, *bio1 = NULL, *bio2 = NULL, *bio3 = NULL; 18 19 id = BIO_get_new_index(); 20 if (!TEST_int_eq(id, BIO_TYPE_START + 1)) 21 goto err; 22 23 if (!TEST_ptr(meth1 = BIO_meth_new(id, "Method1")) 24 || !TEST_ptr(meth2 = BIO_meth_new(BIO_TYPE_NONE, "Method2")) 25 || !TEST_ptr(meth3 = BIO_meth_new(BIO_TYPE_NONE|BIO_TYPE_FILTER, "Method3")) 26 || !TEST_ptr(bio1 = BIO_new(meth1)) 27 || !TEST_ptr(bio2 = BIO_new(meth2)) 28 || !TEST_ptr(bio3 = BIO_new(meth3)) 29 || !TEST_ptr(membio = BIO_new(BIO_s_mem()))) 30 goto err; 31 32 BIO_set_next(bio3, bio2); 33 BIO_set_next(bio2, bio1); 34 BIO_set_next(bio1, membio); 35 36 /* Check that we get an error if we exhaust BIO_get_new_index() */ 37 for (i = id + 1; i <= BIO_TYPE_MASK; ++i) { 38 if (!TEST_int_eq(BIO_get_new_index(), i)) 39 goto err; 40 } 41 if (!TEST_int_eq(BIO_get_new_index(), -1)) 42 goto err; 43 44 /* test searching works */ 45 if (!TEST_ptr_eq(BIO_find_type(bio3, BIO_TYPE_MEM), membio) 46 || !TEST_ptr_eq(BIO_find_type(bio3, id), bio1)) 47 goto err; 48 49 /* Check searching for BIO_TYPE_NONE returns NULL */ 50 if (!TEST_ptr_null(BIO_find_type(bio3, BIO_TYPE_NONE))) 51 goto err; 52 /* Check searching for BIO_TYPE_NONE + BIO_TYPE_FILTER works */ 53 if (!TEST_ptr_eq(BIO_find_type(bio3, BIO_TYPE_FILTER), bio3)) 54 goto err; 55 ret = 1; 56 err: 57 BIO_free(membio); 58 BIO_free(bio3); 59 BIO_free(bio2); 60 BIO_free(bio1); 61 BIO_meth_free(meth3); 62 BIO_meth_free(meth2); 63 BIO_meth_free(meth1); 64 return ret; 65 } 66 67 int setup_tests(void) 68 { 69 ADD_TEST(test_bio_meth); 70 return 1; 71 } 72