cba7cbaa1df8861875e0529e575422716608de7d
[openssl.git] / test / provider_test.c
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
10 #include <stddef.h>
11 #include <openssl/provider.h>
12 #include "testutil.h"
13
14 #if !defined(DSO_VMS) && !defined(DSO_DLCFN) && !defined(DSO_DL) \
15     && !defined(DSO_WIN32) && !defined(DSO_DLFCN)
16 # define OPENSSL_NO_DSO
17 #endif
18
19
20 extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
21
22 static char buf[256];
23 static size_t buf_l = 0;
24 static OSSL_PARAM greeting_request[] = {
25     { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf), &buf_l },
26     { NULL, 0, NULL, 0, NULL }
27 };
28
29 static int test_provider(const char *name)
30 {
31     OSSL_PROVIDER *prov = NULL;
32     const char *greeting = NULL;
33     char expected_greeting[256];
34
35     BIO_snprintf(expected_greeting, sizeof(expected_greeting),
36                  "Hello OpenSSL %.20s, greetings from %s!",
37                  OPENSSL_VERSION_STR, name);
38
39     return
40         TEST_ptr(prov = OSSL_PROVIDER_load(NULL, name))
41         && TEST_true(OSSL_PROVIDER_get_params(prov, greeting_request))
42         && TEST_ptr(greeting = greeting_request[0].data)
43         && TEST_size_t_gt(greeting_request[0].data_size, 0)
44         && TEST_str_eq(greeting, expected_greeting)
45         && TEST_true(OSSL_PROVIDER_unload(prov));
46 }
47
48 static int test_builtin_provider(void)
49 {
50     const char *name = "p_test_builtin";
51
52     return
53         TEST_true(OSSL_PROVIDER_add_builtin(NULL, name,
54                                             PROVIDER_INIT_FUNCTION_NAME))
55         && test_provider(name);
56 }
57
58 #ifndef OPENSSL_NO_DSO
59 static int test_loaded_provider(void)
60 {
61     const char *name = "p_test";
62
63     return test_provider(name);
64 }
65 #endif
66
67 int setup_tests(void)
68 {
69     ADD_TEST(test_builtin_provider);
70 #ifndef OPENSSL_NO_DSO
71     ADD_TEST(test_loaded_provider);
72 #endif
73     return 1;
74 }
75