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 25 TEST(ErrTest, Overflow) { 26 for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) { 27 ERR_put_error(1, 0 /* unused */, i+1, "test", 1); 28 } 29 30 for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) { 31 SCOPED_TRACE(i); 32 uint32_t err = ERR_get_error(); 33 /* Errors are returned in order they were pushed, with the least recent ones 34 * removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are 35 * |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. */ 36 EXPECT_NE(0u, err); 37 EXPECT_EQ(static_cast<int>(i + ERR_NUM_ERRORS + 2), ERR_GET_REASON(err)); 38 } 39 40 EXPECT_EQ(0u, ERR_get_error()); 41 } 42 43 TEST(ErrTest, PutError) { 44 ASSERT_EQ(0u, ERR_get_error()) 45 << "ERR_get_error returned value before an error was added."; 46 47 ERR_put_error(1, 0 /* unused */, 2, "test", 4); 48 ERR_add_error_data(1, "testing"); 49 50 int peeked_line, line, peeked_flags, flags; 51 const char *peeked_file, *file, *peeked_data, *data; 52 uint32_t peeked_packed_error = 53 ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data, 54 &peeked_flags); 55 uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); 56 57 EXPECT_EQ(peeked_packed_error, packed_error); 58 EXPECT_EQ(peeked_file, file); 59 EXPECT_EQ(peeked_data, data); 60 EXPECT_EQ(peeked_flags, flags); 61 62 EXPECT_STREQ("test", file); 63 EXPECT_EQ(4, line); 64 EXPECT_TRUE(flags & ERR_FLAG_STRING); 65 EXPECT_EQ(1, ERR_GET_LIB(packed_error)); 66 EXPECT_EQ(2, ERR_GET_REASON(packed_error)); 67 EXPECT_STREQ("testing", data); 68 } 69 70 TEST(ErrTest, ClearError) { 71 ASSERT_EQ(0u, ERR_get_error()) 72 << "ERR_get_error returned value before an error was added."; 73 74 ERR_put_error(1, 0 /* unused */, 2, "test", 4); 75 ERR_clear_error(); 76 77 // The error queue should be cleared. 78 EXPECT_EQ(0u, ERR_get_error()); 79 } 80 81 TEST(ErrTest, Print) { 82 ERR_put_error(1, 0 /* unused */, 2, "test", 4); 83 ERR_add_error_data(1, "testing"); 84 uint32_t packed_error = ERR_get_error(); 85 86 char buf[256]; 87 for (size_t i = 0; i <= sizeof(buf); i++) { 88 ERR_error_string_n(packed_error, buf, i); 89 } 90 } 91 92 TEST(ErrTest, Release) { 93 ERR_put_error(1, 0 /* unused */, 2, "test", 4); 94 ERR_remove_thread_state(NULL); 95 96 // The error queue should be cleared. 97 EXPECT_EQ(0u, ERR_get_error()); 98 } 99 100 static bool HasSuffix(const char *str, const char *suffix) { 101 size_t suffix_len = strlen(suffix); 102 size_t str_len = strlen(str); 103 if (str_len < suffix_len) { 104 return false; 105 } 106 return strcmp(str + str_len - suffix_len, suffix) == 0; 107 } 108 109 TEST(ErrTest, PutMacro) { 110 int expected_line = __LINE__ + 1; 111 OPENSSL_PUT_ERROR(USER, ERR_R_INTERNAL_ERROR); 112 113 int line; 114 const char *file; 115 uint32_t error = ERR_get_error_line(&file, &line); 116 117 EXPECT_PRED2(HasSuffix, file, "err_test.cc"); 118 EXPECT_EQ(expected_line, line); 119 EXPECT_EQ(ERR_LIB_USER, ERR_GET_LIB(error)); 120 EXPECT_EQ(ERR_R_INTERNAL_ERROR, ERR_GET_REASON(error)); 121 } 122