1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved.
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 <hwext/gtest-ext.h>
17 #include <hwext/gtest-tag.h>
18
19 #include "ebpf_data_parser.h"
20 #include "ebpf_stdtype.h"
21 #include "pbreader_file_header.h"
22 #include "process_filter.h"
23 #include "trace_streamer_selector.h"
24 #include "ts_common.h"
25
26 using namespace testing::ext;
27 using namespace SysTuning::TraceStreamer;
28 using namespace SysTuning::EbpfStdtype;
29 namespace SysTuning {
30 namespace TraceStreamer {
31 const std::string COMMAND_LINE = "hiebpf --events ptrace --duration 50";
32 const uint64_t EPBF_ERROR_MAGIC = 0x12345678;
33 const uint32_t EPBF_ERROR_HEAD_SIZE = 0;
34 class EbpfParserTest : public ::testing::Test {
35 public:
SetUp()36 void SetUp()
37 {
38 stream_.InitFilter();
39 }
40
TearDown()41 void TearDown() {}
42
43 public:
44 SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
45 };
46
47 /**
48 * @tc.name: EbpfDataOnlyEbpfHeadWithErrorMagic
49 * @tc.desc: Test parse Ebpf data with only ebpf head but no command line
50 * @tc.type: FUNC
51 */
HWTEST_F(EbpfParserTest, EbpfDataOnlyEbpfHeadWithErrorMagic, TestSize.Level1)52 HWTEST_F(EbpfParserTest, EbpfDataOnlyEbpfHeadWithErrorMagic, TestSize.Level1)
53 {
54 TS_LOGI("test29-1");
55 EbpfDataHeader ebpfHeader;
56 ebpfHeader.header.magic = EPBF_ERROR_MAGIC;
57 std::deque<uint8_t> dequeBuffer = {};
58 dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t *>(&ebpfHeader),
59 reinterpret_cast<uint8_t *>(&ebpfHeader + 1));
60 std::unique_ptr<EbpfDataParser> parser =
61 std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
62 EXPECT_FALSE(parser->Init(dequeBuffer, dequeBuffer.size()));
63 }
64
65 /**
66 * @tc.name: EbpfDataOnlyEbpfHeadWithErrorSize
67 * @tc.desc: Test parse Ebpf data with only ebpf head but no command line
68 * @tc.type: FUNC
69 */
HWTEST_F(EbpfParserTest, EbpfDataOnlyEbpfHeadWithErrorSize, TestSize.Level1)70 HWTEST_F(EbpfParserTest, EbpfDataOnlyEbpfHeadWithErrorSize, TestSize.Level1)
71 {
72 TS_LOGI("test29-2");
73 EbpfDataHeader ebpfHeader;
74 ebpfHeader.header.headSize = EPBF_ERROR_HEAD_SIZE;
75 std::deque<uint8_t> dequeBuffer = {};
76 dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t *>(&ebpfHeader),
77 reinterpret_cast<uint8_t *>(&ebpfHeader + 1));
78 std::unique_ptr<EbpfDataParser> parser =
79 std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
80 EXPECT_FALSE(parser->Init(dequeBuffer, dequeBuffer.size()));
81 }
82
83 /**
84 * @tc.name: EbpfDataEbpfHeadWithNormalData
85 * @tc.desc: Test parse Ebpf data with normal data
86 * @tc.type: FUNC
87 */
HWTEST_F(EbpfParserTest, EbpfDataEbpfHeadWithNormalData, TestSize.Level1)88 HWTEST_F(EbpfParserTest, EbpfDataEbpfHeadWithNormalData, TestSize.Level1)
89 {
90 TS_LOGI("test29-3");
91 EbpfDataHeader ebpfHeader;
92 ebpfHeader.header.clock = EBPF_CLOCK_BOOTTIME;
93 ebpfHeader.header.cmdLineLen = 0;
94 ebpfHeader.header.headSize = EbpfDataHeader::EBPF_DATA_HEADER_SIZE;
95
96 std::deque<uint8_t> dequeBuffer = {};
97 dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t *>(&ebpfHeader),
98 reinterpret_cast<uint8_t *>(&ebpfHeader + 1));
99 std::unique_ptr<EbpfDataParser> parser =
100 std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
101 EXPECT_TRUE(parser->Init(dequeBuffer, dequeBuffer.size()));
102 parser->Finish();
103 EXPECT_EQ(parser->reader_->ebpfDataHeader_->header.clock, EBPF_CLOCK_BOOTTIME);
104 EXPECT_EQ(parser->reader_->ebpfDataHeader_->header.cmdLineLen, 0);
105 EXPECT_EQ(parser->reader_->ebpfDataHeader_->header.headSize, EbpfDataHeader::EBPF_DATA_HEADER_SIZE);
106 }
107
108 /**
109 * @tc.name: EbpfDataWithOnlyEbpfHeadNoCommandLine
110 * @tc.desc: Test parse Ebpf data with only ebpf head but no command line
111 * @tc.type: FUNC
112 */
HWTEST_F(EbpfParserTest, EbpfDataWithOnlyEbpfHeadNoCommandLine, TestSize.Level1)113 HWTEST_F(EbpfParserTest, EbpfDataWithOnlyEbpfHeadNoCommandLine, TestSize.Level1)
114 {
115 TS_LOGI("test29-4");
116 EbpfDataHeader ebpfHeader;
117 ebpfHeader.header.clock = EBPF_CLOCK_BOOTTIME;
118 ebpfHeader.header.cmdLineLen = 0;
119
120 std::deque<uint8_t> dequeBuffer = {};
121 dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t *>(&ebpfHeader),
122 reinterpret_cast<uint8_t *>(&ebpfHeader + 1));
123 std::unique_ptr<EbpfDataParser> parser =
124 std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
125
126 EXPECT_TRUE(parser->Init(dequeBuffer, dequeBuffer.size()));
127 parser->Finish();
128 EXPECT_TRUE(parser->reader_->ebpfDataHeader_->header.clock == EBPF_CLOCK_BOOTTIME);
129 }
130
131 /**
132 * @tc.name: EbpfDataEbpfHeadHasProcessName
133 * @tc.desc: Test parse Ebpf data with only ebpf head
134 * @tc.type: FUNC
135 */
HWTEST_F(EbpfParserTest, EbpfDataEbpfHeadHasProcessName, TestSize.Level1)136 HWTEST_F(EbpfParserTest, EbpfDataEbpfHeadHasProcessName, TestSize.Level1)
137 {
138 TS_LOGI("test29-5");
139 EbpfDataHeader ebpfHeader;
140 ebpfHeader.header.clock = EBPF_CLOCK_BOOTTIME;
141 ebpfHeader.header.cmdLineLen = COMMAND_LINE.length();
142 strncpy_s(ebpfHeader.cmdline, EbpfDataHeader::EBPF_COMMAND_MAX_SIZE, COMMAND_LINE.c_str(),
143 EbpfDataHeader::EBPF_COMMAND_MAX_SIZE);
144
145 std::deque<uint8_t> dequeBuffer = {};
146 dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t *>(&ebpfHeader),
147 reinterpret_cast<uint8_t *>(&ebpfHeader + 1));
148 std::unique_ptr<EbpfDataParser> parser =
149 std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
150
151 EXPECT_TRUE(parser->Init(dequeBuffer, dequeBuffer.size()));
152 parser->Finish();
153 EXPECT_TRUE(parser->reader_->ebpfDataHeader_->header.clock == EBPF_CLOCK_BOOTTIME);
154 EXPECT_TRUE(parser->reader_->ebpfDataHeader_->header.cmdLineLen == COMMAND_LINE.length());
155 EXPECT_STREQ(parser->reader_->ebpfDataHeader_->cmdline, COMMAND_LINE.c_str());
156 }
157
158 } // namespace TraceStreamer
159 } // namespace SysTuning
160