1 /* 2 * Copyright (c) 2022 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 <chrono> 17 #include <thread> 18 #include <hwext/gtest-ext.h> 19 #include <hwext/gtest-tag.h> 20 #include "common.h" 21 #include "logging.h" 22 23 namespace { 24 using namespace testing::ext; 25 using namespace COMMON; 26 27 class CommonTest : public testing::Test { 28 protected: SetUpTestCase()29 static void SetUpTestCase() {} TearDownTestCase()30 static void TearDownTestCase() {} 31 WriteFile(const std::string & filePath,const std::string & fileContent)32 void WriteFile(const std::string& filePath, const std::string& fileContent) 33 { 34 FILE* file = fopen(filePath.c_str(), "w"); 35 if (file == nullptr) { 36 std::string errorMsg = GetErrorMsg(); 37 HILOG_ERROR(LOG_CORE, "WriteFile: fopen() fail, %s, %s", filePath.c_str(), errorMsg.c_str()); 38 return; 39 } 40 41 size_t len = fwrite(const_cast<char*>(fileContent.c_str()), 1, fileContent.length(), file); 42 if (len < 0) { 43 std::string errorMsg = GetErrorMsg(); 44 HILOG_ERROR(LOG_CORE, "WriteFile: fwrite() fail, %s", errorMsg.c_str()); 45 (void)fclose(file); 46 return; 47 } 48 49 if (fflush(file) == EOF) { 50 std::string errorMsg = GetErrorMsg(); 51 HILOG_ERROR(LOG_CORE, "WriteFile: fflush() error = %s", errorMsg.c_str()); 52 (void)fclose(file); 53 return; 54 } 55 56 fsync(fileno(file)); 57 if (fclose(file) != 0) { 58 std::string errorMsg = GetErrorMsg(); 59 HILOG_ERROR(LOG_CORE, "CreateConfigFile: fclose() error = %s", errorMsg.c_str()); 60 return; 61 } 62 } 63 }; 64 65 /** 66 * @tc.name: CommonTest 67 * @tc.desc: IsProcessExist. 68 * @tc.type: FUNC 69 */ 70 HWTEST_F(CommonTest, IsProcessExist, TestSize.Level1) 71 { 72 std::string procName = "hiprofiler_base_ut"; 73 int pid = 0; 74 EXPECT_TRUE(COMMON::IsProcessExist(procName, pid)); 75 EXPECT_NE(pid, 0); 76 procName = "ls"; 77 pid = 0; 78 EXPECT_FALSE(COMMON::IsProcessExist(procName, pid)); 79 EXPECT_EQ(pid, 0); 80 } 81 82 /** 83 * @tc.name: CommonTest 84 * @tc.desc: StartProcess. 85 * @tc.type: FUNC 86 */ 87 HWTEST_F(CommonTest, StartAndKillProcess, TestSize.Level1) 88 { 89 constexpr int waitProcMills = 300; 90 std::string profilerProcName("hiprofilerd"); 91 std::vector<char*> argvVec; 92 argvVec.push_back(const_cast<char*>(profilerProcName.c_str())); 93 EXPECT_FALSE(COMMON::IsProcessRunning()); 94 int procPid = COMMON::StartProcess(profilerProcName, argvVec); 95 EXPECT_NE(procPid, 0); 96 std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); 97 EXPECT_NE(COMMON::KillProcess(procPid), -1); 98 } 99 100 /** 101 * @tc.name: CommonTest 102 * @tc.desc: VerifyPath. 103 * @tc.type: FUNC 104 */ 105 HWTEST_F(CommonTest, VerifyPath, TestSize.Level1) 106 { 107 std::string filePath = "/data/local/tmp/config.txt"; 108 std::vector<std::string> validPaths = {}; 109 EXPECT_TRUE(VerifyPath(filePath, validPaths)); 110 111 validPaths = { "/tmp/" }; 112 EXPECT_FALSE(VerifyPath(filePath, validPaths)); 113 114 validPaths = { "/tmp/", "/data/" }; 115 EXPECT_TRUE(VerifyPath(filePath, validPaths)); 116 117 validPaths = { "/tmp/", "/data/local/tmp/" }; 118 EXPECT_TRUE(VerifyPath(filePath, validPaths)); 119 120 filePath = "/data/local/tmpconfig.txt"; 121 validPaths = { "/tmp/", "/data/local/tmp/" }; 122 EXPECT_FALSE(VerifyPath(filePath, validPaths)); 123 } 124 125 /** 126 * @tc.name: CommonTest 127 * @tc.desc: ReadFile. 128 * @tc.type: FUNC 129 */ 130 HWTEST_F(CommonTest, ReadFile, TestSize.Level1) 131 { 132 std::string fileName = "/data/local/tmp/config.txt"; 133 std::string fileContent = "Hello world"; 134 WriteFile(fileName, fileContent); 135 136 // invalid path 137 std::vector<std::string> validPaths = { "/tmp/" }; 138 std::string readContent; 139 bool ret = ReadFile(fileName, validPaths, readContent); 140 EXPECT_FALSE(ret); 141 EXPECT_TRUE(readContent.empty()); 142 143 // invalid file path 144 fileName = "config.txt"; 145 validPaths = { "/tmp/", "/data/local/tmp/" }; 146 readContent.clear(); 147 ret = ReadFile(fileName, validPaths, readContent); 148 EXPECT_FALSE(ret); 149 EXPECT_TRUE(readContent.empty()); 150 151 // invalid file name 152 fileName = "configtmp.txt"; 153 validPaths = { "/tmp/", "/data/local/tmp/" }; 154 readContent.clear(); 155 ret = ReadFile(fileName, validPaths, readContent); 156 EXPECT_FALSE(ret); 157 EXPECT_TRUE(readContent.empty()); 158 159 // valid path 160 fileName = "/data/local/tmp/config.txt"; 161 validPaths = { "/tmp/", "/data/local/tmp/" }; 162 readContent.clear(); 163 ret = ReadFile(fileName, validPaths, readContent); 164 EXPECT_TRUE(ret); 165 EXPECT_TRUE(readContent == fileContent); 166 167 // delete file 168 fileName = "/data/local/tmp/config.txt"; 169 std::string cmd = "rm " + fileName; 170 system(cmd.c_str()); 171 } 172 } // namespace