106f6ba60Sopenharmony_ci/*
206f6ba60Sopenharmony_ci * Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.
306f6ba60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
406f6ba60Sopenharmony_ci * you may not use this file except in compliance with the License.
506f6ba60Sopenharmony_ci * You may obtain a copy of the License at
606f6ba60Sopenharmony_ci *
706f6ba60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
806f6ba60Sopenharmony_ci *
906f6ba60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1006f6ba60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1106f6ba60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1206f6ba60Sopenharmony_ci * See the License for the specific language governing permissions and
1306f6ba60Sopenharmony_ci * limitations under the License.
1406f6ba60Sopenharmony_ci */
1506f6ba60Sopenharmony_ci
1606f6ba60Sopenharmony_ci#include <chrono>
1706f6ba60Sopenharmony_ci#include <thread>
1806f6ba60Sopenharmony_ci#include <gtest/gtest.h>
1906f6ba60Sopenharmony_ci#include "common.h"
2006f6ba60Sopenharmony_ci#include "logging.h"
2106f6ba60Sopenharmony_ci
2206f6ba60Sopenharmony_cinamespace {
2306f6ba60Sopenharmony_ciusing namespace testing::ext;
2406f6ba60Sopenharmony_ciusing namespace COMMON;
2506f6ba60Sopenharmony_ci
2606f6ba60Sopenharmony_ciclass CommonTest : public testing::Test {
2706f6ba60Sopenharmony_ciprotected:
2806f6ba60Sopenharmony_ci    static void SetUpTestCase() {}
2906f6ba60Sopenharmony_ci    static void TearDownTestCase() {}
3006f6ba60Sopenharmony_ci
3106f6ba60Sopenharmony_ci    bool WriteFile(const std::string& filePath, const std::string& fileContent)
3206f6ba60Sopenharmony_ci    {
3306f6ba60Sopenharmony_ci        FILE* file = fopen(filePath.c_str(), "w");
3406f6ba60Sopenharmony_ci        if (file == nullptr) {
3506f6ba60Sopenharmony_ci            std::string errorMsg = GetErrorMsg();
3606f6ba60Sopenharmony_ci            PROFILER_LOG_ERROR(LOG_CORE, "WriteFile: fopen() fail, %s, %s", filePath.c_str(), errorMsg.c_str());
3706f6ba60Sopenharmony_ci            return false;
3806f6ba60Sopenharmony_ci        }
3906f6ba60Sopenharmony_ci
4006f6ba60Sopenharmony_ci        size_t len = fwrite(const_cast<char*>(fileContent.c_str()), 1, fileContent.length(), file);
4106f6ba60Sopenharmony_ci        if (len < 0) {
4206f6ba60Sopenharmony_ci            std::string errorMsg = GetErrorMsg();
4306f6ba60Sopenharmony_ci            PROFILER_LOG_ERROR(LOG_CORE, "WriteFile: fwrite() fail, %s", errorMsg.c_str());
4406f6ba60Sopenharmony_ci            (void)fclose(file);
4506f6ba60Sopenharmony_ci            return false;
4606f6ba60Sopenharmony_ci        }
4706f6ba60Sopenharmony_ci
4806f6ba60Sopenharmony_ci        if (fflush(file) == EOF) {
4906f6ba60Sopenharmony_ci            std::string errorMsg = GetErrorMsg();
5006f6ba60Sopenharmony_ci            PROFILER_LOG_ERROR(LOG_CORE, "WriteFile: fflush() error = %s", errorMsg.c_str());
5106f6ba60Sopenharmony_ci            (void)fclose(file);
5206f6ba60Sopenharmony_ci            return false;
5306f6ba60Sopenharmony_ci        }
5406f6ba60Sopenharmony_ci
5506f6ba60Sopenharmony_ci        fsync(fileno(file));
5606f6ba60Sopenharmony_ci        if (fclose(file) != 0) {
5706f6ba60Sopenharmony_ci            std::string errorMsg = GetErrorMsg();
5806f6ba60Sopenharmony_ci            PROFILER_LOG_ERROR(LOG_CORE, "CreateConfigFile: fclose() error = %s", errorMsg.c_str());
5906f6ba60Sopenharmony_ci            return false;
6006f6ba60Sopenharmony_ci        }
6106f6ba60Sopenharmony_ci        return true;
6206f6ba60Sopenharmony_ci    }
6306f6ba60Sopenharmony_ci};
6406f6ba60Sopenharmony_ci
6506f6ba60Sopenharmony_ci/**
6606f6ba60Sopenharmony_ci * @tc.name: CommonTest
6706f6ba60Sopenharmony_ci * @tc.desc: IsProcessExist.
6806f6ba60Sopenharmony_ci * @tc.type: FUNC
6906f6ba60Sopenharmony_ci */
7006f6ba60Sopenharmony_ciHWTEST_F(CommonTest, IsProcessExist, TestSize.Level1)
7106f6ba60Sopenharmony_ci{
7206f6ba60Sopenharmony_ci    const std::string procName = "hiprofiler_base_ut";
7306f6ba60Sopenharmony_ci    int pid = 0;
7406f6ba60Sopenharmony_ci    EXPECT_TRUE(COMMON::IsProcessExist(procName, pid));
7506f6ba60Sopenharmony_ci    EXPECT_NE(pid, 0);
7606f6ba60Sopenharmony_ci    const std::string invalidProcName = "ls";
7706f6ba60Sopenharmony_ci    pid = 0;
7806f6ba60Sopenharmony_ci    EXPECT_FALSE(COMMON::IsProcessExist(invalidProcName, pid));
7906f6ba60Sopenharmony_ci    EXPECT_EQ(pid, 0);
8006f6ba60Sopenharmony_ci}
8106f6ba60Sopenharmony_ci
8206f6ba60Sopenharmony_ci/**
8306f6ba60Sopenharmony_ci * @tc.name: CommonTest
8406f6ba60Sopenharmony_ci * @tc.desc: StartProcess.
8506f6ba60Sopenharmony_ci * @tc.type: FUNC
8606f6ba60Sopenharmony_ci */
8706f6ba60Sopenharmony_ciHWTEST_F(CommonTest, StartAndKillProcess, TestSize.Level1)
8806f6ba60Sopenharmony_ci{
8906f6ba60Sopenharmony_ci    constexpr int waitProcMills = 300;
9006f6ba60Sopenharmony_ci    std::string profilerProcName("hiprofilerd");
9106f6ba60Sopenharmony_ci    std::vector<char*> argvVec;
9206f6ba60Sopenharmony_ci    argvVec.push_back(const_cast<char*>(profilerProcName.c_str()));
9306f6ba60Sopenharmony_ci    int lockFileFd = -1;
9406f6ba60Sopenharmony_ci    EXPECT_FALSE(COMMON::IsProcessRunning(lockFileFd));
9506f6ba60Sopenharmony_ci    int procPid = COMMON::StartProcess("/system/bin/hiprofilerd", argvVec);
9606f6ba60Sopenharmony_ci    EXPECT_NE(procPid, 0);
9706f6ba60Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::milliseconds(waitProcMills));
9806f6ba60Sopenharmony_ci    EXPECT_NE(COMMON::KillProcess(procPid), -1);
9906f6ba60Sopenharmony_ci}
10006f6ba60Sopenharmony_ci
10106f6ba60Sopenharmony_ci/**
10206f6ba60Sopenharmony_ci * @tc.name: CommonTest
10306f6ba60Sopenharmony_ci * @tc.desc: VerifyPath.
10406f6ba60Sopenharmony_ci * @tc.type: FUNC
10506f6ba60Sopenharmony_ci */
10606f6ba60Sopenharmony_ciHWTEST_F(CommonTest, VerifyPath, TestSize.Level1)
10706f6ba60Sopenharmony_ci{
10806f6ba60Sopenharmony_ci    std::string filePath = "/data/local/tmp/config.txt";
10906f6ba60Sopenharmony_ci    std::vector<std::string> validPaths = {};
11006f6ba60Sopenharmony_ci    EXPECT_TRUE(VerifyPath(filePath, validPaths));
11106f6ba60Sopenharmony_ci
11206f6ba60Sopenharmony_ci    validPaths = { "/tmp/" };
11306f6ba60Sopenharmony_ci    EXPECT_FALSE(VerifyPath(filePath, validPaths));
11406f6ba60Sopenharmony_ci
11506f6ba60Sopenharmony_ci    validPaths = { "/tmp/", "/data/" };
11606f6ba60Sopenharmony_ci    EXPECT_TRUE(VerifyPath(filePath, validPaths));
11706f6ba60Sopenharmony_ci
11806f6ba60Sopenharmony_ci    validPaths = { "/tmp/", "/data/local/tmp/" };
11906f6ba60Sopenharmony_ci    EXPECT_TRUE(VerifyPath(filePath, validPaths));
12006f6ba60Sopenharmony_ci
12106f6ba60Sopenharmony_ci    filePath = "/data/local/tmpconfig.txt";
12206f6ba60Sopenharmony_ci    validPaths = { "/tmp/", "/data/local/tmp/" };
12306f6ba60Sopenharmony_ci    EXPECT_FALSE(VerifyPath(filePath, validPaths));
12406f6ba60Sopenharmony_ci}
12506f6ba60Sopenharmony_ci
12606f6ba60Sopenharmony_ci/**
12706f6ba60Sopenharmony_ci * @tc.name: CommonTest
12806f6ba60Sopenharmony_ci * @tc.desc: ReadFile.
12906f6ba60Sopenharmony_ci * @tc.type: FUNC
13006f6ba60Sopenharmony_ci */
13106f6ba60Sopenharmony_ciHWTEST_F(CommonTest, ReadFile, TestSize.Level1)
13206f6ba60Sopenharmony_ci{
13306f6ba60Sopenharmony_ci    std::string fileName = "/data/local/tmp/config.txt";
13406f6ba60Sopenharmony_ci    std::string fileContent = "Hello world";
13506f6ba60Sopenharmony_ci    EXPECT_TRUE(WriteFile(fileName, fileContent));
13606f6ba60Sopenharmony_ci
13706f6ba60Sopenharmony_ci    // invalid path
13806f6ba60Sopenharmony_ci    std::vector<std::string> validPaths = { "/tmp/" };
13906f6ba60Sopenharmony_ci    std::string readContent;
14006f6ba60Sopenharmony_ci    bool ret = ReadFile(fileName, validPaths, readContent);
14106f6ba60Sopenharmony_ci    EXPECT_FALSE(ret);
14206f6ba60Sopenharmony_ci    EXPECT_TRUE(readContent.empty());
14306f6ba60Sopenharmony_ci
14406f6ba60Sopenharmony_ci    // invalid file path
14506f6ba60Sopenharmony_ci    fileName = "config.txt";
14606f6ba60Sopenharmony_ci    validPaths = { "/tmp/", "/data/local/tmp/" };
14706f6ba60Sopenharmony_ci    readContent.clear();
14806f6ba60Sopenharmony_ci    ret = ReadFile(fileName, validPaths, readContent);
14906f6ba60Sopenharmony_ci    EXPECT_FALSE(ret);
15006f6ba60Sopenharmony_ci    EXPECT_TRUE(readContent.empty());
15106f6ba60Sopenharmony_ci
15206f6ba60Sopenharmony_ci    // invalid file name
15306f6ba60Sopenharmony_ci    fileName = "configtmp.txt";
15406f6ba60Sopenharmony_ci    validPaths = { "/tmp/", "/data/local/tmp/" };
15506f6ba60Sopenharmony_ci    readContent.clear();
15606f6ba60Sopenharmony_ci    ret = ReadFile(fileName, validPaths, readContent);
15706f6ba60Sopenharmony_ci    EXPECT_FALSE(ret);
15806f6ba60Sopenharmony_ci    EXPECT_TRUE(readContent.empty());
15906f6ba60Sopenharmony_ci
16006f6ba60Sopenharmony_ci    // valid path
16106f6ba60Sopenharmony_ci    fileName = "/data/local/tmp/config.txt";
16206f6ba60Sopenharmony_ci    validPaths = { "/tmp/", "/data/local/tmp/" };
16306f6ba60Sopenharmony_ci    readContent.clear();
16406f6ba60Sopenharmony_ci    ret = ReadFile(fileName, validPaths, readContent);
16506f6ba60Sopenharmony_ci    EXPECT_TRUE(ret);
16606f6ba60Sopenharmony_ci    EXPECT_TRUE(readContent == fileContent);
16706f6ba60Sopenharmony_ci
16806f6ba60Sopenharmony_ci    // delete file
16906f6ba60Sopenharmony_ci    fileName = "/data/local/tmp/config.txt";
17006f6ba60Sopenharmony_ci    std::string cmd = "rm " + fileName;
17106f6ba60Sopenharmony_ci    system(cmd.c_str());
17206f6ba60Sopenharmony_ci}
17306f6ba60Sopenharmony_ci
17406f6ba60Sopenharmony_ci/**
17506f6ba60Sopenharmony_ci * @tc.name: CommonTest
17606f6ba60Sopenharmony_ci * @tc.desc: WriteFileFailed.
17706f6ba60Sopenharmony_ci * @tc.type: FUNC
17806f6ba60Sopenharmony_ci */
17906f6ba60Sopenharmony_ciHWTEST_F(CommonTest, WriteFileFailed, TestSize.Level1)
18006f6ba60Sopenharmony_ci{
18106f6ba60Sopenharmony_ci    std::string fileName = "/data/local/tmp/invalid/config.txt";
18206f6ba60Sopenharmony_ci    std::string fileContent = "Hello world";
18306f6ba60Sopenharmony_ci    EXPECT_FALSE(WriteFile(fileName, fileContent));
18406f6ba60Sopenharmony_ci}
18506f6ba60Sopenharmony_ci
18606f6ba60Sopenharmony_ci/**
18706f6ba60Sopenharmony_ci * @tc.name: CommonTest
18806f6ba60Sopenharmony_ci * @tc.desc: GetTimeStr.
18906f6ba60Sopenharmony_ci * @tc.type: FUNC
19006f6ba60Sopenharmony_ci */
19106f6ba60Sopenharmony_ciHWTEST_F(CommonTest, GetTimeStr, TestSize.Level1)
19206f6ba60Sopenharmony_ci{
19306f6ba60Sopenharmony_ci    std::string timeStr = GetTimeStr();
19406f6ba60Sopenharmony_ci    EXPECT_FALSE(timeStr.empty());
19506f6ba60Sopenharmony_ci}
19606f6ba60Sopenharmony_ci} // namespace