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 #include <malloc.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 bool 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 PROFILER_LOG_ERROR(LOG_CORE, "WriteFile: fopen() fail, %s, %s", filePath.c_str(), errorMsg.c_str()); 38 return false; 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 PROFILER_LOG_ERROR(LOG_CORE, "WriteFile: fwrite() fail, %s", errorMsg.c_str()); 45 (void)fclose(file); 46 return false; 47 } 48 49 if (fflush(file) == EOF) { 50 std::string errorMsg = GetErrorMsg(); 51 PROFILER_LOG_ERROR(LOG_CORE, "WriteFile: fflush() error = %s", errorMsg.c_str()); 52 (void)fclose(file); 53 return false; 54 } 55 56 fsync(fileno(file)); 57 if (fclose(file) != 0) { 58 std::string errorMsg = GetErrorMsg(); 59 PROFILER_LOG_ERROR(LOG_CORE, "CreateConfigFile: fclose() error = %s", errorMsg.c_str()); 60 return false; 61 } 62 return true; 63 } 64 }; 65 66 /** 67 * @tc.name: CommonTest 68 * @tc.desc: IsProcessExist. 69 * @tc.type: FUNC 70 */ 71 HWTEST_F(CommonTest, IsProcessExist, TestSize.Level1) 72 { 73 const std::string procName = "hiprofiler_base_ut"; 74 int pid = 0; 75 EXPECT_TRUE(COMMON::IsProcessExist(procName, pid)); 76 EXPECT_NE(pid, 0); 77 const std::string invalidProcName = "ls"; 78 pid = 0; 79 EXPECT_FALSE(COMMON::IsProcessExist(invalidProcName, pid)); 80 EXPECT_EQ(pid, 0); 81 } 82 83 /** 84 * @tc.name: CommonTest 85 * @tc.desc: GetUidGidFromPid. 86 * @tc.type: FUNC 87 */ 88 HWTEST_F(CommonTest, GetUidGidFromPid, TestSize.Level1) 89 { 90 const std::string procName = "hiprofiler_base_ut"; 91 int pid = 0; 92 EXPECT_TRUE(COMMON::IsProcessExist(procName, pid)); 93 EXPECT_NE(pid, 0); 94 uid_t uid = 0; 95 gid_t gid = 0; 96 EXPECT_TRUE(COMMON::GetUidGidFromPid(static_cast<pid_t>(pid), uid, gid)); 97 EXPECT_EQ(uid, 0); 98 EXPECT_EQ(gid, 0); 99 100 const std::string hiviewName = "hiview"; 101 pid = 0; 102 EXPECT_TRUE(COMMON::IsProcessExist(hiviewName, pid)); 103 EXPECT_NE(pid, 0); 104 uid = 0; 105 gid = 0; 106 EXPECT_TRUE(COMMON::GetUidGidFromPid(static_cast<pid_t>(pid), uid, gid)); 107 EXPECT_NE(uid, 0); 108 EXPECT_NE(gid, 0); 109 } 110 111 /** 112 * @tc.name: CommonTest 113 * @tc.desc: StartProcess. 114 * @tc.type: FUNC 115 */ 116 HWTEST_F(CommonTest, StartAndKillProcess, TestSize.Level1) 117 { 118 constexpr int waitProcMills = 300; 119 std::string profilerProcName("hiprofilerd"); 120 std::vector<char*> argvVec; 121 argvVec.push_back(const_cast<char*>(profilerProcName.c_str())); 122 int lockFileFd = -1; 123 EXPECT_FALSE(COMMON::IsProcessRunning(lockFileFd)); 124 int procPid = COMMON::StartProcess("/system/bin/hiprofilerd", argvVec); 125 EXPECT_NE(procPid, 0); 126 std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills)); 127 EXPECT_NE(COMMON::KillProcess(procPid), -1); 128 } 129 130 /** 131 * @tc.name: CommonTest 132 * @tc.desc: StartProcess.Start process name is not exit or illegal 133 * @tc.type: FUNC 134 */ 135 HWTEST_F(CommonTest, StartNoexitProcess, TestSize.Level1) 136 { 137 std::vector<char*> argvVec; 138 EXPECT_EQ(COMMON::StartProcess("/system/bin/test_thread", argvVec), -1); 139 argvVec.push_back(const_cast<char*>("")); 140 EXPECT_EQ(COMMON::StartProcess("/system/bin/test_thread", argvVec), -1); 141 argvVec.push_back(const_cast<char*>("test||")); 142 EXPECT_EQ(COMMON::StartProcess("/system/bin/hiprofilerd", argvVec), -1); 143 argvVec.clear(); 144 EXPECT_EQ(COMMON::KillProcess(-1), -1); 145 EXPECT_EQ(COMMON::KillProcess(999999999), -1); 146 } 147 148 /** 149 * @tc.name: CommonTest 150 * @tc.desc: VerifyPath. 151 * @tc.type: FUNC 152 */ 153 HWTEST_F(CommonTest, VerifyPath, TestSize.Level1) 154 { 155 std::string filePath = "/data/local/tmp/config.txt"; 156 std::vector<std::string> validPaths = {}; 157 EXPECT_TRUE(VerifyPath(filePath, validPaths)); 158 159 validPaths = { "/tmp/" }; 160 EXPECT_FALSE(VerifyPath(filePath, validPaths)); 161 162 validPaths = { "/tmp/", "/data/" }; 163 EXPECT_TRUE(VerifyPath(filePath, validPaths)); 164 165 validPaths = { "/tmp/", "/data/local/tmp/" }; 166 EXPECT_TRUE(VerifyPath(filePath, validPaths)); 167 168 filePath = "/data/local/tmpconfig.txt"; 169 validPaths = { "/tmp/", "/data/local/tmp/" }; 170 EXPECT_FALSE(VerifyPath(filePath, validPaths)); 171 } 172 173 /** 174 * @tc.name: CommonTest 175 * @tc.desc: ReadFile. 176 * @tc.type: FUNC 177 */ 178 HWTEST_F(CommonTest, ReadFile, TestSize.Level1) 179 { 180 std::string fileName = "/data/local/tmp/config.txt"; 181 std::string fileContent = "Hello world"; 182 EXPECT_TRUE(WriteFile(fileName, fileContent)); 183 184 // invalid path 185 std::vector<std::string> validPaths = { "/tmp/" }; 186 std::string readContent; 187 bool ret = ReadFile(fileName, validPaths, readContent); 188 EXPECT_FALSE(ret); 189 EXPECT_TRUE(readContent.empty()); 190 191 // invalid file path 192 fileName = "config.txt"; 193 validPaths = { "/tmp/", "/data/local/tmp/" }; 194 readContent.clear(); 195 ret = ReadFile(fileName, validPaths, readContent); 196 EXPECT_FALSE(ret); 197 EXPECT_TRUE(readContent.empty()); 198 199 // invalid file name 200 fileName = "configtmp.txt"; 201 validPaths = { "/tmp/", "/data/local/tmp/" }; 202 readContent.clear(); 203 ret = ReadFile(fileName, validPaths, readContent); 204 EXPECT_FALSE(ret); 205 EXPECT_TRUE(readContent.empty()); 206 207 // valid path 208 fileName = "/data/local/tmp/config.txt"; 209 validPaths = { "/tmp/", "/data/local/tmp/" }; 210 readContent.clear(); 211 ret = ReadFile(fileName, validPaths, readContent); 212 EXPECT_TRUE(ret); 213 EXPECT_TRUE(readContent == fileContent); 214 215 // delete file 216 fileName = "/data/local/tmp/config.txt"; 217 std::string cmd = "rm " + fileName; 218 system(cmd.c_str()); 219 220 // Absolute path 221 fileName = "data/log/bbox"; 222 validPaths = { "/tmp/", "/data/local/tmp/" }; 223 readContent.clear(); 224 ret = ReadFile(fileName, validPaths, readContent); 225 EXPECT_FALSE(ret); 226 EXPECT_TRUE(readContent.empty()); 227 228 // Relative path 229 fileName = "./log/faultlog/faultlogger"; 230 validPaths = { "/tmp/", "/data/local/tmp/" }; 231 readContent.clear(); 232 ret = ReadFile(fileName, validPaths, readContent); 233 EXPECT_FALSE(ret); 234 EXPECT_TRUE(readContent.empty()); 235 } 236 237 /** 238 * @tc.name: CommonTest 239 * @tc.desc: WriteFileFailed. 240 * @tc.type: FUNC 241 */ 242 HWTEST_F(CommonTest, WriteFileFailed, TestSize.Level1) 243 { 244 std::string fileName = "/data/local/tmp/invalid/config.txt"; 245 std::string fileContent = "Hello world"; 246 EXPECT_FALSE(WriteFile(fileName, fileContent)); 247 } 248 249 /** 250 * @tc.name: CommonTest 251 * @tc.desc: GetTimeStr. 252 * @tc.type: FUNC 253 */ 254 HWTEST_F(CommonTest, GetTimeStr, TestSize.Level1) 255 { 256 std::string timeStr = GetTimeStr(); 257 EXPECT_FALSE(timeStr.empty()); 258 } 259 260 /** 261 * @tc.name: CommonTest 262 * @tc.desc: PluginWriteToHisysevent. 263 * @tc.type: FUNC 264 */ 265 HWTEST_F(CommonTest, PluginWriteToHisysevent001, TestSize.Level1) 266 { 267 int ret = PluginWriteToHisysevent("CPU_PLUGIN", "tdd_test", "test_args", RET_SUCC, "success"); 268 EXPECT_EQ(ret, 0); 269 } 270 271 /** 272 * @tc.name: CommonTest 273 * @tc.desc: PluginWriteToHisysevent. 274 * @tc.type: FUNC 275 */ 276 HWTEST_F(CommonTest, PluginWriteToHisysevent002, TestSize.Level1) 277 { 278 int ret = PluginWriteToHisysevent("DISKIO_PLUGIN", "tdd_test", "test_args", RET_SUCC, "success"); 279 EXPECT_EQ(ret, 0); 280 } 281 282 /** 283 * @tc.name: CommonTest 284 * @tc.desc: test CheckSubscribeVersion and PrintMallinfoLog 285 * @tc.type: FUNC 286 */ 287 HWTEST_F(CommonTest, CheckSubscribeVersion, TestSize.Level1) 288 { 289 static struct mallinfo2 miStart = {0}; 290 miStart.arena = 1; 291 miStart.ordblks = 2; 292 miStart.smblks = 3; 293 miStart.hblks = 4; 294 miStart.hblkhd = 5; 295 miStart.usmblks = 6; 296 miStart.fsmblks = 7; 297 miStart.uordblks = 8; 298 miStart.fordblks = 9; 299 miStart.keepcost = 10; 300 std::string testStr = "test:"; 301 COMMON::PrintMallinfoLog(testStr, miStart); 302 EXPECT_EQ(COMMON::CheckSubscribeVersion("0.5_2"), false); 303 EXPECT_EQ(COMMON::CheckSubscribeVersion("1.0"), true); 304 } 305 306 /** 307 * @tc.name: CommonTest 308 * @tc.desc: StartProcess.CustomPopen command name is not exit or illegal 309 * @tc.type: FUNC 310 */ 311 HWTEST_F(CommonTest, CustomPopen, TestSize.Level1) 312 { 313 std::vector<std::string> fullCmdTest; 314 fullCmdTest.push_back("/system/bin/test"); 315 fullCmdTest.push_back("hisysevent"); 316 fullCmdTest.push_back("-rd"); 317 volatile pid_t childPid = -1; 318 int pipeFds[2] = {-1, -1}; 319 FILE* fpr = COMMON::CustomPopen(fullCmdTest, nullptr, pipeFds, childPid, true); 320 EXPECT_EQ(fpr, nullptr); 321 EXPECT_EQ(COMMON::CustomPopen(fullCmdTest, "w", pipeFds, childPid), nullptr); 322 fullCmdTest.clear(); 323 fullCmdTest.push_back("/system/bin/hiprofilerd"); 324 fullCmdTest.push_back("hiprofilerd&"); 325 fullCmdTest.push_back("-rd"); 326 EXPECT_EQ(COMMON::CustomPopen(fullCmdTest, "w", pipeFds, childPid), nullptr); 327 } 328 329 /** 330 * @tc.name: CommonTest 331 * @tc.desc: IsNumeric and SplitString 332 * @tc.type: FUNC 333 */ 334 HWTEST_F(CommonTest, IsNumeric, TestSize.Level1) 335 { 336 EXPECT_EQ(COMMON::IsNumeric("test"), false); 337 EXPECT_EQ(COMMON::IsNumeric("1111test"), false); 338 EXPECT_EQ(COMMON::IsNumeric("1111"), true); 339 340 string str = ""; 341 string seq = "_"; 342 std::vector<string> ret; 343 COMMON::SplitString(str, seq, ret); 344 EXPECT_EQ(ret.size(), 0); 345 } 346 /** 347 * @tc.name: AdaptSandboxPath 348 * @tc.desc: AdaptSandboxPath 349 * @tc.type: FUNC 350 */ 351 HWTEST_F(CommonTest, AdaptSandboxPath, TestSize.Level1) 352 { 353 std::string path = "/data/storage/test"; 354 COMMON::AdaptSandboxPath(path, 1); 355 EXPECT_EQ(path, "/proc/1/root/data/storage/test"); 356 } 357 } // namespace