Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "SerializedLogChunk.h" |
| 18 | |
| 19 | #include <limits> |
| 20 | |
| 21 | #include <android-base/stringprintf.h> |
| 22 | #include <android/log.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | |
| 25 | using android::base::StringPrintf; |
| 26 | |
| 27 | TEST(SerializedLogChunk, smoke) { |
| 28 | size_t chunk_size = 10 * 4096; |
| 29 | auto chunk = SerializedLogChunk{chunk_size}; |
Tom Cherry | 44e1a5d | 2020-07-13 17:28:30 -0700 | [diff] [blame] | 30 | EXPECT_EQ(chunk_size + sizeof(SerializedLogChunk), chunk.PruneSize()); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 31 | |
| 32 | static const char log_message[] = "log message"; |
| 33 | size_t expected_total_len = sizeof(SerializedLogEntry) + sizeof(log_message); |
| 34 | ASSERT_TRUE(chunk.CanLog(expected_total_len)); |
| 35 | EXPECT_TRUE(chunk.CanLog(chunk_size)); |
| 36 | EXPECT_FALSE(chunk.CanLog(chunk_size + 1)); |
| 37 | |
| 38 | log_time time(CLOCK_REALTIME); |
| 39 | auto* entry = chunk.Log(1234, time, 0, 1, 2, log_message, sizeof(log_message)); |
| 40 | ASSERT_NE(nullptr, entry); |
| 41 | |
| 42 | EXPECT_EQ(1234U, entry->sequence()); |
| 43 | EXPECT_EQ(time, entry->realtime()); |
| 44 | EXPECT_EQ(0U, entry->uid()); |
| 45 | EXPECT_EQ(1, entry->pid()); |
| 46 | EXPECT_EQ(2, entry->tid()); |
| 47 | EXPECT_EQ(sizeof(log_message), entry->msg_len()); |
| 48 | EXPECT_STREQ(log_message, entry->msg()); |
| 49 | EXPECT_EQ(expected_total_len, entry->total_len()); |
| 50 | |
| 51 | EXPECT_FALSE(chunk.CanLog(chunk_size)); |
| 52 | EXPECT_EQ(static_cast<int>(expected_total_len), chunk.write_offset()); |
| 53 | EXPECT_EQ(1234U, chunk.highest_sequence_number()); |
| 54 | } |
| 55 | |
| 56 | TEST(SerializedLogChunk, fill_log_exactly) { |
| 57 | static const char log_message[] = "this is a log message"; |
| 58 | size_t individual_message_size = sizeof(SerializedLogEntry) + sizeof(log_message); |
| 59 | size_t chunk_size = individual_message_size * 3; |
| 60 | auto chunk = SerializedLogChunk{chunk_size}; |
Tom Cherry | 44e1a5d | 2020-07-13 17:28:30 -0700 | [diff] [blame] | 61 | EXPECT_EQ(chunk_size + sizeof(SerializedLogChunk), chunk.PruneSize()); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 62 | |
| 63 | ASSERT_TRUE(chunk.CanLog(individual_message_size)); |
| 64 | EXPECT_NE(nullptr, chunk.Log(1, log_time(), 1000, 1, 1, log_message, sizeof(log_message))); |
| 65 | |
| 66 | ASSERT_TRUE(chunk.CanLog(individual_message_size)); |
| 67 | EXPECT_NE(nullptr, chunk.Log(2, log_time(), 1000, 2, 1, log_message, sizeof(log_message))); |
| 68 | |
| 69 | ASSERT_TRUE(chunk.CanLog(individual_message_size)); |
| 70 | EXPECT_NE(nullptr, chunk.Log(3, log_time(), 1000, 3, 1, log_message, sizeof(log_message))); |
| 71 | |
| 72 | EXPECT_FALSE(chunk.CanLog(1)); |
| 73 | } |
| 74 | |
| 75 | TEST(SerializedLogChunk, three_logs) { |
| 76 | size_t chunk_size = 10 * 4096; |
| 77 | auto chunk = SerializedLogChunk{chunk_size}; |
| 78 | |
| 79 | chunk.Log(2, log_time(0x1234, 0x5678), 0x111, 0x222, 0x333, "initial message", |
| 80 | strlen("initial message")); |
| 81 | chunk.Log(3, log_time(0x2345, 0x6789), 0x444, 0x555, 0x666, "second message", |
| 82 | strlen("second message")); |
| 83 | auto uint64_t_max = std::numeric_limits<uint64_t>::max(); |
| 84 | auto uint32_t_max = std::numeric_limits<uint32_t>::max(); |
| 85 | chunk.Log(uint64_t_max, log_time(uint32_t_max, uint32_t_max), uint32_t_max, uint32_t_max, |
| 86 | uint32_t_max, "last message", strlen("last message")); |
| 87 | |
| 88 | static const char expected_buffer_data[] = |
| 89 | "\x11\x01\x00\x00\x22\x02\x00\x00\x33\x03\x00\x00" // UID PID TID |
| 90 | "\x02\x00\x00\x00\x00\x00\x00\x00" // Sequence |
| 91 | "\x34\x12\x00\x00\x78\x56\x00\x00" // Timestamp |
| 92 | "\x0F\x00initial message" // msg_len + message |
| 93 | "\x44\x04\x00\x00\x55\x05\x00\x00\x66\x06\x00\x00" // UID PID TID |
| 94 | "\x03\x00\x00\x00\x00\x00\x00\x00" // Sequence |
| 95 | "\x45\x23\x00\x00\x89\x67\x00\x00" // Timestamp |
| 96 | "\x0E\x00second message" // msg_len + message |
| 97 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" // UID PID TID |
| 98 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" // Sequence |
| 99 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" // Timestamp |
| 100 | "\x0C\x00last message"; // msg_len + message |
| 101 | |
| 102 | for (size_t i = 0; i < chunk_size; ++i) { |
| 103 | if (i < sizeof(expected_buffer_data)) { |
| 104 | EXPECT_EQ(static_cast<uint8_t>(expected_buffer_data[i]), chunk.data()[i]) |
| 105 | << "position: " << i; |
| 106 | } else { |
| 107 | EXPECT_EQ(0, chunk.data()[i]) << "position: " << i; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // Check that the CHECK() in DecReaderRefCount() if the ref count goes bad is caught. |
| 113 | TEST(SerializedLogChunk, catch_DecCompressedRef_CHECK) { |
| 114 | size_t chunk_size = 10 * 4096; |
| 115 | auto chunk = SerializedLogChunk{chunk_size}; |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 116 | EXPECT_DEATH({ chunk.DecReaderRefCount(); }, ""); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Check that the CHECK() in ClearUidLogs() if the ref count is greater than 0 is caught. |
| 120 | TEST(SerializedLogChunk, catch_ClearUidLogs_CHECK) { |
| 121 | size_t chunk_size = 10 * 4096; |
| 122 | auto chunk = SerializedLogChunk{chunk_size}; |
| 123 | chunk.IncReaderRefCount(); |
| 124 | EXPECT_DEATH({ chunk.ClearUidLogs(1000, LOG_ID_MAIN, nullptr); }, ""); |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 125 | chunk.DecReaderRefCount(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | class UidClearTest : public testing::TestWithParam<bool> { |
| 129 | protected: |
| 130 | template <typename Write, typename Check> |
| 131 | void Test(const Write& write, const Check& check, bool expected_result) { |
| 132 | write(chunk_); |
| 133 | |
| 134 | bool finish_writing = GetParam(); |
| 135 | if (finish_writing) { |
| 136 | chunk_.FinishWriting(); |
| 137 | } |
| 138 | EXPECT_EQ(expected_result, chunk_.ClearUidLogs(kUidToClear, LOG_ID_MAIN, nullptr)); |
| 139 | if (finish_writing) { |
| 140 | chunk_.IncReaderRefCount(); |
| 141 | } |
| 142 | |
| 143 | check(chunk_); |
| 144 | |
| 145 | if (finish_writing) { |
Tom Cherry | 59caa7a | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 146 | chunk_.DecReaderRefCount(); |
Tom Cherry | 1a796bc | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | static constexpr size_t kChunkSize = 10 * 4096; |
| 151 | static constexpr uid_t kUidToClear = 1000; |
| 152 | static constexpr uid_t kOtherUid = 1234; |
| 153 | |
| 154 | SerializedLogChunk chunk_{kChunkSize}; |
| 155 | }; |
| 156 | |
| 157 | // Test that ClearUidLogs() is a no-op if there are no logs of that UID in the buffer. |
| 158 | TEST_P(UidClearTest, no_logs_in_chunk) { |
| 159 | auto write = [](SerializedLogChunk&) {}; |
| 160 | auto check = [](SerializedLogChunk&) {}; |
| 161 | |
| 162 | Test(write, check, true); |
| 163 | } |
| 164 | |
| 165 | // Test that ClearUidLogs() is a no-op if there are no logs of that UID in the buffer. |
| 166 | TEST_P(UidClearTest, no_logs_from_uid) { |
| 167 | static const char msg[] = "this is a log message"; |
| 168 | auto write = [](SerializedLogChunk& chunk) { |
| 169 | chunk.Log(1, log_time(), kOtherUid, 1, 2, msg, sizeof(msg)); |
| 170 | }; |
| 171 | |
| 172 | auto check = [](SerializedLogChunk& chunk) { |
| 173 | auto* entry = chunk.log_entry(0); |
| 174 | EXPECT_STREQ(msg, entry->msg()); |
| 175 | }; |
| 176 | |
| 177 | Test(write, check, false); |
| 178 | } |
| 179 | |
| 180 | // Test that ClearUidLogs() returns true if all logs in a given buffer correspond to the given UID. |
| 181 | TEST_P(UidClearTest, all_single) { |
| 182 | static const char msg[] = "this is a log message"; |
| 183 | auto write = [](SerializedLogChunk& chunk) { |
| 184 | chunk.Log(1, log_time(), kUidToClear, 1, 2, msg, sizeof(msg)); |
| 185 | }; |
| 186 | auto check = [](SerializedLogChunk&) {}; |
| 187 | |
| 188 | Test(write, check, true); |
| 189 | } |
| 190 | |
| 191 | // Test that ClearUidLogs() returns true if all logs in a given buffer correspond to the given UID. |
| 192 | TEST_P(UidClearTest, all_multiple) { |
| 193 | static const char msg[] = "this is a log message"; |
| 194 | auto write = [](SerializedLogChunk& chunk) { |
| 195 | chunk.Log(2, log_time(), kUidToClear, 1, 2, msg, sizeof(msg)); |
| 196 | chunk.Log(3, log_time(), kUidToClear, 1, 2, msg, sizeof(msg)); |
| 197 | chunk.Log(4, log_time(), kUidToClear, 1, 2, msg, sizeof(msg)); |
| 198 | }; |
| 199 | auto check = [](SerializedLogChunk&) {}; |
| 200 | |
| 201 | Test(write, check, true); |
| 202 | } |
| 203 | |
| 204 | static std::string MakePrintable(const uint8_t* in, size_t length) { |
| 205 | std::string result; |
| 206 | for (size_t i = 0; i < length; ++i) { |
| 207 | uint8_t c = in[i]; |
| 208 | if (isprint(c)) { |
| 209 | result.push_back(c); |
| 210 | } else { |
| 211 | result.append(StringPrintf("\\%02x", static_cast<int>(c) & 0xFF)); |
| 212 | } |
| 213 | } |
| 214 | return result; |
| 215 | } |
| 216 | |
| 217 | // This test clears UID logs at the beginning and end of the buffer, as well as two back to back |
| 218 | // logs in the interior. |
| 219 | TEST_P(UidClearTest, clear_beginning_and_end) { |
| 220 | static const char msg1[] = "this is a log message"; |
| 221 | static const char msg2[] = "non-cleared message"; |
| 222 | static const char msg3[] = "back to back cleared messages"; |
| 223 | static const char msg4[] = "second in a row gone"; |
| 224 | static const char msg5[] = "but we save this one"; |
| 225 | static const char msg6[] = "and this 1!"; |
| 226 | static const char msg7[] = "the last one goes too"; |
| 227 | auto write = [](SerializedLogChunk& chunk) { |
| 228 | ASSERT_NE(nullptr, chunk.Log(1, log_time(), kUidToClear, 1, 2, msg1, sizeof(msg1))); |
| 229 | ASSERT_NE(nullptr, chunk.Log(2, log_time(), kOtherUid, 1, 2, msg2, sizeof(msg2))); |
| 230 | ASSERT_NE(nullptr, chunk.Log(3, log_time(), kUidToClear, 1, 2, msg3, sizeof(msg3))); |
| 231 | ASSERT_NE(nullptr, chunk.Log(4, log_time(), kUidToClear, 1, 2, msg4, sizeof(msg4))); |
| 232 | ASSERT_NE(nullptr, chunk.Log(5, log_time(), kOtherUid, 1, 2, msg5, sizeof(msg5))); |
| 233 | ASSERT_NE(nullptr, chunk.Log(6, log_time(), kOtherUid, 1, 2, msg6, sizeof(msg6))); |
| 234 | ASSERT_NE(nullptr, chunk.Log(7, log_time(), kUidToClear, 1, 2, msg7, sizeof(msg7))); |
| 235 | }; |
| 236 | |
| 237 | auto check = [](SerializedLogChunk& chunk) { |
| 238 | size_t read_offset = 0; |
| 239 | auto* entry = chunk.log_entry(read_offset); |
| 240 | EXPECT_STREQ(msg2, entry->msg()); |
| 241 | read_offset += entry->total_len(); |
| 242 | |
| 243 | entry = chunk.log_entry(read_offset); |
| 244 | EXPECT_STREQ(msg5, entry->msg()); |
| 245 | read_offset += entry->total_len(); |
| 246 | |
| 247 | entry = chunk.log_entry(read_offset); |
| 248 | EXPECT_STREQ(msg6, entry->msg()) << MakePrintable(chunk.data(), chunk.write_offset()); |
| 249 | read_offset += entry->total_len(); |
| 250 | |
| 251 | EXPECT_EQ(static_cast<int>(read_offset), chunk.write_offset()); |
| 252 | }; |
| 253 | Test(write, check, false); |
| 254 | } |
| 255 | |
| 256 | // This tests the opposite case of beginning_and_end, in which we don't clear the beginning or end |
| 257 | // logs. There is a single log pruned in the middle instead of back to back logs. |
| 258 | TEST_P(UidClearTest, save_beginning_and_end) { |
| 259 | static const char msg1[] = "saved first message"; |
| 260 | static const char msg2[] = "cleared interior message"; |
| 261 | static const char msg3[] = "last message stays"; |
| 262 | auto write = [](SerializedLogChunk& chunk) { |
| 263 | ASSERT_NE(nullptr, chunk.Log(1, log_time(), kOtherUid, 1, 2, msg1, sizeof(msg1))); |
| 264 | ASSERT_NE(nullptr, chunk.Log(2, log_time(), kUidToClear, 1, 2, msg2, sizeof(msg2))); |
| 265 | ASSERT_NE(nullptr, chunk.Log(3, log_time(), kOtherUid, 1, 2, msg3, sizeof(msg3))); |
| 266 | }; |
| 267 | |
| 268 | auto check = [](SerializedLogChunk& chunk) { |
| 269 | size_t read_offset = 0; |
| 270 | auto* entry = chunk.log_entry(read_offset); |
| 271 | EXPECT_STREQ(msg1, entry->msg()); |
| 272 | read_offset += entry->total_len(); |
| 273 | |
| 274 | entry = chunk.log_entry(read_offset); |
| 275 | EXPECT_STREQ(msg3, entry->msg()); |
| 276 | read_offset += entry->total_len(); |
| 277 | |
| 278 | EXPECT_EQ(static_cast<int>(read_offset), chunk.write_offset()); |
| 279 | }; |
| 280 | Test(write, check, false); |
| 281 | } |
| 282 | |
| 283 | INSTANTIATE_TEST_CASE_P(UidClearTests, UidClearTest, testing::Values(true, false)); |