2 * Copyright 2018 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 static const char expected_format[] =
29 ":error::system library:%s:Operation not permitted:"
30 # ifndef OPENSSL_NO_FILENAMES
36 char *out = NULL, *p = NULL;
40 BIO_snprintf(expected, sizeof(expected), expected_format, OPENSSL_FUNC);
42 if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
45 ERR_PUT_error(ERR_LIB_SYS, 0, 1, "errtest.c", 30);
46 ERR_print_errors(bio);
48 if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0))
50 /* Skip over the variable thread id at the start of the string */
51 for (p = out; *p != ':' && *p != 0; ++p) {
52 if (!TEST_true(IS_HEX(*p)))
55 if (!TEST_true(*p != 0)
56 || !TEST_strn_eq(expected, p, strlen(expected)))
66 /* Test that querying the error queue preserves the OS error. */
67 static int preserves_system_error(void)
69 #if defined(OPENSSL_SYS_WINDOWS)
70 SetLastError(ERROR_INVALID_FUNCTION);
72 return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
76 return TEST_int_eq(errno, EINVAL);
80 /* Test that calls to ERR_add_error_[v]data append */
81 static int vdata_appends(void)
85 CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
86 ERR_add_error_data(1, "hello ");
87 ERR_add_error_data(1, "world");
88 ERR_peek_error_data(&data, NULL);
89 return TEST_str_eq(data, "hello world");
92 static int raised_error(void)
99 * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
100 * number is saved, so no point checking them.
102 #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
107 line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
109 ERR_raise_data(ERR_LIB_SYS, ERR_R_INTERNAL_ERROR,
111 if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0)
112 || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
113 #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
114 || !TEST_int_eq(l, line)
115 || !TEST_str_eq(f, file)
117 || !TEST_str_eq(data, "calling exit()"))
122 int setup_tests(void)
124 ADD_TEST(preserves_system_error);
125 ADD_TEST(vdata_appends);
126 ADD_TEST(raised_error);
127 #ifndef OPENSSL_NO_DEPRECATED_3_0
128 ADD_TEST(test_print_error_format);