1 /*
2 * Copyright (c) 2025 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
16 #include <cstring>
17 #include <gtest/gtest.h>
18 #include "lperf_event_record.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS {
24 namespace HiviewDFX {
25 class LperfEventRecordTest : public testing::Test {
26 public:
27 static void SetUpTestCase();
28 static void TearDownTestCase();
29 void SetUp();
30 void TearDown();
31 };
32
SetUpTestCase()33 void LperfEventRecordTest::SetUpTestCase()
34 {}
35
TearDownTestCase()36 void LperfEventRecordTest::TearDownTestCase()
37 {}
38
SetUp()39 void LperfEventRecordTest::SetUp()
40 {}
41
TearDown()42 void LperfEventRecordTest::TearDown()
43 {}
44
45 struct TestRecordSample {
46 perf_event_header header_;
47 LperfRecordSampleData data_;
48 };
49
50 /**
51 * @tc.name: CreateLperfRecordTest001
52 * @tc.desc: test LperfEventRecord type PERF_RECORD_SAMPLE
53 * @tc.type: FUNC
54 */
55 HWTEST_F(LperfEventRecordTest, CreateLperfRecordTest001, TestSize.Level1)
56 {
57 GTEST_LOG_(INFO) << "CreateLperfRecordTest001: start.";
58 TestRecordSample sample = {
59 {PERF_RECORD_SAMPLE, PERF_RECORD_MISC_KERNEL, sizeof(TestRecordSample)},
60 {}};
61 uint64_t ips[4] = {0, 1, 2, 3};
62 sample.data_.ips = ips;
63 sample.data_.nr = 4;
64 sample.data_.pid = 2;
65 sample.data_.tid = 3;
66 sample.data_.time = 4;
67
68 LperfRecordSample record = LperfRecordFactory::GetLperfRecord(PERF_RECORD_SAMPLE, (uint8_t *)&sample);
69 EXPECT_EQ(record.GetType(), PERF_RECORD_SAMPLE);
70 EXPECT_EQ(record.GetName(), "sample");
71 EXPECT_EQ(record.data_.pid, 2);
72 EXPECT_EQ(record.data_.tid, 3);
73 EXPECT_EQ(record.data_.time, 4);
74 EXPECT_EQ(record.data_.nr, 4);
75 GTEST_LOG_(INFO) << "CreateLperfRecordTest001: end.";
76 }
77
78 /**
79 * @tc.name: CreateLperfRecordTest002
80 * @tc.desc: test LperfEventRecord invalid type
81 * @tc.type: FUNC
82 */
83 HWTEST_F(LperfEventRecordTest, CreateLperfRecordTest002, TestSize.Level2)
84 {
85 GTEST_LOG_(INFO) << "CreateLperfRecordTest002: start.";
86 TestRecordSample sample = {
87 {PERF_RECORD_MMAP, PERF_RECORD_MISC_KERNEL, sizeof(TestRecordSample)},
88 {}};
89 uint64_t ips[4] = {0, 1, 2, 3};
90 sample.data_.ips = ips;
91 sample.data_.nr = 4;
92 sample.data_.pid = 2;
93 sample.data_.tid = 3;
94 sample.data_.time = 4;
95 LperfRecordSample record = LperfRecordFactory::GetLperfRecord(PERF_RECORD_MMAP, (uint8_t *)&sample);
96 EXPECT_EQ(record.data_.pid, 0);
97 EXPECT_EQ(record.data_.tid, 0);
98 EXPECT_EQ(record.data_.time, 0);
99 EXPECT_EQ(record.data_.nr, 0);
100 EXPECT_EQ(record.data_.ips, nullptr);
101 GTEST_LOG_(INFO) << "CreateLperfRecordTest002: end.";
102 }
103
104 /**
105 * @tc.name: CreateLperfRecordTest003
106 * @tc.desc: test LperfEventRecord invalid data
107 * @tc.type: FUNC
108 */
109 HWTEST_F(LperfEventRecordTest, CreateLperfRecordTest003, TestSize.Level2)
110 {
111 GTEST_LOG_(INFO) << "CreateLperfRecordTest003: start.";
112 LperfRecordSample record = LperfRecordFactory::GetLperfRecord(PERF_RECORD_SAMPLE, nullptr);
113 EXPECT_EQ(record.GetType(), PERF_RECORD_MMAP);
114 EXPECT_EQ(record.data_.pid, 0);
115 EXPECT_EQ(record.data_.tid, 0);
116 EXPECT_EQ(record.data_.time, 0);
117 EXPECT_EQ(record.data_.nr, 0);
118 EXPECT_EQ(record.data_.ips, nullptr);
119 GTEST_LOG_(INFO) << "CreateLperfRecordTest003: end.";
120 }
121 } // namespace HiviewDFX
122 } // namespace OHOS
123