]> git.ipfire.org Git - thirdparty/openssl.git/blame - test/errtest.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / test / errtest.c
CommitLineData
2de108df 1/*
00c405b3 2 * Copyright 2018-2020 The OpenSSL Project Authors. All Rights Reserved.
2de108df 3 *
909f1a2e 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2de108df
DB
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
f32af93c 10#include <string.h>
2de108df
DB
11#include <openssl/opensslconf.h>
12#include <openssl/err.h>
3d518d3d 13#include <openssl/macros.h>
2de108df
DB
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
f32af93c
SL
23#ifndef OPENSSL_NO_DEPRECATED_3_0
24# define IS_HEX(ch) ((ch >= '0' && ch <='9') || (ch >= 'A' && ch <='F'))
25
26static int test_print_error_format(void)
27{
17b7f896
RL
28 /* Variables used to construct an error line */
29 const char *func = OPENSSL_FUNC;
f32af93c 30# ifndef OPENSSL_NO_FILENAMES
17b7f896
RL
31 const char *file = OPENSSL_FILE;
32 const int line = OPENSSL_LINE;
f32af93c 33# else
17b7f896
RL
34 const char *file = "";
35 const int line = 0;
f32af93c 36# endif
17b7f896
RL
37 /* The format for OpenSSL error lines */
38 const char *expected_format = ":error::system library:%s:%s:%s:%d";
39 /*-
40 * ^^ ^^ ^^ ^^
41 * function name -------------------------------------++ || || ||
42 * reason string (system error string) ------------------++ || ||
43 * file name -----------------------------------------------++ ||
44 * line number ------------------------------------------------++
45 */
46 char expected[512];
47
f32af93c
SL
48 char *out = NULL, *p = NULL;
49 int ret = 0, len;
50 BIO *bio = NULL;
17b7f896
RL
51 const int syserr = EPERM;
52 int reasoncode;
53
54 /*
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.
58 */
59 ERR_set_mark();
f32af93c 60
17b7f896
RL
61 ERR_PUT_error(ERR_LIB_SYS, 0, syserr, file, line);
62 reasoncode = ERR_GET_REASON(ERR_peek_error());
63
64 if (!TEST_int_eq(reasoncode, syserr)) {
65 ERR_pop_to_mark();
66 goto err;
67 }
68
69 BIO_snprintf(expected, sizeof(expected), expected_format,
70 func, strerror(syserr), file, line);
3d518d3d 71
f32af93c 72 if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
17b7f896 73 goto err;
f32af93c 74
f32af93c
SL
75 ERR_print_errors(bio);
76
77 if (!TEST_int_gt(len = BIO_get_mem_data(bio, &out), 0))
78 goto err;
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)))
82 goto err;
83 }
84 if (!TEST_true(*p != 0)
85 || !TEST_strn_eq(expected, p, strlen(expected)))
86 goto err;
87
88 ret = 1;
89err:
90 BIO_free(bio);
91 return ret;
92}
93#endif
94
2de108df
DB
95/* Test that querying the error queue preserves the OS error. */
96static int preserves_system_error(void)
97{
98#if defined(OPENSSL_SYS_WINDOWS)
99 SetLastError(ERROR_INVALID_FUNCTION);
100 ERR_get_error();
101 return TEST_int_eq(GetLastError(), ERROR_INVALID_FUNCTION);
102#else
103 errno = EINVAL;
104 ERR_get_error();
105 return TEST_int_eq(errno, EINVAL);
106#endif
107}
108
8908d18c
RS
109/* Test that calls to ERR_add_error_[v]data append */
110static int vdata_appends(void)
111{
112 const char *data;
113
9311d0c4 114 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
8908d18c
RS
115 ERR_add_error_data(1, "hello ");
116 ERR_add_error_data(1, "world");
b13342e9 117 ERR_peek_error_data(&data, NULL);
8908d18c
RS
118 return TEST_str_eq(data, "hello world");
119}
120
8648a50a 121static int raised_error(void)
56c3a135 122{
b9a75806 123 const char *f, *data;
56c3a135
RS
124 int l;
125 unsigned long e;
8648a50a
RL
126
127 /*
128 * When OPENSSL_NO_ERR or OPENSSL_NO_FILENAMES, no file name or line
129 * number is saved, so no point checking them.
130 */
131#if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
b9a75806
MC
132 const char *file;
133 int line;
56c3a135 134
add8c8e9 135 file = __FILE__;
b9a75806
MC
136 line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
137#endif
17b7f896 138 ERR_raise_data(ERR_LIB_NONE, ERR_R_INTERNAL_ERROR,
ff988500 139 "calling exit()");
b13342e9 140 if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0)
56c3a135 141 || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
8648a50a 142#if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
56c3a135
RS
143 || !TEST_int_eq(l, line)
144 || !TEST_str_eq(f, file)
b9a75806 145#endif
ff988500 146 || !TEST_str_eq(data, "calling exit()"))
56c3a135
RS
147 return 0;
148 return 1;
149}
150
2de108df
DB
151int setup_tests(void)
152{
153 ADD_TEST(preserves_system_error);
8908d18c 154 ADD_TEST(vdata_appends);
8648a50a 155 ADD_TEST(raised_error);
f32af93c
SL
156#ifndef OPENSSL_NO_DEPRECATED_3_0
157 ADD_TEST(test_print_error_format);
158#endif
2de108df
DB
159 return 1;
160}