| 6 | // |
| 7 | // https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 |
| 15 | #include <errno.h> |
| 16 | #include <stdio.h> |
| 17 | #include <string.h> |
| 18 |
| 19 | #include <gtest/gtest.h> |
| 20 |
| 21 | #include <openssl/crypto.h> |
| 22 | #include <openssl/err.h> |
| 23 | #include <openssl/evp.h> |
| 24 | #include <openssl/mem.h> |
| 25 |
| 26 | #include "./internal.h" |
| 27 |
| 28 | #if defined(OPENSSL_WINDOWS) |
| 29 | #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); |
| 42 | uint32_t err = ERR_get_error(); |
| 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 |
| 50 | EXPECT_EQ(0u, ERR_get_error()); |
| 51 | } |
| 52 |
| 53 | TEST(ErrTest, PutError) { |
| 54 | ASSERT_EQ(0u, ERR_get_error()) |
| 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); |
| 74 | EXPECT_EQ(flags, ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 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 | ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 80 | ERR_set_error_data(const_cast<char *>("testing"), ERR_FLAG_STRING); |
| 81 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 82 | EXPECT_STREQ("testing", data); |
| 83 |
| 84 | ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 85 | bssl::UniquePtr<char> str(OPENSSL_strdup("testing")); |
| 86 | ERR_set_error_data(str.release(), ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 87 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 88 | EXPECT_STREQ("testing", data); |
| 89 | } |
| 90 |
| 91 | TEST(ErrTest, ClearError) { |
| 92 | ASSERT_EQ(0u, ERR_get_error()) |
| 93 | << "ERR_get_error returned value before an error was added."; |
| 94 |
| 95 | ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 96 | ERR_clear_error(); |
| 97 |
| 98 | // The error queue should be cleared. |
| 99 | EXPECT_EQ(0u, ERR_get_error()); |
| 100 | } |
| 101 |
| 102 | TEST(ErrTest, Print) { |
| 103 | ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 104 | ERR_add_error_data(1, "testing"); |
| 105 | uint32_t packed_error = ERR_get_error(); |
| 106 |
| 107 | char buf[256]; |
| 108 | for (size_t i = 0; i <= sizeof(buf); i++) { |
| 109 | ERR_error_string_n(packed_error, buf, i); |
| 110 | } |
| 111 | } |
| 112 |
| 113 | TEST(ErrTest, Release) { |
| 114 | ERR_put_error(1, 0 /* unused */, 2, "test", 4); |
| 115 | ERR_remove_thread_state(NULL); |
| 116 |
| 117 | // The error queue should be cleared. |
| 118 | EXPECT_EQ(0u, ERR_get_error()); |
| 119 | } |
| 120 |
| 121 | static bool HasSuffix(const char *str, const char *suffix) { |
| 122 | size_t suffix_len = strlen(suffix); |
| 123 | size_t str_len = strlen(str); |
| 124 | if (str_len < suffix_len) { |
| 125 | return false; |
| 126 | } |
| 127 | return strcmp(str + str_len - suffix_len, suffix) == 0; |
| 128 | } |
| 129 |
| 130 | TEST(ErrTest, PutMacro) { |
| 131 | int expected_line = __LINE__ + 1; |
| 132 | OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR); |
| 133 |
| 134 | int line; |
| 135 | const char *file; |
| 136 | uint32_t error = ERR_get_error_line(&file, &line); |
| 137 |
| 138 | EXPECT_PRED2(HasSuffix, file, "err_test.cc"); |
| 139 | EXPECT_EQ(expected_line, line); |
| 140 | EXPECT_EQ(ERR_LIB_USER, ERR_GET_LIB(error)); |
| 141 | EXPECT_EQ(ERR_R_INTERNAL_ERROR, ERR_GET_REASON(error)); |
| 142 | } |
| 143 |
| 144 | TEST(ErrTest, SaveAndRestore) { |
| 145 | // Restoring no state clears the error queue, including error data. |
| 146 | ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1); |
| 147 | ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2); |
| 148 | ERR_add_error_data(1, "data1"); |
| 149 | ERR_restore_state(nullptr); |
| 150 | EXPECT_EQ(0u, ERR_get_error()); |
| 151 |
| 152 | // Add some entries to the error queue and save it. |
| 153 | ERR_put_error(1, 0 /* unused */, 1, "test1.c", 1); |
| 154 | ERR_add_error_data(1, "data1"); |
| 155 | ERR_put_error(2, 0 /* unused */, 2, "test2.c", 2); |
| 156 | ERR_put_error(3, 0 /* unused */, 3, "test3.c", 3); |
| 157 | ERR_add_error_data(1, "data3"); |
| 158 | bssl::UniquePtr<ERR_SAVE_STATE> saved(ERR_save_state()); |
| 159 | ASSERT_TRUE(saved); |
| 160 |
| 161 | // The existing error queue entries still exist. |
| 162 | int line, flags; |
| 163 | const char *file, *data; |
| 164 | uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 165 | EXPECT_EQ(ERR_GET_LIB(packed_error), 1); |
| 166 | EXPECT_EQ(ERR_GET_REASON(packed_error), 1); |
| 167 | EXPECT_STREQ("test1.c", file); |
| 168 | EXPECT_EQ(line, 1); |
| 169 | EXPECT_STREQ(data, "data1"); |
| 170 | EXPECT_EQ(flags, ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 171 |
| 172 | // The state may be restored, both over an empty and non-empty state. |
| 173 | for (unsigned i = 0; i < 2; i++) { |
| 174 | SCOPED_TRACE(i); |
| 175 | ERR_restore_state(saved.get()); |
| 176 |
| 177 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 178 | EXPECT_EQ(ERR_GET_LIB(packed_error), 1); |
| 179 | EXPECT_EQ(ERR_GET_REASON(packed_error), 1); |
| 180 | EXPECT_STREQ("test1.c", file); |
| 181 | EXPECT_EQ(line, 1); |
| 182 | EXPECT_STREQ(data, "data1"); |
| 183 | EXPECT_EQ(flags, ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 184 |
| 185 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 186 | EXPECT_EQ(ERR_GET_LIB(packed_error), 2); |
| 187 | EXPECT_EQ(ERR_GET_REASON(packed_error), 2); |
| 188 | EXPECT_STREQ("test2.c", file); |
| 189 | EXPECT_EQ(line, 2); |
| 190 | EXPECT_STREQ(data, ""); // No error data is reported as the empty string. |
| 191 | EXPECT_EQ(flags, 0); |
| 192 |
| 193 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 194 | EXPECT_EQ(ERR_GET_LIB(packed_error), 3); |
| 195 | EXPECT_EQ(ERR_GET_REASON(packed_error), 3); |
| 196 | EXPECT_STREQ("test3.c", file); |
| 197 | EXPECT_EQ(line, 3); |
| 198 | EXPECT_STREQ(data, "data3"); |
| 199 | EXPECT_EQ(flags, ERR_FLAG_STRING | ERR_FLAG_MALLOCED); |
| 200 |
| 201 | // The error queue is now empty for the next iteration. |
| 202 | EXPECT_EQ(0u, ERR_get_error()); |
| 203 | } |
| 204 |
| 205 | // Test a case where the error queue wraps around. The first set of errors |
| 206 | // will all be discarded, but result in wrapping the list around. |
| 207 | ERR_clear_error(); |
| 208 | for (unsigned i = 0; i < ERR_NUM_ERRORS / 2; i++) { |
| 209 | ERR_put_error(0, 0 /* unused */, 0, "invalid", 0); |
| 210 | } |
| 211 | for (unsigned i = 1; i < ERR_NUM_ERRORS; i++) { |
| 212 | ERR_put_error(i, 0 /* unused */, i, "test", i); |
| 213 | } |
| 214 | saved.reset(ERR_save_state()); |
| 215 |
| 216 | // The state may be restored, both over an empty and non-empty state. Pop one |
| 217 | // error off so the first iteration is tested to not be a no-op. |
| 218 | ERR_get_error(); |
| 219 | for (int i = 0; i < 2; i++) { |
| 220 | SCOPED_TRACE(i); |
| 221 | ERR_restore_state(saved.get()); |
| 222 | for (int j = 1; j < ERR_NUM_ERRORS; j++) { |
| 223 | SCOPED_TRACE(j); |
| 224 | packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); |
| 225 | EXPECT_EQ(ERR_GET_LIB(packed_error), j); |
| 226 | EXPECT_EQ(ERR_GET_REASON(packed_error), j); |
| 227 | EXPECT_STREQ("test", file); |
| 228 | EXPECT_EQ(line, j); |
| 229 | } |
| 230 | // The error queue is now empty for the next iteration. |
| 231 | EXPECT_EQ(0u, ERR_get_error()); |
| 232 | } |
| 233 | } |
| 234 |
| 235 | // Querying the error queue should not affect the OS error. |
| 236 | #if defined(OPENSSL_WINDOWS) |
| 237 | TEST(ErrTest, PreservesLastError) { |
| 238 | SetLastError(ERROR_INVALID_FUNCTION); |
| 239 | ERR_get_error(); |
| 240 | EXPECT_EQ(static_cast<DWORD>(ERROR_INVALID_FUNCTION), GetLastError()); |
| 241 | } |
| 242 | #else |
| 243 | TEST(ErrTest, PreservesErrno) { |
| 244 | errno = EINVAL; |
| 245 | ERR_get_error(); |
| 246 | EXPECT_EQ(EINVAL, errno); |
| 247 | } |
| 248 | #endif |
| 249 |
| 250 | TEST(ErrTest, String) { |
| 251 | char buf[128]; |
| 252 | uint32_t err = ERR_PACK(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR); |
| 253 |
| 254 | EXPECT_STREQ( |
| 255 | "error:0e000044:common libcrypto routines:OPENSSL_internal:internal " |
| 256 | "error", |
| 257 | ERR_error_string_n(err, buf, sizeof(buf))); |
| 258 |
| 259 | // The buffer is exactly the right size. |
| 260 | EXPECT_STREQ( |
| 261 | "error:0e000044:common libcrypto routines:OPENSSL_internal:internal " |
| 262 | "error", |
| 263 | ERR_error_string_n(err, buf, 73)); |
| 264 |
| 265 | // If the buffer is too short, the string is truncated. |
| 266 | EXPECT_STREQ( |
| 267 | "error:0e000044:common libcrypto routines:OPENSSL_internal:internal " |
| 268 | "erro", |
| 269 | ERR_error_string_n(err, buf, 72)); |
| 270 | EXPECT_STREQ("error:0e000044:common libcrypto routines:OPENSSL_internal:", |
| 271 | ERR_error_string_n(err, buf, 59)); |
| 272 |
| 273 | // Truncated log lines always have the right number of colons. |
| 274 | EXPECT_STREQ("error:0e000044:common libcrypto routines:OPENSSL_interna:", |
| 275 | ERR_error_string_n(err, buf, 58)); |
| 276 | EXPECT_STREQ("error:0e000044:common libcrypto routines:OPENSSL_intern:", |
| 277 | ERR_error_string_n(err, buf, 57)); |
| 278 | EXPECT_STREQ("error:0e000044:common libcryp::", |
| 279 | ERR_error_string_n(err, buf, 32)); |
| 280 | EXPECT_STREQ("error:0e0000:::", |
| 281 | ERR_error_string_n(err, buf, 16)); |
| 282 | EXPECT_STREQ("err::::", |
| 283 | ERR_error_string_n(err, buf, 8)); |
| 284 | EXPECT_STREQ("::::", |
| 285 | ERR_error_string_n(err, buf, 5)); |
| 286 |
| 287 | // If the buffer is too short for even four colons, |ERR_error_string_n| does |
| 288 | // not bother trying to preserve the format. |
| 289 | EXPECT_STREQ("err", ERR_error_string_n(err, buf, 4)); |
| 290 | EXPECT_STREQ("er", ERR_error_string_n(err, buf, 3)); |
| 291 | EXPECT_STREQ("e", ERR_error_string_n(err, buf, 2)); |
| 292 | EXPECT_STREQ("", ERR_error_string_n(err, buf, 1)); |
| 293 |
| 294 | // A buffer length of zero should not touch the buffer. |
| 295 | ERR_error_string_n(err, nullptr, 0); |
| 296 |
| 297 | EXPECT_STREQ(ERR_lib_error_string(err), "common libcrypto routines"); |
| 298 | EXPECT_STREQ(ERR_lib_symbol_name(err), "CRYPTO"); |
| 299 | EXPECT_STREQ(ERR_reason_error_string(err), "internal error"); |
| 300 | EXPECT_STREQ(ERR_reason_symbol_name(err), "INTERNAL_ERROR"); |
| 301 |
| 302 | // Check a normal error. |
| 303 | err = ERR_PACK(ERR_LIB_EVP, EVP_R_DECODE_ERROR); |
| 304 | EXPECT_STREQ(ERR_lib_error_string(err), "public key routines"); |
| 305 | EXPECT_STREQ(ERR_lib_symbol_name(err), "EVP"); |
| 306 | EXPECT_STREQ(ERR_reason_error_string(err), "DECODE_ERROR"); |
| 307 | EXPECT_STREQ(ERR_reason_symbol_name(err), "DECODE_ERROR"); |
| 308 |
| 309 | // Check an error that forwards to another library. |
| 310 | err = ERR_PACK(ERR_LIB_EVP, ERR_R_BN_LIB); |
| 311 | EXPECT_STREQ(ERR_lib_error_string(err), "public key routines"); |
| 312 | EXPECT_STREQ(ERR_lib_symbol_name(err), "EVP"); |
| 313 | EXPECT_STREQ(ERR_reason_error_string(err), "bignum routines"); |
| 314 | EXPECT_STREQ(ERR_reason_symbol_name(err), "BN_LIB"); |
| 315 |
| 316 | // Errors in |ERR_LIB_SYS| are |errno| values, so we don't have their symbolic |
| 317 | // names. Their human-readable strings are OS- and even locale-dependent. |
| 318 | err = ERR_PACK(ERR_LIB_SYS, ERANGE); |
| 319 | EXPECT_STREQ(ERR_lib_error_string(err), "system library"); |
| 320 | EXPECT_STREQ(ERR_lib_symbol_name(err), "SYS"); |
| 321 | EXPECT_NE(ERR_reason_error_string(err), nullptr); |
| 322 | EXPECT_STRNE(ERR_reason_error_string(err), "unknown error"); |
| 323 | EXPECT_EQ(ERR_reason_symbol_name(err), nullptr); |
| 324 | } |
| 325 |
| 326 | // Error-printing functions should return something with unknown errors. |
| 327 | TEST(ErrTest, UnknownError) { |
| 328 | uint32_t err = ERR_PACK(0xff, 0xfff); |
| 329 | EXPECT_TRUE(ERR_lib_error_string(err)); |
| 330 | EXPECT_TRUE(ERR_reason_error_string(err)); |
| 331 | char buf[128]; |
| 332 | ERR_error_string_n(err, buf, sizeof(buf)); |
| 333 | EXPECT_NE(0u, strlen(buf)); |
| 334 | } |
| 335 |