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