]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/testutil/provider.c
Use adapted test_get_libctx() for simpler test setup and better error reporting
[thirdparty/openssl.git] / test / testutil / provider.c
1 /*
2 * Copyright 2018-2020 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 "../testutil.h"
11 #include <openssl/provider.h>
12 #include <string.h>
13
14 int test_get_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
15 const char *config_file,
16 OSSL_PROVIDER **provider, const char *module_name)
17 {
18 if ((*libctx = OSSL_LIB_CTX_new()) == NULL) {
19 opt_printf_stderr("Failed to create libctx\n");
20 goto err;
21 }
22
23 if (default_null_prov != NULL
24 && (*default_null_prov = OSSL_PROVIDER_load(NULL, "null")) == NULL) {
25 opt_printf_stderr("Failed to load null provider into default libctx\n");
26 goto err;
27 }
28
29 if (config_file != NULL
30 && !OSSL_LIB_CTX_load_config(*libctx, config_file)) {
31 opt_printf_stderr("Error loading config from file %s\n", config_file);
32 goto err;
33 }
34
35 if (module_name != NULL
36 && (*provider = OSSL_PROVIDER_load(*libctx, module_name)) == NULL) {
37 opt_printf_stderr("Failed to load provider %s\n", module_name);
38 goto err;
39 }
40 return 1;
41
42 err:
43 ERR_print_errors_fp(stderr);
44 return 0;
45 }
46
47 int test_arg_libctx(OSSL_LIB_CTX **libctx, OSSL_PROVIDER **default_null_prov,
48 OSSL_PROVIDER **provider, int argn, const char *usage)
49 {
50 const char *module_name;
51
52 if (!TEST_ptr(module_name = test_get_argument(argn))) {
53 TEST_error("usage: <prog> %s", usage);
54 return 0;
55 }
56 if (strcmp(module_name, "none") == 0)
57 return 1;
58 return test_get_libctx(libctx, default_null_prov,
59 test_get_argument(argn + 1), provider, module_name);
60 }