provider_internal_test.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2019-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 <stddef.h>
  10. #include <openssl/crypto.h>
  11. #include "internal/provider.h"
  12. #include "testutil.h"
  13. extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
  14. static char buf[256];
  15. static OSSL_PARAM greeting_request[] = {
  16. { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf), 0 },
  17. { NULL, 0, NULL, 0, 0 }
  18. };
  19. static int test_provider(OSSL_PROVIDER *prov, const char *expected_greeting)
  20. {
  21. const char *greeting = NULL;
  22. int ret = 0;
  23. ret =
  24. TEST_true(ossl_provider_activate(prov, 0))
  25. && TEST_true(ossl_provider_get_params(prov, greeting_request))
  26. && TEST_ptr(greeting = greeting_request[0].data)
  27. && TEST_size_t_gt(greeting_request[0].data_size, 0)
  28. && TEST_str_eq(greeting, expected_greeting)
  29. && TEST_true(ossl_provider_deactivate(prov));
  30. TEST_info("Got this greeting: %s\n", greeting);
  31. ossl_provider_free(prov);
  32. return ret;
  33. }
  34. static const char *expected_greeting1(const char *name)
  35. {
  36. static char expected_greeting[256] = "";
  37. BIO_snprintf(expected_greeting, sizeof(expected_greeting),
  38. "Hello OpenSSL %.20s, greetings from %s!",
  39. OPENSSL_VERSION_STR, name);
  40. return expected_greeting;
  41. }
  42. static int test_builtin_provider(void)
  43. {
  44. const char *name = "p_test_builtin";
  45. OSSL_PROVIDER *prov = NULL;
  46. return
  47. TEST_ptr(prov =
  48. ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME, 0))
  49. && test_provider(prov, expected_greeting1(name));
  50. }
  51. #ifndef NO_PROVIDER_MODULE
  52. static int test_loaded_provider(void)
  53. {
  54. const char *name = "p_test";
  55. OSSL_PROVIDER *prov = NULL;
  56. return
  57. TEST_ptr(prov = ossl_provider_new(NULL, name, NULL, 0))
  58. && test_provider(prov, expected_greeting1(name));
  59. }
  60. static int test_configured_provider(void)
  61. {
  62. const char *name = "p_test_configured";
  63. OSSL_PROVIDER *prov = NULL;
  64. /* This MUST match the config file */
  65. const char *expected_greeting =
  66. "Hello OpenSSL, greetings from Test Provider";
  67. return
  68. TEST_ptr(prov = ossl_provider_find(NULL, name, 0))
  69. && test_provider(prov, expected_greeting);
  70. }
  71. #endif
  72. int setup_tests(void)
  73. {
  74. ADD_TEST(test_builtin_provider);
  75. #ifndef NO_PROVIDER_MODULE
  76. ADD_TEST(test_loaded_provider);
  77. ADD_TEST(test_configured_provider);
  78. #endif
  79. return 1;
  80. }