]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/errtest.c
Fix errtest for older compilers
[thirdparty/openssl.git] / test / errtest.c
1 /*
2 * Copyright 2018 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 <string.h>
11 #include <openssl/opensslconf.h>
12 #include <openssl/err.h>
13 #include <openssl/macros.h>
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
23 #ifndef OPENSSL_NO_DEPRECATED_3_0
24 # define IS_HEX(ch) ((ch >= '0' && ch <='9') || (ch >= 'A' && ch <='F'))
25
26 static int test_print_error_format(void)
27 {
28 static const char expected_format[] =
29 ":error::system library:%s:Operation not permitted:"
30 # ifndef OPENSSL_NO_FILENAMES
31 "errtest.c:30:";
32 # else
33 ":0:";
34 # endif
35 char expected[256];
36 char *out = NULL, *p = NULL;
37 int ret = 0, len;
38 BIO *bio = NULL;
39
40 BIO_snprintf(expected, sizeof(expected), expected_format, OPENSSL_FUNC);
41
42 if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
43 return 0;
44
45 ERR_PUT_error(ERR_LIB_SYS, 0, 1, "errtest.c", 30);
46 ERR_print_errors(bio);
47
48 if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0))
49 goto err;
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)))
53 goto err;
54 }
55 if (!TEST_true(*p != 0)
56 || !TEST_strn_eq(expected, p, strlen(expected)))
57 goto err;
58
59 ret = 1;
60 err:
61 BIO_free(bio);
62 return ret;
63 }
64 #endif
65
66 /* Test that querying the error queue preserves the OS error. */
67 static int preserves_system_error(void)
68 {
69 #if defined(OPENSSL_SYS_WINDOWS)
70 SetLastError(ERROR_INVALID_FUNCTION);
71 ERR_get_error();
72 return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
73 #else
74 errno = EINVAL;
75 ERR_get_error();
76 return TEST_int_eq(errno, EINVAL);
77 #endif
78 }
79
80 /* Test that calls to ERR_add_error_[v]data append */
81 static int vdata_appends(void)
82 {
83 const char *data;
84
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");
90 }
91
92 static int raised_error(void)
93 {
94 const char *f, *data;
95 int l;
96 unsigned long e;
97
98 /*
99 * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
100 * number is saved, so no point checking them.
101 */
102 #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
103 const char *file;
104 int line;
105
106 file = __FILE__;
107 line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
108 #endif
109 ERR_raise_data(ERR_LIB_SYS, ERR_R_INTERNAL_ERROR,
110 "calling exit()");
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)
116 #endif
117 || !TEST_str_eq(data, "calling exit()"))
118 return 0;
119 return 1;
120 }
121
122 int setup_tests(void)
123 {
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);
129 #endif
130 return 1;
131 }