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 <gtest/gtest.h>
26
27 #include "../trace-dev.cpp"
28
29 class TraceDevTest : public ::testing::Test {
30 protected:
SetUp()31 void SetUp() override {
32 lseek(tmp_file_.fd, 0, SEEK_SET);
33 atrace_marker_fd = tmp_file_.fd;
34 }
35
TearDown()36 void TearDown() override {
37 atrace_marker_fd = -1;
38 }
39
40 TemporaryFile tmp_file_;
41
MakeName(size_t length)42 static std::string MakeName(size_t length) {
43 std::string name;
44 for (size_t i = 0; i < length; i++) {
45 name += '0' + (i % 10);
46 }
47 return name;
48 }
49 };
50
TEST_F(TraceDevTest,atrace_begin_body_normal)51 TEST_F(TraceDevTest, atrace_begin_body_normal) {
52 atrace_begin_body("fake_name");
53
54 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
55
56 std::string actual;
57 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
58 std::string expected = android::base::StringPrintf("B|%d|fake_name", getpid());
59 ASSERT_STREQ(expected.c_str(), actual.c_str());
60 }
61
TEST_F(TraceDevTest,atrace_begin_body_exact)62 TEST_F(TraceDevTest, atrace_begin_body_exact) {
63 std::string expected = android::base::StringPrintf("B|%d|", getpid());
64 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 1);
65 atrace_begin_body(name.c_str());
66
67 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
68 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
69
70 std::string actual;
71 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
72 expected += name;
73 ASSERT_STREQ(expected.c_str(), actual.c_str());
74
75 // Add a single character and verify we get the exact same value as before.
76 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
77 name += '*';
78 atrace_begin_body(name.c_str());
79 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
80 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
81 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
82 ASSERT_STREQ(expected.c_str(), actual.c_str());
83 }
84
TEST_F(TraceDevTest,atrace_begin_body_truncated)85 TEST_F(TraceDevTest, atrace_begin_body_truncated) {
86 std::string expected = android::base::StringPrintf("B|%d|", getpid());
87 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
88 atrace_begin_body(name.c_str());
89
90 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
91 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
92
93 std::string actual;
94 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
95 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 1;
96 expected += android::base::StringPrintf("%.*s", expected_len, name.c_str());
97 ASSERT_STREQ(expected.c_str(), actual.c_str());
98 }
99
TEST_F(TraceDevTest,atrace_async_begin_body_normal)100 TEST_F(TraceDevTest, atrace_async_begin_body_normal) {
101 atrace_async_begin_body("fake_name", 12345);
102
103 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
104
105 std::string actual;
106 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
107 std::string expected = android::base::StringPrintf("S|%d|fake_name|12345", getpid());
108 ASSERT_STREQ(expected.c_str(), actual.c_str());
109 }
110
TEST_F(TraceDevTest,atrace_async_begin_body_exact)111 TEST_F(TraceDevTest, atrace_async_begin_body_exact) {
112 std::string expected = android::base::StringPrintf("S|%d|", getpid());
113 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
114 atrace_async_begin_body(name.c_str(), 12345);
115
116 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
117 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
118
119 std::string actual;
120 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
121 expected += name + "|12345";
122 ASSERT_STREQ(expected.c_str(), actual.c_str());
123
124 // Add a single character and verify we get the exact same value as before.
125 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
126 name += '*';
127 atrace_async_begin_body(name.c_str(), 12345);
128 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
129 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
130 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
131 ASSERT_STREQ(expected.c_str(), actual.c_str());
132 }
133
TEST_F(TraceDevTest,atrace_async_begin_body_truncated)134 TEST_F(TraceDevTest, atrace_async_begin_body_truncated) {
135 std::string expected = android::base::StringPrintf("S|%d|", getpid());
136 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
137 atrace_async_begin_body(name.c_str(), 12345);
138
139 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
140 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
141
142 std::string actual;
143 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
144 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
145 expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
146 ASSERT_STREQ(expected.c_str(), actual.c_str());
147 }
148
TEST_F(TraceDevTest,atrace_async_end_body_normal)149 TEST_F(TraceDevTest, atrace_async_end_body_normal) {
150 atrace_async_end_body("fake_name", 12345);
151
152 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
153
154 std::string actual;
155 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
156 std::string expected = android::base::StringPrintf("F|%d|fake_name|12345", getpid());
157 ASSERT_STREQ(expected.c_str(), actual.c_str());
158 }
159
TEST_F(TraceDevTest,atrace_async_end_body_exact)160 TEST_F(TraceDevTest, atrace_async_end_body_exact) {
161 std::string expected = android::base::StringPrintf("F|%d|", getpid());
162 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
163 atrace_async_end_body(name.c_str(), 12345);
164
165 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
166 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
167
168 std::string actual;
169 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
170 expected += name + "|12345";
171 ASSERT_STREQ(expected.c_str(), actual.c_str());
172
173 // Add a single character and verify we get the exact same value as before.
174 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
175 name += '*';
176 atrace_async_end_body(name.c_str(), 12345);
177 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
178 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
179 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
180 ASSERT_STREQ(expected.c_str(), actual.c_str());
181 }
182
TEST_F(TraceDevTest,atrace_async_end_body_truncated)183 TEST_F(TraceDevTest, atrace_async_end_body_truncated) {
184 std::string expected = android::base::StringPrintf("F|%d|", getpid());
185 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
186 atrace_async_end_body(name.c_str(), 12345);
187
188 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
189 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
190
191 std::string actual;
192 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
193 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
194 expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
195 ASSERT_STREQ(expected.c_str(), actual.c_str());
196 }
197
TEST_F(TraceDevTest,atrace_async_for_track_begin_body_normal)198 TEST_F(TraceDevTest, atrace_async_for_track_begin_body_normal) {
199 atrace_async_for_track_begin_body("fake_track", "fake_name", 12345);
200
201 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
202
203 std::string actual;
204 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
205 std::string expected = android::base::StringPrintf("G|%d|fake_track|fake_name|12345", getpid());
206 ASSERT_STREQ(expected.c_str(), actual.c_str());
207 }
208
TEST_F(TraceDevTest,atrace_async_for_track_begin_body_exact_track_name)209 TEST_F(TraceDevTest, atrace_async_for_track_begin_body_exact_track_name) {
210 const int name_size = 5;
211 std::string expected = android::base::StringPrintf("G|%d|", getpid());
212 std::string track_name =
213 MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 1 - name_size - 6);
214 atrace_async_for_track_begin_body(track_name.c_str(), "name", 12345);
215
216 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
217 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
218
219 std::string actual;
220 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
221 expected += track_name + "|name|12345";
222 ASSERT_STREQ(expected.c_str(), actual.c_str());
223
224 // Add a single character and verify name truncation
225 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
226 track_name += '*';
227 expected = android::base::StringPrintf("G|%d|", getpid());
228 expected += track_name + "|nam|12345";
229 atrace_async_for_track_begin_body(track_name.c_str(), "name", 12345);
230 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
231 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
232 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
233 ASSERT_STREQ(expected.c_str(), actual.c_str());
234 }
235
TEST_F(TraceDevTest,atrace_async_for_track_begin_body_truncated_track_name)236 TEST_F(TraceDevTest, atrace_async_for_track_begin_body_truncated_track_name) {
237 std::string expected = android::base::StringPrintf("G|%d|", getpid());
238 std::string track_name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
239 atrace_async_for_track_begin_body(track_name.c_str(), "name", 12345);
240
241 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
242 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
243
244 std::string actual;
245 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
246 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 9;
247 expected += android::base::StringPrintf("%.*s|n|12345", expected_len, track_name.c_str());
248 ASSERT_STREQ(expected.c_str(), actual.c_str());
249 }
250
TEST_F(TraceDevTest,atrace_async_for_track_begin_body_exact_name)251 TEST_F(TraceDevTest, atrace_async_for_track_begin_body_exact_name) {
252 const int track_name_size = 11;
253 std::string expected = android::base::StringPrintf("G|%d|", getpid());
254 std::string name =
255 MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 1 - track_name_size - 6);
256 atrace_async_for_track_begin_body("track_name", name.c_str(), 12345);
257
258 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
259 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
260
261 std::string actual;
262 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
263 expected += "track_name|" + name + "|12345";
264 ASSERT_STREQ(expected.c_str(), actual.c_str());
265
266 // Add a single character and verify we get the same value as before.
267 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
268 name += '*';
269 atrace_async_for_track_begin_body("track_name", name.c_str(), 12345);
270 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
271 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
272 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
273 ASSERT_STREQ(expected.c_str(), actual.c_str());
274 }
275
TEST_F(TraceDevTest,atrace_async_for_track_begin_body_truncated_name)276 TEST_F(TraceDevTest, atrace_async_for_track_begin_body_truncated_name) {
277 std::string expected = android::base::StringPrintf("G|%d|track_name|", getpid());
278 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
279 atrace_async_for_track_begin_body("track_name", name.c_str(), 12345);
280
281 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
282 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
283
284 std::string actual;
285 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
286 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 1 - 6;
287 expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
288 ASSERT_STREQ(expected.c_str(), actual.c_str());
289 }
290
TEST_F(TraceDevTest,atrace_async_for_track_begin_body_truncated_both)291 TEST_F(TraceDevTest, atrace_async_for_track_begin_body_truncated_both) {
292 std::string expected = android::base::StringPrintf("G|%d|", getpid());
293 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
294 std::string track_name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
295 atrace_async_for_track_begin_body(track_name.c_str(), name.c_str(), 12345);
296
297 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
298 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
299
300 std::string actual;
301 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
302 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 3 - 6;
303 expected += android::base::StringPrintf("%.*s|%.1s|12345", expected_len, track_name.c_str(),
304 name.c_str());
305 ASSERT_STREQ(expected.c_str(), actual.c_str());
306 }
307
TEST_F(TraceDevTest,atrace_async_for_track_end_body_normal)308 TEST_F(TraceDevTest, atrace_async_for_track_end_body_normal) {
309 atrace_async_for_track_end_body("fake_track", 12345);
310
311 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
312
313 std::string actual;
314 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
315 std::string expected = android::base::StringPrintf("H|%d|fake_track|12345", getpid());
316 ASSERT_STREQ(expected.c_str(), actual.c_str());
317 }
318
TEST_F(TraceDevTest,atrace_async_for_track_end_body_exact)319 TEST_F(TraceDevTest, atrace_async_for_track_end_body_exact) {
320 std::string expected = android::base::StringPrintf("H|%d|", getpid());
321 std::string track_name =
322 MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
323 atrace_async_for_track_end_body(track_name.c_str(), 12345);
324
325 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
326 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
327
328 std::string actual;
329 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
330 expected += track_name + "|12345";
331 ASSERT_STREQ(expected.c_str(), actual.c_str());
332
333 // Add a single character and verify we get the exact same value as before.
334 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
335 track_name += '*';
336 atrace_async_for_track_end_body(track_name.c_str(), 12345);
337 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
338 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
339 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
340 ASSERT_STREQ(expected.c_str(), actual.c_str());
341 }
342
TEST_F(TraceDevTest,atrace_async_for_track_end_body_truncated)343 TEST_F(TraceDevTest, atrace_async_for_track_end_body_truncated) {
344 std::string expected = android::base::StringPrintf("H|%d|", getpid());
345 std::string track_name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
346 atrace_async_for_track_end_body(track_name.c_str(), 12345);
347
348 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
349 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
350
351 std::string actual;
352 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
353 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
354 expected += android::base::StringPrintf("%.*s|12345", expected_len, track_name.c_str());
355 ASSERT_STREQ(expected.c_str(), actual.c_str());
356 }
357
TEST_F(TraceDevTest,atrace_instant_body_normal)358 TEST_F(TraceDevTest, atrace_instant_body_normal) {
359 atrace_instant_body("fake_name");
360
361 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
362
363 std::string actual;
364 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
365 std::string expected = android::base::StringPrintf("I|%d|fake_name", getpid());
366 ASSERT_STREQ(expected.c_str(), actual.c_str());
367 }
368
TEST_F(TraceDevTest,atrace_instant_body_exact)369 TEST_F(TraceDevTest, atrace_instant_body_exact) {
370 std::string expected = android::base::StringPrintf("I|%d|", getpid());
371 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 1);
372 atrace_instant_body(name.c_str());
373
374 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
375 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
376
377 std::string actual;
378 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
379 expected += name;
380 ASSERT_STREQ(expected.c_str(), actual.c_str());
381
382 // Add a single character and verify we get the exact same value as before.
383 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
384 name += '*';
385 atrace_instant_body(name.c_str());
386 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
387 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
388 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
389 ASSERT_STREQ(expected.c_str(), actual.c_str());
390 }
391
TEST_F(TraceDevTest,atrace_instant_body_truncated)392 TEST_F(TraceDevTest, atrace_instant_body_truncated) {
393 std::string expected = android::base::StringPrintf("I|%d|", getpid());
394 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
395 atrace_instant_body(name.c_str());
396
397 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
398 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
399
400 std::string actual;
401 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
402 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 1;
403 expected += android::base::StringPrintf("%.*s", expected_len, name.c_str());
404 ASSERT_STREQ(expected.c_str(), actual.c_str());
405 }
406
TEST_F(TraceDevTest,atrace_instant_for_track_body_normal)407 TEST_F(TraceDevTest, atrace_instant_for_track_body_normal) {
408 atrace_instant_for_track_body("fake_track", "fake_name");
409
410 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
411
412 std::string actual;
413 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
414 std::string expected = android::base::StringPrintf("N|%d|fake_track|fake_name", getpid());
415 ASSERT_STREQ(expected.c_str(), actual.c_str());
416 }
417
TEST_F(TraceDevTest,atrace_instant_for_track_body_exact_track_name)418 TEST_F(TraceDevTest, atrace_instant_for_track_body_exact_track_name) {
419 const int name_size = 5;
420 std::string expected = android::base::StringPrintf("N|%d|", getpid());
421 std::string track_name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 1 - name_size);
422 atrace_instant_for_track_body(track_name.c_str(), "name");
423
424 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
425 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
426
427 std::string actual;
428 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
429 expected += track_name + "|name";
430 ASSERT_STREQ(expected.c_str(), actual.c_str());
431
432 // Add a single character and verify name truncation
433 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
434 track_name += '*';
435 expected = android::base::StringPrintf("N|%d|", getpid());
436 expected += track_name + "|nam";
437 atrace_instant_for_track_body(track_name.c_str(), "name");
438 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
439 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
440 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
441 ASSERT_STREQ(expected.c_str(), actual.c_str());
442 }
443
TEST_F(TraceDevTest,atrace_instant_for_track_body_truncated_track_name)444 TEST_F(TraceDevTest, atrace_instant_for_track_body_truncated_track_name) {
445 std::string expected = android::base::StringPrintf("N|%d|", getpid());
446 std::string track_name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
447 atrace_instant_for_track_body(track_name.c_str(), "name");
448
449 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
450 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
451
452 std::string actual;
453 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
454 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 3;
455 expected += android::base::StringPrintf("%.*s|n", expected_len, track_name.c_str());
456 ASSERT_STREQ(expected.c_str(), actual.c_str());
457 }
458
TEST_F(TraceDevTest,atrace_instant_for_track_body_exact_name)459 TEST_F(TraceDevTest, atrace_instant_for_track_body_exact_name) {
460 const int track_name_size = 11;
461 std::string expected = android::base::StringPrintf("N|%d|", getpid());
462 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 1 - track_name_size);
463 atrace_instant_for_track_body("track_name", name.c_str());
464
465 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
466 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
467
468 std::string actual;
469 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
470 expected += "track_name|" + name;
471 ASSERT_STREQ(expected.c_str(), actual.c_str());
472
473 // Add a single character and verify we get the same value as before.
474 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
475 name += '*';
476 atrace_instant_for_track_body("track_name", name.c_str());
477 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
478 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
479 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
480 ASSERT_STREQ(expected.c_str(), actual.c_str());
481 }
482
TEST_F(TraceDevTest,atrace_instant_for_track_body_truncated_name)483 TEST_F(TraceDevTest, atrace_instant_for_track_body_truncated_name) {
484 std::string expected = android::base::StringPrintf("N|%d|track_name|", getpid());
485 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
486 atrace_instant_for_track_body("track_name", name.c_str());
487
488 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
489 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
490
491 std::string actual;
492 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
493 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 1;
494 expected += android::base::StringPrintf("%.*s", expected_len, name.c_str());
495 ASSERT_STREQ(expected.c_str(), actual.c_str());
496 }
497
TEST_F(TraceDevTest,atrace_instant_for_track_body_truncated_both)498 TEST_F(TraceDevTest, atrace_instant_for_track_body_truncated_both) {
499 std::string expected = android::base::StringPrintf("N|%d|", getpid());
500 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
501 std::string track_name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
502 atrace_instant_for_track_body(track_name.c_str(), name.c_str());
503
504 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
505 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
506
507 std::string actual;
508 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
509 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 3;
510 expected +=
511 android::base::StringPrintf("%.*s|%.1s", expected_len, track_name.c_str(), name.c_str());
512 ASSERT_STREQ(expected.c_str(), actual.c_str());
513 }
514
TEST_F(TraceDevTest,atrace_int_body_normal)515 TEST_F(TraceDevTest, atrace_int_body_normal) {
516 atrace_int_body("fake_name", 12345);
517
518 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
519
520 std::string actual;
521 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
522 std::string expected = android::base::StringPrintf("C|%d|fake_name|12345", getpid());
523 ASSERT_STREQ(expected.c_str(), actual.c_str());
524 }
525
TEST_F(TraceDevTest,atrace_int_body_exact)526 TEST_F(TraceDevTest, atrace_int_body_exact) {
527 std::string expected = android::base::StringPrintf("C|%d|", getpid());
528 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 7);
529 atrace_int_body(name.c_str(), 12345);
530
531 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
532 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
533
534 std::string actual;
535 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
536 expected += name + "|12345";
537 ASSERT_STREQ(expected.c_str(), actual.c_str());
538
539 // Add a single character and verify we get the exact same value as before.
540 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
541 name += '*';
542 atrace_int_body(name.c_str(), 12345);
543 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
544 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
545 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
546 ASSERT_STREQ(expected.c_str(), actual.c_str());
547 }
548
TEST_F(TraceDevTest,atrace_int_body_truncated)549 TEST_F(TraceDevTest, atrace_int_body_truncated) {
550 std::string expected = android::base::StringPrintf("C|%d|", getpid());
551 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
552 atrace_int_body(name.c_str(), 12345);
553
554 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
555 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
556
557 std::string actual;
558 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
559 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 7;
560 expected += android::base::StringPrintf("%.*s|12345", expected_len, name.c_str());
561 ASSERT_STREQ(expected.c_str(), actual.c_str());
562 }
563
TEST_F(TraceDevTest,atrace_int64_body_normal)564 TEST_F(TraceDevTest, atrace_int64_body_normal) {
565 atrace_int64_body("fake_name", 17179869183L);
566
567 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
568
569 std::string actual;
570 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
571 std::string expected = android::base::StringPrintf("C|%d|fake_name|17179869183", getpid());
572 ASSERT_STREQ(expected.c_str(), actual.c_str());
573 }
574
TEST_F(TraceDevTest,atrace_int64_body_exact)575 TEST_F(TraceDevTest, atrace_int64_body_exact) {
576 std::string expected = android::base::StringPrintf("C|%d|", getpid());
577 std::string name = MakeName(ATRACE_MESSAGE_LENGTH - expected.length() - 13);
578 atrace_int64_body(name.c_str(), 17179869183L);
579
580 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
581 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
582
583 std::string actual;
584 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
585 expected += name + "|17179869183";
586 ASSERT_STREQ(expected.c_str(), actual.c_str());
587
588 // Add a single character and verify we get the exact same value as before.
589 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
590 name += '*';
591 atrace_int64_body(name.c_str(), 17179869183L);
592 EXPECT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
593 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
594 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
595 ASSERT_STREQ(expected.c_str(), actual.c_str());
596 }
597
TEST_F(TraceDevTest,atrace_int64_body_truncated)598 TEST_F(TraceDevTest, atrace_int64_body_truncated) {
599 std::string expected = android::base::StringPrintf("C|%d|", getpid());
600 std::string name = MakeName(2 * ATRACE_MESSAGE_LENGTH);
601 atrace_int64_body(name.c_str(), 17179869183L);
602
603 ASSERT_EQ(ATRACE_MESSAGE_LENGTH - 1, lseek(atrace_marker_fd, 0, SEEK_CUR));
604 ASSERT_EQ(0, lseek(atrace_marker_fd, 0, SEEK_SET));
605
606 std::string actual;
607 ASSERT_TRUE(android::base::ReadFdToString(atrace_marker_fd, &actual));
608 int expected_len = ATRACE_MESSAGE_LENGTH - expected.length() - 13;
609 expected += android::base::StringPrintf("%.*s|17179869183", expected_len, name.c_str());
610 ASSERT_STREQ(expected.c_str(), actual.c_str());
611 }
612