| 1 |
// Copyright (c) 2014, Google Inc. |
| 2 |
// SPDX-License-Identifier: ISC |
| 3 |
|
| 4 |
#include <stdio.h> |
| 5 |
#include <string.h> |
| 6 |
|
| 7 |
#include <gtest/gtest.h> |
| 8 |
|
| 9 |
#include <openssl/crypto.h> |
| 10 |
#include <openssl/err.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
|
| 13 |
#include "./internal.h" |
| 14 |
|
| 15 |
#if defined(OPENSSL_WINDOWS) |
| 16 |
OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
| 17 |
#include <windows.h> |
| 18 |
OPENSSL_MSVC_PRAGMA(warning(pop)) |
| 19 |
#else |
| 20 |
#include <errno.h> |
| 21 |
#endif |
| 22 |
|
| 23 |
|
| 24 |
TEST(ErrTest, Overflow) { |
| 25 |
for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) { |
| 26 |
ERR_put_error(1, 0 /* unused */, i+1, "test", 1); |
| 27 |
} |
| 28 |
|
| 29 |
for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) { |
| 30 |
SCOPED_TRACE(i); |
| 31 |
uint32_t err = ERR_get_error(); |
| 32 |
// Errors are returned in order they were pushed, with the least recent ones |
| 33 |
// removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are |
| 34 |
// |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. |
| 35 |
EXPECT_NE(0u, err); |
| 36 |
EXPECT_EQ(static_cast<int>(i + ERR_NUM_ERRORS + 2), ERR_GET_REASON(err)); |
| 37 |
} |
| 38 |
|
| 39 |
EXPECT_EQ(0u, ERR_get_error()); |
| 40 |
} |
| 41 |
|
| 42 |
TEST(ErrTest, PutError) { |
| 43 |
ASSERT_EQ(0u, ERR_get_error()) |
| 44 |
<< "ERR_get_error returned value before an error was added."; |
| 45 |
|
| 46 |
ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 47 |
ERR_add_error_data(1, "testing"); |
| 48 |
|
| 49 |
int peeked_line, line, peeked_flags, flags; |
| 50 |
const char *peeked_file, *file, *peeked_data, *data; |
| 51 |
uint32_t peeked_packed_error = |
| 52 |
ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data, |
| 53 |
&peeked_flags); |
| 54 |
uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 55 |
|
| 56 |
EXPECT_EQ(peeked_packed_error, packed_error); |
| 57 |
EXPECT_EQ(peeked_file, file); |
| 58 |
EXPECT_EQ(peeked_data, data); |
| 59 |
EXPECT_EQ(peeked_flags, flags); |
| 60 |
|
| 61 |
EXPECT_STREQ("test", file); |
| 62 |
EXPECT_EQ(4, line); |
| 63 |
EXPECT_EQ(flags, ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 64 |
EXPECT_EQ(1, ERR_GET_LIB(packed_error)); |
| 65 |
EXPECT_EQ(2, ERR_GET_REASON(packed_error)); |
| 66 |
EXPECT_STREQ("testing", data); |
| 67 |
|
| 68 |
ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 69 |
ERR_set_error_data(const_cast<char *>("testing"), ERR_FLAG_STRING); |
| 70 |
packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 71 |
EXPECT_NE(0u, packed_error); |
| 72 |
EXPECT_STREQ("testing", data); |
| 73 |
|
| 74 |
ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 75 |
bssl::UniquePtr<char> str(OPENSSL_strdup("testing")); |
| 76 |
ERR_set_error_data(str.release(), ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 77 |
packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 78 |
EXPECT_NE(0u, packed_error); |
| 79 |
EXPECT_STREQ("testing", data); |
| 80 |
} |
| 81 |
|
| 82 |
TEST(ErrTest, ClearError) { |
| 83 |
ASSERT_EQ(0u, ERR_get_error()) |
| 84 |
<< "ERR_get_error returned value before an error was added."; |
| 85 |
|
| 86 |
ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 87 |
ERR_clear_error(); |
| 88 |
|
| 89 |
// The error queue should be cleared. |
| 90 |
EXPECT_EQ(0u, ERR_get_error()); |
| 91 |
} |
| 92 |
|
| 93 |
TEST(ErrTest, Print) { |
| 94 |
ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 95 |
ERR_add_error_data(1, "testing"); |
| 96 |
uint32_t packed_error = ERR_get_error(); |
| 97 |
|
| 98 |
char buf[256]; |
| 99 |
for (size_t i = 0; i <= sizeof(buf); i++) { |
| 100 |
ERR_error_string_n(packed_error, buf, i); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
TEST(ErrTest, Release) { |
| 105 |
ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 106 |
ERR_remove_thread_state(NULL); |
| 107 |
|
| 108 |
// The error queue should be cleared. |
| 109 |
EXPECT_EQ(0u, ERR_get_error()); |
| 110 |
} |
| 111 |
|
| 112 |
static bool HasSuffix(const char *str, const char *suffix) { |
| 113 |
size_t suffix_len = strlen(suffix); |
| 114 |
size_t str_len = strlen(str); |
| 115 |
if (str_len < suffix_len) { |
| 116 |
return false; |
| 117 |
} |
| 118 |
return strcmp(str + str_len - suffix_len, suffix) == 0; |
| 119 |
} |
| 120 |
|
| 121 |
TEST(ErrTest, PutMacro) { |
| 122 |
int expected_line = __LINE__ + 1; |
| 123 |
OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR); |
| 124 |
|
| 125 |
int line; |
| 126 |
const char *file; |
| 127 |
uint32_t error = ERR_get_error_line(&file, &line); |
| 128 |
|
| 129 |
EXPECT_PRED2(HasSuffix, file, "err_test.cc"); |
| 130 |
EXPECT_EQ(expected_line, line); |
| 131 |
EXPECT_EQ(ERR_LIB_USER, ERR_GET_LIB(error)); |
| 132 |
EXPECT_EQ(ERR_R_INTERNAL_ERROR, ERR_GET_REASON(error)); |
| 133 |
} |
| 134 |
|
| 135 |
TEST(ErrTest, SaveAndRestore) { |
| 136 |
// Restoring no state clears the error queue, including error data. |
| 137 |
ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1); |
| 138 |
ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2); |
| 139 |
ERR_add_error_data(1, "data1"); |
| 140 |
ERR_restore_state(nullptr); |
| 141 |
EXPECT_EQ(0u, ERR_get_error()); |
| 142 |
|
| 143 |
// Add some entries to the error queue and save it. |
| 144 |
ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1); |
| 145 |
ERR_add_error_data(1, "data1"); |
| 146 |
ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2); |
| 147 |
ERR_put_error(3, 0 /* unused */, 3, "test3.c", 3); |
| 148 |
ERR_add_error_data(1, "data3"); |
| 149 |
bssl::UniquePtr<ERR_SAVE_STATE> saved(ERR_save_state()); |
| 150 |
ASSERT_TRUE(saved); |
| 151 |
|
| 152 |
// The existing error queue entries still exist. |
| 153 |
int line, flags; |
| 154 |
const char *file, *data; |
| 155 |
uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 156 |
EXPECT_EQ(ERR_GET_LIB(packed_error), 1); |
| 157 |
EXPECT_EQ(ERR_GET_REASON(packed_error), 1); |
| 158 |
EXPECT_STREQ("test1.c", file); |
| 159 |
EXPECT_EQ(line, 1); |
| 160 |
EXPECT_STREQ(data, "data1"); |
| 161 |
EXPECT_EQ(flags, ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 162 |
|
| 163 |
// The state may be restored, both over an empty and non-empty state. |
| 164 |
for (unsigned i = 0; i < 2; i++) { |
| 165 |
SCOPED_TRACE(i); |
| 166 |
ERR_restore_state(saved.get()); |
| 167 |
|
| 168 |
packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 169 |
EXPECT_EQ(ERR_GET_LIB(packed_error), 1); |
| 170 |
EXPECT_EQ(ERR_GET_REASON(packed_error), 1); |
| 171 |
EXPECT_STREQ("test1.c", file); |
| 172 |
EXPECT_EQ(line, 1); |
| 173 |
EXPECT_STREQ(data, "data1"); |
| 174 |
EXPECT_EQ(flags, ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 175 |
|
| 176 |
packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 177 |
EXPECT_EQ(ERR_GET_LIB(packed_error), 2); |
| 178 |
EXPECT_EQ(ERR_GET_REASON(packed_error), 2); |
| 179 |
EXPECT_STREQ("test2.c", file); |
| 180 |
EXPECT_EQ(line, 2); |
| 181 |
EXPECT_STREQ(data, ""); // No error data is reported as the empty string. |
| 182 |
EXPECT_EQ(flags, 0); |
| 183 |
|
| 184 |
packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 185 |
EXPECT_EQ(ERR_GET_LIB(packed_error), 3); |
| 186 |
EXPECT_EQ(ERR_GET_REASON(packed_error), 3); |
| 187 |
EXPECT_STREQ("test3.c", file); |
| 188 |
EXPECT_EQ(line, 3); |
| 189 |
EXPECT_STREQ(data, "data3"); |
| 190 |
EXPECT_EQ(flags, ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 191 |
|
| 192 |
// The error queue is now empty for the next iteration. |
| 193 |
EXPECT_EQ(0u, ERR_get_error()); |
| 194 |
} |
| 195 |
|
| 196 |
// Test a case where the error queue wraps around. The first set of errors |
| 197 |
// will all be discarded, but result in wrapping the list around. |
| 198 |
ERR_clear_error(); |
| 199 |
for (unsigned i = 0; i < ERR_NUM_ERRORS / 2; i++) { |
| 200 |
ERR_put_error(0, 0 /* unused */, 0, "invalid", 0); |
| 201 |
} |
| 202 |
for (unsigned i = 1; i < ERR_NUM_ERRORS; i++) { |
| 203 |
ERR_put_error(i, 0 /* unused */, i, "test", i); |
| 204 |
} |
| 205 |
saved.reset(ERR_save_state()); |
| 206 |
|
| 207 |
// The state may be restored, both over an empty and non-empty state. Pop one |
| 208 |
// error off so the first iteration is tested to not be a no-op. |
| 209 |
ERR_get_error(); |
| 210 |
for (int i = 0; i < 2; i++) { |
| 211 |
SCOPED_TRACE(i); |
| 212 |
ERR_restore_state(saved.get()); |
| 213 |
for (int j = 1; j < ERR_NUM_ERRORS; j++) { |
| 214 |
SCOPED_TRACE(j); |
| 215 |
packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 216 |
EXPECT_EQ(ERR_GET_LIB(packed_error), j); |
| 217 |
EXPECT_EQ(ERR_GET_REASON(packed_error), j); |
| 218 |
EXPECT_STREQ("test", file); |
| 219 |
EXPECT_EQ(line, j); |
| 220 |
} |
| 221 |
// The error queue is now empty for the next iteration. |
| 222 |
EXPECT_EQ(0u, ERR_get_error()); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
// Querying the error queue should not affect the OS error. |
| 227 |
#if defined(OPENSSL_WINDOWS) |
| 228 |
TEST(ErrTest, PreservesLastError) { |
| 229 |
SetLastError(ERROR_INVALID_FUNCTION); |
| 230 |
ERR_get_error(); |
| 231 |
EXPECT_EQ(static_cast<DWORD>(ERROR_INVALID_FUNCTION), GetLastError()); |
| 232 |
} |
| 233 |
#else |
| 234 |
TEST(ErrTest, PreservesErrno) { |
| 235 |
errno = EINVAL; |
| 236 |
ERR_get_error(); |
| 237 |
EXPECT_EQ(EINVAL, errno); |
| 238 |
} |
| 239 |
#endif |
| 240 |
|
| 241 |
TEST(ErrTest, String) { |
| 242 |
char buf[128]; |
| 243 |
const uint32_t err = ERR_PACK(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
| 244 |
|
| 245 |
EXPECT_STREQ( |
| 246 |
"error:0e000044:common libcrypto routines:OPENSSL_internal:internal " |
| 247 |
"error", |
| 248 |
ERR_error_string_n(err, buf, sizeof(buf))); |
| 249 |
|
| 250 |
// The buffer is exactly the right size. |
| 251 |
EXPECT_STREQ( |
| 252 |
"error:0e000044:common libcrypto routines:OPENSSL_internal:internal " |
| 253 |
"error", |
| 254 |
ERR_error_string_n(err, buf, 73)); |
| 255 |
|
| 256 |
// If the buffer is too short, the string is truncated. |
| 257 |
EXPECT_STREQ( |
| 258 |
"error:0e000044:common libcrypto routines:OPENSSL_internal:internal " |
| 259 |
"erro", |
| 260 |
ERR_error_string_n(err, buf, 72)); |
| 261 |
EXPECT_STREQ("error:0e000044:common libcrypto routines:OPENSSL_internal:", |
| 262 |
ERR_error_string_n(err, buf, 59)); |
| 263 |
|
| 264 |
// Truncated log lines always have the right number of colons. |
| 265 |
EXPECT_STREQ("error:0e000044:common libcrypto routines:OPENSSL_interna:", |
| 266 |
ERR_error_string_n(err, buf, 58)); |
| 267 |
EXPECT_STREQ("error:0e000044:common libcrypto routines:OPENSSL_intern:", |
| 268 |
ERR_error_string_n(err, buf, 57)); |
| 269 |
EXPECT_STREQ("error:0e000044:common libcryp::", |
| 270 |
ERR_error_string_n(err, buf, 32)); |
| 271 |
EXPECT_STREQ("error:0e0000:::", |
| 272 |
ERR_error_string_n(err, buf, 16)); |
| 273 |
EXPECT_STREQ("err::::", |
| 274 |
ERR_error_string_n(err, buf, 8)); |
| 275 |
EXPECT_STREQ("::::", |
| 276 |
ERR_error_string_n(err, buf, 5)); |
| 277 |
|
| 278 |
// If the buffer is too short for even four colons, |ERR_error_string_n| does |
| 279 |
// not bother trying to preserve the format. |
| 280 |
EXPECT_STREQ("err", ERR_error_string_n(err, buf, 4)); |
| 281 |
EXPECT_STREQ("er", ERR_error_string_n(err, buf, 3)); |
| 282 |
EXPECT_STREQ("e", ERR_error_string_n(err, buf, 2)); |
| 283 |
EXPECT_STREQ("", ERR_error_string_n(err, buf, 1)); |
| 284 |
|
| 285 |
// A buffer length of zero should not touch the buffer. |
| 286 |
ERR_error_string_n(err, nullptr, 0); |
| 287 |
} |
| 288 |
|
| 289 |
// Error-printing functions should return something with unknown errors. |
| 290 |
TEST(ErrTest, UnknownError) { |
| 291 |
uint32_t err = ERR_PACK(0xff, 0xfff); |
| 292 |
EXPECT_TRUE(ERR_lib_error_string(err)); |
| 293 |
EXPECT_TRUE(ERR_reason_error_string(err)); |
| 294 |
char buf[128]; |
| 295 |
ERR_error_string_n(err, buf, sizeof(buf)); |
| 296 |
EXPECT_NE(0u, strlen(buf)); |
| 297 |
} |