summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjsing <>2024-10-02 14:41:46 +0000
committerjsing <>2024-10-02 14:41:46 +0000
commite147a56192d185970d03166257112bcdee324029 (patch)
tree1c001ea47852b9c70f110375f527ccd17208b97c
parent1b4359e60e8f540ae13ad11b386bab4ea2a6c6e9 (diff)
downloadopenbsd-e147a56192d185970d03166257112bcdee324029.tar.gz
openbsd-e147a56192d185970d03166257112bcdee324029.tar.bz2
openbsd-e147a56192d185970d03166257112bcdee324029.zip
Add initial regress for the error stack and ERR_* APIs.
-rw-r--r--src/regress/lib/libcrypto/err/Makefile9
-rw-r--r--src/regress/lib/libcrypto/err/err_test.c201
2 files changed, 210 insertions, 0 deletions
diff --git a/src/regress/lib/libcrypto/err/Makefile b/src/regress/lib/libcrypto/err/Makefile
new file mode 100644
index 0000000000..bfac64cc00
--- /dev/null
+++ b/src/regress/lib/libcrypto/err/Makefile
@@ -0,0 +1,9 @@
1# $OpenBSD: Makefile,v 1.1 2024/10/02 14:41:46 jsing Exp $
2
3PROG = err_test
4LDADD = -lcrypto
5DPADD = ${LIBCRYPTO}
6WARNINGS = Yes
7CFLAGS += -DLIBRESSL_INTERNAL -Werror
8
9.include <bsd.regress.mk>
diff --git a/src/regress/lib/libcrypto/err/err_test.c b/src/regress/lib/libcrypto/err/err_test.c
new file mode 100644
index 0000000000..16fd675e11
--- /dev/null
+++ b/src/regress/lib/libcrypto/err/err_test.c
@@ -0,0 +1,201 @@
1/* $OpenBSD: err_test.c,v 1.1 2024/10/02 14:41:46 jsing Exp $ */
2/*
3 * Copyright (c) 2024 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <string.h>
19
20#include <openssl/bn.h>
21#include <openssl/crypto.h>
22#include <openssl/err.h>
23
24/*
25 * This should also test:
26 * - error handling with more than ERR_NUM_ERRORS.
27 * - multi-threaded use.
28 */
29
30static int
31err_test(void)
32{
33 const char *file, *s;
34 char buf[2048];
35 unsigned long err;
36 int line;
37 int failed = 1;
38
39 ERR_load_crypto_strings();
40
41 ERR_remove_state(0);
42
43 ERR_clear_error();
44
45 if ((err = ERR_peek_error()) != 0) {
46 fprintf(stderr, "FAIL: ERR_peek_error() = %lx, want "
47 "0x0\n", err);
48 goto failure;
49 }
50 if ((err = ERR_get_error()) != 0) {
51 fprintf(stderr, "FAIL: ERR_get_error() = %lx, want "
52 "0x0\n", err);
53 goto failure;
54 }
55
56 ERR_put_error(ERR_LIB_SYS, SYS_F_SOCKET, ERR_R_MALLOC_FAILURE,
57 "sys.c", 100);
58 ERR_put_error(ERR_LIB_BN, BN_F_BN_USUB, BN_R_DIV_BY_ZERO,
59 "bn.c", 200);
60
61 if ((err = ERR_peek_error()) != 0x2004041UL) {
62 fprintf(stderr, "FAIL: ERR_peek_error() = %lx, want "
63 "0x2004041UL\n", err);
64 goto failure;
65 }
66 if ((err = ERR_peek_error_line(&file, &line)) != 0x2004041UL) {
67 fprintf(stderr, "FAIL: ERR_peek_error_line() = %lx, want "
68 "0x2004041\n", err);
69 goto failure;
70 }
71 if (strcmp(file, "sys.c") != 0) {
72 fprintf(stderr, "FAIL: got file '%s', want 'sys.c'", file);
73 goto failure;
74 }
75 if (line != 100) {
76 fprintf(stderr, "FAIL: line = %d, want 100", line);
77 goto failure;
78 }
79
80 if ((err = ERR_peek_last_error()) != 0x3073067UL) {
81 fprintf(stderr, "FAIL: ERR_peek_error() = %lx, want "
82 "0x3073067\n", err);
83 goto failure;
84 }
85 if ((err = ERR_peek_last_error_line(&file, &line)) != 0x3073067UL) {
86 fprintf(stderr, "FAIL: ERR_peek_last_error_line() = %lx, want "
87 "0x3073067\n", err);
88 goto failure;
89 }
90 if (strcmp(file, "bn.c") != 0) {
91 fprintf(stderr, "FAIL: got file '%s', want 'bn.c'", file);
92 goto failure;
93 }
94 if (line != 200) {
95 fprintf(stderr, "FAIL: line = %d, want 200", line);
96 goto failure;
97 }
98
99 if ((err = ERR_get_error()) != 0x2004041UL) {
100 fprintf(stderr, "FAIL: ERR_get_error() = %lx, want "
101 "0x2004041\n", err);
102 goto failure;
103 }
104
105 if ((err = ERR_peek_error()) != 0x3073067UL) {
106 fprintf(stderr, "FAIL: ERR_peek_error() = %lx, want "
107 "0x3073067\n", err);
108 goto failure;
109 }
110
111 if ((err = ERR_get_error_line(&file, &line)) != 0x3073067UL) {
112 fprintf(stderr, "FAIL: ERR_get_error_line() = %lx, want "
113 "0x3073067\n", err);
114 goto failure;
115 }
116 if (strcmp(file, "bn.c") != 0) {
117 fprintf(stderr, "FAIL: got file '%s', want 'bn.c'", file);
118 goto failure;
119 }
120 if (line != 200) {
121 fprintf(stderr, "FAIL: line = %d, want 200", line);
122 goto failure;
123 }
124
125 if ((err = ERR_get_error()) != 0) {
126 fprintf(stderr, "FAIL: ERR_get_error() = %lx, want "
127 "0x0\n", err);
128 goto failure;
129 }
130
131 ERR_clear_error();
132
133 s = ERR_lib_error_string(0x3fff067UL);
134 if (strcmp(s, "bignum routines") != 0) {
135 fprintf(stderr, "FAIL: ERR_lib_error_string() = '%s', "
136 "want 'bignum routines'\n", s);
137 goto failure;
138 }
139
140 s = ERR_func_error_string(0x3fff067UL);
141 if (strcmp(s, "CRYPTO_internal") != 0) {
142 fprintf(stderr, "FAIL: ERR_func_error_string() = '%s', "
143 "want 'CRYPTO_internal'\n", s);
144 goto failure;
145 }
146
147 s = ERR_reason_error_string(0x3fff067UL);
148 if (strcmp(s, "div by zero") != 0) {
149 fprintf(stderr, "FAIL: ERR_reason_error_string() = '%s', "
150 "want 'div by zero'\n", s);
151 goto failure;
152 }
153
154 ERR_remove_state(0);
155
156 s = ERR_error_string(0x3fff067UL, NULL);
157 if (strcmp(s, "error:03FFF067:bignum routines:CRYPTO_internal:div by zero") != 0) {
158 fprintf(stderr, "FAIL: ERR_error_string() = '%s', "
159 "want 'error:03FFF067:bignum routines:CRYPTO_internal:div by zero'\n", s);
160 goto failure;
161 }
162 memset(buf, 0xdb, sizeof(buf));
163 s = ERR_error_string(0x3fff067UL, buf);
164 if (s != buf) {
165 fprintf(stderr, "FAIL: ERR_error_string() did not "
166 "return buffer\n");
167 goto failure;
168 }
169 if (strcmp(s, "error:03FFF067:bignum routines:CRYPTO_internal:div by zero") != 0) {
170 fprintf(stderr, "FAIL: ERR_error_string() = '%s', "
171 "want 'error:03FFF067:bignum routines:CRYPTO_internal:div by zero'\n", s);
172 goto failure;
173 }
174
175 memset(buf, 0xdb, sizeof(buf));
176 ERR_error_string_n(0x3fff067UL, buf, sizeof(buf));
177 if (strcmp(s, "error:03FFF067:bignum routines:CRYPTO_internal:div by zero") != 0) {
178 fprintf(stderr, "FAIL: ERR_error_string() = '%s', "
179 "want 'error:03FFF067:bignum routines:CRYPTO_internal:div by zero'\n", s);
180 goto failure;
181 }
182
183 failed = 0;
184
185 failure:
186
187 return failed;
188}
189
190int
191main(int argc, char **argv)
192{
193 int failed = 0;
194
195 failed |= err_test();
196
197 /* Force a clean up. */
198 OPENSSL_cleanup();
199
200 return failed;
201}