1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2022. 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 <cstdio> 17 18 #include <gtest/gtest.h> 19 20 #include "hiebpf_data_file.h" 21 22 using namespace testing::ext; 23 using namespace std; 24 namespace { 25 const std::string FILE_NAME = "/data/local/tmp/hiebpf.txt"; 26 } // namespace 27 28 namespace OHOS { 29 namespace Developtools { 30 namespace Hiebpf { 31 class HiebpfDataFileTest : public ::testing::Test { 32 public: SetUpTestCase()33 static void SetUpTestCase() {}; TearDownTestCase()34 static void TearDownTestCase() 35 { 36 if (access(FILE_NAME.c_str(), F_OK) == 0) { 37 std::string cmd = "rm " + FILE_NAME; 38 system(cmd.c_str()); 39 } 40 } 41 SetUp()42 void SetUp() {} TearDown()43 void TearDown() {} 44 }; 45 46 /** 47 * @tc.name: MakeShared 48 * @tc.desc: Test HiebpfDataFile MakeShared 49 * @tc.type: FUNC 50 */ 51 HWTEST_F(HiebpfDataFileTest, MakeShared, TestSize.Level1) 52 { 53 auto hiebpfDataFile = HiebpfDataFile::MakeShared("", ""); 54 EXPECT_TRUE(hiebpfDataFile == nullptr); 55 56 hiebpfDataFile = HiebpfDataFile::MakeShared("hiebpf", FILE_NAME); 57 EXPECT_TRUE(hiebpfDataFile != nullptr); 58 } 59 60 /** 61 * @tc.name: MapFile 62 * @tc.desc: Test HiebpfDataFile MapFile 63 * @tc.type: FUNC 64 */ 65 HWTEST_F(HiebpfDataFileTest, MapFile, TestSize.Level1) 66 { 67 HiebpfDataFile hiebpfDataFile("hiebpf", FILE_NAME); 68 auto ret = hiebpfDataFile.OpenFile(); 69 EXPECT_EQ(ret, 0); 70 ret = hiebpfDataFile.MapFile(); 71 EXPECT_EQ(ret, 0); 72 ret = hiebpfDataFile.RemapFile(1); 73 EXPECT_EQ(ret, 0); 74 75 close(hiebpfDataFile.fd_); 76 hiebpfDataFile.fd_ = -1; 77 ret = hiebpfDataFile.MapFile(); 78 EXPECT_EQ(ret, -1); 79 ret = hiebpfDataFile.RemapFile(1); 80 EXPECT_EQ(ret, -1); 81 } 82 83 /** 84 * @tc.name: WriteKernelSymbol 85 * @tc.desc: Test HiebpfDataFile WriteKernelSymbol 86 * @tc.type: FUNC 87 */ 88 HWTEST_F(HiebpfDataFileTest, WriteKernelSymbol, TestSize.Level1) 89 { 90 HiebpfDataFile hiebpfDataFile("hiebpf", FILE_NAME); 91 hiebpfDataFile.WriteKernelSymbol(); 92 EXPECT_TRUE(hiebpfDataFile.mapAddr_ == MAP_FAILED); 93 94 auto ret = hiebpfDataFile.OpenFile(); 95 EXPECT_EQ(ret, 0); 96 EXPECT_TRUE(hiebpfDataFile.mapAddr_ == MAP_FAILED); 97 ret = hiebpfDataFile.MapFile(); 98 EXPECT_EQ(ret, 0); 99 hiebpfDataFile.WriteKernelSymbol(); 100 EXPECT_TRUE(hiebpfDataFile.mapAddr_ != MAP_FAILED); 101 } 102 103 /** 104 * @tc.name: Discard 105 * @tc.desc: Test HiebpfDataFile Discard 106 * @tc.type: FUNC 107 */ 108 HWTEST_F(HiebpfDataFileTest, Discard, TestSize.Level1) 109 { 110 HiebpfDataFile hiebpfDataFile("hiebpf", FILE_NAME); 111 hiebpfDataFile.Discard(nullptr); 112 113 auto ret = hiebpfDataFile.OpenFile(); 114 EXPECT_EQ(ret, 0); 115 ret = hiebpfDataFile.MapFile(); 116 EXPECT_EQ(ret, 0); 117 void *dest = hiebpfDataFile.Reserve(1); 118 EXPECT_TRUE(dest != nullptr); 119 hiebpfDataFile.Discard(dest); 120 } 121 } // namespace Hiebpf 122 } // namespace Developtools 123 } // namespace OHOS 124