1 /*
2 * Copyright (C) 2016 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 <sys/types.h>
18 #include <unistd.h>
19
20 #include <memory>
21 #include <string>
22
23 #include <android-base/file.h>
24 #include <android-base/stringprintf.h>
25 #include <android-base/test_utils.h>
26 #include <gtest/gtest.h>
27
28 #include "../trace-dev.c"
29
30 class TraceDevTest : public ::testing::Test {
31 protected:
SetUp()32 void SetUp() override {
33 lseek(tmp_file_.fd, 0, SEEK_SET);
34 atrace_marker_fd = tmp_file_.fd;
35 }
36
TearDown()37 void TearDown() override {
38 atrace_marker_fd = -1;
39 }
40
41 TemporaryFile tmp_file_;
42
MakeName(size_t length)43 static std::string MakeName(size_t length) {
44 std::string name;
45 for (size_t i = 0; i < length; i++) {
46 name += '0' + (i % 10);
47 }
48 return name;
49 }
50 };
51
TEST_F(TraceDevTest,atrace_begin_body_normal)52 TEST_F(TraceDevTest, atrace_begin_body_normal) {
53 atrace_begin_body("fake_name");
54
55 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
56
57 std::string actual;
58 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
59 std::string expected = android::base::StringPrintf("B|%d|fake_name", getpid());
60 ASSERT_STREQ(expected.c_str(), actual.c_str());
61 }
62
TEST_F(TraceDevTest,atrace_begin_body_exact)63 TEST_F(TraceDevTest, atrace_begin_body_exact) {
64 std::string expected = android::base::StringPrintf("B|%d|", getpid());
65 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 1);
66 atrace_begin_body(name.c_str());
67
68 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
69 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
70
71 std::string actual;
72 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
73 expected += name;
74 ASSERT_STREQ(expected.c_str(), actual.c_str());
75
76 // Add a single character and verify we get the exact same value as before.
77 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
78 name += '*';
79 atrace_begin_body(name.c_str());
80 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
81 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
82 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
83 ASSERT_STREQ(expected.c_str(), actual.c_str());
84 }
85
TEST_F(TraceDevTest,atrace_begin_body_truncated)86 TEST_F(TraceDevTest, atrace_begin_body_truncated) {
87 std::string expected = android::base::StringPrintf("B|%d|", getpid());
88 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
89 atrace_begin_body(name.c_str());
90
91 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
92 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
93
94 std::string actual;
95 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
96 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 1;
97 expected += android::base::StringPrintf("%.*s", expected_len, name.c_str());
98 ASSERT_STREQ(expected.c_str(), actual.c_str());
99 }
100
TEST_F(TraceDevTest,atrace_async_begin_body_normal)101 TEST_F(TraceDevTest, atrace_async_begin_body_normal) {
102 atrace_async_begin_body("fake_name", 12345);
103
104 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
105
106 std::string actual;
107 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
108 std::string expected = android::base::StringPrintf("S|%d|fake_name|12345", getpid());
109 ASSERT_STREQ(expected.c_str(), actual.c_str());
110 }
111
TEST_F(TraceDevTest,atrace_async_begin_body_exact)112 TEST_F(TraceDevTest, atrace_async_begin_body_exact) {
113 std::string expected = android::base::StringPrintf("S|%d|", getpid());
114 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
115 atrace_async_begin_body(name.c_str(), 12345);
116
117 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
118 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
119
120 std::string actual;
121 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
122 expected += name + "|12345";
123 ASSERT_STREQ(expected.c_str(), actual.c_str());
124
125 // Add a single character and verify we get the exact same value as before.
126 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
127 name += '*';
128 atrace_async_begin_body(name.c_str(), 12345);
129 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
130 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
131 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
132 ASSERT_STREQ(expected.c_str(), actual.c_str());
133 }
134
TEST_F(TraceDevTest,atrace_async_begin_body_truncated)135 TEST_F(TraceDevTest, atrace_async_begin_body_truncated) {
136 std::string expected = android::base::StringPrintf("S|%d|", getpid());
137 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
138 atrace_async_begin_body(name.c_str(), 12345);
139
140 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
141 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
142
143 std::string actual;
144 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
145 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
146 expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
147 ASSERT_STREQ(expected.c_str(), actual.c_str());
148 }
149
TEST_F(TraceDevTest,atrace_async_end_body_normal)150 TEST_F(TraceDevTest, atrace_async_end_body_normal) {
151 atrace_async_end_body("fake_name", 12345);
152
153 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
154
155 std::string actual;
156 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
157 std::string expected = android::base::StringPrintf("F|%d|fake_name|12345", getpid());
158 ASSERT_STREQ(expected.c_str(), actual.c_str());
159 }
160
TEST_F(TraceDevTest,atrace_async_end_body_exact)161 TEST_F(TraceDevTest, atrace_async_end_body_exact) {
162 std::string expected = android::base::StringPrintf("F|%d|", getpid());
163 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
164 atrace_async_end_body(name.c_str(), 12345);
165
166 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
167 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
168
169 std::string actual;
170 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
171 expected += name + "|12345";
172 ASSERT_STREQ(expected.c_str(), actual.c_str());
173
174 // Add a single character and verify we get the exact same value as before.
175 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
176 name += '*';
177 atrace_async_end_body(name.c_str(), 12345);
178 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
179 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
180 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
181 ASSERT_STREQ(expected.c_str(), actual.c_str());
182 }
183
TEST_F(TraceDevTest,atrace_async_end_body_truncated)184 TEST_F(TraceDevTest, atrace_async_end_body_truncated) {
185 std::string expected = android::base::StringPrintf("F|%d|", getpid());
186 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
187 atrace_async_end_body(name.c_str(), 12345);
188
189 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
190 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
191
192 std::string actual;
193 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
194 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
195 expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
196 ASSERT_STREQ(expected.c_str(), actual.c_str());
197 }
198
TEST_F(TraceDevTest,atrace_int_body_normal)199 TEST_F(TraceDevTest, atrace_int_body_normal) {
200 atrace_int_body("fake_name", 12345);
201
202 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
203
204 std::string actual;
205 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
206 std::string expected = android::base::StringPrintf("C|%d|fake_name|12345", getpid());
207 ASSERT_STREQ(expected.c_str(), actual.c_str());
208 }
209
TEST_F(TraceDevTest,atrace_int_body_exact)210 TEST_F(TraceDevTest, atrace_int_body_exact) {
211 std::string expected = android::base::StringPrintf("C|%d|", getpid());
212 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
213 atrace_int_body(name.c_str(), 12345);
214
215 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
216 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
217
218 std::string actual;
219 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
220 expected += name + "|12345";
221 ASSERT_STREQ(expected.c_str(), actual.c_str());
222
223 // Add a single character and verify we get the exact same value as before.
224 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
225 name += '*';
226 atrace_int_body(name.c_str(), 12345);
227 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
228 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
229 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
230 ASSERT_STREQ(expected.c_str(), actual.c_str());
231 }
232
TEST_F(TraceDevTest,atrace_int_body_truncated)233 TEST_F(TraceDevTest, atrace_int_body_truncated) {
234 std::string expected = android::base::StringPrintf("C|%d|", getpid());
235 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
236 atrace_int_body(name.c_str(), 12345);
237
238 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
239 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
240
241 std::string actual;
242 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
243 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
244 expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
245 ASSERT_STREQ(expected.c_str(), actual.c_str());
246 }
247
TEST_F(TraceDevTest,atrace_int64_body_normal)248 TEST_F(TraceDevTest, atrace_int64_body_normal) {
249 atrace_int64_body("fake_name", 17179869183L);
250
251 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
252
253 std::string actual;
254 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
255 std::string expected = android::base::StringPrintf("C|%d|fake_name|17179869183", getpid());
256 ASSERT_STREQ(expected.c_str(), actual.c_str());
257 }
258
TEST_F(TraceDevTest,atrace_int64_body_exact)259 TEST_F(TraceDevTest, atrace_int64_body_exact) {
260 std::string expected = android::base::StringPrintf("C|%d|", getpid());
261 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 13);
262 atrace_int64_body(name.c_str(), 17179869183L);
263
264 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
265 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
266
267 std::string actual;
268 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
269 expected += name + "|17179869183";
270 ASSERT_STREQ(expected.c_str(), actual.c_str());
271
272 // Add a single character and verify we get the exact same value as before.
273 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
274 name += '*';
275 atrace_int64_body(name.c_str(), 17179869183L);
276 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
277 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
278 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
279 ASSERT_STREQ(expected.c_str(), actual.c_str());
280 }
281
TEST_F(TraceDevTest,atrace_int64_body_truncated)282 TEST_F(TraceDevTest, atrace_int64_body_truncated) {
283 std::string expected = android::base::StringPrintf("C|%d|", getpid());
284 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
285 atrace_int64_body(name.c_str(), 17179869183L);
286
287 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
288 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
289
290 std::string actual;
291 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
292 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 13;
293 expected += android::base::StringPrintf("%.*s|17179869183", expected_len, name.c_str());
294 ASSERT_STREQ(expected.c_str(), actual.c_str());
295 }
296