]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/errtest.c
Deprecate EC_KEY + Update ec apps to use EVP_PKEY
[thirdparty/openssl.git] / test / errtest.c
CommitLineData
2de108df 1/*
00c405b3 2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
2de108df 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2de108df
DB
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
f32af93c 10#include <string.h>
2de108df
DB
11#include <openssl/opensslconf.h>
12#include <openssl/err.h>
3d518d3d 13#include <openssl/macros.h>
2de108df
DB
14
15#include "testutil.h"
16
17#if defined(OPENSSL_SYS_WINDOWS)
18# include <windows.h>
19#else
20# include <errno.h>
21#endif
22
f32af93c
SL
23#ifndef OPENSSL_NO_DEPRECATED_3_0
24# define IS_HEX(ch) ((ch >= '0' && ch <='9') || (ch >= 'A' && ch <='F'))
25
26static int test_print_error_format(void)
27{
17b7f896 28 /* Variables used to construct an error line */
abcca507 29 char *lib;
17b7f896 30 const char *func = OPENSSL_FUNC;
abcca507
RL
31 char *reason;
32# ifdef OPENSSL_NO_ERR
33 char reasonbuf[255];
34# endif
f32af93c 35# ifndef OPENSSL_NO_FILENAMES
17b7f896
RL
36 const char *file = OPENSSL_FILE;
37 const int line = OPENSSL_LINE;
f32af93c 38# else
17b7f896
RL
39 const char *file = "";
40 const int line = 0;
f32af93c 41# endif
17b7f896 42 /* The format for OpenSSL error lines */
abcca507 43 const char *expected_format = ":error:%08lX:%s:%s:%s:%s:%d";
17b7f896 44 /*-
abcca507
RL
45 * ^^ ^^ ^^ ^^ ^^
46 * "library" name --------------------------++ || || || ||
47 * function name ------------------------------++ || || ||
48 * reason string (system error string) -----------++ || ||
49 * file name ----------------------------------------++ ||
50 * line number -----------------------------------------++
17b7f896
RL
51 */
52 char expected[512];
53
f32af93c
SL
54 char *out = NULL, *p = NULL;
55 int ret = 0, len;
56 BIO *bio = NULL;
17b7f896 57 const int syserr = EPERM;
abcca507
RL
58 unsigned long errorcode;
59 unsigned long reasoncode;
17b7f896
RL
60
61 /*
62 * We set a mark here so we can clear the system error that we generate
63 * with ERR_PUT_error(). That is, after all, just a simulation to verify
64 * ERR_print_errors() output, not a real error.
65 */
66 ERR_set_mark();
f32af93c 67
17b7f896 68 ERR_PUT_error(ERR_LIB_SYS, 0, syserr, file, line);
abcca507
RL
69 errorcode = ERR_peek_error();
70 reasoncode = ERR_GET_REASON(errorcode);
17b7f896
RL
71
72 if (!TEST_int_eq(reasoncode, syserr)) {
73 ERR_pop_to_mark();
74 goto err;
75 }
76
abcca507
RL
77# ifndef OPENSSL_NO_ERR
78 lib = "system library";
79 reason = strerror(syserr);
80# else
81 lib = "lib(2)";
82 BIO_snprintf(reasonbuf, sizeof(reasonbuf), "reason(%lu)", reasoncode);
83 reason = reasonbuf;
84# endif
85
17b7f896 86 BIO_snprintf(expected, sizeof(expected), expected_format,
abcca507 87 errorcode, lib, func, reason, file, line);
3d518d3d 88
f32af93c 89 if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
17b7f896 90 goto err;
f32af93c 91
f32af93c
SL
92 ERR_print_errors(bio);
93
94 if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0))
95 goto err;
96 /* Skip over the variable thread id at the start of the string */
97 for (p = out; *p != ':' && *p != 0; ++p) {
98 if (!TEST_true(IS_HEX(*p)))
99 goto err;
100 }
101 if (!TEST_true(*p != 0)
102 || !TEST_strn_eq(expected, p, strlen(expected)))
103 goto err;
104
105 ret = 1;
106err:
107 BIO_free(bio);
108 return ret;
109}
110#endif
111
2de108df
DB
112/* Test that querying the error queue preserves the OS error. */
113static int preserves_system_error(void)
114{
115#if defined(OPENSSL_SYS_WINDOWS)
116 SetLastError(ERROR_INVALID_FUNCTION);
117 ERR_get_error();
118 return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
119#else
120 errno = EINVAL;
121 ERR_get_error();
122 return TEST_int_eq(errno, EINVAL);
123#endif
124}
125
8908d18c
RS
126/* Test that calls to ERR_add_error_[v]data append */
127static int vdata_appends(void)
128{
129 const char *data;
130
9311d0c4 131 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
8908d18c
RS
132 ERR_add_error_data(1, "hello ");
133 ERR_add_error_data(1, "world");
b13342e9 134 ERR_peek_error_data(&data, NULL);
8908d18c
RS
135 return TEST_str_eq(data, "hello world");
136}
137
8648a50a 138static int raised_error(void)
56c3a135 139{
b9a75806 140 const char *f, *data;
56c3a135
RS
141 int l;
142 unsigned long e;
8648a50a
RL
143
144 /*
145 * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
146 * number is saved, so no point checking them.
147 */
148#if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
b9a75806
MC
149 const char *file;
150 int line;
56c3a135 151
add8c8e9 152 file = __FILE__;
b9a75806
MC
153 line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
154#endif
17b7f896 155 ERR_raise_data(ERR_LIB_NONE, ERR_R_INTERNAL_ERROR,
ff988500 156 "calling exit()");
b13342e9 157 if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0)
56c3a135 158 || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
8648a50a 159#if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
56c3a135
RS
160 || !TEST_int_eq(l, line)
161 || !TEST_str_eq(f, file)
b9a75806 162#endif
ff988500 163 || !TEST_str_eq(data, "calling exit()"))
56c3a135
RS
164 return 0;
165 return 1;
166}
167
c34063d7
MC
168static int test_marks(void)
169{
170 unsigned long mallocfail, shouldnot;
171
172 /* Set an initial error */
173 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
174 mallocfail = ERR_peek_last_error();
175 if (!TEST_ulong_gt(mallocfail, 0))
176 return 0;
177
178 /* Setting and clearing a mark should not affect the error */
179 if (!TEST_true(ERR_set_mark())
180 || !TEST_true(ERR_pop_to_mark())
181 || !TEST_ulong_eq(mallocfail, ERR_peek_last_error())
182 || !TEST_true(ERR_set_mark())
183 || !TEST_true(ERR_clear_last_mark())
184 || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
185 return 0;
186
187 /* Test popping errors */
188 if (!TEST_true(ERR_set_mark()))
189 return 0;
190 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
191 if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error())
192 || !TEST_true(ERR_pop_to_mark())
193 || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
194 return 0;
195
196 /* Nested marks should also work */
197 if (!TEST_true(ERR_set_mark())
198 || !TEST_true(ERR_set_mark()))
199 return 0;
200 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
201 if (!TEST_ulong_ne(mallocfail, ERR_peek_last_error())
202 || !TEST_true(ERR_pop_to_mark())
203 || !TEST_true(ERR_pop_to_mark())
204 || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
205 return 0;
206
207 if (!TEST_true(ERR_set_mark()))
208 return 0;
209 ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
210 shouldnot = ERR_peek_last_error();
211 if (!TEST_ulong_ne(mallocfail, shouldnot)
212 || !TEST_true(ERR_set_mark()))
213 return 0;
214 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
215 if (!TEST_ulong_ne(shouldnot, ERR_peek_last_error())
216 || !TEST_true(ERR_pop_to_mark())
217 || !TEST_ulong_eq(shouldnot, ERR_peek_last_error())
218 || !TEST_true(ERR_pop_to_mark())
219 || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
220 return 0;
221
222 /* Setting and clearing a mark should not affect the errors on the stack */
223 if (!TEST_true(ERR_set_mark()))
224 return 0;
225 ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
226 if (!TEST_true(ERR_clear_last_mark())
227 || !TEST_ulong_eq(shouldnot, ERR_peek_last_error()))
228 return 0;
229
230 /*
231 * Popping where no mark has been set should pop everything - but return
232 * a failure result
233 */
234 if (!TEST_false(ERR_pop_to_mark())
235 || !TEST_ulong_eq(0, ERR_peek_last_error()))
236 return 0;
237
238 /* Clearing where there is no mark should fail */
239 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
240 if (!TEST_false(ERR_clear_last_mark())
241 /* "get" the last error to remove it */
242 || !TEST_ulong_eq(mallocfail, ERR_get_error())
243 || !TEST_ulong_eq(0, ERR_peek_last_error()))
244 return 0;
245
246 /*
247 * Setting a mark where there are no errors in the stack should fail.
248 * NOTE: This is somewhat surprising behaviour but is historically how this
249 * function behaves. In practice we typically set marks without first
250 * checking whether there is anything on the stack - but we also don't
251 * tend to check the success of this function. It turns out to work anyway
252 * because although setting a mark with no errors fails, a subsequent call
253 * to ERR_pop_to_mark() or ERR_clear_last_mark() will do the right thing
254 * anyway (even though they will report a failure result).
255 */
256 if (!TEST_false(ERR_set_mark()))
257 return 0;
258
259 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
260 if (!TEST_true(ERR_set_mark()))
261 return 0;
262 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
263 ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
264
265 /* Should be able to "pop" past 2 errors */
266 if (!TEST_true(ERR_pop_to_mark())
267 || !TEST_ulong_eq(mallocfail, ERR_peek_last_error()))
268 return 0;
269
270 if (!TEST_true(ERR_set_mark()))
271 return 0;
272 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
273 ERR_raise(ERR_LIB_CRYPTO, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
274
275 /* Should be able to "clear" past 2 errors */
276 if (!TEST_true(ERR_clear_last_mark())
277 || !TEST_ulong_eq(shouldnot, ERR_peek_last_error()))
278 return 0;
279
280 /* Clear remaining errors from last test */
281 ERR_clear_error();
282
283 return 1;
284}
285
2de108df
DB
286int setup_tests(void)
287{
288 ADD_TEST(preserves_system_error);
8908d18c 289 ADD_TEST(vdata_appends);
8648a50a 290 ADD_TEST(raised_error);
f32af93c
SL
291#ifndef OPENSSL_NO_DEPRECATED_3_0
292 ADD_TEST(test_print_error_format);
293#endif
c34063d7 294 ADD_TEST(test_marks);
2de108df
DB
295 return 1;
296}