xref: /freebsd/crypto/openssl/test/nodefltctxtest.c (revision e0c4386e7e71d93b0edc0c8fa156263fc4a8b0b6)
1*e0c4386eSCy Schubert /*
2*e0c4386eSCy Schubert  * Copyright 2023 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 <openssl/evp.h>
11*e0c4386eSCy Schubert #include "testutil.h"
12*e0c4386eSCy Schubert 
13*e0c4386eSCy Schubert /*
14*e0c4386eSCy Schubert  * Test that the default libctx does not get initialised when using a custom
15*e0c4386eSCy Schubert  * libctx. We assume that this test application has been executed such that the
16*e0c4386eSCy Schubert  * null provider is loaded via the config file.
17*e0c4386eSCy Schubert  */
test_no_deflt_ctx_init(void)18*e0c4386eSCy Schubert static int test_no_deflt_ctx_init(void)
19*e0c4386eSCy Schubert {
20*e0c4386eSCy Schubert     int testresult = 0;
21*e0c4386eSCy Schubert     EVP_MD *md = NULL;
22*e0c4386eSCy Schubert     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
23*e0c4386eSCy Schubert 
24*e0c4386eSCy Schubert     if (!TEST_ptr(ctx))
25*e0c4386eSCy Schubert         return 0;
26*e0c4386eSCy Schubert 
27*e0c4386eSCy Schubert     md = EVP_MD_fetch(ctx, "SHA2-256", NULL);
28*e0c4386eSCy Schubert     if (!TEST_ptr(md))
29*e0c4386eSCy Schubert         goto err;
30*e0c4386eSCy Schubert 
31*e0c4386eSCy Schubert     /*
32*e0c4386eSCy Schubert      * Since we're using a non-default libctx above, the default libctx should
33*e0c4386eSCy Schubert      * not have been initialised via config file, and so it is not too late to
34*e0c4386eSCy Schubert      * use OPENSSL_INIT_NO_LOAD_CONFIG.
35*e0c4386eSCy Schubert      */
36*e0c4386eSCy Schubert     OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
37*e0c4386eSCy Schubert 
38*e0c4386eSCy Schubert     /*
39*e0c4386eSCy Schubert      * If the config file was incorrectly loaded then the null provider will
40*e0c4386eSCy Schubert      * have been initialised and the default provider loading will have been
41*e0c4386eSCy Schubert      * blocked. If the config file was NOT loaded (as we expect) then the
42*e0c4386eSCy Schubert      * default provider should be available.
43*e0c4386eSCy Schubert      */
44*e0c4386eSCy Schubert     if (!TEST_true(OSSL_PROVIDER_available(NULL, "default")))
45*e0c4386eSCy Schubert         goto err;
46*e0c4386eSCy Schubert     if (!TEST_false(OSSL_PROVIDER_available(NULL, "null")))
47*e0c4386eSCy Schubert         goto err;
48*e0c4386eSCy Schubert 
49*e0c4386eSCy Schubert     testresult = 1;
50*e0c4386eSCy Schubert  err:
51*e0c4386eSCy Schubert     EVP_MD_free(md);
52*e0c4386eSCy Schubert     OSSL_LIB_CTX_free(ctx);
53*e0c4386eSCy Schubert     return testresult;
54*e0c4386eSCy Schubert }
55*e0c4386eSCy Schubert 
setup_tests(void)56*e0c4386eSCy Schubert int setup_tests(void)
57*e0c4386eSCy Schubert {
58*e0c4386eSCy Schubert     ADD_TEST(test_no_deflt_ctx_init);
59*e0c4386eSCy Schubert     return 1;
60*e0c4386eSCy Schubert }
61