nodefltctxtest.c 1.7 KB

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