provider_internal_test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, 0))
  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, 0))
  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. TEST_ptr(prov = ossl_provider_find(NULL, name, 0))
  68. && test_provider(prov, expected_greeting);
  69. }
  70. #endif
  71. int setup_tests(void)
  72. {
  73. ADD_TEST(test_builtin_provider);
  74. #ifndef NO_PROVIDER_MODULE
  75. ADD_TEST(test_loaded_provider);
  76. ADD_TEST(test_configured_provider);
  77. #endif
  78. return 1;
  79. }