Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /*
      2      1.1  christos  * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
      3      1.1  christos  *
      4      1.1  christos  * Licensed under the Apache License 2.0 (the "License").  You may not use
      5      1.1  christos  * this file except in compliance with the License.  You can obtain a copy
      6      1.1  christos  * in the file LICENSE in the source distribution or at
      7      1.1  christos  * https://www.openssl.org/source/license.html
      8      1.1  christos  */
      9      1.1  christos 
     10      1.1  christos #include <openssl/evp.h>
     11      1.1  christos #include "testutil.h"
     12      1.1  christos 
     13      1.1  christos /*
     14      1.1  christos  * Test that the default libctx does not get initialised when using a custom
     15      1.1  christos  * libctx. We assume that this test application has been executed such that the
     16      1.1  christos  * null provider is loaded via the config file.
     17      1.1  christos  */
     18      1.1  christos static int test_no_deflt_ctx_init(void)
     19      1.1  christos {
     20      1.1  christos     int testresult = 0;
     21      1.1  christos     EVP_MD *md = NULL;
     22      1.1  christos     OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
     23      1.1  christos 
     24      1.1  christos     if (!TEST_ptr(ctx))
     25      1.1  christos         return 0;
     26      1.1  christos 
     27      1.1  christos     md = EVP_MD_fetch(ctx, "SHA2-256", NULL);
     28      1.1  christos     if (!TEST_ptr(md))
     29      1.1  christos         goto err;
     30      1.1  christos 
     31      1.1  christos     /*
     32      1.1  christos      * Since we're using a non-default libctx above, the default libctx should
     33      1.1  christos      * not have been initialised via config file, and so it is not too late to
     34      1.1  christos      * use OPENSSL_INIT_NO_LOAD_CONFIG.
     35      1.1  christos      */
     36      1.1  christos     OPENSSL_init_crypto(OPENSSL_INIT_NO_LOAD_CONFIG, NULL);
     37      1.1  christos 
     38      1.1  christos     /*
     39      1.1  christos      * If the config file was incorrectly loaded then the null provider will
     40      1.1  christos      * have been initialised and the default provider loading will have been
     41      1.1  christos      * blocked. If the config file was NOT loaded (as we expect) then the
     42      1.1  christos      * default provider should be available.
     43      1.1  christos      */
     44      1.1  christos     if (!TEST_true(OSSL_PROVIDER_available(NULL, "default")))
     45      1.1  christos         goto err;
     46      1.1  christos     if (!TEST_false(OSSL_PROVIDER_available(NULL, "null")))
     47      1.1  christos         goto err;
     48      1.1  christos 
     49      1.1  christos     testresult = 1;
     50  1.1.1.2  christos err:
     51      1.1  christos     EVP_MD_free(md);
     52      1.1  christos     OSSL_LIB_CTX_free(ctx);
     53      1.1  christos     return testresult;
     54      1.1  christos }
     55      1.1  christos 
     56      1.1  christos int setup_tests(void)
     57      1.1  christos {
     58      1.1  christos     ADD_TEST(test_no_deflt_ctx_init);
     59      1.1  christos     return 1;
     60      1.1  christos }
     61