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 <chrono> 17 #include <thread> 18 #include <gtest/gtest.h> 19 #include "common.h" 20 #include "logging.h" 21 22 namespace { 23 using namespace testing::ext; 24 using namespace COMMON; 25 26 class CommonTest : public testing::Test { 27 protected: SetUpTestCase()28 static void SetUpTestCase() {} TearDownTestCase()29 static void TearDownTestCase() {} 30 WriteFile(const std::string & filePath,const std::string & fileContent)31 bool WriteFile(const std::string& filePath, const std::string& fileContent) 32 { 33 FILE* file = fopen(filePath.c_str(), "w"); 34 if (file == nullptr) { 35 std::string errorMsg = GetErrorMsg(); 36 PROFILER_LOG_ERROR(LOG_CORE, "WriteFile: fopen() fail, %s, %s", filePath.c_str(), errorMsg.c_str()); 37 return false; 38 } 39 40 size_t len = fwrite(const_cast<char*>(fileContent.c_str()), 1, fileContent.length(), file); 41 if (len < 0) { 42 std::string errorMsg = GetErrorMsg(); 43 PROFILER_LOG_ERROR(LOG_CORE, "WriteFile: fwrite() fail, %s", errorMsg.c_str()); 44 (void)fclose(file); 45 return false; 46 } 47 48 if (fflush(file) == EOF) { 49 std::string errorMsg = GetErrorMsg(); 50 PROFILER_LOG_ERROR(LOG_CORE, "WriteFile: fflush() error = %s", errorMsg.c_str()); 51 (void)fclose(file); 52 return false; 53 } 54 55 fsync(fileno(file)); 56 if (fclose(file) != 0) { 57 std::string errorMsg = GetErrorMsg(); 58 PROFILER_LOG_ERROR(LOG_CORE, "CreateConfigFile: fclose() error = %s", errorMsg.c_str()); 59 return false; 60 } 61 return true; 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 const std::string procName = "hiprofiler_base_ut"; 73 int pid = 0; 74 EXPECT_TRUE(COMMON::IsProcessExist(procName, pid)); 75 EXPECT_NE(pid, 0); 76 const std::string invalidProcName = "ls"; 77 pid = 0; 78 EXPECT_FALSE(COMMON::IsProcessExist(invalidProcName, 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 int lockFileFd = -1; 94 EXPECT_FALSE(COMMON::IsProcessRunning(lockFileFd)); 95 int procPid = COMMON::StartProcess("/system/bin/hiprofilerd", argvVec); 96 EXPECT_NE(procPid, 0); 97 std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); 98 EXPECT_NE(COMMON::KillProcess(procPid), -1); 99 } 100 101 /** 102 * @tc.name: CommonTest 103 * @tc.desc: VerifyPath. 104 * @tc.type: FUNC 105 */ 106 HWTEST_F(CommonTest, VerifyPath, TestSize.Level1) 107 { 108 std::string filePath = "/data/local/tmp/config.txt"; 109 std::vector<std::string> validPaths = {}; 110 EXPECT_TRUE(VerifyPath(filePath, validPaths)); 111 112 validPaths = { "/tmp/" }; 113 EXPECT_FALSE(VerifyPath(filePath, validPaths)); 114 115 validPaths = { "/tmp/", "/data/" }; 116 EXPECT_TRUE(VerifyPath(filePath, validPaths)); 117 118 validPaths = { "/tmp/", "/data/local/tmp/" }; 119 EXPECT_TRUE(VerifyPath(filePath, validPaths)); 120 121 filePath = "/data/local/tmpconfig.txt"; 122 validPaths = { "/tmp/", "/data/local/tmp/" }; 123 EXPECT_FALSE(VerifyPath(filePath, validPaths)); 124 } 125 126 /** 127 * @tc.name: CommonTest 128 * @tc.desc: ReadFile. 129 * @tc.type: FUNC 130 */ 131 HWTEST_F(CommonTest, ReadFile, TestSize.Level1) 132 { 133 std::string fileName = "/data/local/tmp/config.txt"; 134 std::string fileContent = "Hello world"; 135 EXPECT_TRUE(WriteFile(fileName, fileContent)); 136 137 // invalid path 138 std::vector<std::string> validPaths = { "/tmp/" }; 139 std::string readContent; 140 bool ret = ReadFile(fileName, validPaths, readContent); 141 EXPECT_FALSE(ret); 142 EXPECT_TRUE(readContent.empty()); 143 144 // invalid file path 145 fileName = "config.txt"; 146 validPaths = { "/tmp/", "/data/local/tmp/" }; 147 readContent.clear(); 148 ret = ReadFile(fileName, validPaths, readContent); 149 EXPECT_FALSE(ret); 150 EXPECT_TRUE(readContent.empty()); 151 152 // invalid file name 153 fileName = "configtmp.txt"; 154 validPaths = { "/tmp/", "/data/local/tmp/" }; 155 readContent.clear(); 156 ret = ReadFile(fileName, validPaths, readContent); 157 EXPECT_FALSE(ret); 158 EXPECT_TRUE(readContent.empty()); 159 160 // valid path 161 fileName = "/data/local/tmp/config.txt"; 162 validPaths = { "/tmp/", "/data/local/tmp/" }; 163 readContent.clear(); 164 ret = ReadFile(fileName, validPaths, readContent); 165 EXPECT_TRUE(ret); 166 EXPECT_TRUE(readContent == fileContent); 167 168 // delete file 169 fileName = "/data/local/tmp/config.txt"; 170 std::string cmd = "rm " + fileName; 171 system(cmd.c_str()); 172 } 173 174 /** 175 * @tc.name: CommonTest 176 * @tc.desc: WriteFileFailed. 177 * @tc.type: FUNC 178 */ 179 HWTEST_F(CommonTest, WriteFileFailed, TestSize.Level1) 180 { 181 std::string fileName = "/data/local/tmp/invalid/config.txt"; 182 std::string fileContent = "Hello world"; 183 EXPECT_FALSE(WriteFile(fileName, fileContent)); 184 } 185 186 /** 187 * @tc.name: CommonTest 188 * @tc.desc: GetTimeStr. 189 * @tc.type: FUNC 190 */ 191 HWTEST_F(CommonTest, GetTimeStr, TestSize.Level1) 192 { 193 std::string timeStr = GetTimeStr(); 194 EXPECT_FALSE(timeStr.empty()); 195 } 196 197 /** 198 * @tc.name: CommonTest 199 * @tc.desc: PluginWriteToHisysevent. 200 * @tc.type: FUNC 201 */ 202 HWTEST_F(CommonTest, PluginWriteToHisysevent001, TestSize.Level1) 203 { 204 int ret = PluginWriteToHisysevent("CPU_PLUGIN", "tdd_test", "test_args", RET_SUCC, "success"); 205 EXPECT_EQ(ret, 0); 206 } 207 208 /** 209 * @tc.name: CommonTest 210 * @tc.desc: PluginWriteToHisysevent. 211 * @tc.type: FUNC 212 */ 213 HWTEST_F(CommonTest, PluginWriteToHisysevent002, TestSize.Level1) 214 { 215 int ret = PluginWriteToHisysevent("DISKIO_PLUGIN", "tdd_test", "test_args", RET_SUCC, "success"); 216 EXPECT_EQ(ret, 0); 217 } 218 } // namespace