2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/opensslconf.h>
12 #include <openssl/err.h>
13 #include <openssl/macros.h>
17 #if defined(OPENSSL_SYS_WINDOWS)
23 #ifndef OPENSSL_NO_DEPRECATED_3_0
24 # define IS_HEX(ch) ((ch >= '0' && ch <='9') || (ch >= 'A' && ch <='F'))
26 static int test_print_error_format(void)
28 /* Variables used to construct an error line */
29 const char *func = OPENSSL_FUNC;
30 # ifndef OPENSSL_NO_FILENAMES
31 const char *file = OPENSSL_FILE;
32 const int line = OPENSSL_LINE;
34 const char *file = "";
37 /* The format for OpenSSL error lines */
38 const char *expected_format = ":error::system library:%s:%s:%s:%d";
41 * function name -------------------------------------++ || || ||
42 * reason string (system error string) ------------------++ || ||
43 * file name -----------------------------------------------++ ||
44 * line number ------------------------------------------------++
48 char *out = NULL, *p = NULL;
51 const int syserr = EPERM;
55 * We set a mark here so we can clear the system error that we generate
56 * with ERR_PUT_error(). That is, after all, just a simulation to verify
57 * ERR_print_errors() output, not a real error.
61 ERR_PUT_error(ERR_LIB_SYS, 0, syserr, file, line);
62 reasoncode = ERR_GET_REASON(ERR_peek_error());
64 if (!TEST_int_eq(reasoncode, syserr)) {
69 BIO_snprintf(expected, sizeof(expected), expected_format,
70 func, strerror(syserr), file, line);
72 if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
75 ERR_print_errors(bio);
77 if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0))
79 /* Skip over the variable thread id at the start of the string */
80 for (p = out; *p != ':' && *p != 0; ++p) {
81 if (!TEST_true(IS_HEX(*p)))
84 if (!TEST_true(*p != 0)
85 || !TEST_strn_eq(expected, p, strlen(expected)))
95 /* Test that querying the error queue preserves the OS error. */
96 static int preserves_system_error(void)
98 #if defined(OPENSSL_SYS_WINDOWS)
99 SetLastError(ERROR_INVALID_FUNCTION);
101 return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
105 return TEST_int_eq(errno, EINVAL);
109 /* Test that calls to ERR_add_error_[v]data append */
110 static int vdata_appends(void)
114 CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
115 ERR_add_error_data(1, "hello ");
116 ERR_add_error_data(1, "world");
117 ERR_peek_error_data(&data, NULL);
118 return TEST_str_eq(data, "hello world");
121 static int raised_error(void)
123 const char *f, *data;
128 * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
129 * number is saved, so no point checking them.
131 #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
136 line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
138 ERR_raise_data(ERR_LIB_NONE, ERR_R_INTERNAL_ERROR,
140 if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0)
141 || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
142 #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
143 || !TEST_int_eq(l, line)
144 || !TEST_str_eq(f, file)
146 || !TEST_str_eq(data, "calling exit()"))
151 int setup_tests(void)
153 ADD_TEST(preserves_system_error);
154 ADD_TEST(vdata_appends);
155 ADD_TEST(raised_error);
156 #ifndef OPENSSL_NO_DEPRECATED_3_0
157 ADD_TEST(test_print_error_format);