1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include <chrono>
16 #include <iostream>
17
18 #include "accesstoken_kit.h"
19 #include "nativetoken_kit.h"
20 #include "token_setproc.h"
21 #include "trace_collector.h"
22 #include "parameter_ex.h"
23
24 #include <gtest/gtest.h>
25 #include <unistd.h>
26 #include <ctime>
27
28 using namespace testing::ext;
29 using namespace OHOS::HiviewDFX;
30 using namespace OHOS::HiviewDFX::UCollectClient;
31 using namespace OHOS::HiviewDFX::UCollect;
32
33 namespace {
34 constexpr int SLEEP_DURATION = 10;
35
NativeTokenGet(const char * perms[],int size)36 void NativeTokenGet(const char* perms[], int size)
37 {
38 uint64_t tokenId;
39 NativeTokenInfoParams infoInstance = {
40 .dcapsNum = 0,
41 .permsNum = size,
42 .aclsNum = 0,
43 .dcaps = nullptr,
44 .perms = perms,
45 .acls = nullptr,
46 .aplStr = "system_basic",
47 };
48
49 infoInstance.processName = "UCollectionClientUnitTest";
50 tokenId = GetAccessTokenId(&infoInstance);
51 SetSelfTokenID(tokenId);
52 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
53 }
54
EnablePermissionAccess()55 void EnablePermissionAccess()
56 {
57 const char* perms[] = {
58 "ohos.permission.WRITE_HIVIEW_SYSTEM",
59 "ohos.permission.READ_HIVIEW_SYSTEM",
60 };
61 NativeTokenGet(perms, 2); // 2 is the size of the array which consists of required permissions.
62 }
63
DisablePermissionAccess()64 void DisablePermissionAccess()
65 {
66 NativeTokenGet(nullptr, 0); // empty permission array.
67 }
68
Sleep()69 void Sleep()
70 {
71 sleep(SLEEP_DURATION);
72 }
73
GenerateUid()74 int GenerateUid()
75 {
76 struct tm *t;
77 time_t tt;
78 (void)time(&tt);
79 t = localtime(&tt);
80 int uid = t->tm_hour * 10000 + t->tm_min * 100 + t->tm_sec; //generate non-repetitive uid during one day
81 return uid;
82 }
83 }
84
85 class TraceCollectorTest : public testing::Test {
86 public:
SetUp()87 void SetUp() {};
TearDown()88 void TearDown() {};
SetUpTestCase()89 static void SetUpTestCase() {};
TearDownTestCase()90 static void TearDownTestCase() {};
91 };
92
93 /**
94 * @tc.name: TraceCollectorTest001
95 * @tc.desc: use trace in snapshot mode.
96 * @tc.type: FUNC
97 */
98 HWTEST_F(TraceCollectorTest, TraceCollectorTest001, TestSize.Level1)
99 {
100 auto traceCollector = TraceCollector::Create();
101 ASSERT_TRUE(traceCollector != nullptr);
102 EnablePermissionAccess();
103 traceCollector->Close();
104 const std::vector<std::string> tagGroups = {"scene_performance"};
105 auto openRet = traceCollector->OpenSnapshot(tagGroups);
106 if (openRet.retCode == UcError::SUCCESS) {
107 Sleep();
108 auto dumpRes = traceCollector->DumpSnapshot();
109 ASSERT_TRUE(dumpRes.data.size() >= 0);
110 auto closeRet = traceCollector->Close();
111 ASSERT_TRUE(closeRet.retCode == UcError::SUCCESS);
112 }
113 DisablePermissionAccess();
114 }
115
116 /**
117 * @tc.name: TraceCollectorTest002
118 * @tc.desc: use trace in recording mode.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(TraceCollectorTest, TraceCollectorTest002, TestSize.Level1)
122 {
123 auto traceCollector = TraceCollector::Create();
124 ASSERT_TRUE(traceCollector != nullptr);
125 EnablePermissionAccess();
126 traceCollector->Close();
127 std::string args = "tags:sched clockType:boot bufferSize:1024 overwrite:1 output:/data/log/test.sys";
128 auto openRet = traceCollector->OpenRecording(args);
129 if (openRet.retCode == UcError::SUCCESS) {
130 auto recOnRet = traceCollector->RecordingOn();
131 ASSERT_TRUE(recOnRet.retCode == UcError::SUCCESS);
132 Sleep();
133 auto recOffRet = traceCollector->RecordingOff();
134 ASSERT_TRUE(recOffRet.data.size() >= 0);
135 }
136 DisablePermissionAccess();
137 }
138
GetMilliseconds()139 static uint64_t GetMilliseconds()
140 {
141 auto now = std::chrono::system_clock::now();
142 auto millisecs = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
143 return millisecs.count();
144 }
145
146 /**
147 * @tc.name: TraceCollectorTest003
148 * @tc.desc: start app trace.
149 * @tc.type: FUNC
150 */
151 HWTEST_F(TraceCollectorTest, TraceCollectorTest003, TestSize.Level1)
152 {
153 auto traceCollector = TraceCollector::Create();
154 ASSERT_TRUE(traceCollector != nullptr);
155 EnablePermissionAccess();
156 AppCaller appCaller;
157 appCaller.actionId = ACTION_ID_START_TRACE;
158 appCaller.bundleName = "com.example.helloworld";
159 appCaller.bundleVersion = "2.0.1";
160 appCaller.foreground = 1;
161 appCaller.threadName = "mainThread";
162 appCaller.uid = 20020143; // 20020143: user uid
163 appCaller.pid = 100; // 100: pid
164 appCaller.happenTime = GetMilliseconds();
165 appCaller.beginTime = appCaller.happenTime - 100; // 100: ms
166 appCaller.endTime = appCaller.happenTime + 100; // 100: ms
167 auto result = traceCollector->CaptureDurationTrace(appCaller);
168 std::cout << "retCode=" << result.retCode << ", data=" << result.data << std::endl;
169 ASSERT_TRUE(result.data == 0);
170 DisablePermissionAccess();
171 sleep(10);
172 }
173
174 /**
175 * @tc.name: TraceCollectorTest004
176 * @tc.desc: stop app trace.
177 * @tc.type: FUNC
178 */
179 HWTEST_F(TraceCollectorTest, TraceCollectorTest004, TestSize.Level1)
180 {
181 auto traceCollector = TraceCollector::Create();
182 ASSERT_TRUE(traceCollector != nullptr);
183 EnablePermissionAccess();
184 AppCaller appCaller;
185 appCaller.actionId = ACTION_ID_DUMP_TRACE;
186 appCaller.bundleName = "com.example.helloworld";
187 appCaller.bundleVersion = "2.0.1";
188 appCaller.foreground = 1;
189 appCaller.threadName = "mainThread";
190 appCaller.uid = 20020143; // 20020143: user id
191 appCaller.pid = 100; // 100: pid
192 appCaller.happenTime = GetMilliseconds();
193 appCaller.beginTime = appCaller.happenTime - 100; // 100: ms
194 appCaller.endTime = appCaller.happenTime + 100; // 100: ms
195 auto result = traceCollector->CaptureDurationTrace(appCaller);
196 std::cout << "retCode=" << result.retCode << ", data=" << result.data << std::endl;
197 ASSERT_TRUE(result.data == 0);
198 DisablePermissionAccess();
199 sleep(10);
200 }
201
202 /**
203 * @tc.name: TraceCollectorTest005
204 * @tc.desc: App trace start and dump.
205 * @tc.type: FUNC
206 */
207 HWTEST_F(TraceCollectorTest, TraceCollectorTest005, TestSize.Level1)
208 {
209 auto traceCollector = TraceCollector::Create();
210 ASSERT_TRUE(traceCollector != nullptr);
211 bool isBetaVersion = Parameter::IsBetaVersion();
212 bool isDevelopMode = Parameter::IsDeveloperMode();
213 bool isUCollectionSwitchOn = Parameter::IsUCollectionSwitchOn();
214 bool isTraceCollectionSwitchOn = Parameter::IsTraceCollectionSwitchOn();
215 bool isTestAppTraceOn = Parameter::IsTestAppTraceOn();
216 if (!isBetaVersion && isDevelopMode && !isUCollectionSwitchOn && !isTraceCollectionSwitchOn && isTestAppTraceOn) {
217 int tempId = GenerateUid();
218 AppCaller appCaller1;
219 appCaller1.actionId = ACTION_ID_START_TRACE;
220 appCaller1.bundleName = "com.example.helloworld";
221 appCaller1.bundleVersion = "2.0.1";
222 appCaller1.foreground = 1;
223 appCaller1.threadName = "mainThread";
224 appCaller1.uid = tempId;
225 appCaller1.pid = tempId;
226 appCaller1.happenTime = GetMilliseconds();
227 appCaller1.beginTime = appCaller1.happenTime - 100;
228 appCaller1.endTime = appCaller1.happenTime + 100;
229 auto result1 = traceCollector->CaptureDurationTrace(appCaller1);
230 std::cout << "retCode=" << result1.retCode << ", data=" << result1.data << std::endl;
231 ASSERT_TRUE(result1.retCode == 0);
232 sleep(5);
233
234 AppCaller appCaller2;
235 appCaller2.actionId = ACTION_ID_DUMP_TRACE;
236 appCaller2.bundleName = "com.example.helloworld";
237 appCaller2.bundleVersion = "2.0.1";
238 appCaller2.foreground = 1;
239 appCaller2.threadName = "mainThread";
240 appCaller2.uid = tempId;
241 appCaller2.pid = tempId;
242 appCaller2.happenTime = GetMilliseconds();
243 appCaller2.beginTime = appCaller2.happenTime - 100;
244 appCaller2.endTime = appCaller2.happenTime + 100;
245 auto result2 = traceCollector->CaptureDurationTrace(appCaller2);
246 std::cout << "retCode=" << result2.retCode << ", data=" << result2.data << std::endl;
247 ASSERT_TRUE(result2.retCode == 0);
248 sleep(10);
249 }
250 }
251
252 /**
253 * @tc.name: TraceCollectorTest006
254 * @tc.desc: App trace dump without start.
255 * @tc.type: FUNC
256 */
257 HWTEST_F(TraceCollectorTest, TraceCollectorTest006, TestSize.Level1)
258 {
259 auto traceCollector = TraceCollector::Create();
260 ASSERT_TRUE(traceCollector != nullptr);
261 bool isBetaVersion = Parameter::IsBetaVersion();
262 bool isDevelopMode = Parameter::IsDeveloperMode();
263 bool isUCollectionSwitchOn = Parameter::IsUCollectionSwitchOn();
264 bool isTraceCollectionSwitchOn = Parameter::IsTraceCollectionSwitchOn();
265 bool isTestAppTraceOn = Parameter::IsTestAppTraceOn();
266 if (!isBetaVersion && isDevelopMode && !isUCollectionSwitchOn && !isTraceCollectionSwitchOn && isTestAppTraceOn) {
267 int tempId = GenerateUid();
268 AppCaller appCaller1;
269 appCaller1.actionId = ACTION_ID_DUMP_TRACE;
270 appCaller1.bundleName = "com.example.helloworld";
271 appCaller1.bundleVersion = "2.0.1";
272 appCaller1.foreground = 1;
273 appCaller1.threadName = "mainThread";
274 appCaller1.uid = tempId;
275 appCaller1.pid = tempId;
276 appCaller1.happenTime = GetMilliseconds();
277 appCaller1.beginTime = appCaller1.happenTime - 100;
278 appCaller1.endTime = appCaller1.happenTime + 100;
279 auto result1 = traceCollector->CaptureDurationTrace(appCaller1);
280 std::cout << "retCode=" << result1.retCode << ", data=" << result1.data << std::endl;
281 ASSERT_FALSE(result1.retCode == 0);
282 sleep(10);
283 }
284 }
285
286 /**
287 * @tc.name: TraceCollectorTest007
288 * @tc.desc: App trace dump twice without start.
289 * @tc.type: FUNC
290 */
291 HWTEST_F(TraceCollectorTest, TraceCollectorTest007, TestSize.Level1)
292 {
293 auto traceCollector = TraceCollector::Create();
294 ASSERT_TRUE(traceCollector != nullptr);
295 bool isBetaVersion = Parameter::IsBetaVersion();
296 bool isDevelopMode = Parameter::IsDeveloperMode();
297 bool isUCollectionSwitchOn = Parameter::IsUCollectionSwitchOn();
298 bool isTraceCollectionSwitchOn = Parameter::IsTraceCollectionSwitchOn();
299 bool isTestAppTraceOn = Parameter::IsTestAppTraceOn();
300 if (!isBetaVersion && isDevelopMode && !isUCollectionSwitchOn && !isTraceCollectionSwitchOn && isTestAppTraceOn) {
301 int tempId = GenerateUid();
302 AppCaller appCaller1;
303 appCaller1.actionId = ACTION_ID_DUMP_TRACE;
304 appCaller1.bundleName = "com.example.helloworld";
305 appCaller1.bundleVersion = "2.0.1";
306 appCaller1.foreground = 1;
307 appCaller1.threadName = "mainThread";
308 appCaller1.uid = tempId;
309 appCaller1.pid = tempId;
310 appCaller1.happenTime = GetMilliseconds();
311 appCaller1.beginTime = appCaller1.happenTime - 100;
312 appCaller1.endTime = appCaller1.happenTime + 100;
313 auto result1 = traceCollector->CaptureDurationTrace(appCaller1);
314 std::cout << "retCode=" << result1.retCode << ", data=" << result1.data << std::endl;
315 ASSERT_FALSE(result1.retCode == 0);
316 sleep(5);
317
318 AppCaller appCaller2;
319 appCaller2.actionId = ACTION_ID_DUMP_TRACE;
320 appCaller2.bundleName = "com.example.helloworld";
321 appCaller2.bundleVersion = "2.0.1";
322 appCaller2.foreground = 1;
323 appCaller2.threadName = "mainThread";
324 appCaller2.uid = tempId;
325 appCaller2.pid = tempId;
326 appCaller2.happenTime = GetMilliseconds();
327 appCaller2.beginTime = appCaller2.happenTime - 100;
328 appCaller2.endTime = appCaller2.happenTime + 100;
329 auto result2 = traceCollector->CaptureDurationTrace(appCaller2);
330 std::cout << "retCode=" << result2.retCode << ", data=" << result2.data << std::endl;
331 ASSERT_FALSE(result2.retCode == 0);
332 sleep(10);
333 }
334 }
335
336 /**
337 * @tc.name: TraceCollectorTest008
338 * @tc.desc: App trace start then start.
339 * @tc.type: FUNC
340 */
341 HWTEST_F(TraceCollectorTest, TraceCollectorTest008, TestSize.Level1)
342 {
343 auto traceCollector = TraceCollector::Create();
344 ASSERT_TRUE(traceCollector != nullptr);
345 bool isBetaVersion = Parameter::IsBetaVersion();
346 bool isDevelopMode = Parameter::IsDeveloperMode();
347 bool isUCollectionSwitchOn = Parameter::IsUCollectionSwitchOn();
348 bool isTraceCollectionSwitchOn = Parameter::IsTraceCollectionSwitchOn();
349 bool isTestAppTraceOn = Parameter::IsTestAppTraceOn();
350 if (!isBetaVersion && isDevelopMode && !isUCollectionSwitchOn && !isTraceCollectionSwitchOn && isTestAppTraceOn) {
351 int tempId = GenerateUid();
352 AppCaller appCaller1;
353 appCaller1.actionId = ACTION_ID_START_TRACE;
354 appCaller1.bundleName = "com.example.helloworld";
355 appCaller1.bundleVersion = "2.0.1";
356 appCaller1.foreground = 1;
357 appCaller1.threadName = "mainThread";
358 appCaller1.uid = tempId;
359 appCaller1.pid = tempId;
360 appCaller1.happenTime = GetMilliseconds();
361 appCaller1.beginTime = appCaller1.happenTime - 100;
362 appCaller1.endTime = appCaller1.happenTime + 100;
363 auto result1 = traceCollector->CaptureDurationTrace(appCaller1);
364 std::cout << "retCode=" << result1.retCode << ", data=" << result1.data << std::endl;
365 ASSERT_TRUE(result1.retCode == 0);
366 sleep(5);
367
368 AppCaller appCaller2 = appCaller1;
369 auto result2 = traceCollector->CaptureDurationTrace(appCaller2);
370 std::cout << "retCode=" << result2.retCode << ", data=" << result2.data << std::endl;
371 ASSERT_FALSE(result2.retCode == 0);
372 sleep(5);
373
374 AppCaller appCaller3;
375 appCaller3.actionId = ACTION_ID_DUMP_TRACE;
376 appCaller3.bundleName = "com.example.helloworld";
377 appCaller3.bundleVersion = "2.0.1";
378 appCaller3.foreground = 1;
379 appCaller3.threadName = "mainThread";
380 appCaller3.uid = tempId;
381 appCaller3.pid = tempId;
382 appCaller3.happenTime = GetMilliseconds();
383 appCaller3.beginTime = appCaller3.happenTime - 100;
384 appCaller3.endTime = appCaller3.happenTime + 100;
385 auto result3 = traceCollector->CaptureDurationTrace(appCaller3);
386 std::cout << "retCode=" << result3.retCode << ", data=" << result3.data << std::endl;
387 sleep(10);
388 }
389 }
390
391 /**
392 * @tc.name: TraceCollectorTest009
393 * @tc.desc: App trace start and dump by two users.
394 * @tc.type: FUNC
395 */
396 HWTEST_F(TraceCollectorTest, TraceCollectorTest009, TestSize.Level1)
397 {
398 auto traceCollector = TraceCollector::Create();
399 ASSERT_TRUE(traceCollector != nullptr);
400 bool isBetaVersion = Parameter::IsBetaVersion();
401 bool isDevelopMode = Parameter::IsDeveloperMode();
402 bool isUCollectionSwitchOn = Parameter::IsUCollectionSwitchOn();
403 bool isTraceCollectionSwitchOn = Parameter::IsTraceCollectionSwitchOn();
404 bool isTestAppTraceOn = Parameter::IsTestAppTraceOn();
405 if (!isBetaVersion && isDevelopMode && !isUCollectionSwitchOn && !isTraceCollectionSwitchOn && isTestAppTraceOn) {
406 int tempId1 = GenerateUid();
407 AppCaller appCaller1;
408 appCaller1.actionId = ACTION_ID_START_TRACE;
409 appCaller1.bundleName = "com.example.helloworld";
410 appCaller1.bundleVersion = "2.0.1";
411 appCaller1.foreground = 1;
412 appCaller1.threadName = "mainThread";
413 appCaller1.uid = tempId1;
414 appCaller1.pid = tempId1;
415 appCaller1.happenTime = GetMilliseconds();
416 appCaller1.beginTime = appCaller1.happenTime - 100;
417 appCaller1.endTime = appCaller1.happenTime + 100;
418 auto result1 = traceCollector->CaptureDurationTrace(appCaller1);
419 std::cout << "retCode=" << result1.retCode << ", data=" << result1.data << std::endl;
420 ASSERT_TRUE(result1.retCode == 0);
421 sleep(5);
422
423 AppCaller appCaller2 = appCaller1;
424 appCaller2.actionId = ACTION_ID_DUMP_TRACE;
425 auto result2 = traceCollector->CaptureDurationTrace(appCaller2);
426 std::cout << "retCode=" << result2.retCode << ", data=" << result2.data << std::endl;
427 ASSERT_TRUE(result2.retCode == 0);
428 sleep(10);
429
430 int tempId2 = GenerateUid();
431 AppCaller appCaller3 = appCaller1;
432 appCaller3.uid = tempId2;
433 appCaller3.pid = tempId2;
434 auto result3 = traceCollector->CaptureDurationTrace(appCaller3);
435 std::cout << "retCode=" << result3.retCode << ", data=" << result3.data << std::endl;
436 ASSERT_TRUE(result3.retCode == 0);
437 sleep(5);
438
439 AppCaller appCaller4 = appCaller3;
440 appCaller4.actionId = ACTION_ID_DUMP_TRACE;
441 auto result4 = traceCollector->CaptureDurationTrace(appCaller4);
442 std::cout << "retCode=" << result4.retCode << ", data=" << result4.data << std::endl;
443 ASSERT_TRUE(result4.retCode == 0);
444 sleep(10);
445 }
446 }
447
448 /**
449 * @tc.name: TraceCollectorTest010
450 * @tc.desc: App trace dump with different pid.
451 * @tc.type: FUNC
452 */
453 HWTEST_F(TraceCollectorTest, TraceCollectorTest010, TestSize.Level1)
454 {
455 auto traceCollector = TraceCollector::Create();
456 ASSERT_TRUE(traceCollector != nullptr);
457 bool isBetaVersion = Parameter::IsBetaVersion();
458 bool isDevelopMode = Parameter::IsDeveloperMode();
459 bool isUCollectionSwitchOn = Parameter::IsUCollectionSwitchOn();
460 bool isTraceCollectionSwitchOn = Parameter::IsTraceCollectionSwitchOn();
461 bool isTestAppTraceOn = Parameter::IsTestAppTraceOn();
462 if (!isBetaVersion && isDevelopMode && !isUCollectionSwitchOn && !isTraceCollectionSwitchOn && isTestAppTraceOn) {
463 int tempId = GenerateUid();
464 AppCaller appCaller1;
465 appCaller1.actionId = ACTION_ID_START_TRACE;
466 appCaller1.bundleName = "com.example.helloworld";
467 appCaller1.bundleVersion = "2.0.1";
468 appCaller1.foreground = 1;
469 appCaller1.threadName = "mainThread";
470 appCaller1.uid = tempId;
471 appCaller1.pid = tempId;
472 appCaller1.happenTime = GetMilliseconds();
473 appCaller1.beginTime = appCaller1.happenTime - 100;
474 appCaller1.endTime = appCaller1.happenTime + 100;
475 auto result1 = traceCollector->CaptureDurationTrace(appCaller1);
476 std::cout << "retCode=" << result1.retCode << ", data=" << result1.data << std::endl;
477 ASSERT_TRUE(result1.retCode == 0);
478 sleep(5);
479
480 AppCaller appCaller2;
481 appCaller2.actionId = ACTION_ID_DUMP_TRACE;
482 appCaller2.bundleName = "com.example.helloworld";
483 appCaller2.bundleVersion = "2.0.1";
484 appCaller2.foreground = 1;
485 appCaller2.threadName = "mainThread";
486 appCaller2.uid = tempId;
487 appCaller2.pid = tempId+1;
488 appCaller2.happenTime = GetMilliseconds();
489 appCaller2.beginTime = appCaller2.happenTime - 100;
490 appCaller2.endTime = appCaller2.happenTime + 100;
491 auto result2 = traceCollector->CaptureDurationTrace(appCaller2);
492 std::cout << "retCode=" << result2.retCode << ", data=" << result2.data << std::endl;
493 ASSERT_FALSE(result2.retCode == 0);
494 sleep(10);
495
496 AppCaller appCaller3 = appCaller2;
497 appCaller3.pid = tempId;
498 auto result3 = traceCollector->CaptureDurationTrace(appCaller3);
499 std::cout << "retCode=" << result3.retCode << ", data=" << result3.data << std::endl;
500 sleep(10);
501 }
502 }
503
504 /**
505 * @tc.name: TraceCollectorTest011
506 * @tc.desc: App trace start and dump twice by same user.
507 * @tc.type: FUNC
508 */
509 HWTEST_F(TraceCollectorTest, TraceCollectorTest011, TestSize.Level1)
510 {
511 auto traceCollector = TraceCollector::Create();
512 ASSERT_TRUE(traceCollector != nullptr);
513 bool isBetaVersion = Parameter::IsBetaVersion();
514 bool isDevelopMode = Parameter::IsDeveloperMode();
515 bool isUCollectionSwitchOn = Parameter::IsUCollectionSwitchOn();
516 bool isTraceCollectionSwitchOn = Parameter::IsTraceCollectionSwitchOn();
517 bool isTestAppTraceOn = Parameter::IsTestAppTraceOn();
518 if (!isBetaVersion && isDevelopMode && !isUCollectionSwitchOn && !isTraceCollectionSwitchOn && isTestAppTraceOn) {
519 int tempId = GenerateUid();
520 AppCaller appCaller1;
521 appCaller1.actionId = ACTION_ID_START_TRACE;
522 appCaller1.bundleName = "com.example.helloworld";
523 appCaller1.bundleVersion = "2.0.1";
524 appCaller1.foreground = 1;
525 appCaller1.threadName = "mainThread";
526 appCaller1.uid = tempId;
527 appCaller1.pid = tempId;
528 appCaller1.happenTime = GetMilliseconds();
529 appCaller1.beginTime = appCaller1.happenTime - 100;
530 appCaller1.endTime = appCaller1.happenTime + 100;
531 auto result1 = traceCollector->CaptureDurationTrace(appCaller1);
532 std::cout << "retCode=" << result1.retCode << ", data=" << result1.data << std::endl;
533 ASSERT_TRUE(result1.retCode == 0);
534 sleep(5);
535
536 AppCaller appCaller2;
537 appCaller2.actionId = ACTION_ID_DUMP_TRACE;
538 appCaller2.bundleName = "com.example.helloworld";
539 appCaller2.bundleVersion = "2.0.1";
540 appCaller2.foreground = 1;
541 appCaller2.threadName = "mainThread";
542 appCaller2.uid = tempId;
543 appCaller2.pid = tempId;
544 appCaller2.happenTime = GetMilliseconds();
545 appCaller2.beginTime = appCaller2.happenTime - 100;
546 appCaller2.endTime = appCaller2.happenTime + 100;
547 auto result2 = traceCollector->CaptureDurationTrace(appCaller2);
548 std::cout << "retCode=" << result2.retCode << ", data=" << result2.data << std::endl;
549 ASSERT_TRUE(result2.retCode == 0);
550 sleep(10);
551
552 AppCaller appCaller3 = appCaller1;
553 auto result3 = traceCollector->CaptureDurationTrace(appCaller3);
554 std::cout << "retCode=" << result3.retCode << ", data=" << result3.data << std::endl;
555 ASSERT_FALSE(result3.retCode == 0);
556 sleep(10);
557 }
558 }