test/params_test.c: Add API test case, and mixed methods
[openssl.git] / test / provider_internal_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 "internal/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 extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
20
21 static char buf[256];
22 static size_t buf_l = 0;
23 static OSSL_PARAM greeting_request[] = {
24     { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf), &buf_l },
25     { NULL, 0, NULL, 0, NULL }
26 };
27
28 static int test_provider(OSSL_PROVIDER *prov)
29 {
30     const char *name = NULL;
31     const char *greeting = NULL;
32     char expected_greeting[256];
33     int ret = 0;
34
35     if (!TEST_ptr(name = ossl_provider_name(prov)))
36         return 0;
37
38     snprintf(expected_greeting, sizeof(expected_greeting),
39              "Hello OpenSSL %.20s, greetings from %s!",
40              OPENSSL_VERSION_STR, name);
41
42     ret =
43         TEST_true(ossl_provider_activate(prov))
44         && TEST_true(ossl_provider_get_params(prov, greeting_request))
45         && TEST_ptr(greeting = greeting_request[0].data)
46         && TEST_size_t_gt(greeting_request[0].data_size, 0)
47         && TEST_str_eq(greeting, expected_greeting);
48
49     ossl_provider_free(prov);
50     return ret;
51 }
52
53 static int test_builtin_provider(void)
54 {
55     const char *name = "p_test_builtin";
56     OSSL_PROVIDER *prov = NULL;
57
58     return
59         TEST_ptr(prov =
60                  ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME))
61         && test_provider(prov);
62 }
63
64 #ifndef OPENSSL_NO_DSO
65 static int test_loaded_provider(void)
66 {
67     const char *name = "p_test";
68     OSSL_PROVIDER *prov = NULL;
69
70     return
71         TEST_ptr(prov = ossl_provider_new(NULL, name, NULL))
72         && test_provider(prov);
73 }
74 #endif
75
76 int setup_tests(void)
77 {
78     ADD_TEST(test_builtin_provider);
79 #ifndef OPENSSL_NO_DSO
80     ADD_TEST(test_loaded_provider);
81 #endif
82     return 1;
83 }
84