prov_config_test.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 <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(configfile))
  26. return 0;
  27. if (!TEST_ptr(ctx))
  28. return 0;
  29. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
  30. return 0;
  31. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, configfile)))
  32. return 0;
  33. /* Check we can actually fetch something */
  34. sha256 = EVP_MD_fetch(ctx, "SHA2-256", NULL);
  35. if (!TEST_ptr(sha256))
  36. goto err;
  37. testresult = 1;
  38. err:
  39. EVP_MD_free(sha256);
  40. OSSL_LIB_CTX_free(ctx);
  41. return testresult;
  42. }
  43. static int test_recursive_config(void)
  44. {
  45. OSSL_LIB_CTX *ctx = OSSL_LIB_CTX_new();
  46. int testresult = 0;
  47. unsigned long err;
  48. if (!TEST_ptr(recurseconfigfile))
  49. goto err;
  50. if (!TEST_ptr(ctx))
  51. goto err;
  52. if (!TEST_false(OSSL_LIB_CTX_load_config(ctx, recurseconfigfile)))
  53. goto err;
  54. err = ERR_peek_error();
  55. /* We expect to get a recursion error here */
  56. if (ERR_GET_REASON(err) == CONF_R_RECURSIVE_SECTION_REFERENCE)
  57. testresult = 1;
  58. err:
  59. OSSL_LIB_CTX_free(ctx);
  60. return testresult;
  61. }
  62. #define P_TEST_PATH "/../test/p_test.so"
  63. static int test_path_config(void)
  64. {
  65. OSSL_LIB_CTX *ctx = NULL;
  66. OSSL_PROVIDER *prov;
  67. int testresult = 0;
  68. struct stat sbuf;
  69. char *module_path = getenv("OPENSSL_MODULES");
  70. char *full_path = NULL;
  71. int rc;
  72. full_path = OPENSSL_zalloc(strlen(module_path) + strlen(P_TEST_PATH) + 1);
  73. if (!TEST_ptr(full_path))
  74. return 0;
  75. strcpy(full_path, module_path);
  76. full_path = strcat(full_path, P_TEST_PATH);
  77. TEST_info("full path is %s", full_path);
  78. rc = stat(full_path, &sbuf);
  79. OPENSSL_free(full_path);
  80. if (rc == -1)
  81. return TEST_skip("Skipping modulepath test as provider not present");
  82. if (!TEST_ptr(pathedconfig))
  83. return 0;
  84. ctx = OSSL_LIB_CTX_new();
  85. if (!TEST_ptr(ctx))
  86. return 0;
  87. if (!TEST_true(OSSL_LIB_CTX_load_config(ctx, pathedconfig)))
  88. goto err;
  89. /* attempt to manually load the test provider */
  90. if (!TEST_ptr(prov = OSSL_PROVIDER_load(ctx, "test")))
  91. goto err;
  92. OSSL_PROVIDER_unload(prov);
  93. testresult = 1;
  94. err:
  95. OSSL_LIB_CTX_free(ctx);
  96. return testresult;
  97. }
  98. OPT_TEST_DECLARE_USAGE("configfile\n")
  99. int setup_tests(void)
  100. {
  101. if (!test_skip_common_options()) {
  102. TEST_error("Error parsing test options\n");
  103. return 0;
  104. }
  105. if (!TEST_ptr(configfile = test_get_argument(0)))
  106. return 0;
  107. if (!TEST_ptr(recurseconfigfile = test_get_argument(1)))
  108. return 0;
  109. if (!TEST_ptr(pathedconfig = test_get_argument(2)))
  110. return 0;
  111. ADD_TEST(test_recursive_config);
  112. ADD_TEST(test_double_config);
  113. ADD_TEST(test_path_config);
  114. return 1;
  115. }