Tom Cherry | fb150dd | 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 | |
Elliott Hughes | 6265212 | 2021-04-09 17:20:15 -0700 | [diff] [blame] | 21 | #include <android-base/silent_death_test.h> |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 22 | #include <android-base/stringprintf.h> |
| 23 | #include <android/log.h> |
| 24 | #include <gtest/gtest.h> |
| 25 | |
Elliott Hughes | 6265212 | 2021-04-09 17:20:15 -0700 | [diff] [blame] | 26 | using SerializedLogChunk_DeathTest = SilentDeathTest; |
| 27 | |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 28 | using android::base::StringPrintf; |
| 29 | |
| 30 | TEST(SerializedLogChunk, smoke) { |
| 31 | size_t chunk_size = 10 * 4096; |
| 32 | auto chunk = SerializedLogChunk{chunk_size}; |
Tom Cherry | 4c71a2c | 2020-07-13 17:28:30 -0700 | [diff] [blame] | 33 | EXPECT_EQ(chunk_size + sizeof(SerializedLogChunk), chunk.PruneSize()); |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 34 | |
| 35 | static const char log_message[] = "log message"; |
| 36 | size_t expected_total_len = sizeof(SerializedLogEntry) + sizeof(log_message); |
| 37 | ASSERT_TRUE(chunk.CanLog(expected_total_len)); |
| 38 | EXPECT_TRUE(chunk.CanLog(chunk_size)); |
| 39 | EXPECT_FALSE(chunk.CanLog(chunk_size + 1)); |
| 40 | |
| 41 | log_time time(CLOCK_REALTIME); |
| 42 | auto* entry = chunk.Log(1234, time, 0, 1, 2, log_message, sizeof(log_message)); |
| 43 | ASSERT_NE(nullptr, entry); |
| 44 | |
| 45 | EXPECT_EQ(1234U, entry->sequence()); |
| 46 | EXPECT_EQ(time, entry->realtime()); |
| 47 | EXPECT_EQ(0U, entry->uid()); |
| 48 | EXPECT_EQ(1, entry->pid()); |
| 49 | EXPECT_EQ(2, entry->tid()); |
| 50 | EXPECT_EQ(sizeof(log_message), entry->msg_len()); |
| 51 | EXPECT_STREQ(log_message, entry->msg()); |
| 52 | EXPECT_EQ(expected_total_len, entry->total_len()); |
| 53 | |
| 54 | EXPECT_FALSE(chunk.CanLog(chunk_size)); |
| 55 | EXPECT_EQ(static_cast<int>(expected_total_len), chunk.write_offset()); |
| 56 | EXPECT_EQ(1234U, chunk.highest_sequence_number()); |
| 57 | } |
| 58 | |
| 59 | TEST(SerializedLogChunk, fill_log_exactly) { |
| 60 | static const char log_message[] = "this is a log message"; |
| 61 | size_t individual_message_size = sizeof(SerializedLogEntry) + sizeof(log_message); |
| 62 | size_t chunk_size = individual_message_size * 3; |
| 63 | auto chunk = SerializedLogChunk{chunk_size}; |
Tom Cherry | 4c71a2c | 2020-07-13 17:28:30 -0700 | [diff] [blame] | 64 | EXPECT_EQ(chunk_size + sizeof(SerializedLogChunk), chunk.PruneSize()); |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 65 | |
| 66 | ASSERT_TRUE(chunk.CanLog(individual_message_size)); |
| 67 | EXPECT_NE(nullptr, chunk.Log(1, log_time(), 1000, 1, 1, log_message, sizeof(log_message))); |
| 68 | |
| 69 | ASSERT_TRUE(chunk.CanLog(individual_message_size)); |
| 70 | EXPECT_NE(nullptr, chunk.Log(2, log_time(), 1000, 2, 1, log_message, sizeof(log_message))); |
| 71 | |
| 72 | ASSERT_TRUE(chunk.CanLog(individual_message_size)); |
| 73 | EXPECT_NE(nullptr, chunk.Log(3, log_time(), 1000, 3, 1, log_message, sizeof(log_message))); |
| 74 | |
| 75 | EXPECT_FALSE(chunk.CanLog(1)); |
| 76 | } |
| 77 | |
| 78 | TEST(SerializedLogChunk, three_logs) { |
| 79 | size_t chunk_size = 10 * 4096; |
| 80 | auto chunk = SerializedLogChunk{chunk_size}; |
| 81 | |
| 82 | chunk.Log(2, log_time(0x1234, 0x5678), 0x111, 0x222, 0x333, "initial message", |
| 83 | strlen("initial message")); |
| 84 | chunk.Log(3, log_time(0x2345, 0x6789), 0x444, 0x555, 0x666, "second message", |
| 85 | strlen("second message")); |
| 86 | auto uint64_t_max = std::numeric_limits<uint64_t>::max(); |
| 87 | auto uint32_t_max = std::numeric_limits<uint32_t>::max(); |
| 88 | chunk.Log(uint64_t_max, log_time(uint32_t_max, uint32_t_max), uint32_t_max, uint32_t_max, |
| 89 | uint32_t_max, "last message", strlen("last message")); |
| 90 | |
| 91 | static const char expected_buffer_data[] = |
| 92 | "\x11\x01\x00\x00\x22\x02\x00\x00\x33\x03\x00\x00" // UID PID TID |
| 93 | "\x02\x00\x00\x00\x00\x00\x00\x00" // Sequence |
| 94 | "\x34\x12\x00\x00\x78\x56\x00\x00" // Timestamp |
| 95 | "\x0F\x00initial message" // msg_len + message |
| 96 | "\x44\x04\x00\x00\x55\x05\x00\x00\x66\x06\x00\x00" // UID PID TID |
| 97 | "\x03\x00\x00\x00\x00\x00\x00\x00" // Sequence |
| 98 | "\x45\x23\x00\x00\x89\x67\x00\x00" // Timestamp |
| 99 | "\x0E\x00second message" // msg_len + message |
| 100 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" // UID PID TID |
| 101 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" // Sequence |
| 102 | "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF" // Timestamp |
| 103 | "\x0C\x00last message"; // msg_len + message |
| 104 | |
Tom Cherry | 3e8d3a7 | 2021-06-22 17:17:36 -0700 | [diff] [blame] | 105 | for (size_t i = 0; i < sizeof(expected_buffer_data) - 1; ++i) { |
Tom Cherry | 40ea726 | 2020-10-06 15:54:26 -0700 | [diff] [blame] | 106 | EXPECT_EQ(static_cast<uint8_t>(expected_buffer_data[i]), chunk.data()[i]) |
| 107 | << "position: " << i; |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | // Check that the CHECK() in DecReaderRefCount() if the ref count goes bad is caught. |
Elliott Hughes | 6265212 | 2021-04-09 17:20:15 -0700 | [diff] [blame] | 112 | TEST_F(SerializedLogChunk_DeathTest, catch_DecCompressedRef_CHECK) { |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 113 | size_t chunk_size = 10 * 4096; |
| 114 | auto chunk = SerializedLogChunk{chunk_size}; |
Tom Cherry | b8c967e | 2020-07-16 20:46:14 -0700 | [diff] [blame] | 115 | EXPECT_DEATH({ chunk.DecReaderRefCount(); }, ""); |
Tom Cherry | fb150dd | 2020-05-13 09:28:37 -0700 | [diff] [blame] | 116 | } |
| 117 | |