blob: 2b478a3fbb1588b4549561e2355e8e290c4f3de6 [file] [log] [blame]
Tom Cherry1a796bc2020-05-13 09:28:37 -07001/*
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
25using android::base::StringPrintf;
26
27TEST(SerializedLogChunk, smoke) {
28 size_t chunk_size = 10 * 4096;
29 auto chunk = SerializedLogChunk{chunk_size};
Tom Cherry44e1a5d2020-07-13 17:28:30 -070030 EXPECT_EQ(chunk_size + sizeof(SerializedLogChunk), chunk.PruneSize());
Tom Cherry1a796bc2020-05-13 09:28:37 -070031
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
56TEST(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 Cherry44e1a5d2020-07-13 17:28:30 -070061 EXPECT_EQ(chunk_size + sizeof(SerializedLogChunk), chunk.PruneSize());
Tom Cherry1a796bc2020-05-13 09:28:37 -070062
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
75TEST(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.
113TEST(SerializedLogChunk, catch_DecCompressedRef_CHECK) {
114 size_t chunk_size = 10 * 4096;
115 auto chunk = SerializedLogChunk{chunk_size};
116 EXPECT_DEATH({ chunk.DecReaderRefCount(true); }, "");
117 EXPECT_DEATH({ chunk.DecReaderRefCount(false); }, "");
118}
119
120// Check that the CHECK() in ClearUidLogs() if the ref count is greater than 0 is caught.
121TEST(SerializedLogChunk, catch_ClearUidLogs_CHECK) {
122 size_t chunk_size = 10 * 4096;
123 auto chunk = SerializedLogChunk{chunk_size};
124 chunk.IncReaderRefCount();
125 EXPECT_DEATH({ chunk.ClearUidLogs(1000, LOG_ID_MAIN, nullptr); }, "");
126 chunk.DecReaderRefCount(false);
127}
128
129class UidClearTest : public testing::TestWithParam<bool> {
130 protected:
131 template <typename Write, typename Check>
132 void Test(const Write& write, const Check& check, bool expected_result) {
133 write(chunk_);
134
135 bool finish_writing = GetParam();
136 if (finish_writing) {
137 chunk_.FinishWriting();
138 }
139 EXPECT_EQ(expected_result, chunk_.ClearUidLogs(kUidToClear, LOG_ID_MAIN, nullptr));
140 if (finish_writing) {
141 chunk_.IncReaderRefCount();
142 }
143
144 check(chunk_);
145
146 if (finish_writing) {
147 chunk_.DecReaderRefCount(false);
148 }
149 }
150
151 static constexpr size_t kChunkSize = 10 * 4096;
152 static constexpr uid_t kUidToClear = 1000;
153 static constexpr uid_t kOtherUid = 1234;
154
155 SerializedLogChunk chunk_{kChunkSize};
156};
157
158// Test that ClearUidLogs() is a no-op if there are no logs of that UID in the buffer.
159TEST_P(UidClearTest, no_logs_in_chunk) {
160 auto write = [](SerializedLogChunk&) {};
161 auto check = [](SerializedLogChunk&) {};
162
163 Test(write, check, true);
164}
165
166// Test that ClearUidLogs() is a no-op if there are no logs of that UID in the buffer.
167TEST_P(UidClearTest, no_logs_from_uid) {
168 static const char msg[] = "this is a log message";
169 auto write = [](SerializedLogChunk& chunk) {
170 chunk.Log(1, log_time(), kOtherUid, 1, 2, msg, sizeof(msg));
171 };
172
173 auto check = [](SerializedLogChunk& chunk) {
174 auto* entry = chunk.log_entry(0);
175 EXPECT_STREQ(msg, entry->msg());
176 };
177
178 Test(write, check, false);
179}
180
181// Test that ClearUidLogs() returns true if all logs in a given buffer correspond to the given UID.
182TEST_P(UidClearTest, all_single) {
183 static const char msg[] = "this is a log message";
184 auto write = [](SerializedLogChunk& chunk) {
185 chunk.Log(1, log_time(), kUidToClear, 1, 2, msg, sizeof(msg));
186 };
187 auto check = [](SerializedLogChunk&) {};
188
189 Test(write, check, true);
190}
191
192// Test that ClearUidLogs() returns true if all logs in a given buffer correspond to the given UID.
193TEST_P(UidClearTest, all_multiple) {
194 static const char msg[] = "this is a log message";
195 auto write = [](SerializedLogChunk& chunk) {
196 chunk.Log(2, log_time(), kUidToClear, 1, 2, msg, sizeof(msg));
197 chunk.Log(3, log_time(), kUidToClear, 1, 2, msg, sizeof(msg));
198 chunk.Log(4, log_time(), kUidToClear, 1, 2, msg, sizeof(msg));
199 };
200 auto check = [](SerializedLogChunk&) {};
201
202 Test(write, check, true);
203}
204
205static std::string MakePrintable(const uint8_t* in, size_t length) {
206 std::string result;
207 for (size_t i = 0; i < length; ++i) {
208 uint8_t c = in[i];
209 if (isprint(c)) {
210 result.push_back(c);
211 } else {
212 result.append(StringPrintf("\\%02x", static_cast<int>(c) & 0xFF));
213 }
214 }
215 return result;
216}
217
218// This test clears UID logs at the beginning and end of the buffer, as well as two back to back
219// logs in the interior.
220TEST_P(UidClearTest, clear_beginning_and_end) {
221 static const char msg1[] = "this is a log message";
222 static const char msg2[] = "non-cleared message";
223 static const char msg3[] = "back to back cleared messages";
224 static const char msg4[] = "second in a row gone";
225 static const char msg5[] = "but we save this one";
226 static const char msg6[] = "and this 1!";
227 static const char msg7[] = "the last one goes too";
228 auto write = [](SerializedLogChunk& chunk) {
229 ASSERT_NE(nullptr, chunk.Log(1, log_time(), kUidToClear, 1, 2, msg1, sizeof(msg1)));
230 ASSERT_NE(nullptr, chunk.Log(2, log_time(), kOtherUid, 1, 2, msg2, sizeof(msg2)));
231 ASSERT_NE(nullptr, chunk.Log(3, log_time(), kUidToClear, 1, 2, msg3, sizeof(msg3)));
232 ASSERT_NE(nullptr, chunk.Log(4, log_time(), kUidToClear, 1, 2, msg4, sizeof(msg4)));
233 ASSERT_NE(nullptr, chunk.Log(5, log_time(), kOtherUid, 1, 2, msg5, sizeof(msg5)));
234 ASSERT_NE(nullptr, chunk.Log(6, log_time(), kOtherUid, 1, 2, msg6, sizeof(msg6)));
235 ASSERT_NE(nullptr, chunk.Log(7, log_time(), kUidToClear, 1, 2, msg7, sizeof(msg7)));
236 };
237
238 auto check = [](SerializedLogChunk& chunk) {
239 size_t read_offset = 0;
240 auto* entry = chunk.log_entry(read_offset);
241 EXPECT_STREQ(msg2, entry->msg());
242 read_offset += entry->total_len();
243
244 entry = chunk.log_entry(read_offset);
245 EXPECT_STREQ(msg5, entry->msg());
246 read_offset += entry->total_len();
247
248 entry = chunk.log_entry(read_offset);
249 EXPECT_STREQ(msg6, entry->msg()) << MakePrintable(chunk.data(), chunk.write_offset());
250 read_offset += entry->total_len();
251
252 EXPECT_EQ(static_cast<int>(read_offset), chunk.write_offset());
253 };
254 Test(write, check, false);
255}
256
257// This tests the opposite case of beginning_and_end, in which we don't clear the beginning or end
258// logs. There is a single log pruned in the middle instead of back to back logs.
259TEST_P(UidClearTest, save_beginning_and_end) {
260 static const char msg1[] = "saved first message";
261 static const char msg2[] = "cleared interior message";
262 static const char msg3[] = "last message stays";
263 auto write = [](SerializedLogChunk& chunk) {
264 ASSERT_NE(nullptr, chunk.Log(1, log_time(), kOtherUid, 1, 2, msg1, sizeof(msg1)));
265 ASSERT_NE(nullptr, chunk.Log(2, log_time(), kUidToClear, 1, 2, msg2, sizeof(msg2)));
266 ASSERT_NE(nullptr, chunk.Log(3, log_time(), kOtherUid, 1, 2, msg3, sizeof(msg3)));
267 };
268
269 auto check = [](SerializedLogChunk& chunk) {
270 size_t read_offset = 0;
271 auto* entry = chunk.log_entry(read_offset);
272 EXPECT_STREQ(msg1, entry->msg());
273 read_offset += entry->total_len();
274
275 entry = chunk.log_entry(read_offset);
276 EXPECT_STREQ(msg3, entry->msg());
277 read_offset += entry->total_len();
278
279 EXPECT_EQ(static_cast<int>(read_offset), chunk.write_offset());
280 };
281 Test(write, check, false);
282}
283
284INSTANTIATE_TEST_CASE_P(UidClearTests, UidClearTest, testing::Values(true, false));