]>
git.ipfire.org Git - thirdparty/openssl.git/blob - test/errtest.c
cc2f6612d144ee6145ede291f5be86bae5788671
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>
16 #if defined(OPENSSL_SYS_WINDOWS)
22 #ifndef OPENSSL_NO_DEPRECATED_3_0
23 # define IS_HEX(ch) ((ch >= '0' && ch <='9') || (ch >= 'A' && ch <='F'))
25 static int test_print_error_format(void)
27 static const char expected
[] =
28 ":error::system library:test_print_error_format:Operation not permitted:"
29 # ifndef OPENSSL_NO_FILENAMES
34 char *out
= NULL
, *p
= NULL
;
38 if (!TEST_ptr(bio
= BIO_new(BIO_s_mem())))
41 ERR_PUT_error(ERR_LIB_SYS
, 0, 1, "errtest.c", 30);
42 ERR_print_errors(bio
);
44 if (!TEST_int_gt(len
= BIO_get_mem_data(bio
, &out
), 0))
46 /* Skip over the variable thread id at the start of the string */
47 for (p
= out
; *p
!= ':' && *p
!= 0; ++p
) {
48 if (!TEST_true(IS_HEX(*p
)))
51 if (!TEST_true(*p
!= 0)
52 || !TEST_strn_eq(expected
, p
, strlen(expected
)))
62 /* Test that querying the error queue preserves the OS error. */
63 static int preserves_system_error(void)
65 #if defined(OPENSSL_SYS_WINDOWS)
66 SetLastError(ERROR_INVALID_FUNCTION
);
68 return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION
);
72 return TEST_int_eq(errno
, EINVAL
);
76 /* Test that calls to ERR_add_error_[v]data append */
77 static int vdata_appends(void)
81 CRYPTOerr(0, ERR_R_MALLOC_FAILURE
);
82 ERR_add_error_data(1, "hello ");
83 ERR_add_error_data(1, "world");
84 ERR_peek_error_data(&data
, NULL
);
85 return TEST_str_eq(data
, "hello world");
88 static int raised_error(void)
95 * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
96 * number is saved, so no point checking them.
98 #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
103 line
= __LINE__
+ 2; /* The error is generated on the ERR_raise_data line */
105 ERR_raise_data(ERR_LIB_SYS
, ERR_R_INTERNAL_ERROR
,
107 if (!TEST_ulong_ne(e
= ERR_get_error_all(&f
, &l
, NULL
, &data
, NULL
), 0)
108 || !TEST_int_eq(ERR_GET_REASON(e
), ERR_R_INTERNAL_ERROR
)
109 #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
110 || !TEST_int_eq(l
, line
)
111 || !TEST_str_eq(f
, file
)
113 || !TEST_str_eq(data
, "calling exit()"))
118 int setup_tests(void)
120 ADD_TEST(preserves_system_error
);
121 ADD_TEST(vdata_appends
);
122 ADD_TEST(raised_error
);
123 #ifndef OPENSSL_NO_DEPRECATED_3_0
124 ADD_TEST(test_print_error_format
);