prov_config_test.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright 2021-2024 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 <sys/stat.h>
  10. #include <openssl/evp.h>
  11. #include <openssl/conf.h>
  12. #include "testutil.h"
  13. static char *configfile = NULL;
  14. static char *recurseconfigfile = NULL;
  15. static char *pathedconfig = NULL;
  16. /*
  17. * Test to make sure there are no leaks or failures from loading the config
  18. * file twice.
  19. */
  20. static int test_double_config(void)
  21. {
  22. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
  23. int testresult = 0;
  24. EVP_MD *sha256 = NULL;
  25. if (!TEST_ptr(ctx))
  26. return 0;
  27. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
  28. goto err;
  29. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
  30. goto err;
  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(ctx))
  47. goto err;
  48. if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile)))
  49. goto err;
  50. err = ERR_peek_error();
  51. /* We expect to get a recursion error here */
  52. if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE)
  53. testresult = 1;
  54. err:
  55. OSSL_LIB_CTX_free(ctx);
  56. return testresult;
  57. }
  58. #define P_TEST_PATH "/../test/p_test.so"
  59. static int test_path_config(void)
  60. {
  61. OSSL_LIB_CTX *ctx = NULL;
  62. OSSL_PROVIDER *prov;
  63. int testresult = 0;
  64. struct stat sbuf;
  65. char *module_path = getenv("OPENSSL_MODULES");
  66. char *full_path = NULL;
  67. int rc;
  68. if (!TEST_ptr(module_path))
  69. return 0;
  70. full_path = OPENSSL_zalloc(strlen(module_path) + strlen(P_TEST_PATH) + 1);
  71. if (!TEST_ptr(full_path))
  72. return 0;
  73. strcpy(full_path, module_path);
  74. full_path = strcat(full_path, P_TEST_PATH);
  75. TEST_info("full path is %s", full_path);
  76. rc = stat(full_path, &sbuf);
  77. OPENSSL_free(full_path);
  78. if (rc == -1)
  79. return TEST_skip("Skipping modulepath test as provider not present");
  80. if (!TEST_ptr(pathedconfig))
  81. return 0;
  82. ctx = OSSL_LIB_CTX_new();
  83. if (!TEST_ptr(ctx))
  84. return 0;
  85. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, pathedconfig)))
  86. goto err;
  87. /* attempt to manually load the test provider */
  88. if (!TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "test")))
  89. goto err;
  90. OSSL_PROVIDER_unload(prov);
  91. testresult = 1;
  92. err:
  93. OSSL_LIB_CTX_free(ctx);
  94. return testresult;
  95. }
  96. OPT_TEST_DECLARE_USAGE("configfile\n")
  97. int setup_tests(void)
  98. {
  99. if (!test_skip_common_options()) {
  100. TEST_error("Error parsing test options\n");
  101. return 0;
  102. }
  103. if (!TEST_ptr(configfile = test_get_argument(0)))
  104. return 0;
  105. if (!TEST_ptr(recurseconfigfile = test_get_argument(1)))
  106. return 0;
  107. if (!TEST_ptr(pathedconfig = test_get_argument(2)))
  108. return 0;
  109. ADD_TEST(test_recursive_config);
  110. ADD_TEST(test_double_config);
  111. ADD_TEST(test_path_config);
  112. return 1;
  113. }