]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/ossl_store_test.c
fix some code with obvious wrong coding style
[thirdparty/openssl.git] / test / ossl_store_test.c
CommitLineData
34816949 1/*
8020d79b 2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
34816949
SL
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
511fb472 10#include <string.h>
d27a8e92 11#include <limits.h>
34816949
SL
12#include <openssl/store.h>
13#include <openssl/ui.h>
14#include "testutil.h"
15
d27a8e92
RL
16#ifndef PATH_MAX
17# if defined(_WIN32) && defined(_MAX_PATH)
18# define PATH_MAX _MAX_PATH
19# else
20# define PATH_MAX 4096
21# endif
22#endif
23
34816949
SL
24typedef enum OPTION_choice {
25 OPT_ERR = -1,
26 OPT_EOF = 0,
903a6558 27 OPT_INPUTDIR,
34816949 28 OPT_INFILE,
903a6558 29 OPT_SM2FILE,
1950e0e3 30 OPT_DATADIR,
34816949
SL
31 OPT_TEST_ENUM
32} OPTION_CHOICE;
33
903a6558 34static const char *inputdir = NULL;
34816949 35static const char *infile = NULL;
903a6558 36static const char *sm2file = NULL;
1950e0e3 37static const char *datadir = NULL;
34816949
SL
38
39static int test_store_open(void)
40{
41 int ret = 0;
42 OSSL_STORE_CTX *sctx = NULL;
6e417f95 43 OSSL_STORE_SEARCH *search = NULL;
34816949 44 UI_METHOD *ui_method = NULL;
903a6558 45 char *input = test_mk_file_path(inputdir, infile);
34816949 46
903a6558
P
47 ret = TEST_ptr(input)
48 && TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing"))
6e417f95 49 && TEST_ptr(ui_method= UI_create_method("DummyUI"))
903a6558 50 && TEST_ptr(sctx = OSSL_STORE_open_ex(input, NULL, NULL, ui_method,
d382e796 51 NULL, NULL, NULL, NULL))
6e417f95
SL
52 && TEST_false(OSSL_STORE_find(sctx, NULL))
53 && TEST_true(OSSL_STORE_find(sctx, search));
34816949 54 UI_destroy_method(ui_method);
6e417f95 55 OSSL_STORE_SEARCH_free(search);
34816949 56 OSSL_STORE_close(sctx);
903a6558 57 OPENSSL_free(input);
34816949
SL
58 return ret;
59}
60
97f7a6d4
SL
61static int test_store_search_by_key_fingerprint_fail(void)
62{
63 int ret;
64 OSSL_STORE_SEARCH *search = NULL;
65
66 ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint(
67 EVP_sha256(), NULL, 0));
68 OSSL_STORE_SEARCH_free(search);
69 return ret;
70}
71
1950e0e3
MC
72static int get_params(const char *uri, const char *type)
73{
74 EVP_PKEY *pkey = NULL;
75 OSSL_STORE_CTX *ctx = NULL;
76 OSSL_STORE_INFO *info;
77 int ret = 0;
78
d382e796 79 ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
1950e0e3
MC
80 if (!TEST_ptr(ctx))
81 goto err;
82
83 while (!OSSL_STORE_eof(ctx)
84 && (info = OSSL_STORE_load(ctx)) != NULL
85 && pkey == NULL) {
86 if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
87 pkey = OSSL_STORE_INFO_get1_PARAMS(info);
88 }
89 OSSL_STORE_INFO_free(info);
90 info = NULL;
91 }
92
93 if (pkey != NULL)
94 ret = EVP_PKEY_is_a(pkey, type);
95 EVP_PKEY_free(pkey);
96
97 err:
98 OSSL_STORE_close(ctx);
99 return ret;
100}
101
102static int test_store_get_params(int idx)
103{
104 const char *type;
511fb472 105 const char *urifmt;
d27a8e92 106 char uri[PATH_MAX];
1950e0e3 107
1287dabd 108 switch (idx) {
1950e0e3
MC
109#ifndef OPENSSL_NO_DH
110 case 0:
111 type = "DH";
112 break;
113 case 1:
114 type = "DHX";
115 break;
116#else
117 case 0:
118 case 1:
119 return 1;
120#endif
121 case 2:
122#ifndef OPENSSL_NO_DSA
123 type = "DSA";
124 break;
125#else
126 return 1;
127#endif
128 default:
129 TEST_error("Invalid test index");
130 return 0;
131 }
132
511fb472
RL
133 urifmt = "%s/%s-params.pem";
134#ifdef __VMS
135 {
136 char datadir_end = datadir[strlen(datadir) - 1];
137
138 if (datadir_end == ':' || datadir_end == ']' || datadir_end == '>')
139 urifmt = "%s%s-params.pem";
140 }
141#endif
142 if (!TEST_true(BIO_snprintf(uri, sizeof(uri), urifmt, datadir, type)))
1950e0e3
MC
143 return 0;
144
145 TEST_info("Testing uri: %s", uri);
146 if (!TEST_true(get_params(uri, type)))
147 return 0;
148
149 return 1;
150}
151
c60b5723
DB
152/*
153 * This test verifies that calling OSSL_STORE_ATTACH does not set an
154 * "unregistered scheme" error when called.
155 */
156static int test_store_attach_unregistered_scheme(void)
157{
158 int ret;
141cc94e
P
159 OSSL_STORE_CTX *store_ctx = NULL;
160 OSSL_PROVIDER *provider = NULL;
161 OSSL_LIB_CTX *libctx = NULL;
162 BIO *bio = NULL;
163 char *input = test_mk_file_path(inputdir, sm2file);
164
165 ret = TEST_ptr(input)
166 && TEST_ptr(libctx = OSSL_LIB_CTX_new())
167 && TEST_ptr(provider = OSSL_PROVIDER_load(libctx, "default"))
168 && TEST_ptr(bio = BIO_new_file(input, "r"))
169 && TEST_ptr(store_ctx = OSSL_STORE_attach(bio, "file", libctx, NULL,
d382e796 170 NULL, NULL, NULL, NULL, NULL))
141cc94e
P
171 && TEST_int_ne(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OSSL_STORE)
172 && TEST_int_ne(ERR_GET_REASON(ERR_peek_error()),
173 OSSL_STORE_R_UNREGISTERED_SCHEME);
c60b5723
DB
174
175 BIO_free(bio);
176 OSSL_STORE_close(store_ctx);
177 OSSL_PROVIDER_unload(provider);
178 OSSL_LIB_CTX_free(libctx);
903a6558 179 OPENSSL_free(input);
c60b5723
DB
180 return ret;
181}
1950e0e3 182
34816949
SL
183const OPTIONS *test_get_options(void)
184{
185 static const OPTIONS test_options[] = {
186 OPT_TEST_OPTIONS_DEFAULT_USAGE,
903a6558
P
187 { "dir", OPT_INPUTDIR, '/' },
188 { "in", OPT_INFILE, '<' },
189 { "sm2", OPT_SM2FILE, '<' },
1950e0e3 190 { "data", OPT_DATADIR, 's' },
34816949
SL
191 { NULL }
192 };
193 return test_options;
194}
195
196int setup_tests(void)
197{
198 OPTION_CHOICE o;
199
200 while ((o = opt_next()) != OPT_EOF) {
201 switch (o) {
903a6558
P
202 case OPT_INPUTDIR:
203 inputdir = opt_arg();
204 break;
34816949
SL
205 case OPT_INFILE:
206 infile = opt_arg();
207 break;
903a6558
P
208 case OPT_SM2FILE:
209 sm2file = opt_arg();
210 break;
1950e0e3
MC
211 case OPT_DATADIR:
212 datadir = opt_arg();
213 break;
34816949
SL
214 case OPT_TEST_CASES:
215 break;
216 default:
217 case OPT_ERR:
218 return 0;
219 }
220 }
221
1950e0e3 222 if (datadir == NULL) {
903a6558
P
223 TEST_error("No data directory specified");
224 return 0;
225 }
226 if (inputdir == NULL) {
227 TEST_error("No input directory specified");
1950e0e3
MC
228 return 0;
229 }
230
903a6558
P
231 if (infile != NULL)
232 ADD_TEST(test_store_open);
97f7a6d4 233 ADD_TEST(test_store_search_by_key_fingerprint_fail);
1950e0e3 234 ADD_ALL_TESTS(test_store_get_params, 3);
903a6558
P
235 if (sm2file != NULL)
236 ADD_TEST(test_store_attach_unregistered_scheme);
34816949
SL
237 return 1;
238}