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