12498b56bSopenharmony_ci/*
22498b56bSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
32498b56bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
42498b56bSopenharmony_ci * you may not use this file except in compliance with the License.
52498b56bSopenharmony_ci * You may obtain a copy of the License at
62498b56bSopenharmony_ci *
72498b56bSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
82498b56bSopenharmony_ci *
92498b56bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
102498b56bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
112498b56bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
122498b56bSopenharmony_ci * See the License for the specific language governing permissions and
132498b56bSopenharmony_ci * limitations under the License.
142498b56bSopenharmony_ci */
152498b56bSopenharmony_ci#include "hilog_utils_test.h"
162498b56bSopenharmony_ci#include "hilog_common.h"
172498b56bSopenharmony_ci#include <log_utils.h>
182498b56bSopenharmony_ci#include <hilog/log_c.h>
192498b56bSopenharmony_ci#include <list>
202498b56bSopenharmony_ci
212498b56bSopenharmony_ciusing namespace std;
222498b56bSopenharmony_ciusing namespace testing::ext;
232498b56bSopenharmony_ciusing namespace OHOS;
242498b56bSopenharmony_ciusing namespace OHOS::HiviewDFX;
252498b56bSopenharmony_ci
262498b56bSopenharmony_cistatic std::string GetCmdResultFromPopen(const std::string& cmd)
272498b56bSopenharmony_ci{
282498b56bSopenharmony_ci    if (cmd.empty()) {
292498b56bSopenharmony_ci        return "";
302498b56bSopenharmony_ci    }
312498b56bSopenharmony_ci    FILE* fp = popen(cmd.c_str(), "r");
322498b56bSopenharmony_ci    if (fp == nullptr) {
332498b56bSopenharmony_ci        return "";
342498b56bSopenharmony_ci    }
352498b56bSopenharmony_ci    std::string ret = "";
362498b56bSopenharmony_ci    char* buffer = nullptr;
372498b56bSopenharmony_ci    size_t len = 0;
382498b56bSopenharmony_ci    while (getline(&buffer, &len, fp) != -1) {
392498b56bSopenharmony_ci        std::string line = buffer;
402498b56bSopenharmony_ci        ret += line;
412498b56bSopenharmony_ci    }
422498b56bSopenharmony_ci    if (buffer != nullptr) {
432498b56bSopenharmony_ci        free(buffer);
442498b56bSopenharmony_ci        buffer = nullptr;
452498b56bSopenharmony_ci    }
462498b56bSopenharmony_ci    pclose(fp);
472498b56bSopenharmony_ci    return ret;
482498b56bSopenharmony_ci}
492498b56bSopenharmony_ci
502498b56bSopenharmony_cinamespace {
512498b56bSopenharmony_ci/**
522498b56bSopenharmony_ci * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_001
532498b56bSopenharmony_ci * @tc.desc: Size2Str & Str2Size.
542498b56bSopenharmony_ci * @tc.type: FUNC
552498b56bSopenharmony_ci */
562498b56bSopenharmony_ciHWTEST_F(HilogUtilsTest, HilogUtilsTest_001, TestSize.Level1)
572498b56bSopenharmony_ci{
582498b56bSopenharmony_ci    GTEST_LOG_(INFO) << "HilogUtilsTest_001: start.";
592498b56bSopenharmony_ci    const std::list<pair<uint64_t, string>> sizeStrList = {
602498b56bSopenharmony_ci        /* size, unit */
612498b56bSopenharmony_ci        {1, "B"},
622498b56bSopenharmony_ci        {1ULL << 10, "K"},
632498b56bSopenharmony_ci        {1ULL << 20, "M"},
642498b56bSopenharmony_ci        {1ULL << 30, "G"},
652498b56bSopenharmony_ci        {1ULL << 40, "T"},
662498b56bSopenharmony_ci    };
672498b56bSopenharmony_ci    for (auto &it : sizeStrList) {
682498b56bSopenharmony_ci        EXPECT_EQ(Size2Str(it.first), "1.0" + it.second);
692498b56bSopenharmony_ci        EXPECT_EQ(Str2Size("1" + it.second), it.first);
702498b56bSopenharmony_ci    }
712498b56bSopenharmony_ci
722498b56bSopenharmony_ci    // valid str reg [0-9]+[BKMGT]?
732498b56bSopenharmony_ci    EXPECT_EQ(Str2Size("1.2A"), 0);
742498b56bSopenharmony_ci}
752498b56bSopenharmony_ci
762498b56bSopenharmony_ci/**
772498b56bSopenharmony_ci * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_002
782498b56bSopenharmony_ci * @tc.desc: LogType2Str & Str2LogType.
792498b56bSopenharmony_ci * @tc.type: FUNC
802498b56bSopenharmony_ci */
812498b56bSopenharmony_ciHWTEST_F(HilogUtilsTest, HilogUtilsTest_002, TestSize.Level1)
822498b56bSopenharmony_ci{
832498b56bSopenharmony_ci    GTEST_LOG_(INFO) << "HilogUtilsTest_002: start.";
842498b56bSopenharmony_ci    const std::list<pair<LogType, string>> logTypesList = {
852498b56bSopenharmony_ci        {LOG_INIT, "init"},
862498b56bSopenharmony_ci        {LOG_CORE, "core"},
872498b56bSopenharmony_ci        {LOG_APP, "app"},
882498b56bSopenharmony_ci        {LOG_KMSG, "kmsg"},
892498b56bSopenharmony_ci        {LOG_ONLY_PRERELEASE, "only_prerelease"},
902498b56bSopenharmony_ci        {LOG_TYPE_MAX, "invalid"},
912498b56bSopenharmony_ci    };
922498b56bSopenharmony_ci    for (auto &it : logTypesList) {
932498b56bSopenharmony_ci        EXPECT_EQ(LogType2Str(it.first), it.second);
942498b56bSopenharmony_ci        EXPECT_EQ(Str2LogType(it.second), it.first);
952498b56bSopenharmony_ci    }
962498b56bSopenharmony_ci}
972498b56bSopenharmony_ci
982498b56bSopenharmony_ci/**
992498b56bSopenharmony_ci * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_003
1002498b56bSopenharmony_ci * @tc.desc: ComboLogType2Str & Str2ComboLogType.
1012498b56bSopenharmony_ci * @tc.type: FUNC
1022498b56bSopenharmony_ci */
1032498b56bSopenharmony_ciHWTEST_F(HilogUtilsTest, HilogUtilsTest_003, TestSize.Level1)
1042498b56bSopenharmony_ci{
1052498b56bSopenharmony_ci    GTEST_LOG_(INFO) << "HilogUtilsTest_003: start.";
1062498b56bSopenharmony_ci    const std::list<pair<uint16_t, string>> logTypesList = {
1072498b56bSopenharmony_ci        /* ComboLogType, str */
1082498b56bSopenharmony_ci        {1 << LOG_APP, "app"},
1092498b56bSopenharmony_ci        {1 << LOG_INIT, "init"},
1102498b56bSopenharmony_ci        {1 << LOG_CORE, "core"},
1112498b56bSopenharmony_ci        {1 << LOG_ONLY_PRERELEASE, "only_prerelease"},
1122498b56bSopenharmony_ci        {1 << LOG_KMSG, "kmsg"},
1132498b56bSopenharmony_ci        {(1 << LOG_APP) + (1 << LOG_INIT) + (1 << LOG_CORE) + (1 << LOG_ONLY_PRERELEASE) + (1 << LOG_KMSG),
1142498b56bSopenharmony_ci            "init,core,app,only_prerelease,kmsg"},
1152498b56bSopenharmony_ci    };
1162498b56bSopenharmony_ci    for (auto &it : logTypesList) {
1172498b56bSopenharmony_ci        EXPECT_EQ(ComboLogType2Str(it.first), it.second);
1182498b56bSopenharmony_ci        EXPECT_EQ(Str2ComboLogType(it.second), it.first);
1192498b56bSopenharmony_ci    }
1202498b56bSopenharmony_ci
1212498b56bSopenharmony_ci    EXPECT_EQ(Str2ComboLogType(""), (1 << LOG_APP) + (1 << LOG_CORE) + (1 << LOG_ONLY_PRERELEASE));
1222498b56bSopenharmony_ci    EXPECT_EQ(Str2ComboLogType("invalid"), 0);
1232498b56bSopenharmony_ci}
1242498b56bSopenharmony_ci
1252498b56bSopenharmony_ci/**
1262498b56bSopenharmony_ci * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_004
1272498b56bSopenharmony_ci * @tc.desc: LogLevel2Str & Str2LogLevel.
1282498b56bSopenharmony_ci * @tc.type: FUNC
1292498b56bSopenharmony_ci */
1302498b56bSopenharmony_ciHWTEST_F(HilogUtilsTest, HilogUtilsTest_004, TestSize.Level1)
1312498b56bSopenharmony_ci{
1322498b56bSopenharmony_ci    GTEST_LOG_(INFO) << "HilogUtilsTest_004: start.";
1332498b56bSopenharmony_ci    struct LogLevelEntry {
1342498b56bSopenharmony_ci        const LogLevel logLevel;
1352498b56bSopenharmony_ci        const std::string str;
1362498b56bSopenharmony_ci        const std::string shortStr;
1372498b56bSopenharmony_ci        const int comboLogLevel;
1382498b56bSopenharmony_ci    };
1392498b56bSopenharmony_ci
1402498b56bSopenharmony_ci    LogLevelEntry logLevelEntries[] = {
1412498b56bSopenharmony_ci        {LOG_LEVEL_MIN, "INVALID", "V", 0},
1422498b56bSopenharmony_ci        {LOG_DEBUG, "DEBUG", "D", 1 << LOG_DEBUG},
1432498b56bSopenharmony_ci        {LOG_INFO, "INFO", "I", 1 << LOG_INFO},
1442498b56bSopenharmony_ci        {LOG_WARN, "WARN", "W", 1 << LOG_WARN},
1452498b56bSopenharmony_ci        {LOG_ERROR, "ERROR", "E", 1 << LOG_ERROR},
1462498b56bSopenharmony_ci        {LOG_FATAL, "FATAL", "F", 1 << LOG_FATAL, },
1472498b56bSopenharmony_ci        {LOG_LEVEL_MAX, "X", "X", 0},
1482498b56bSopenharmony_ci    };
1492498b56bSopenharmony_ci
1502498b56bSopenharmony_ci    constexpr int logLevelEntryCnt = sizeof(logLevelEntries) / sizeof(LogLevelEntry);
1512498b56bSopenharmony_ci
1522498b56bSopenharmony_ci    for (int i = 0; i < logLevelEntryCnt; i++) {
1532498b56bSopenharmony_ci        EXPECT_EQ(LogLevel2Str(logLevelEntries[i].logLevel), logLevelEntries[i].str);
1542498b56bSopenharmony_ci        EXPECT_EQ(Str2LogLevel(logLevelEntries[i].str), logLevelEntries[i].logLevel);
1552498b56bSopenharmony_ci        EXPECT_EQ(LogLevel2ShortStr(logLevelEntries[i].logLevel), logLevelEntries[i].shortStr);
1562498b56bSopenharmony_ci        EXPECT_EQ(ShortStr2LogLevel(logLevelEntries[i].shortStr), logLevelEntries[i].logLevel);
1572498b56bSopenharmony_ci        if (logLevelEntries[i].comboLogLevel != 0) {
1582498b56bSopenharmony_ci            EXPECT_EQ(ComboLogLevel2Str(logLevelEntries[i].comboLogLevel), logLevelEntries[i].str);
1592498b56bSopenharmony_ci        }
1602498b56bSopenharmony_ci        EXPECT_EQ(Str2ComboLogLevel(logLevelEntries[i].str), logLevelEntries[i].comboLogLevel);
1612498b56bSopenharmony_ci    }
1622498b56bSopenharmony_ci
1632498b56bSopenharmony_ci    EXPECT_EQ(Str2ComboLogLevel(""), 0xFFFF);
1642498b56bSopenharmony_ci}
1652498b56bSopenharmony_ci
1662498b56bSopenharmony_ci/**
1672498b56bSopenharmony_ci * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_005
1682498b56bSopenharmony_ci * @tc.desc: GetBitsCount & GetBitPos.
1692498b56bSopenharmony_ci * @tc.type: FUNC
1702498b56bSopenharmony_ci */
1712498b56bSopenharmony_ciHWTEST_F(HilogUtilsTest, HilogUtilsTest_005, TestSize.Level1)
1722498b56bSopenharmony_ci{
1732498b56bSopenharmony_ci    GTEST_LOG_(INFO) << "HelperTest_005: start.";
1742498b56bSopenharmony_ci    uint64_t num1 = 1 << 4;
1752498b56bSopenharmony_ci    uint64_t num2 = (1 << 2) + (1 << 3) + (1 << 4);
1762498b56bSopenharmony_ci    EXPECT_EQ(GetBitPos(num1), 4);
1772498b56bSopenharmony_ci    // only accpet the number which is power of 2
1782498b56bSopenharmony_ci    EXPECT_EQ(GetBitPos(num2), 0);
1792498b56bSopenharmony_ci    EXPECT_EQ(GetBitsCount(num2), 3);
1802498b56bSopenharmony_ci}
1812498b56bSopenharmony_ci
1822498b56bSopenharmony_ci/**
1832498b56bSopenharmony_ci * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_006
1842498b56bSopenharmony_ci * @tc.desc: Uint2DecStr DecStr2Uint Uint2HexStr & HexStr2Uint.
1852498b56bSopenharmony_ci * @tc.type: FUNC
1862498b56bSopenharmony_ci */
1872498b56bSopenharmony_ciHWTEST_F(HilogUtilsTest, HilogUtilsTest_006, TestSize.Level1)
1882498b56bSopenharmony_ci{
1892498b56bSopenharmony_ci    GTEST_LOG_(INFO) << "HilogUtilsTest_006: start.";
1902498b56bSopenharmony_ci    uint32_t decNum = 1250;
1912498b56bSopenharmony_ci    uint32_t hexNum = 0xd002d00;
1922498b56bSopenharmony_ci    std::string decStr = "1250";
1932498b56bSopenharmony_ci    std::string hexStr = "d002d00";
1942498b56bSopenharmony_ci    EXPECT_EQ(Uint2DecStr(decNum), decStr);
1952498b56bSopenharmony_ci    EXPECT_EQ(DecStr2Uint(decStr), decNum);
1962498b56bSopenharmony_ci    EXPECT_EQ(Uint2HexStr(hexNum), hexStr);
1972498b56bSopenharmony_ci    EXPECT_EQ(HexStr2Uint(hexStr), hexNum);
1982498b56bSopenharmony_ci}
1992498b56bSopenharmony_ci
2002498b56bSopenharmony_ci/**
2012498b56bSopenharmony_ci * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_007
2022498b56bSopenharmony_ci * @tc.desc: GetAllLogTypes.
2032498b56bSopenharmony_ci * @tc.type: FUNC
2042498b56bSopenharmony_ci */
2052498b56bSopenharmony_ciHWTEST_F(HilogUtilsTest, HilogUtilsTest_007, TestSize.Level1)
2062498b56bSopenharmony_ci{
2072498b56bSopenharmony_ci    GTEST_LOG_(INFO) << "HilogUtilsTest_007: start.";
2082498b56bSopenharmony_ci    vector<uint16_t> vec = GetAllLogTypes();
2092498b56bSopenharmony_ci    sort(vec.begin(), vec.end());
2102498b56bSopenharmony_ci    vector<uint16_t> allTypes {0, 1, 3, 4, 5};
2112498b56bSopenharmony_ci    EXPECT_TRUE(vec == allTypes);
2122498b56bSopenharmony_ci}
2132498b56bSopenharmony_ci
2142498b56bSopenharmony_ci/**
2152498b56bSopenharmony_ci * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_008
2162498b56bSopenharmony_ci * @tc.desc: GetPPidByPid.
2172498b56bSopenharmony_ci * @tc.type: FUNC
2182498b56bSopenharmony_ci */
2192498b56bSopenharmony_ciHWTEST_F(HilogUtilsTest, HilogUtilsTest_008, TestSize.Level1)
2202498b56bSopenharmony_ci{
2212498b56bSopenharmony_ci    GTEST_LOG_(INFO) << "HilogUtilsTest_008: start.";
2222498b56bSopenharmony_ci    uint32_t pid = stoi(GetCmdResultFromPopen("pidof hilogd"));
2232498b56bSopenharmony_ci    EXPECT_EQ(GetPPidByPid(pid), 1);
2242498b56bSopenharmony_ci
2252498b56bSopenharmony_ci    uint32_t invalidPid = 999999;
2262498b56bSopenharmony_ci    EXPECT_EQ(GetPPidByPid(invalidPid), 0);
2272498b56bSopenharmony_ci}
2282498b56bSopenharmony_ci
2292498b56bSopenharmony_ci/**
2302498b56bSopenharmony_ci * @tc.name: Dfx_HilogUtilsTest_HilogUtilsTest_009
2312498b56bSopenharmony_ci * @tc.desc: WaitingToDo.
2322498b56bSopenharmony_ci * @tc.type: FUNC
2332498b56bSopenharmony_ci */
2342498b56bSopenharmony_ciHWTEST_F(HilogUtilsTest, HilogUtilsTest_009, TestSize.Level1)
2352498b56bSopenharmony_ci{
2362498b56bSopenharmony_ci    GTEST_LOG_(INFO) << "HilogUtilsTest_009: start.";
2372498b56bSopenharmony_ci    int ret = WaitingToDo(WAITING_DATA_MS, "/data/log", [](const string &path) {
2382498b56bSopenharmony_ci        if (!access(path.c_str(), F_OK)) {
2392498b56bSopenharmony_ci            return RET_SUCCESS;
2402498b56bSopenharmony_ci        }
2412498b56bSopenharmony_ci        return RET_FAIL;
2422498b56bSopenharmony_ci    });
2432498b56bSopenharmony_ci    EXPECT_EQ(ret, RET_SUCCESS);
2442498b56bSopenharmony_ci
2452498b56bSopenharmony_ci    ret = WaitingToDo(WAITING_DATA_MS, "/test/ttt", [](const string &path) {
2462498b56bSopenharmony_ci        if (!access(path.c_str(), F_OK)) {
2472498b56bSopenharmony_ci            return RET_SUCCESS;
2482498b56bSopenharmony_ci        }
2492498b56bSopenharmony_ci        return RET_FAIL;
2502498b56bSopenharmony_ci    });
2512498b56bSopenharmony_ci    PrintErrorno(errno);
2522498b56bSopenharmony_ci    EXPECT_EQ(ret, RET_FAIL);
2532498b56bSopenharmony_ci}
2542498b56bSopenharmony_ci} // namespace
255