• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 <hwext/gtest-ext.h>
17 #include <hwext/gtest-tag.h>
18 
19 #include "ebpf_data_parser.h"
20 #include "ebpf_stdtype.h"
21 #include "process_filter.h"
22 #include "trace_streamer_selector.h"
23 #include "ts_common.h"
24 
25 using namespace testing::ext;
26 using namespace SysTuning::TraceStreamer;
27 using namespace SysTuning::EbpfStdtype;
28 namespace SysTuning {
29 namespace TraceStreamer {
30 class EbpfFileSystemTest : public ::testing::Test {
31 public:
SetUp()32     void SetUp()
33     {
34         stream_.InitFilter();
35     }
36 
TearDown()37     void TearDown() {}
38 
39 public:
40     SysTuning::TraceStreamer::TraceStreamerSelector stream_ = {};
41 };
42 
43 const uint32_t PID_01 = 32;
44 const uint32_t TID_01 = 12;
45 const uint32_t PID_02 = 33;
46 const uint32_t TID_02 = 13;
47 const uint64_t START_TIME_01 = 1725645867369;
48 const uint64_t END_TIME_01 = 1725645967369;
49 const uint64_t START_TIME_02 = 1725645867369;
50 const uint64_t END_TIME_02 = 1725645967369;
51 const int32_t RET_01 = 8;
52 const int32_t RET_02 = -1;
53 const uint16_t IPS_NUM_00 = 0;
54 const uint16_t IPS_NUM_01 = 1;
55 const uint16_t IPS_NUM_02 = 2;
56 const uint64_t ARGS_01[ARGS_MAX] = {101, 102, 103, 104};
57 const uint64_t ARGS_02[ARGS_MAX] = {201, 202, 203, 204};
58 const char PROCESS_NAME_01[MAX_PROCESS_NAME_SZIE] = "process01";
59 const char PROCESS_NAME_02[MAX_PROCESS_NAME_SZIE] = "process02";
60 const uint64_t IPS_01[IPS_NUM_01] = {0x100000000};
61 const uint64_t IPS_02[IPS_NUM_02] = {0x100000000, 0x100000001};
62 
63 /**
64  * @tc.name: ParseFileSystemWithTypeOpen
65  * @tc.desc: Test parse Ebpf data has one file system data with type open and no ips
66  * @tc.type: FUNC
67  */
68 HWTEST_F(EbpfFileSystemTest, ParseFileSystemWithTypeOpen, TestSize.Level1)
69 {
70     TS_LOGI("test30-1");
71 
72     EbpfDataHeader ebpfHeader;
73     ebpfHeader.header.clock = EBPF_CLOCK_BOOTTIME;
74 
75     std::deque<uint8_t> dequeBuffer = {};
76     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfHeader),
77                        reinterpret_cast<uint8_t*>(&ebpfHeader + 1));
78 
79     FsFixedHeader fsFixedHeader;
80     fsFixedHeader.pid = PID_01;
81     fsFixedHeader.tid = TID_01;
82     fsFixedHeader.startTime = START_TIME_01;
83     fsFixedHeader.endTime = END_TIME_01;
84     fsFixedHeader.ret = RET_01;
85     fsFixedHeader.nrUserIPs = IPS_NUM_00;
86     fsFixedHeader.type = SYS_OPENAT2;
87     for (auto i = 0; i < ARGS_MAX; i++) {
88         fsFixedHeader.args[i] = ARGS_01[i];
89     }
90     strncpy_s(fsFixedHeader.processName, MAX_PROCESS_NAME_SZIE, PROCESS_NAME_01, MAX_PROCESS_NAME_SZIE);
91 
92     EbpfTypeAndLength ebpfTypeAndLength;
93     ebpfTypeAndLength.length = sizeof(fsFixedHeader);
94     ebpfTypeAndLength.type = ITEM_EVENT_FS;
95 
96     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfTypeAndLength),
97                        reinterpret_cast<uint8_t*>(&ebpfTypeAndLength + 1));
98     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&fsFixedHeader),
99                        reinterpret_cast<uint8_t*>(&fsFixedHeader + 1));
100 
101     std::unique_ptr<EbpfDataParser> parser =
102         std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
103     EXPECT_TRUE(parser->Init(dequeBuffer, dequeBuffer.size()));
104     EXPECT_TRUE(parser->reader_->GetFileSystemEventMap().size());
105     parser->ParseFileSystemEvent();
106     parser->Finish();
107     EXPECT_TRUE(parser->reader_->ebpfDataHeader_->header.clock == EBPF_CLOCK_BOOTTIME);
108     auto callChainId = stream_.traceDataCache_->GetConstFileSystemSample().CallChainIds()[0];
109     EXPECT_EQ(callChainId, INVALID_UINT32);
110     auto type = stream_.traceDataCache_->GetConstFileSystemSample().Types()[0];
111     EXPECT_EQ(type, OPEN);
112     auto startTs = stream_.traceDataCache_->GetConstFileSystemSample().StartTs()[0];
113     EXPECT_EQ(startTs, START_TIME_01);
114     auto endTs = stream_.traceDataCache_->GetConstFileSystemSample().EndTs()[0];
115     EXPECT_EQ(endTs, END_TIME_01);
116     auto dur = stream_.traceDataCache_->GetConstFileSystemSample().Durs()[0];
117     EXPECT_EQ(dur, END_TIME_01 - START_TIME_01);
118     auto ExpectReturnValue = parser->ConvertToHexTextIndex(RET_01);
119     auto returnValue = stream_.traceDataCache_->GetConstFileSystemSample().ReturnValues()[0];
120     EXPECT_EQ(returnValue, ExpectReturnValue);
121     auto errorCode = stream_.traceDataCache_->GetConstFileSystemSample().ErrorCodes()[0];
122     EXPECT_EQ(errorCode, INVALID_UINT64);
123     auto fd = stream_.traceDataCache_->GetConstFileSystemSample().Fds()[0];
124     EXPECT_EQ(fd, RET_01);
125     auto fileId = stream_.traceDataCache_->GetConstFileSystemSample().FileIds()[0];
126     EXPECT_EQ(fileId, INVALID_UINT64);
127     auto size = stream_.traceDataCache_->GetConstFileSystemSample().Sizes()[0];
128     EXPECT_EQ(size, MAX_SIZE_T);
129     auto i = 0;
130     auto ExpectFirstArg = parser->ConvertToHexTextIndex(ARGS_01[i++]);
131     auto firstArg = stream_.traceDataCache_->GetConstFileSystemSample().FirstArguments()[0];
132     EXPECT_EQ(firstArg, ExpectFirstArg);
133     auto ExpectSecondArg = parser->ConvertToHexTextIndex(ARGS_01[i++]);
134     auto secondArg = stream_.traceDataCache_->GetConstFileSystemSample().SecondArguments()[0];
135     EXPECT_EQ(secondArg, ExpectSecondArg);
136     auto ExpectThirdArg = parser->ConvertToHexTextIndex(ARGS_01[i++]);
137     auto thirdArg = stream_.traceDataCache_->GetConstFileSystemSample().ThirdArguments()[0];
138     EXPECT_EQ(thirdArg, ExpectThirdArg);
139     auto ExpectFourthArg = parser->ConvertToHexTextIndex(ARGS_01[i]);
140     auto fourthArg = stream_.traceDataCache_->GetConstFileSystemSample().FourthArguments()[0];
141     EXPECT_EQ(fourthArg, ExpectFourthArg);
142 }
143 
144 /**
145  * @tc.name: ParseFileSystemWithTypeClose
146  * @tc.desc: Test parse Ebpf data has one file system data with type close and no ips and return value little to zero
147  * @tc.type: FUNC
148  */
149 HWTEST_F(EbpfFileSystemTest, ParseFileSystemWithTypeClose, TestSize.Level1)
150 {
151     TS_LOGI("test30-2");
152 
153     EbpfDataHeader ebpfHeader;
154     ebpfHeader.header.clock = EBPF_CLOCK_BOOTTIME;
155 
156     std::deque<uint8_t> dequeBuffer = {};
157     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfHeader),
158                        reinterpret_cast<uint8_t*>(&ebpfHeader + 1));
159 
160     FsFixedHeader fsFixedHeader;
161     fsFixedHeader.pid = PID_02;
162     fsFixedHeader.tid = TID_02;
163     fsFixedHeader.startTime = START_TIME_02;
164     fsFixedHeader.endTime = END_TIME_02;
165     fsFixedHeader.ret = RET_02;
166     fsFixedHeader.nrUserIPs = IPS_NUM_00;
167     fsFixedHeader.type = SYS_CLOSE;
168     for (auto i = 0; i < ARGS_MAX; i++) {
169         fsFixedHeader.args[i] = ARGS_02[i];
170     }
171     strncpy_s(fsFixedHeader.processName, MAX_PROCESS_NAME_SZIE, PROCESS_NAME_02, MAX_PROCESS_NAME_SZIE);
172 
173     EbpfTypeAndLength ebpfTypeAndLength;
174     ebpfTypeAndLength.length = sizeof(fsFixedHeader);
175     ebpfTypeAndLength.type = ITEM_EVENT_FS;
176 
177     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfTypeAndLength),
178                        reinterpret_cast<uint8_t*>(&ebpfTypeAndLength + 1));
179     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&fsFixedHeader),
180                        reinterpret_cast<uint8_t*>(&fsFixedHeader + 1));
181 
182     std::unique_ptr<EbpfDataParser> parser =
183         std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
184     EXPECT_TRUE(parser->Init(dequeBuffer, dequeBuffer.size()));
185     EXPECT_TRUE(parser->reader_->GetFileSystemEventMap().size());
186     parser->ParseFileSystemEvent();
187     parser->Finish();
188     EXPECT_TRUE(parser->reader_->ebpfDataHeader_->header.clock == EBPF_CLOCK_BOOTTIME);
189     auto callChainId = stream_.traceDataCache_->GetConstFileSystemSample().CallChainIds()[0];
190     EXPECT_EQ(callChainId, INVALID_UINT32);
191     auto type = stream_.traceDataCache_->GetConstFileSystemSample().Types()[0];
192     EXPECT_EQ(type, CLOSE);
193     auto startTs = stream_.traceDataCache_->GetConstFileSystemSample().StartTs()[0];
194     EXPECT_EQ(startTs, START_TIME_02);
195     auto endTs = stream_.traceDataCache_->GetConstFileSystemSample().EndTs()[0];
196     EXPECT_EQ(endTs, END_TIME_02);
197     auto dur = stream_.traceDataCache_->GetConstFileSystemSample().Durs()[0];
198     EXPECT_EQ(dur, END_TIME_02 - START_TIME_02);
199     auto ExpectReturnValue = parser->ConvertToHexTextIndex(0);
200     auto returnValue = stream_.traceDataCache_->GetConstFileSystemSample().ReturnValues()[0];
201     EXPECT_EQ(returnValue, ExpectReturnValue);
202     auto ExpectErrorValue = parser->ConvertToHexTextIndex(-RET_02);
203     auto errorCode = stream_.traceDataCache_->GetConstFileSystemSample().ErrorCodes()[0];
204     EXPECT_EQ(errorCode, ExpectErrorValue);
205     auto fd = stream_.traceDataCache_->GetConstFileSystemSample().Fds()[0];
206     EXPECT_EQ(fd, ARGS_02[1]);
207     auto fileId = stream_.traceDataCache_->GetConstFileSystemSample().FileIds()[0];
208     EXPECT_EQ(fileId, INVALID_UINT64);
209     auto size = stream_.traceDataCache_->GetConstFileSystemSample().Sizes()[0];
210     EXPECT_EQ(size, MAX_SIZE_T);
211     auto i = 0;
212     auto ExpectFirstArg = parser->ConvertToHexTextIndex(ARGS_02[i++]);
213     auto firstArg = stream_.traceDataCache_->GetConstFileSystemSample().FirstArguments()[0];
214     EXPECT_EQ(firstArg, ExpectFirstArg);
215     auto ExpectSecondArg = parser->ConvertToHexTextIndex(ARGS_02[i++]);
216     auto secondArg = stream_.traceDataCache_->GetConstFileSystemSample().SecondArguments()[0];
217     EXPECT_EQ(secondArg, ExpectSecondArg);
218     auto ExpectThirdArg = parser->ConvertToHexTextIndex(ARGS_02[i++]);
219     auto thirdArg = stream_.traceDataCache_->GetConstFileSystemSample().ThirdArguments()[0];
220     EXPECT_EQ(thirdArg, ExpectThirdArg);
221     auto ExpectFourthArg = parser->ConvertToHexTextIndex(ARGS_02[i]);
222     auto fourthArg = stream_.traceDataCache_->GetConstFileSystemSample().FourthArguments()[0];
223     EXPECT_EQ(fourthArg, ExpectFourthArg);
224 }
225 
226 /**
227  * @tc.name: ParseFileSystemWithTypeRead
228  * @tc.desc: Test parse Ebpf data has one file system data with type read and no ips
229  * @tc.type: FUNC
230  */
231 HWTEST_F(EbpfFileSystemTest, ParseFileSystemWithTypeRead, TestSize.Level1)
232 {
233     TS_LOGI("test30-3");
234 
235     EbpfDataHeader ebpfHeader;
236     ebpfHeader.header.clock = EBPF_CLOCK_BOOTTIME;
237 
238     std::deque<uint8_t> dequeBuffer = {};
239     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfHeader),
240                        reinterpret_cast<uint8_t*>(&ebpfHeader + 1));
241 
242     FsFixedHeader fsFixedHeader;
243     fsFixedHeader.pid = PID_01;
244     fsFixedHeader.tid = TID_01;
245     fsFixedHeader.startTime = START_TIME_01;
246     fsFixedHeader.endTime = END_TIME_01;
247     fsFixedHeader.ret = RET_01;
248     fsFixedHeader.nrUserIPs = IPS_NUM_00;
249     fsFixedHeader.type = SYS_READ;
250     for (auto i = 0; i < ARGS_MAX; i++) {
251         fsFixedHeader.args[i] = ARGS_01[i];
252     }
253     strncpy_s(fsFixedHeader.processName, MAX_PROCESS_NAME_SZIE, PROCESS_NAME_01, MAX_PROCESS_NAME_SZIE);
254 
255     EbpfTypeAndLength ebpfTypeAndLength;
256     ebpfTypeAndLength.length = sizeof(fsFixedHeader);
257     ebpfTypeAndLength.type = ITEM_EVENT_FS;
258 
259     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfTypeAndLength),
260                        reinterpret_cast<uint8_t*>(&ebpfTypeAndLength + 1));
261     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&fsFixedHeader),
262                        reinterpret_cast<uint8_t*>(&fsFixedHeader + 1));
263 
264     std::unique_ptr<EbpfDataParser> parser =
265         std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
266     EXPECT_TRUE(parser->Init(dequeBuffer, dequeBuffer.size()));
267     EXPECT_TRUE(parser->reader_->GetFileSystemEventMap().size());
268     parser->ParseFileSystemEvent();
269     parser->Finish();
270     EXPECT_TRUE(parser->reader_->ebpfDataHeader_->header.clock == EBPF_CLOCK_BOOTTIME);
271     auto callChainId = stream_.traceDataCache_->GetConstFileSystemSample().CallChainIds()[0];
272     EXPECT_EQ(callChainId, INVALID_UINT32);
273     auto type = stream_.traceDataCache_->GetConstFileSystemSample().Types()[0];
274     EXPECT_EQ(type, READ);
275     auto startTs = stream_.traceDataCache_->GetConstFileSystemSample().StartTs()[0];
276     EXPECT_EQ(startTs, START_TIME_01);
277     auto endTs = stream_.traceDataCache_->GetConstFileSystemSample().EndTs()[0];
278     EXPECT_EQ(endTs, END_TIME_01);
279     auto dur = stream_.traceDataCache_->GetConstFileSystemSample().Durs()[0];
280     EXPECT_EQ(dur, END_TIME_01 - START_TIME_01);
281     auto ExpectReturnValue = parser->ConvertToHexTextIndex(RET_01);
282     auto returnValue = stream_.traceDataCache_->GetConstFileSystemSample().ReturnValues()[0];
283     EXPECT_EQ(returnValue, ExpectReturnValue);
284     auto errorCode = stream_.traceDataCache_->GetConstFileSystemSample().ErrorCodes()[0];
285     EXPECT_EQ(errorCode, INVALID_UINT64);
286     auto fd = stream_.traceDataCache_->GetConstFileSystemSample().Fds()[0];
287     EXPECT_EQ(fd, ARGS_01[0]);
288     auto fileId = stream_.traceDataCache_->GetConstFileSystemSample().FileIds()[0];
289     EXPECT_EQ(fileId, INVALID_UINT64);
290     auto size = stream_.traceDataCache_->GetConstFileSystemSample().Sizes()[0];
291     EXPECT_EQ(size, RET_01);
292     auto i = 0;
293     auto ExpectFirstArg = parser->ConvertToHexTextIndex(ARGS_01[i++]);
294     auto firstArg = stream_.traceDataCache_->GetConstFileSystemSample().FirstArguments()[0];
295     EXPECT_EQ(firstArg, ExpectFirstArg);
296     auto ExpectSecondArg = parser->ConvertToHexTextIndex(ARGS_01[i++]);
297     auto secondArg = stream_.traceDataCache_->GetConstFileSystemSample().SecondArguments()[0];
298     EXPECT_EQ(secondArg, ExpectSecondArg);
299     auto ExpectThirdArg = parser->ConvertToHexTextIndex(ARGS_01[i++]);
300     auto thirdArg = stream_.traceDataCache_->GetConstFileSystemSample().ThirdArguments()[0];
301     EXPECT_EQ(thirdArg, ExpectThirdArg);
302     auto ExpectFourthArg = parser->ConvertToHexTextIndex(ARGS_01[i]);
303     auto fourthArg = stream_.traceDataCache_->GetConstFileSystemSample().FourthArguments()[0];
304     EXPECT_EQ(fourthArg, ExpectFourthArg);
305 }
306 
307 /**
308  * @tc.name: ParseFileSystemWithTypeWrite
309  * @tc.desc: Test parse Ebpf data has one file system data with type read and no ips
310  * @tc.type: FUNC
311  */
312 HWTEST_F(EbpfFileSystemTest, ParseFileSystemWithTypeWrite, TestSize.Level1)
313 {
314     TS_LOGI("test30-4");
315 
316     EbpfDataHeader ebpfHeader;
317     ebpfHeader.header.clock = EBPF_CLOCK_BOOTTIME;
318 
319     std::deque<uint8_t> dequeBuffer = {};
320     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfHeader),
321                        reinterpret_cast<uint8_t*>(&ebpfHeader + 1));
322 
323     FsFixedHeader fsFixedHeader;
324     fsFixedHeader.pid = PID_02;
325     fsFixedHeader.tid = TID_02;
326     fsFixedHeader.startTime = START_TIME_02;
327     fsFixedHeader.endTime = END_TIME_02;
328     fsFixedHeader.ret = RET_02;
329     fsFixedHeader.nrUserIPs = IPS_NUM_00;
330     fsFixedHeader.type = SYS_WRITE;
331     for (auto i = 0; i < ARGS_MAX; i++) {
332         fsFixedHeader.args[i] = ARGS_02[i];
333     }
334     strncpy_s(fsFixedHeader.processName, MAX_PROCESS_NAME_SZIE, PROCESS_NAME_02, MAX_PROCESS_NAME_SZIE);
335 
336     EbpfTypeAndLength ebpfTypeAndLength;
337     ebpfTypeAndLength.length = sizeof(fsFixedHeader);
338     ebpfTypeAndLength.type = ITEM_EVENT_FS;
339 
340     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfTypeAndLength),
341                        reinterpret_cast<uint8_t*>(&ebpfTypeAndLength + 1));
342     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&fsFixedHeader),
343                        reinterpret_cast<uint8_t*>(&fsFixedHeader + 1));
344 
345     std::unique_ptr<EbpfDataParser> parser =
346         std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
347     EXPECT_TRUE(parser->Init(dequeBuffer, dequeBuffer.size()));
348     EXPECT_TRUE(parser->reader_->GetFileSystemEventMap().size());
349     parser->ParseFileSystemEvent();
350     parser->Finish();
351     EXPECT_TRUE(parser->reader_->ebpfDataHeader_->header.clock == EBPF_CLOCK_BOOTTIME);
352     auto callChainId = stream_.traceDataCache_->GetConstFileSystemSample().CallChainIds()[0];
353     EXPECT_EQ(callChainId, INVALID_UINT32);
354     auto type = stream_.traceDataCache_->GetConstFileSystemSample().Types()[0];
355     EXPECT_EQ(type, WRITE);
356     auto startTs = stream_.traceDataCache_->GetConstFileSystemSample().StartTs()[0];
357     EXPECT_EQ(startTs, START_TIME_02);
358     auto endTs = stream_.traceDataCache_->GetConstFileSystemSample().EndTs()[0];
359     EXPECT_EQ(endTs, END_TIME_02);
360     auto dur = stream_.traceDataCache_->GetConstFileSystemSample().Durs()[0];
361     EXPECT_EQ(dur, END_TIME_02 - START_TIME_02);
362     auto ExpectReturnValue = parser->ConvertToHexTextIndex(0);
363     auto returnValue = stream_.traceDataCache_->GetConstFileSystemSample().ReturnValues()[0];
364     EXPECT_EQ(returnValue, ExpectReturnValue);
365     auto errorCode = stream_.traceDataCache_->GetConstFileSystemSample().ErrorCodes()[0];
366     auto ExpectErrorValue = parser->ConvertToHexTextIndex(-RET_02);
367     EXPECT_EQ(errorCode, ExpectErrorValue);
368     auto fd = stream_.traceDataCache_->GetConstFileSystemSample().Fds()[0];
369     EXPECT_EQ(fd, ARGS_02[0]);
370     auto fileId = stream_.traceDataCache_->GetConstFileSystemSample().FileIds()[0];
371     EXPECT_EQ(fileId, INVALID_UINT64);
372     auto size = stream_.traceDataCache_->GetConstFileSystemSample().Sizes()[0];
373     EXPECT_EQ(size, MAX_SIZE_T);
374     auto i = 0;
375     auto ExpectFirstArg = parser->ConvertToHexTextIndex(ARGS_02[i++]);
376     auto firstArg = stream_.traceDataCache_->GetConstFileSystemSample().FirstArguments()[0];
377     EXPECT_EQ(firstArg, ExpectFirstArg);
378     auto ExpectSecondArg = parser->ConvertToHexTextIndex(ARGS_02[i++]);
379     auto secondArg = stream_.traceDataCache_->GetConstFileSystemSample().SecondArguments()[0];
380     EXPECT_EQ(secondArg, ExpectSecondArg);
381     auto ExpectThirdArg = parser->ConvertToHexTextIndex(ARGS_02[i++]);
382     auto thirdArg = stream_.traceDataCache_->GetConstFileSystemSample().ThirdArguments()[0];
383     EXPECT_EQ(thirdArg, ExpectThirdArg);
384     auto ExpectFourthArg = parser->ConvertToHexTextIndex(ARGS_02[i]);
385     auto fourthArg = stream_.traceDataCache_->GetConstFileSystemSample().FourthArguments()[0];
386     EXPECT_EQ(fourthArg, ExpectFourthArg);
387 }
388 
389 /**
390  * @tc.name: ParseFileSystemWithErrorType
391  * @tc.desc: Test parse Ebpf data has one file system data with error type and no ips
392  * @tc.type: FUNC
393  */
394 HWTEST_F(EbpfFileSystemTest, ParseFileSystemWithErrorType, TestSize.Level1)
395 {
396     TS_LOGI("test30-5");
397 
398     EbpfDataHeader ebpfHeader;
399     ebpfHeader.header.clock = EBPF_CLOCK_BOOTTIME;
400 
401     std::deque<uint8_t> dequeBuffer = {};
402     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfHeader),
403                        reinterpret_cast<uint8_t*>(&ebpfHeader + 1));
404 
405     FsFixedHeader fsFixedHeader;
406     fsFixedHeader.pid = PID_01;
407     fsFixedHeader.tid = TID_01;
408     fsFixedHeader.startTime = START_TIME_01;
409     fsFixedHeader.endTime = END_TIME_01;
410     fsFixedHeader.ret = RET_01;
411     fsFixedHeader.nrUserIPs = IPS_NUM_00;
412     fsFixedHeader.type = 0;
413     for (auto i = 0; i < ARGS_MAX; i++) {
414         fsFixedHeader.args[i] = ARGS_01[i];
415     }
416     strncpy_s(fsFixedHeader.processName, MAX_PROCESS_NAME_SZIE, PROCESS_NAME_01, MAX_PROCESS_NAME_SZIE);
417 
418     EbpfTypeAndLength ebpfTypeAndLength;
419     ebpfTypeAndLength.length = sizeof(fsFixedHeader);
420     ebpfTypeAndLength.type = ITEM_EVENT_FS;
421 
422     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfTypeAndLength),
423                        reinterpret_cast<uint8_t*>(&ebpfTypeAndLength + 1));
424     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&fsFixedHeader),
425                        reinterpret_cast<uint8_t*>(&fsFixedHeader + 1));
426 
427     std::unique_ptr<EbpfDataParser> parser =
428         std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
429     EXPECT_TRUE(parser->Init(dequeBuffer, dequeBuffer.size()));
430     EXPECT_TRUE(parser->reader_->GetFileSystemEventMap().size());
431     parser->ParseFileSystemEvent();
432     parser->Finish();
433     EXPECT_TRUE(parser->reader_->ebpfDataHeader_->header.clock == EBPF_CLOCK_BOOTTIME);
434     EXPECT_FALSE(stream_.traceDataCache_->GetConstFileSystemSample().Size());
435 }
436 
437 /**
438  * @tc.name: ParseFileSystemWithIPsButNoSymTable
439  * @tc.desc: Test parse Ebpf data has one file system data with ips but no maps
440  * @tc.type: FUNC
441  */
442 HWTEST_F(EbpfFileSystemTest, ParseFileSystemWithIPsButNoMaps, TestSize.Level1)
443 {
444     TS_LOGI("test30-6");
445 
446     EbpfDataHeader ebpfHeader;
447     ebpfHeader.header.clock = EBPF_CLOCK_BOOTTIME;
448 
449     std::deque<uint8_t> dequeBuffer = {};
450     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfHeader),
451                        reinterpret_cast<uint8_t*>(&ebpfHeader + 1));
452 
453     FsFixedHeader fsFixedHeader;
454     fsFixedHeader.pid = PID_01;
455     fsFixedHeader.tid = TID_01;
456     fsFixedHeader.startTime = START_TIME_01;
457     fsFixedHeader.endTime = END_TIME_01;
458     fsFixedHeader.ret = RET_01;
459     fsFixedHeader.nrUserIPs = IPS_NUM_02;
460     fsFixedHeader.type = SYS_OPENAT2;
461     for (auto i = 0; i < ARGS_MAX; i++) {
462         fsFixedHeader.args[i] = ARGS_01[i];
463     }
464     strncpy_s(fsFixedHeader.processName, MAX_PROCESS_NAME_SZIE, PROCESS_NAME_01, MAX_PROCESS_NAME_SZIE);
465 
466     EbpfTypeAndLength ebpfTypeAndLength;
467     ebpfTypeAndLength.length = sizeof(fsFixedHeader) + IPS_NUM_02 * sizeof(uint64_t);
468     ebpfTypeAndLength.type = ITEM_EVENT_FS;
469 
470     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&ebpfTypeAndLength),
471                        reinterpret_cast<uint8_t*>(&ebpfTypeAndLength + 1));
472     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<uint8_t*>(&fsFixedHeader),
473                        reinterpret_cast<uint8_t*>(&fsFixedHeader + 1));
474     dequeBuffer.insert(dequeBuffer.end(), reinterpret_cast<const uint8_t*>(IPS_02),
475                        reinterpret_cast<const uint8_t*>(&IPS_02 + 1));
476 
477     std::unique_ptr<EbpfDataParser> parser =
478         std::make_unique<EbpfDataParser>(stream_.traceDataCache_.get(), stream_.streamFilters_.get());
479     EXPECT_TRUE(parser->Init(dequeBuffer, dequeBuffer.size()));
480     EXPECT_TRUE(parser->reader_->GetFileSystemEventMap().size());
481     parser->ParseFileSystemEvent();
482     parser->Finish();
483     EXPECT_TRUE(parser->reader_->ebpfDataHeader_->header.clock == EBPF_CLOCK_BOOTTIME);
484     auto callChainId = stream_.traceDataCache_->GetConstFileSystemSample().CallChainIds()[0];
485     EXPECT_EQ(callChainId, 0);
486     auto callStackFirstLevelCallChainId = stream_.traceDataCache_->GetConstEbpfCallStackData().CallChainIds()[0];
487     EXPECT_EQ(callStackFirstLevelCallChainId, 0);
488     auto callStackSecondLevelCallChainId = stream_.traceDataCache_->GetConstEbpfCallStackData().CallChainIds()[1];
489     EXPECT_EQ(callStackSecondLevelCallChainId, 0);
490     auto callStackFirstLevelDepth = stream_.traceDataCache_->GetConstEbpfCallStackData().Depths()[0];
491     EXPECT_EQ(callStackFirstLevelDepth, 0);
492     auto callStackSecondLevelDepth = stream_.traceDataCache_->GetConstEbpfCallStackData().Depths()[1];
493     EXPECT_EQ(callStackSecondLevelDepth, 1);
494     auto ExpectCallStackFirstLevelIp = parser->ConvertToHexTextIndex(IPS_02[1]);
495     auto callStackFirstLevelIp = stream_.traceDataCache_->GetConstEbpfCallStackData().Ips()[0];
496     EXPECT_EQ(callStackFirstLevelIp, ExpectCallStackFirstLevelIp);
497     auto ExpectCallStackSecondLevelIp = parser->ConvertToHexTextIndex(IPS_02[0]);
498     auto callStackSecondLevelIp = stream_.traceDataCache_->GetConstEbpfCallStackData().Ips()[1];
499     EXPECT_EQ(callStackSecondLevelIp, ExpectCallStackSecondLevelIp);
500     auto callStackFirstLevelSymbolId = stream_.traceDataCache_->GetConstEbpfCallStackData().SymbolIds()[0];
501     EXPECT_EQ(callStackFirstLevelSymbolId, INVALID_UINT64);
502     auto callStackSecondLevelSymbolId = stream_.traceDataCache_->GetConstEbpfCallStackData().SymbolIds()[1];
503     EXPECT_EQ(callStackSecondLevelSymbolId, INVALID_UINT64);
504     auto callStackFirstLevelFilePathIds = stream_.traceDataCache_->GetConstEbpfCallStackData().FilePathIds()[0];
505     EXPECT_EQ(callStackFirstLevelFilePathIds, INVALID_UINT64);
506     auto callStackSecondLevelFilePathIds = stream_.traceDataCache_->GetConstEbpfCallStackData().FilePathIds()[1];
507     EXPECT_EQ(callStackSecondLevelFilePathIds, INVALID_UINT64);
508 }
509 } // namespace TraceStreamer
510 } // namespace SysTuning
511