err_test.cc
Go to the documentation of this file.
1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <stdio.h>
16 #include <string.h>
17 
18 #include <gtest/gtest.h>
19 
20 #include <openssl/crypto.h>
21 #include <openssl/err.h>
22 #include <openssl/mem.h>
23 
24 #include "./internal.h"
25 
26 #if defined(OPENSSL_WINDOWS)
28 #include <windows.h>
30 #else
31 #include <errno.h>
32 #endif
33 
34 
35 TEST(ErrTest, Overflow) {
36  for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) {
37  ERR_put_error(1, 0 /* unused */, i+1, "test", 1);
38  }
39 
40  for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) {
41  SCOPED_TRACE(i);
43  // Errors are returned in order they were pushed, with the least recent ones
44  // removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are
45  // |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive.
46  EXPECT_NE(0u, err);
47  EXPECT_EQ(static_cast<int>(i + ERR_NUM_ERRORS + 2), ERR_GET_REASON(err));
48  }
49 
51 }
52 
53 TEST(ErrTest, PutError) {
55  << "ERR_get_error returned value before an error was added.";
56 
57  ERR_put_error(1, 0 /* unused */, 2, "test", 4);
58  ERR_add_error_data(1, "testing");
59 
60  int peeked_line, line, peeked_flags, flags;
61  const char *peeked_file, *file, *peeked_data, *data;
62  uint32_t peeked_packed_error =
63  ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data,
64  &peeked_flags);
65  uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
66 
67  EXPECT_EQ(peeked_packed_error, packed_error);
68  EXPECT_EQ(peeked_file, file);
69  EXPECT_EQ(peeked_data, data);
70  EXPECT_EQ(peeked_flags, flags);
71 
72  EXPECT_STREQ("test", file);
73  EXPECT_EQ(4, line);
75  EXPECT_EQ(1, ERR_GET_LIB(packed_error));
76  EXPECT_EQ(2, ERR_GET_REASON(packed_error));
77  EXPECT_STREQ("testing", data);
78 }
79 
80 TEST(ErrTest, ClearError) {
82  << "ERR_get_error returned value before an error was added.";
83 
84  ERR_put_error(1, 0 /* unused */, 2, "test", 4);
86 
87  // The error queue should be cleared.
89 }
90 
91 TEST(ErrTest, Print) {
92  ERR_put_error(1, 0 /* unused */, 2, "test", 4);
93  ERR_add_error_data(1, "testing");
94  uint32_t packed_error = ERR_get_error();
95 
96  char buf[256];
97  for (size_t i = 0; i <= sizeof(buf); i++) {
98  ERR_error_string_n(packed_error, buf, i);
99  }
100 }
101 
102 TEST(ErrTest, Release) {
103  ERR_put_error(1, 0 /* unused */, 2, "test", 4);
105 
106  // The error queue should be cleared.
108 }
109 
110 static bool HasSuffix(const char *str, const char *suffix) {
111  size_t suffix_len = strlen(suffix);
112  size_t str_len = strlen(str);
113  if (str_len < suffix_len) {
114  return false;
115  }
116  return strcmp(str + str_len - suffix_len, suffix) == 0;
117 }
118 
119 TEST(ErrTest, PutMacro) {
120  int expected_line = __LINE__ + 1;
122 
123  int line;
124  const char *file;
126 
127  EXPECT_PRED2(HasSuffix, file, "err_test.cc");
128  EXPECT_EQ(expected_line, line);
131 }
132 
133 TEST(ErrTest, SaveAndRestore) {
134  // Restoring no state clears the error queue, including error data.
135  ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1);
136  ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2);
137  ERR_add_error_data(1, "data1");
138  ERR_restore_state(nullptr);
140 
141  // Add some entries to the error queue and save it.
142  ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1);
143  ERR_add_error_data(1, "data1");
144  ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2);
145  ERR_put_error(3, 0 /* unused */, 3, "test3.c", 3);
146  ERR_add_error_data(1, "data3");
147  bssl::UniquePtr<ERR_SAVE_STATE> saved(ERR_save_state());
148  ASSERT_TRUE(saved);
149 
150  // The existing error queue entries still exist.
151  int line, flags;
152  const char *file, *data;
153  uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
154  EXPECT_EQ(ERR_GET_LIB(packed_error), 1);
155  EXPECT_EQ(ERR_GET_REASON(packed_error), 1);
156  EXPECT_STREQ("test1.c", file);
157  EXPECT_EQ(line, 1);
158  EXPECT_STREQ(data, "data1");
160 
161  // The state may be restored, both over an empty and non-empty state.
162  for (unsigned i = 0; i < 2; i++) {
163  SCOPED_TRACE(i);
164  ERR_restore_state(saved.get());
165 
166  packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
167  EXPECT_EQ(ERR_GET_LIB(packed_error), 1);
168  EXPECT_EQ(ERR_GET_REASON(packed_error), 1);
169  EXPECT_STREQ("test1.c", file);
170  EXPECT_EQ(line, 1);
171  EXPECT_STREQ(data, "data1");
173 
174  packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
175  EXPECT_EQ(ERR_GET_LIB(packed_error), 2);
176  EXPECT_EQ(ERR_GET_REASON(packed_error), 2);
177  EXPECT_STREQ("test2.c", file);
178  EXPECT_EQ(line, 2);
179  EXPECT_STREQ(data, ""); // No error data is reported as the empty string.
180  EXPECT_EQ(flags, 0);
181 
182  packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
183  EXPECT_EQ(ERR_GET_LIB(packed_error), 3);
184  EXPECT_EQ(ERR_GET_REASON(packed_error), 3);
185  EXPECT_STREQ("test3.c", file);
186  EXPECT_EQ(line, 3);
187  EXPECT_STREQ(data, "data3");
189 
190  // The error queue is now empty for the next iteration.
192  }
193 
194  // Test a case where the error queue wraps around. The first set of errors
195  // will all be discarded, but result in wrapping the list around.
196  ERR_clear_error();
197  for (unsigned i = 0; i < ERR_NUM_ERRORS / 2; i++) {
198  ERR_put_error(0, 0 /* unused */, 0, "invalid", 0);
199  }
200  for (unsigned i = 1; i < ERR_NUM_ERRORS; i++) {
201  ERR_put_error(i, 0 /* unused */, i, "test", i);
202  }
203  saved.reset(ERR_save_state());
204 
205  // The state may be restored, both over an empty and non-empty state. Pop one
206  // error off so the first iteration is tested to not be a no-op.
207  ERR_get_error();
208  for (int i = 0; i < 2; i++) {
209  SCOPED_TRACE(i);
210  ERR_restore_state(saved.get());
211  for (int j = 1; j < ERR_NUM_ERRORS; j++) {
212  SCOPED_TRACE(j);
213  packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
214  EXPECT_EQ(ERR_GET_LIB(packed_error), j);
215  EXPECT_EQ(ERR_GET_REASON(packed_error), j);
216  EXPECT_STREQ("test", file);
217  EXPECT_EQ(line, j);
218  }
219  // The error queue is now empty for the next iteration.
221  }
222 }
223 
224 // Querying the error queue should not affect the OS error.
225 #if defined(OPENSSL_WINDOWS)
226 TEST(ErrTest, PreservesLastError) {
227  SetLastError(ERROR_INVALID_FUNCTION);
228  ERR_get_error();
229  EXPECT_EQ(static_cast<DWORD>(ERROR_INVALID_FUNCTION), GetLastError());
230 }
231 #else
232 TEST(ErrTest, PreservesErrno) {
233  errno = EINVAL;
234  ERR_get_error();
235  EXPECT_EQ(EINVAL, errno);
236 }
237 #endif
238 
239 TEST(ErrTest, String) {
240  char buf[128];
242 
243  EXPECT_STREQ(
244  "error:0e000044:common libcrypto routines:OPENSSL_internal:internal "
245  "error",
246  ERR_error_string_n(err, buf, sizeof(buf)));
247 
248  // The buffer is exactly the right size.
249  EXPECT_STREQ(
250  "error:0e000044:common libcrypto routines:OPENSSL_internal:internal "
251  "error",
252  ERR_error_string_n(err, buf, 73));
253 
254  // If the buffer is too short, the string is truncated.
255  EXPECT_STREQ(
256  "error:0e000044:common libcrypto routines:OPENSSL_internal:internal "
257  "erro",
258  ERR_error_string_n(err, buf, 72));
259  EXPECT_STREQ("error:0e000044:common libcrypto routines:OPENSSL_internal:",
260  ERR_error_string_n(err, buf, 59));
261 
262  // Truncated log lines always have the right number of colons.
263  EXPECT_STREQ("error:0e000044:common libcrypto routines:OPENSSL_interna:",
264  ERR_error_string_n(err, buf, 58));
265  EXPECT_STREQ("error:0e000044:common libcrypto routines:OPENSSL_intern:",
266  ERR_error_string_n(err, buf, 57));
267  EXPECT_STREQ("error:0e000044:common libcryp::",
268  ERR_error_string_n(err, buf, 32));
269  EXPECT_STREQ("error:0e0000:::",
270  ERR_error_string_n(err, buf, 16));
271  EXPECT_STREQ("err::::",
272  ERR_error_string_n(err, buf, 8));
273  EXPECT_STREQ("::::",
274  ERR_error_string_n(err, buf, 5));
275 
276  // If the buffer is too short for even four colons, |ERR_error_string_n| does
277  // not bother trying to preserve the format.
278  EXPECT_STREQ("err", ERR_error_string_n(err, buf, 4));
282 
283  // A buffer length of zero should not touch the buffer.
284  ERR_error_string_n(err, nullptr, 0);
285 }
286 
287 // Error-printing functions should return something with unknown errors.
288 TEST(ErrTest, UnknownError) {
289  uint32_t err = ERR_PACK(0xff, 0xfff);
292  char buf[128];
293  ERR_error_string_n(err, buf, sizeof(buf));
294  EXPECT_NE(0u, strlen(buf));
295 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
ERR_FLAG_STRING
#define ERR_FLAG_STRING
Definition: err.h:184
ERR_save_state
#define ERR_save_state
Definition: boringssl_prefix_symbols.h:1443
ERR_put_error
#define ERR_put_error
Definition: boringssl_prefix_symbols.h:1438
file
const grpc_generator::File * file
Definition: python_private_generator.h:38
check_version.warning
string warning
Definition: check_version.py:46
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
testing::gtest_printers_test::Print
std::string Print(const T &value)
Definition: bloaty/third_party/googletest/googletest/test/googletest-printers-test.cc:233
string.h
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
ERR_get_error_line_data
#define ERR_get_error_line_data
Definition: boringssl_prefix_symbols.h:1421
error
grpc_error_handle error
Definition: retry_filter.cc:499
error_ref_leak.err
err
Definition: error_ref_leak.py:35
HasSuffix
static bool HasSuffix(const char *str, const char *suffix)
Definition: err_test.cc:110
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
ERR_lib_error_string
#define ERR_lib_error_string
Definition: boringssl_prefix_symbols.h:1423
internal.h
ERR_peek_error_line_data
#define ERR_peek_error_line_data
Definition: boringssl_prefix_symbols.h:1430
ERR_get_error
#define ERR_get_error
Definition: boringssl_prefix_symbols.h:1419
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
ERR_get_error_line
#define ERR_get_error_line
Definition: boringssl_prefix_symbols.h:1420
SCOPED_TRACE
#define SCOPED_TRACE(message)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2264
ERR_GET_REASON
#define ERR_GET_REASON(packed_error)
Definition: err.h:171
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
absl::UnknownError
Status UnknownError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:383
ERR_GET_LIB
#define ERR_GET_LIB(packed_error)
Definition: err.h:166
err.h
crypto.h
ERR_R_INTERNAL_ERROR
#define ERR_R_INTERNAL_ERROR
Definition: err.h:374
ERR_PACK
#define ERR_PACK(lib, reason)
Definition: err.h:451
OPENSSL_MSVC_PRAGMA
OPENSSL_MSVC_PRAGMA(warning(disable:4702))
Definition: e_aes.c:69
ERR_LIB_CRYPTO
@ ERR_LIB_CRYPTO
Definition: err.h:305
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
ERR_error_string_n
#define ERR_error_string_n
Definition: boringssl_prefix_symbols.h:1416
ERR_remove_thread_state
#define ERR_remove_thread_state
Definition: boringssl_prefix_symbols.h:1441
push
int push(void *desc, unsigned char *buf, unsigned len)
Definition: bloaty/third_party/zlib/test/infcover.c:463
EXPECT_STREQ
#define EXPECT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2095
ERR_restore_state
#define ERR_restore_state
Definition: boringssl_prefix_symbols.h:1442
suffix
unsigned char suffix[65536]
Definition: bloaty/third_party/zlib/examples/gun.c:164
absl::flags_internal
Definition: abseil-cpp/absl/flags/commandlineflag.h:40
ERR_add_error_data
#define ERR_add_error_data
Definition: boringssl_prefix_symbols.h:1411
regen-readme.line
line
Definition: regen-readme.py:30
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
ERR_clear_error
#define ERR_clear_error
Definition: boringssl_prefix_symbols.h:1413
EXPECT_PRED2
#define EXPECT_PRED2(pred, v1, v2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest_pred_impl.h:165
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
TEST
TEST(ErrTest, Overflow)
Definition: err_test.cc:35
flags
uint32_t flags
Definition: retry_filter.cc:632
mem.h
ERR_LIB_USER
@ ERR_LIB_USER
Definition: err.h:324
ERR_NUM_ERRORS
#define ERR_NUM_ERRORS
Definition: err.h:449
errno.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
ERR_reason_error_string
#define ERR_reason_error_string
Definition: boringssl_prefix_symbols.h:1439


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:15