s_client.pod: Fix grammar in NOTES section.
[openssl.git] / test / errtest.c
1 /*
2  * Copyright 2018-2020 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     /* Variables used to construct an error line */
29     const char *func = OPENSSL_FUNC;
30 # ifndef OPENSSL_NO_FILENAMES
31     const char *file = OPENSSL_FILE;
32     const int line = OPENSSL_LINE;
33 # else
34     const char *file = "";
35     const int line = 0;
36 # endif
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
48     char *out = NULL, *p = NULL;
49     int ret = 0, len;
50     BIO *bio = NULL;
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();
60
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);
71
72     if (!TEST_ptr(bio = BIO_new(BIO_s_mem())))
73         goto err;
74
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;
89 err:
90     BIO_free(bio);
91     return ret;
92 }
93 #endif
94
95 /* Test that querying the error queue preserves the OS error. */
96 static 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
109 /* Test that calls to ERR_add_error_[v]data append */
110 static int vdata_appends(void)
111 {
112     const char *data;
113
114     CRYPTOerr(0, ERR_R_MALLOC_FAILURE);
115     ERR_add_error_data(1, "hello ");
116     ERR_add_error_data(1, "world");
117     ERR_peek_error_data(&data, NULL);
118     return TEST_str_eq(data, "hello world");
119 }
120
121 static int raised_error(void)
122 {
123     const char *f, *data;
124     int l;
125     unsigned long e;
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)
132     const char *file;
133     int line;
134
135     file = __FILE__;
136     line = __LINE__ + 2; /* The error is generated on the ERR_raise_data line */
137 #endif
138     ERR_raise_data(ERR_LIB_NONE, ERR_R_INTERNAL_ERROR,
139                    "calling exit()");
140     if (!TEST_ulong_ne(e = ERR_get_error_all(&f, &l, NULL, &data, NULL), 0)
141             || !TEST_int_eq(ERR_GET_REASON(e), ERR_R_INTERNAL_ERROR)
142 #if !defined(OPENSSL_NO_FILENAMES) && !defined(OPENSSL_NO_ERR)
143             || !TEST_int_eq(l, line)
144             || !TEST_str_eq(f, file)
145 #endif
146             || !TEST_str_eq(data, "calling exit()"))
147         return 0;
148     return 1;
149 }
150
151 int setup_tests(void)
152 {
153     ADD_TEST(preserves_system_error);
154     ADD_TEST(vdata_appends);
155     ADD_TEST(raised_error);
156 #ifndef OPENSSL_NO_DEPRECATED_3_0
157     ADD_TEST(test_print_error_format);
158 #endif
159     return 1;
160 }