prov_config_test.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2021 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 <openssl/conf.h>
  11. #include "testutil.h"
  12. static char *configfile = NULL;
  13. static char *recurseconfigfile = NULL;
  14. /*
  15. * Test to make sure there are no leaks or failures from loading the config
  16. * file twice.
  17. */
  18. static int test_double_config(void)
  19. {
  20. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
  21. int testresult = 0;
  22. EVP_MD *sha256 = NULL;
  23. if (!TEST_ptr(configfile))
  24. return 0;
  25. if (!TEST_ptr(ctx))
  26. return 0;
  27. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
  28. return 0;
  29. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
  30. return 0;
  31. /* Check we can actually fetch something */
  32. sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
  33. if (!TEST_ptr(sha256))
  34. goto err;
  35. testresult = 1;
  36. err:
  37. EVP_MD_free(sha256);
  38. OSSL_LIB_CTX_free(ctx);
  39. return testresult;
  40. }
  41. static int test_recursive_config(void)
  42. {
  43. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
  44. int testresult = 0;
  45. unsigned long err;
  46. if (!TEST_ptr(recurseconfigfile))
  47. goto err;
  48. if (!TEST_ptr(ctx))
  49. goto err;
  50. if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile)))
  51. goto err;
  52. err = ERR_peek_error();
  53. /* We expect to get a recursion error here */
  54. if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE)
  55. testresult = 1;
  56. err:
  57. OSSL_LIB_CTX_free(ctx);
  58. return testresult;
  59. }
  60. OPT_TEST_DECLARE_USAGE("configfile\n")
  61. int setup_tests(void)
  62. {
  63. if (!test_skip_common_options()) {
  64. TEST_error("Error parsing test options\n");
  65. return 0;
  66. }
  67. if (!TEST_ptr(configfile = test_get_argument(0)))
  68. return 0;
  69. if (!TEST_ptr(recurseconfigfile = test_get_argument(1)))
  70. return 0;
  71. ADD_TEST(test_recursive_config);
  72. ADD_TEST(test_double_config);
  73. return 1;
  74. }