]> git.ipfire.org Git - thirdparty/openssl.git/blob - test/errtest.c
cc2f6612d144ee6145ede291f5be86bae5788671
[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
14 #include "testutil.h"
15
16 #if defined(OPENSSL_SYS_WINDOWS)
17 # include <windows.h>
18 #else
19 # include <errno.h>
20 #endif
21
22 #ifndef OPENSSL_NO_DEPRECATED_3_0
23 # define IS_HEX(ch) ((ch >= '0' && ch <='9') || (ch >= 'A' && ch <='F'))
24
25 static int test_print_error_format(void)
26 {
27 static const char expected[] =
28 ":error::system library:test_print_error_format:Operation not permitted:"
29 # ifndef OPENSSL_NO_FILENAMES
30 "errtest.c:30:";
31 # else
32 ":0:";
33 # endif
34 char *out = NULL, *p = NULL;
35 int ret = 0, len;
36 BIO *bio = NULL;
37
38 if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
39 return 0;
40
41 ERR_PUT_error(ERR_LIB_SYS, 0, 1, "errtest.c", 30);
42 ERR_print_errors(bio);
43
44 if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0))
45 goto err;
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)))
49 goto err;
50 }
51 if (!TEST_true(*p != 0)
52 || !TEST_strn_eq(expected, p, strlen(expected)))
53 goto err;
54
55 ret = 1;
56 err:
57 BIO_free(bio);
58 return ret;
59 }
60 #endif
61
62 /* Test that querying the error queue preserves the OS error. */
63 static int preserves_system_error(void)
64 {
65 #if defined(OPENSSL_SYS_WINDOWS)
66 SetLastError(ERROR_INVALID_FUNCTION);
67 ERR_get_error();
68 return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
69 #else
70 errno = EINVAL;
71 ERR_get_error();
72 return TEST_int_eq(errno, EINVAL);
73 #endif
74 }
75
76 /* Test that calls to ERR_add_error_[v]data append */
77 static int vdata_appends(void)
78 {
79 const char *data;
80
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");
86 }
87
88 static int raised_error(void)
89 {
90 const char *f, *data;
91 int l;
92 unsigned long e;
93
94 /*
95 * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
96 * number is saved, so no point checking them.
97 */
98 #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
99 const char *file;
100 int line;
101
102 file = __FILE__;
103 line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
104 #endif
105 ERR_raise_data(ERR_LIB_SYS, ERR_R_INTERNAL_ERROR,
106 "calling exit()");
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)
112 #endif
113 || !TEST_str_eq(data, "calling exit()"))
114 return 0;
115 return 1;
116 }
117
118 int setup_tests(void)
119 {
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);
125 #endif
126 return 1;
127 }