provider_internal_test.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright 2019 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))
  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_info("Got this greeting: %s\n", greeting);
  30. ossl_provider_free(prov);
  31. return ret;
  32. }
  33. static const char *expected_greeting1(const char *name)
  34. {
  35. static char expected_greeting[256] = "";
  36. BIO_snprintf(expected_greeting, sizeof(expected_greeting),
  37. "Hello OpenSSL %.20s, greetings from %s!",
  38. OPENSSL_VERSION_STR, name);
  39. return expected_greeting;
  40. }
  41. static int test_builtin_provider(void)
  42. {
  43. const char *name = "p_test_builtin";
  44. OSSL_PROVIDER *prov = NULL;
  45. return
  46. TEST_ptr(prov =
  47. ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME))
  48. && test_provider(prov, expected_greeting1(name));
  49. }
  50. #ifndef NO_PROVIDER_MODULE
  51. static int test_loaded_provider(void)
  52. {
  53. const char *name = "p_test";
  54. OSSL_PROVIDER *prov = NULL;
  55. return
  56. TEST_ptr(prov = ossl_provider_new(NULL, name, NULL))
  57. && test_provider(prov, expected_greeting1(name));
  58. }
  59. static int test_configured_provider(void)
  60. {
  61. const char *name = "p_test_configured";
  62. OSSL_PROVIDER *prov = NULL;
  63. /* This MUST match the config file */
  64. const char *expected_greeting =
  65. "Hello OpenSSL, greetings from Test Provider";
  66. return
  67. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL)
  68. && TEST_ptr(prov = ossl_provider_find(NULL, name))
  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. }