| 97 | #undef COMPARE |
| 98 | |
| 99 | TEST(TestSecureString, AssertSecurelyCleared) { |
| 100 | // This tests AssertSecurelyCleared helper methods is actually able to identify secret |
| 101 | // leakage. It retrieves assertion results and asserts result type and message. |
| 102 | testing::AssertionResult result = testing::AssertionSuccess(); |
| 103 | |
| 104 | // check short string with all zeros |
| 105 | auto short_zeros = std::string(8, '\0'); |
| 106 | short_zeros.resize(short_zeros.capacity(), '\0'); // for string buffers longer than 8 |
| 107 | short_zeros.resize(8); // now the entire string buffer has zeros |
| 108 | // checks the entire string buffer (capacity) |
| 109 | ASSERT_TRUE(IsSecurelyCleared(short_zeros)); |
| 110 | // checks only 10 bytes (length) |
| 111 | ASSERT_TRUE(IsSecurelyCleared(std::string_view(short_zeros))); |
| 112 | |
| 113 | // check long string with all zeros |
| 114 | auto long_zeros = std::string(1000, '\0'); |
| 115 | long_zeros.resize(long_zeros.capacity(), '\0'); // for longer string buffers |
| 116 | long_zeros.resize(1000); // now the entire string buffer has zeros |
| 117 | // checks the entire string buffer (capacity) |
| 118 | ASSERT_TRUE(IsSecurelyCleared(long_zeros)); |
| 119 | // checks only 1000 bytes (length) |
| 120 | ASSERT_TRUE(IsSecurelyCleared(std::string_view(long_zeros))); |
| 121 | |
| 122 | auto no_zeros = std::string("abcdefghijklmnopqrstuvwxyz"); |
| 123 | // string buffer in no_zeros can be larger than no_zeros.length() |
| 124 | // assert only the area that we can control |
| 125 | auto no_zeros_view = std::string_view(no_zeros); |
| 126 | result = IsSecurelyCleared(no_zeros_view); |
| 127 | ASSERT_FALSE(result); |
| 128 | ASSERT_EQ(std::string(result.message()), |
| 129 | "Expected equality of these values:\n" |
| 130 | " area\n" |
| 131 | " Which is: \"abcdefghijklmnopqrstuvwxyz\"\n" |
| 132 | " std::string_view(zeros)\n" |
| 133 | " Which is: " |
| 134 | "\"\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\" |
| 135 | "0\\0\\0\\0\\0\""); |
| 136 | |
| 137 | // check short string with zeros and non-zeros after string length |
| 138 | auto stars = std::string(12, '*'); |
| 139 | auto short_some_zeros = stars; |
| 140 | memset(short_some_zeros.data(), '\0', 8); |
| 141 | short_some_zeros.resize(8); |
| 142 | // string buffer in short_some_zeros can be larger than 12 |
| 143 | // assert only the area that we can control |
| 144 | auto short_some_zeros_view = std::string_view(short_some_zeros.data(), 12); |
| 145 | result = IsSecurelyCleared(short_some_zeros_view); |
| 146 | ASSERT_FALSE(result); |
| 147 | ASSERT_EQ(std::string(result.message()), |
| 148 | "Expected equality of these values:\n" |
| 149 | " area\n" |
| 150 | " Which is: \"\\0\\0\\0\\0\\0\\0\\0\\0\\0***\"\n" |
| 151 | " std::string_view(zeros)\n" |
| 152 | " Which is: \"\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\""); |
| 153 | |
| 154 | ASSERT_TRUE(IsSecurelyCleared(short_some_zeros, stars)); |
| 155 | #if CAN_TEST_DEALLOCATED_AREAS |
| 156 | result = IsSecurelyCleared(short_some_zeros_view, stars); |
nothing calls this directly
no test coverage detected