]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/provider_internal_test.c
Fix invalid expression syntax
[thirdparty/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 <openssl/crypto.h>
12 #include "internal/provider.h"
13 #include "testutil.h"
14
15 extern OSSL_provider_init_fn PROVIDER_INIT_FUNCTION_NAME;
16
17 static char buf[256];
18 static size_t buf_l = 0;
19 static OSSL_PARAM greeting_request[] = {
20 { "greeting", OSSL_PARAM_UTF8_STRING, buf, sizeof(buf), &buf_l },
21 { NULL, 0, NULL, 0, NULL }
22 };
23
24 static int test_provider(OSSL_PROVIDER *prov, const char *expected_greeting)
25 {
26 const char *greeting = NULL;
27 int ret = 0;
28
29 ret =
30 TEST_true(ossl_provider_activate(prov))
31 && TEST_true(ossl_provider_get_params(prov, greeting_request))
32 && TEST_ptr(greeting = greeting_request[0].data)
33 && TEST_size_t_gt(greeting_request[0].data_size, 0)
34 && TEST_str_eq(greeting, expected_greeting);
35
36 TEST_info("Got this greeting: %s\n", greeting);
37 ossl_provider_free(prov);
38 return ret;
39 }
40
41 static const char *expected_greeting1(const char *name)
42 {
43 static char expected_greeting[256] = "";
44
45 BIO_snprintf(expected_greeting, sizeof(expected_greeting),
46 "Hello OpenSSL %.20s, greetings from %s!",
47 OPENSSL_VERSION_STR, name);
48
49 return expected_greeting;
50 }
51
52 static int test_builtin_provider(void)
53 {
54 const char *name = "p_test_builtin";
55 OSSL_PROVIDER *prov = NULL;
56
57 return
58 TEST_ptr(prov =
59 ossl_provider_new(NULL, name, PROVIDER_INIT_FUNCTION_NAME))
60 && test_provider(prov, expected_greeting1(name));
61 }
62
63 #ifndef NO_PROVIDER_MODULE
64 static int test_loaded_provider(void)
65 {
66 const char *name = "p_test";
67 OSSL_PROVIDER *prov = NULL;
68
69 return
70 TEST_ptr(prov = ossl_provider_new(NULL, name, NULL))
71 && test_provider(prov, expected_greeting1(name));
72 }
73
74 static int test_configured_provider(void)
75 {
76 const char *name = "p_test_configured";
77 OSSL_PROVIDER *prov = NULL;
78 /* This MUST match the config file */
79 const char *expected_greeting =
80 "Hello OpenSSL, greetings from Test Provider";
81
82 return
83 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL)
84 && TEST_ptr(prov = ossl_provider_find(NULL, name))
85 && test_provider(prov, expected_greeting);
86 }
87 #endif
88
89 int setup_tests(void)
90 {
91 ADD_TEST(test_builtin_provider);
92 #ifndef NO_PROVIDER_MODULE
93 ADD_TEST(test_loaded_provider);
94 ADD_TEST(test_configured_provider);
95 #endif
96 return 1;
97 }
98