Raise an error on syscall failure in tls_retry_write_records
[openssl.git] / test / ossl_store_test.c
1 /*
2  * Copyright 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 <limits.h>
11 #include <openssl/store.h>
12 #include <openssl/ui.h>
13 #include "testutil.h"
14
15 #ifndef PATH_MAX
16 # if defined(_WIN32) && defined(_MAX_PATH)
17 #  define PATH_MAX _MAX_PATH
18 # else
19 #  define PATH_MAX 4096
20 # endif
21 #endif
22
23 typedef enum OPTION_choice {
24     OPT_ERR = -1,
25     OPT_EOF = 0,
26     OPT_INFILE,
27     OPT_DATADIR,
28     OPT_TEST_ENUM
29 } OPTION_CHOICE;
30
31 static const char *infile = NULL;
32 static const char *datadir = NULL;
33
34 static int test_store_open(void)
35 {
36     int ret = 0;
37     OSSL_STORE_CTX *sctx = NULL;
38     OSSL_STORE_SEARCH *search = NULL;
39     UI_METHOD *ui_method = NULL;
40
41     ret = TEST_ptr(search = OSSL_STORE_SEARCH_by_alias("nothing"))
42           && TEST_ptr(ui_method= UI_create_method("DummyUI"))
43           && TEST_ptr(sctx = OSSL_STORE_open_ex(infile, NULL, NULL, ui_method,
44                                                 NULL, NULL, NULL))
45           && TEST_false(OSSL_STORE_find(sctx, NULL))
46           && TEST_true(OSSL_STORE_find(sctx, search));
47     UI_destroy_method(ui_method);
48     OSSL_STORE_SEARCH_free(search);
49     OSSL_STORE_close(sctx);
50     return ret;
51 }
52
53 static int test_store_search_by_key_fingerprint_fail(void)
54 {
55     int ret;
56     OSSL_STORE_SEARCH *search = NULL;
57
58     ret = TEST_ptr_null(search = OSSL_STORE_SEARCH_by_key_fingerprint(
59                                      EVP_sha256(), NULL, 0));
60     OSSL_STORE_SEARCH_free(search);
61     return ret;
62 }
63
64 static int get_params(const char *uri, const char *type)
65 {
66     EVP_PKEY *pkey = NULL;
67     OSSL_STORE_CTX *ctx = NULL;
68     OSSL_STORE_INFO *info;
69     int ret = 0;
70
71     ctx = OSSL_STORE_open_ex(uri, NULL, NULL, NULL, NULL, NULL, NULL);
72     if (!TEST_ptr(ctx))
73         goto err;
74
75     while (!OSSL_STORE_eof(ctx)
76             && (info = OSSL_STORE_load(ctx)) != NULL
77             && pkey == NULL) {
78         if (OSSL_STORE_INFO_get_type(info) == OSSL_STORE_INFO_PARAMS) {
79             pkey = OSSL_STORE_INFO_get1_PARAMS(info);
80         }
81         OSSL_STORE_INFO_free(info);
82         info = NULL;
83     }
84
85     if (pkey != NULL)
86         ret = EVP_PKEY_is_a(pkey, type);
87     EVP_PKEY_free(pkey);
88
89  err:
90     OSSL_STORE_close(ctx);
91     return ret;
92 }
93
94 static int test_store_get_params(int idx)
95 {
96     const char *type;
97     char uri[PATH_MAX];
98
99     switch(idx) {
100 #ifndef OPENSSL_NO_DH
101     case 0:
102         type = "DH";
103         break;
104     case 1:
105         type = "DHX";
106         break;
107 #else
108     case 0:
109     case 1:
110         return 1;
111 #endif
112     case 2:
113 #ifndef OPENSSL_NO_DSA
114         type = "DSA";
115         break;
116 #else
117         return 1;
118 #endif
119     default:
120         TEST_error("Invalid test index");
121         return 0;
122     }
123
124     if (!TEST_true(BIO_snprintf(uri, sizeof(uri), "%s/%s-params.pem",
125                                 datadir, type)))
126         return 0;
127
128     TEST_info("Testing uri: %s", uri);
129     if (!TEST_true(get_params(uri, type)))
130         return 0;
131
132     return 1;
133 }
134
135 /*
136  * This test verifies that calling OSSL_STORE_ATTACH does not set an
137  * "unregistered scheme" error when called.
138  */
139 static int test_store_attach_unregistered_scheme(void)
140 {
141     int ret;
142     OSSL_STORE_CTX *store_ctx;
143     OSSL_PROVIDER *provider;
144     OSSL_LIB_CTX *libctx;
145     BIO *bio;
146     libctx = OSSL_LIB_CTX_new();
147     provider = OSSL_PROVIDER_load(libctx, "default");
148     bio = BIO_new_file("test/certs/sm2-root.crt", "r");
149
150     ret = TEST_ptr(store_ctx = OSSL_STORE_attach(bio, "file", libctx, NULL,
151                                                  NULL, NULL, NULL, NULL)) &&
152           TEST_int_ne(ERR_GET_LIB(ERR_peek_error()), ERR_LIB_OSSL_STORE) &&
153           TEST_int_ne(ERR_GET_REASON(ERR_peek_error()),
154                       OSSL_STORE_R_UNREGISTERED_SCHEME);
155
156     BIO_free(bio);
157     OSSL_STORE_close(store_ctx);
158     OSSL_PROVIDER_unload(provider);
159     OSSL_LIB_CTX_free(libctx);
160     return ret;
161 }
162
163 const OPTIONS *test_get_options(void)
164 {
165     static const OPTIONS test_options[] = {
166         OPT_TEST_OPTIONS_DEFAULT_USAGE,
167         { "in", OPT_INFILE, '<', },
168         { "data", OPT_DATADIR, 's' },
169         { NULL }
170     };
171     return test_options;
172 }
173
174 int setup_tests(void)
175 {
176     OPTION_CHOICE o;
177
178     while ((o = opt_next()) != OPT_EOF) {
179         switch (o) {
180         case OPT_INFILE:
181             infile = opt_arg();
182             break;
183         case OPT_DATADIR:
184             datadir = opt_arg();
185             break;
186         case OPT_TEST_CASES:
187            break;
188         default:
189         case OPT_ERR:
190             return 0;
191         }
192     }
193
194     if (datadir == NULL) {
195         TEST_error("No datadir specified");
196         return 0;
197     }
198
199     ADD_TEST(test_store_open);
200     ADD_TEST(test_store_search_by_key_fingerprint_fail);
201     ADD_ALL_TESTS(test_store_get_params, 3);
202     ADD_TEST(test_store_attach_unregistered_scheme);
203     return 1;
204 }