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 "hilogtool_test.h" 162498b56bSopenharmony_ci#include "hilog/log_c.h" 172498b56bSopenharmony_ci#include <dirent.h> 182498b56bSopenharmony_ci#include <log_utils.h> 192498b56bSopenharmony_ci#include <properties.h> 202498b56bSopenharmony_ci#include <hilog_common.h> 212498b56bSopenharmony_ci#include <list> 222498b56bSopenharmony_ci#include <regex> 232498b56bSopenharmony_ci 242498b56bSopenharmony_ciusing namespace std; 252498b56bSopenharmony_ciusing namespace testing::ext; 262498b56bSopenharmony_ciusing namespace OHOS; 272498b56bSopenharmony_ciusing namespace OHOS::HiviewDFX; 282498b56bSopenharmony_ci 292498b56bSopenharmony_cistatic int GetCmdLinesFromPopen(const std::string& cmd) 302498b56bSopenharmony_ci{ 312498b56bSopenharmony_ci if (cmd.empty()) { 322498b56bSopenharmony_ci return 0; 332498b56bSopenharmony_ci } 342498b56bSopenharmony_ci FILE* fp = popen(cmd.c_str(), "r"); 352498b56bSopenharmony_ci if (fp == nullptr) { 362498b56bSopenharmony_ci return 0; 372498b56bSopenharmony_ci } 382498b56bSopenharmony_ci int ret = 0; 392498b56bSopenharmony_ci char* buffer = nullptr; 402498b56bSopenharmony_ci size_t len = 0; 412498b56bSopenharmony_ci while (getline(&buffer, &len, fp) != -1) { 422498b56bSopenharmony_ci ret++; 432498b56bSopenharmony_ci } 442498b56bSopenharmony_ci if (buffer != nullptr) { 452498b56bSopenharmony_ci free(buffer); 462498b56bSopenharmony_ci buffer = nullptr; 472498b56bSopenharmony_ci } 482498b56bSopenharmony_ci pclose(fp); 492498b56bSopenharmony_ci return ret; 502498b56bSopenharmony_ci} 512498b56bSopenharmony_ci 522498b56bSopenharmony_cistatic std::string GetCmdResultFromPopen(const std::string& cmd) 532498b56bSopenharmony_ci{ 542498b56bSopenharmony_ci if (cmd.empty()) { 552498b56bSopenharmony_ci return ""; 562498b56bSopenharmony_ci } 572498b56bSopenharmony_ci FILE* fp = popen(cmd.c_str(), "r"); 582498b56bSopenharmony_ci if (fp == nullptr) { 592498b56bSopenharmony_ci return ""; 602498b56bSopenharmony_ci } 612498b56bSopenharmony_ci std::string ret = ""; 622498b56bSopenharmony_ci char* buffer = nullptr; 632498b56bSopenharmony_ci size_t len = 0; 642498b56bSopenharmony_ci while (getline(&buffer, &len, fp) != -1) { 652498b56bSopenharmony_ci std::string line = buffer; 662498b56bSopenharmony_ci ret += line; 672498b56bSopenharmony_ci } 682498b56bSopenharmony_ci if (buffer != nullptr) { 692498b56bSopenharmony_ci free(buffer); 702498b56bSopenharmony_ci buffer = nullptr; 712498b56bSopenharmony_ci } 722498b56bSopenharmony_ci pclose(fp); 732498b56bSopenharmony_ci return ret; 742498b56bSopenharmony_ci} 752498b56bSopenharmony_ci 762498b56bSopenharmony_cistatic bool IsExistInCmdResult(const std::string &cmd, const std::string &str) 772498b56bSopenharmony_ci{ 782498b56bSopenharmony_ci if (cmd.empty()) { 792498b56bSopenharmony_ci return false; 802498b56bSopenharmony_ci } 812498b56bSopenharmony_ci FILE* fp = popen(cmd.c_str(), "r"); 822498b56bSopenharmony_ci if (fp == nullptr) { 832498b56bSopenharmony_ci return false; 842498b56bSopenharmony_ci } 852498b56bSopenharmony_ci bool ret = false; 862498b56bSopenharmony_ci char* buffer = nullptr; 872498b56bSopenharmony_ci size_t len = 0; 882498b56bSopenharmony_ci while (getline(&buffer, &len, fp) != -1) { 892498b56bSopenharmony_ci std::string line = buffer; 902498b56bSopenharmony_ci if (line.find(str) != string::npos) { 912498b56bSopenharmony_ci ret = true; 922498b56bSopenharmony_ci break; 932498b56bSopenharmony_ci } 942498b56bSopenharmony_ci } 952498b56bSopenharmony_ci if (buffer != nullptr) { 962498b56bSopenharmony_ci free(buffer); 972498b56bSopenharmony_ci buffer = nullptr; 982498b56bSopenharmony_ci } 992498b56bSopenharmony_ci pclose(fp); 1002498b56bSopenharmony_ci return ret; 1012498b56bSopenharmony_ci} 1022498b56bSopenharmony_ci 1032498b56bSopenharmony_civoid HilogToolTest::TearDownTestCase() 1042498b56bSopenharmony_ci{ 1052498b56bSopenharmony_ci (void)GetCmdResultFromPopen("hilog -b I"); 1062498b56bSopenharmony_ci (void)GetCmdResultFromPopen("hilog -G 256K"); 1072498b56bSopenharmony_ci (void)GetCmdResultFromPopen("hilog -w stop"); 1082498b56bSopenharmony_ci (void)GetCmdResultFromPopen("hilog -w start"); 1092498b56bSopenharmony_ci} 1102498b56bSopenharmony_ci 1112498b56bSopenharmony_cinamespace { 1122498b56bSopenharmony_ciconst std::list<pair<string, string>> helperList = { 1132498b56bSopenharmony_ci /* help cmd suffix, information key word */ 1142498b56bSopenharmony_ci {"", "Usage"}, 1152498b56bSopenharmony_ci {"query", "Query"}, 1162498b56bSopenharmony_ci {"clear", "Remove"}, 1172498b56bSopenharmony_ci {"buffer", "buffer"}, 1182498b56bSopenharmony_ci {"stats", "statistics"}, 1192498b56bSopenharmony_ci {"persist", "persistance"}, 1202498b56bSopenharmony_ci {"private", "privacy"}, 1212498b56bSopenharmony_ci {"kmsg", "kmsg"}, 1222498b56bSopenharmony_ci {"flowcontrol", "flow-control"}, 1232498b56bSopenharmony_ci {"baselevel", "baselevel"}, 1242498b56bSopenharmony_ci {"combo", "combination"}, 1252498b56bSopenharmony_ci {"domain", "domain"}, 1262498b56bSopenharmony_ci}; 1272498b56bSopenharmony_ci 1282498b56bSopenharmony_ci/** 1292498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HelperTest_001 1302498b56bSopenharmony_ci * @tc.desc: hilog help information. 1312498b56bSopenharmony_ci * @tc.type: FUNC 1322498b56bSopenharmony_ci */ 1332498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HelperTest_001, TestSize.Level1) 1342498b56bSopenharmony_ci{ 1352498b56bSopenharmony_ci /** 1362498b56bSopenharmony_ci * @tc.steps: step1. show hilog help information. 1372498b56bSopenharmony_ci * @tc.steps: step2. invalid cmd. 1382498b56bSopenharmony_ci */ 1392498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HelperTest_001: start."; 1402498b56bSopenharmony_ci std::string prefix = "hilog -h "; 1412498b56bSopenharmony_ci std::string cmd = ""; 1422498b56bSopenharmony_ci for (auto &it : helperList) { 1432498b56bSopenharmony_ci cmd = prefix + it.first; 1442498b56bSopenharmony_ci EXPECT_TRUE(IsExistInCmdResult(cmd, it.second)); 1452498b56bSopenharmony_ci } 1462498b56bSopenharmony_ci 1472498b56bSopenharmony_ci prefix = "hilog --help "; 1482498b56bSopenharmony_ci for (auto &it : helperList) { 1492498b56bSopenharmony_ci cmd = prefix + it.first; 1502498b56bSopenharmony_ci EXPECT_TRUE(IsExistInCmdResult(cmd, it.second)); 1512498b56bSopenharmony_ci } 1522498b56bSopenharmony_ci} 1532498b56bSopenharmony_ci 1542498b56bSopenharmony_ci/** 1552498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_001 1562498b56bSopenharmony_ci * @tc.desc: BaseLogLevelHandler. 1572498b56bSopenharmony_ci * @tc.type: FUNC 1582498b56bSopenharmony_ci */ 1592498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_001, TestSize.Level1) 1602498b56bSopenharmony_ci{ 1612498b56bSopenharmony_ci /** 1622498b56bSopenharmony_ci * @tc.steps: step1. set global log level to INFO. 1632498b56bSopenharmony_ci * @tc.steps: step2. invalid log level. 1642498b56bSopenharmony_ci */ 1652498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_001: start."; 1662498b56bSopenharmony_ci std::string level = "I"; 1672498b56bSopenharmony_ci std::string cmd = "hilog -b " + level; 1682498b56bSopenharmony_ci std::string str = "Set global log level to " + level + " successfully\n"; 1692498b56bSopenharmony_ci std::string query = "param get hilog.loggable.global"; 1702498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 1712498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), level + " \n"); 1722498b56bSopenharmony_ci 1732498b56bSopenharmony_ci // stderr redirect to stdout 1742498b56bSopenharmony_ci cmd = "hilog -b test_level 2>&1"; 1752498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_LOG_LEVEL_INVALID) + "\n"; 1762498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 1772498b56bSopenharmony_ci} 1782498b56bSopenharmony_ci 1792498b56bSopenharmony_ci/** 1802498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_002 1812498b56bSopenharmony_ci * @tc.desc: DomainHandler. 1822498b56bSopenharmony_ci * @tc.type: FUNC 1832498b56bSopenharmony_ci */ 1842498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_002, TestSize.Level1) 1852498b56bSopenharmony_ci{ 1862498b56bSopenharmony_ci /** 1872498b56bSopenharmony_ci * @tc.steps: step1. set domain xxx log level to INFO. 1882498b56bSopenharmony_ci * @tc.steps: step2. invaild domain. 1892498b56bSopenharmony_ci */ 1902498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_002: start."; 1912498b56bSopenharmony_ci uint32_t domain = 0xd002d00; 1922498b56bSopenharmony_ci std::string level = "I"; 1932498b56bSopenharmony_ci std::string cmd = "hilog -b " + level + " -D " + Uint2HexStr(domain); 1942498b56bSopenharmony_ci std::string str = "Set domain 0x" + Uint2HexStr(domain) + " log level to " + level + " successfully\n"; 1952498b56bSopenharmony_ci std::string query = "param get hilog.loggable.domain." + Uint2HexStr(domain); 1962498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 1972498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), level + " \n"); 1982498b56bSopenharmony_ci 1992498b56bSopenharmony_ci cmd = "hilog -D test_domain 2>&1"; 2002498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_INVALID_DOMAIN_STR) + "\n"; 2012498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 2022498b56bSopenharmony_ci} 2032498b56bSopenharmony_ci 2042498b56bSopenharmony_ci/** 2052498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_003 2062498b56bSopenharmony_ci * @tc.desc: TagHandler. 2072498b56bSopenharmony_ci * @tc.type: FUNC 2082498b56bSopenharmony_ci */ 2092498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_003, TestSize.Level1) 2102498b56bSopenharmony_ci{ 2112498b56bSopenharmony_ci /** 2122498b56bSopenharmony_ci * @tc.steps: step1. set tag xxx log level to INFO. 2132498b56bSopenharmony_ci * @tc.steps: step2. invalid tag. 2142498b56bSopenharmony_ci */ 2152498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_003: start."; 2162498b56bSopenharmony_ci std::string tag = "test"; 2172498b56bSopenharmony_ci std::string level = "I"; 2182498b56bSopenharmony_ci std::string cmd = "hilog -b " + level + " -T " + tag; 2192498b56bSopenharmony_ci std::string str = "Set tag " + tag + " log level to " + level + " successfully\n"; 2202498b56bSopenharmony_ci std::string query = "param get hilog.loggable.tag." + tag; 2212498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 2222498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), level + " \n"); 2232498b56bSopenharmony_ci 2242498b56bSopenharmony_ci cmd = "hilog -T abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 2>&1"; 2252498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_TAG_STR_TOO_LONG) + "\n"; 2262498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 2272498b56bSopenharmony_ci} 2282498b56bSopenharmony_ci 2292498b56bSopenharmony_ci/** 2302498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_004 2312498b56bSopenharmony_ci * @tc.desc: BufferSizeSetHandler. 2322498b56bSopenharmony_ci * @tc.type: FUNC 2332498b56bSopenharmony_ci */ 2342498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_004, TestSize.Level1) 2352498b56bSopenharmony_ci{ 2362498b56bSopenharmony_ci /** 2372498b56bSopenharmony_ci * @tc.steps: step1. set app,init.core buffer size [valid]. 2382498b56bSopenharmony_ci * @tc.expected: step1. set app,init.core buffer size successfully. 2392498b56bSopenharmony_ci * @tc.steps: step2. set app,init.core buffer size [invalid]. 2402498b56bSopenharmony_ci * @tc.expected: step2 set app,init.core buffer size failed. 2412498b56bSopenharmony_ci * buffer size should be in range [64.0K, 512.0M]. 2422498b56bSopenharmony_ci * @tc.expected: step3 invalid buffer size str. 2432498b56bSopenharmony_ci */ 2442498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_004: start."; 2452498b56bSopenharmony_ci std::string cmd = "hilog -G 512K"; 2462498b56bSopenharmony_ci std::string str = "Set log type app buffer size to 512.0K successfully\n" 2472498b56bSopenharmony_ci "Set log type init buffer size to 512.0K successfully\n" 2482498b56bSopenharmony_ci "Set log type core buffer size to 512.0K successfully\n" 2492498b56bSopenharmony_ci "Set log type only_prerelease buffer size to 512.0K successfully\n"; 2502498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 2512498b56bSopenharmony_ci 2522498b56bSopenharmony_ci cmd = "hilog -G 512G"; 2532498b56bSopenharmony_ci str = "failed"; 2542498b56bSopenharmony_ci EXPECT_TRUE(IsExistInCmdResult(cmd, str)); 2552498b56bSopenharmony_ci 2562498b56bSopenharmony_ci std::string inValidStrCmd = "hilog -G test_buffersize 2>&1"; 2572498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_INVALID_SIZE_STR) + "\n"; 2582498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(inValidStrCmd), errMsg); 2592498b56bSopenharmony_ci} 2602498b56bSopenharmony_ci 2612498b56bSopenharmony_ci/** 2622498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_005 2632498b56bSopenharmony_ci * @tc.desc: BufferSizeGetHandler. 2642498b56bSopenharmony_ci * @tc.type: FUNC 2652498b56bSopenharmony_ci */ 2662498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_005, TestSize.Level1) 2672498b56bSopenharmony_ci{ 2682498b56bSopenharmony_ci /** 2692498b56bSopenharmony_ci * @tc.steps: step1. get app,init.core valid buffer size. 2702498b56bSopenharmony_ci */ 2712498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_005: start."; 2722498b56bSopenharmony_ci std::string cmd = "hilog -g"; 2732498b56bSopenharmony_ci std::string str = "Log type app buffer size is 512.0K\n" 2742498b56bSopenharmony_ci "Log type init buffer size is 512.0K\n" 2752498b56bSopenharmony_ci "Log type core buffer size is 512.0K\n" 2762498b56bSopenharmony_ci "Log type only_prerelease buffer size is 512.0K\n"; 2772498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 2782498b56bSopenharmony_ci} 2792498b56bSopenharmony_ci 2802498b56bSopenharmony_ci/** 2812498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_006 2822498b56bSopenharmony_ci * @tc.desc: KmsgFeatureSetHandler. 2832498b56bSopenharmony_ci * @tc.type: FUNC 2842498b56bSopenharmony_ci */ 2852498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_006, TestSize.Level1) 2862498b56bSopenharmony_ci{ 2872498b56bSopenharmony_ci /** 2882498b56bSopenharmony_ci * @tc.steps: step1. set hilogd storing kmsg log feature on. 2892498b56bSopenharmony_ci * @tc.steps: step2. set hilogd storing kmsg log feature off. 2902498b56bSopenharmony_ci * @tc.steps: step3. set hilogd storing kmsg log feature invalid. 2912498b56bSopenharmony_ci */ 2922498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_006: start."; 2932498b56bSopenharmony_ci std::string cmd = "hilog -k on"; 2942498b56bSopenharmony_ci std::string str = "Set hilogd storing kmsg log on successfully\n"; 2952498b56bSopenharmony_ci std::string query = "param get persist.sys.hilog.kmsg.on"; 2962498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 2972498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), "true \n"); 2982498b56bSopenharmony_ci 2992498b56bSopenharmony_ci cmd = "hilog -k off"; 3002498b56bSopenharmony_ci str = "Set hilogd storing kmsg log off successfully\n"; 3012498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 3022498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), "false \n"); 3032498b56bSopenharmony_ci 3042498b56bSopenharmony_ci cmd = "hilog -k test_feature 2>&1"; 3052498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_INVALID_ARGUMENT) + "\n"; 3062498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 3072498b56bSopenharmony_ci} 3082498b56bSopenharmony_ci 3092498b56bSopenharmony_ci/** 3102498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_007 3112498b56bSopenharmony_ci * @tc.desc: PrivateFeatureSetHandler. 3122498b56bSopenharmony_ci * @tc.type: FUNC 3132498b56bSopenharmony_ci */ 3142498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_007, TestSize.Level1) 3152498b56bSopenharmony_ci{ 3162498b56bSopenharmony_ci /** 3172498b56bSopenharmony_ci * @tc.steps: step1. set hilog api privacy formatter feature on. 3182498b56bSopenharmony_ci * @tc.steps: step2. set hilog api privacy formatter feature off. 3192498b56bSopenharmony_ci * @tc.steps: step3. set hilog api privacy formatter feature invalid. 3202498b56bSopenharmony_ci */ 3212498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_007: start."; 3222498b56bSopenharmony_ci std::string cmd = "hilog -p on"; 3232498b56bSopenharmony_ci std::string str = "Set hilog privacy format on successfully\n"; 3242498b56bSopenharmony_ci std::string query = "param get hilog.private.on"; 3252498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 3262498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), "true \n"); 3272498b56bSopenharmony_ci 3282498b56bSopenharmony_ci cmd = "hilog -p off"; 3292498b56bSopenharmony_ci str = "Set hilog privacy format off successfully\n"; 3302498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 3312498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), "false \n"); 3322498b56bSopenharmony_ci 3332498b56bSopenharmony_ci cmd = "hilog -p test_feature 2>&1"; 3342498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_INVALID_ARGUMENT) + "\n"; 3352498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 3362498b56bSopenharmony_ci} 3372498b56bSopenharmony_ci 3382498b56bSopenharmony_ci/** 3392498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_008 3402498b56bSopenharmony_ci * @tc.desc: FlowControlFeatureSetHandler. 3412498b56bSopenharmony_ci * @tc.type: FUNC 3422498b56bSopenharmony_ci */ 3432498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_008, TestSize.Level1) 3442498b56bSopenharmony_ci{ 3452498b56bSopenharmony_ci /** 3462498b56bSopenharmony_ci * @tc.steps: step1. set process flow control on. 3472498b56bSopenharmony_ci * @tc.steps: step2. set process flow control off. 3482498b56bSopenharmony_ci * @tc.steps: step3. set domain flow control on. 3492498b56bSopenharmony_ci * @tc.steps: step4. set domain flow control off. 3502498b56bSopenharmony_ci * @tc.steps: step5. invalid cmd. 3512498b56bSopenharmony_ci */ 3522498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_008: start."; 3532498b56bSopenharmony_ci std::string cmd = "hilog -Q pidon"; 3542498b56bSopenharmony_ci std::string str = "Set flow control by process to enabled, result: Success [CODE: 0]\n"; 3552498b56bSopenharmony_ci std::string query = "param get hilog.flowctrl.proc.on"; 3562498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 3572498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), "true \n"); 3582498b56bSopenharmony_ci 3592498b56bSopenharmony_ci cmd = "hilog -Q pidoff"; 3602498b56bSopenharmony_ci str = "Set flow control by process to disabled, result: Success [CODE: 0]\n"; 3612498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 3622498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), "false \n"); 3632498b56bSopenharmony_ci 3642498b56bSopenharmony_ci cmd = "hilog -Q domainon"; 3652498b56bSopenharmony_ci str = "Set flow control by domain to enabled, result: Success [CODE: 0]\n"; 3662498b56bSopenharmony_ci query = "param get hilog.flowctrl.domain.on"; 3672498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 3682498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), "true \n"); 3692498b56bSopenharmony_ci 3702498b56bSopenharmony_ci cmd = "hilog -Q domainoff"; 3712498b56bSopenharmony_ci str = "Set flow control by domain to disabled, result: Success [CODE: 0]\n"; 3722498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 3732498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(query), "false \n"); 3742498b56bSopenharmony_ci 3752498b56bSopenharmony_ci cmd = "hilog -Q test_cmd 2>&1"; 3762498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_INVALID_ARGUMENT) + "\n"; 3772498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 3782498b56bSopenharmony_ci} 3792498b56bSopenharmony_ci 3802498b56bSopenharmony_ci/** 3812498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_009 3822498b56bSopenharmony_ci * @tc.desc: HeadHandler & TailHandler. 3832498b56bSopenharmony_ci * @tc.type: FUNC 3842498b56bSopenharmony_ci */ 3852498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_009, TestSize.Level1) 3862498b56bSopenharmony_ci{ 3872498b56bSopenharmony_ci /** 3882498b56bSopenharmony_ci * @tc.steps: step1. show n lines logs on head of buffer. 3892498b56bSopenharmony_ci * @tc.steps: step2. show n lines logs on tail of buffer. 3902498b56bSopenharmony_ci * @tc.steps: step3. invalid cmd. 3912498b56bSopenharmony_ci */ 3922498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_009: start."; 3932498b56bSopenharmony_ci int lines = 5; 3942498b56bSopenharmony_ci std::string cmd = "hilog -a " + std::to_string(lines); 3952498b56bSopenharmony_ci EXPECT_EQ(GetCmdLinesFromPopen(cmd), lines); 3962498b56bSopenharmony_ci 3972498b56bSopenharmony_ci cmd = "hilog -z " + std::to_string(lines); 3982498b56bSopenharmony_ci EXPECT_EQ(GetCmdLinesFromPopen(cmd), lines); 3992498b56bSopenharmony_ci 4002498b56bSopenharmony_ci cmd = "hilog -a test 2>&1"; 4012498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_NOT_NUMBER_STR) + "\n"; 4022498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 4032498b56bSopenharmony_ci 4042498b56bSopenharmony_ci cmd = "hilog -z test 2>&1"; 4052498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 4062498b56bSopenharmony_ci 4072498b56bSopenharmony_ci cmd = "hilog -a 10 -z 10 2>&1"; 4082498b56bSopenharmony_ci errMsg = ErrorCode2Str(ERR_COMMAND_INVALID) + "\n"; 4092498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 4102498b56bSopenharmony_ci} 4112498b56bSopenharmony_ci 4122498b56bSopenharmony_ci/** 4132498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_010 4142498b56bSopenharmony_ci * @tc.desc: RemoveHandler. 4152498b56bSopenharmony_ci * @tc.type: FUNC 4162498b56bSopenharmony_ci */ 4172498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_010, TestSize.Level1) 4182498b56bSopenharmony_ci{ 4192498b56bSopenharmony_ci /** 4202498b56bSopenharmony_ci * @tc.steps: step1. get the localtime. 4212498b56bSopenharmony_ci * @tc.steps: step2. remove all logs in hilogd buffer. 4222498b56bSopenharmony_ci * @tc.steps: step3. compare the logtime to localtime. 4232498b56bSopenharmony_ci */ 4242498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_010: start."; 4252498b56bSopenharmony_ci time_t tnow = time(nullptr); 4262498b56bSopenharmony_ci struct tm *tmNow = localtime(&tnow); 4272498b56bSopenharmony_ci char clearTime[32] = {0}; 4282498b56bSopenharmony_ci if (tmNow != nullptr) { 4292498b56bSopenharmony_ci strftime(clearTime, sizeof(clearTime), "%m-%d %H:%M:%S.%s", tmNow); 4302498b56bSopenharmony_ci } 4312498b56bSopenharmony_ci std::string cmd = "hilog -r"; 4322498b56bSopenharmony_ci std::string str = "Log type core,app,only_prerelease buffer clear successfully\n"; 4332498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 4342498b56bSopenharmony_ci 4352498b56bSopenharmony_ci sleep(1); 4362498b56bSopenharmony_ci std::string res = GetCmdResultFromPopen("hilog -a 5"); 4372498b56bSopenharmony_ci std::string initStr = "HiLog: ========Zeroth log of type: init"; 4382498b56bSopenharmony_ci vector<string> vec; 4392498b56bSopenharmony_ci std::string logTime = ""; 4402498b56bSopenharmony_ci Split(res, vec, "\n"); 4412498b56bSopenharmony_ci for (auto& it : vec) { 4422498b56bSopenharmony_ci if (it.find(initStr) == string::npos) { 4432498b56bSopenharmony_ci logTime = it.substr(0, 18); 4442498b56bSopenharmony_ci EXPECT_LT(clearTime, logTime); 4452498b56bSopenharmony_ci } 4462498b56bSopenharmony_ci } 4472498b56bSopenharmony_ci} 4482498b56bSopenharmony_ci 4492498b56bSopenharmony_ci/** 4502498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_011 4512498b56bSopenharmony_ci * @tc.desc: TypeHandler. 4522498b56bSopenharmony_ci * @tc.type: FUNC 4532498b56bSopenharmony_ci */ 4542498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_011, TestSize.Level1) 4552498b56bSopenharmony_ci{ 4562498b56bSopenharmony_ci /** 4572498b56bSopenharmony_ci * @tc.steps: step1. remove app logs in hilogd buffer. 4582498b56bSopenharmony_ci * @tc.steps: step2. remove core logs in hilogd buffer. 4592498b56bSopenharmony_ci * @tc.steps: step3. invalid log type. 4602498b56bSopenharmony_ci */ 4612498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_011: start."; 4622498b56bSopenharmony_ci std::string cmd = "hilog -r -t app"; 4632498b56bSopenharmony_ci std::string str = "Log type app buffer clear successfully\n"; 4642498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 4652498b56bSopenharmony_ci 4662498b56bSopenharmony_ci cmd = "hilog -r -t core"; 4672498b56bSopenharmony_ci str = "Log type core buffer clear successfully\n"; 4682498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 4692498b56bSopenharmony_ci 4702498b56bSopenharmony_ci cmd = "hilog -r -t test_type 2>&1"; 4712498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_LOG_TYPE_INVALID) + "\n"; 4722498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 4732498b56bSopenharmony_ci} 4742498b56bSopenharmony_ci 4752498b56bSopenharmony_ci/** 4762498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_012 4772498b56bSopenharmony_ci * @tc.desc: PersistTaskHandler FileNameHandler JobIdHandler FileLengthHandler FileNumberHandler. 4782498b56bSopenharmony_ci * @tc.type: FUNC 4792498b56bSopenharmony_ci */ 4802498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_012, TestSize.Level1) 4812498b56bSopenharmony_ci{ 4822498b56bSopenharmony_ci /** 4832498b56bSopenharmony_ci * @tc.steps: step1. start hilog persistance task control. 4842498b56bSopenharmony_ci * @tc.steps: step2. stop hilog persistance task control. 4852498b56bSopenharmony_ci * @tc.steps: step3. start hilog persistance task control with advanced options. 4862498b56bSopenharmony_ci * @tc.steps: step4. query tasks informations. 4872498b56bSopenharmony_ci * @tc.steps: step5. invalid persistance cmd. 4882498b56bSopenharmony_ci * @tc.steps: step6. query invalid filename. 4892498b56bSopenharmony_ci * @tc.steps: step7. query invalid jobid. 4902498b56bSopenharmony_ci * @tc.steps: step8. query invalid filelength. 4912498b56bSopenharmony_ci * @tc.steps: step9. query invalid filenumber. 4922498b56bSopenharmony_ci */ 4932498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_012: start."; 4942498b56bSopenharmony_ci (void)GetCmdResultFromPopen("hilog -w stop"); 4952498b56bSopenharmony_ci std::string cmd = "hilog -w start"; 4962498b56bSopenharmony_ci std::string str = "Persist task [jobid:1] start successfully\n"; 4972498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 4982498b56bSopenharmony_ci 4992498b56bSopenharmony_ci cmd = "hilog -w stop"; 5002498b56bSopenharmony_ci str = "Persist task [jobid:1] stop successfully\n"; 5012498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 5022498b56bSopenharmony_ci 5032498b56bSopenharmony_ci std::string filename = "test"; 5042498b56bSopenharmony_ci uint64_t length = 2 * 1024 * 1024; 5052498b56bSopenharmony_ci std::string unit = "B"; 5062498b56bSopenharmony_ci std::string compress = "zlib"; 5072498b56bSopenharmony_ci int num = 25; 5082498b56bSopenharmony_ci int jobid = 200; 5092498b56bSopenharmony_ci cmd = "hilog -w start -f " + filename + " -l " + std::to_string(length) + unit 5102498b56bSopenharmony_ci + " -n " + std::to_string(num) + " -m " + compress + " -j " + std::to_string(jobid); 5112498b56bSopenharmony_ci str = "Persist task [jobid:" + std::to_string(jobid) + "] start successfully\n"; 5122498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 5132498b56bSopenharmony_ci 5142498b56bSopenharmony_ci cmd = "hilog -w query"; 5152498b56bSopenharmony_ci str = std::to_string(jobid) + " init,core,app,only_prerelease " + compress + " /data/log/hilog/" + filename 5162498b56bSopenharmony_ci + " " + Size2Str(length) + " " + std::to_string(num) + "\n"; 5172498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 5182498b56bSopenharmony_ci 5192498b56bSopenharmony_ci cmd = "hilog -w test 2>&1"; 5202498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_INVALID_ARGUMENT) + "\n"; 5212498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 5222498b56bSopenharmony_ci 5232498b56bSopenharmony_ci filename = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 5242498b56bSopenharmony_ci "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 5252498b56bSopenharmony_ci cmd = "hilog -w query -f " + filename + " 2>&1"; 5262498b56bSopenharmony_ci errMsg = ErrorCode2Str(ERR_FILE_NAME_TOO_LONG) + "\n"; 5272498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 5282498b56bSopenharmony_ci 5292498b56bSopenharmony_ci cmd = "hilog -w query -j test 2>&1"; 5302498b56bSopenharmony_ci errMsg = ErrorCode2Str(ERR_NOT_NUMBER_STR) + "\n"; 5312498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 5322498b56bSopenharmony_ci 5332498b56bSopenharmony_ci cmd = "hilog -w query -l test 2>&1"; 5342498b56bSopenharmony_ci errMsg = ErrorCode2Str(ERR_INVALID_SIZE_STR) + "\n"; 5352498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 5362498b56bSopenharmony_ci 5372498b56bSopenharmony_ci cmd = "hilog -w query -n test 2>&1"; 5382498b56bSopenharmony_ci errMsg = ErrorCode2Str(ERR_NOT_NUMBER_STR) + "\n"; 5392498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 5402498b56bSopenharmony_ci} 5412498b56bSopenharmony_ci 5422498b56bSopenharmony_ci/** 5432498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_013 5442498b56bSopenharmony_ci * @tc.desc: RegexHandler. 5452498b56bSopenharmony_ci * @tc.type: FUNC 5462498b56bSopenharmony_ci */ 5472498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_013, TestSize.Level1) 5482498b56bSopenharmony_ci{ 5492498b56bSopenharmony_ci /** 5502498b56bSopenharmony_ci * @tc.steps: step1. show the logs which match the regular expression. 5512498b56bSopenharmony_ci * @tc.steps: step2. invaild regex. 5522498b56bSopenharmony_ci */ 5532498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_013: start."; 5542498b56bSopenharmony_ci std::string cmd = "hilog -x -e "; 5552498b56bSopenharmony_ci std::string regex = "service"; 5562498b56bSopenharmony_ci std::string res = GetCmdResultFromPopen(cmd + regex); 5572498b56bSopenharmony_ci if (res != "") { 5582498b56bSopenharmony_ci vector<string> vec; 5592498b56bSopenharmony_ci Split(res, vec, "\n"); 5602498b56bSopenharmony_ci for (auto& it : vec) { 5612498b56bSopenharmony_ci EXPECT_TRUE(it.find(regex) != string::npos); 5622498b56bSopenharmony_ci } 5632498b56bSopenharmony_ci } 5642498b56bSopenharmony_ci 5652498b56bSopenharmony_ci cmd = "hilog -x -e abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 5662498b56bSopenharmony_ci "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 5672498b56bSopenharmony_ci "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 2>&1"; 5682498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_REGEX_STR_TOO_LONG) + "\n"; 5692498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 5702498b56bSopenharmony_ci} 5712498b56bSopenharmony_ci 5722498b56bSopenharmony_ci/** 5732498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_013 5742498b56bSopenharmony_ci * @tc.desc: LevelHandler. 5752498b56bSopenharmony_ci * @tc.type: FUNC 5762498b56bSopenharmony_ci */ 5772498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_014, TestSize.Level1) 5782498b56bSopenharmony_ci{ 5792498b56bSopenharmony_ci /** 5802498b56bSopenharmony_ci * @tc.steps: step1. filter log level. 5812498b56bSopenharmony_ci * @tc.steps: step2. invaild log level usage. 5822498b56bSopenharmony_ci */ 5832498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_014: start."; 5842498b56bSopenharmony_ci std::string cmd = "hilog -a 10 -L "; 5852498b56bSopenharmony_ci std::string level = "I"; 5862498b56bSopenharmony_ci std::string res = GetCmdResultFromPopen(cmd + level); 5872498b56bSopenharmony_ci vector<string> vec; 5882498b56bSopenharmony_ci Split(res, vec, "\n"); 5892498b56bSopenharmony_ci for (auto& it : vec) { 5902498b56bSopenharmony_ci std::string logLevel = it.substr(31, 1); 5912498b56bSopenharmony_ci EXPECT_EQ(logLevel, level); 5922498b56bSopenharmony_ci } 5932498b56bSopenharmony_ci 5942498b56bSopenharmony_ci cmd = "hilog -L test_level 2>&1"; 5952498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_LOG_LEVEL_INVALID) + "\n"; 5962498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 5972498b56bSopenharmony_ci 5982498b56bSopenharmony_ci cmd = "hilog -L E F 2>&1"; 5992498b56bSopenharmony_ci errMsg = ErrorCode2Str(ERR_TOO_MANY_ARGUMENTS) + "\n"; 6002498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 6012498b56bSopenharmony_ci 6022498b56bSopenharmony_ci cmd = "hilog -L E -L F 2>&1"; 6032498b56bSopenharmony_ci errMsg = ErrorCode2Str(ERR_DUPLICATE_OPTION) + "\n"; 6042498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 6052498b56bSopenharmony_ci} 6062498b56bSopenharmony_ci 6072498b56bSopenharmony_ci/** 6082498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_013 6092498b56bSopenharmony_ci * @tc.desc: PidHandler. 6102498b56bSopenharmony_ci * @tc.type: FUNC 6112498b56bSopenharmony_ci */ 6122498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_015, TestSize.Level1) 6132498b56bSopenharmony_ci{ 6142498b56bSopenharmony_ci /** 6152498b56bSopenharmony_ci * @tc.steps: step1. filter PID. 6162498b56bSopenharmony_ci */ 6172498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_015: start."; 6182498b56bSopenharmony_ci std::string pid = GetCmdResultFromPopen("hilog -z 1").substr(19, 5); 6192498b56bSopenharmony_ci std::string cmd = "hilog -a 10 -P "; 6202498b56bSopenharmony_ci std::string res = GetCmdResultFromPopen(cmd + pid); 6212498b56bSopenharmony_ci if (res != "") { 6222498b56bSopenharmony_ci vector<string> vec; 6232498b56bSopenharmony_ci Split(res, vec, "\n"); 6242498b56bSopenharmony_ci for (auto& it : vec) { 6252498b56bSopenharmony_ci std::string logPid = it.substr(19, 5); 6262498b56bSopenharmony_ci EXPECT_EQ(logPid, pid); 6272498b56bSopenharmony_ci } 6282498b56bSopenharmony_ci } 6292498b56bSopenharmony_ci 6302498b56bSopenharmony_ci cmd = "hilog -P test 2>&1"; 6312498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_NOT_NUMBER_STR) + "\n"; 6322498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 6332498b56bSopenharmony_ci} 6342498b56bSopenharmony_ci 6352498b56bSopenharmony_ci/** 6362498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_016 6372498b56bSopenharmony_ci * @tc.desc: StatsInfoQueryHandler. 6382498b56bSopenharmony_ci * @tc.type: FUNC 6392498b56bSopenharmony_ci */ 6402498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_016, TestSize.Level1) 6412498b56bSopenharmony_ci{ 6422498b56bSopenharmony_ci /** 6432498b56bSopenharmony_ci * @tc.steps: step1. set stats property. 6442498b56bSopenharmony_ci * @tc.steps: step2. restart hilog service. 6452498b56bSopenharmony_ci * @tc.steps: step3. show log statistic report. 6462498b56bSopenharmony_ci * @tc.steps: step4. clear hilogd statistic information. 6472498b56bSopenharmony_ci */ 6482498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_016: start."; 6492498b56bSopenharmony_ci (void)GetCmdResultFromPopen("param set persist.sys.hilog.stats false"); 6502498b56bSopenharmony_ci (void)GetCmdResultFromPopen("param set persist.sys.hilog.stats.tag false"); 6512498b56bSopenharmony_ci (void)GetCmdResultFromPopen("service_control stop hilogd"); 6522498b56bSopenharmony_ci (void)GetCmdResultFromPopen("service_control start hilogd"); 6532498b56bSopenharmony_ci sleep(3); 6542498b56bSopenharmony_ci std::string cmd = "hilog -s"; 6552498b56bSopenharmony_ci std::string str = "Statistic info query failed"; 6562498b56bSopenharmony_ci EXPECT_TRUE(IsExistInCmdResult(cmd, str)); 6572498b56bSopenharmony_ci 6582498b56bSopenharmony_ci (void)GetCmdResultFromPopen("param set persist.sys.hilog.stats true"); 6592498b56bSopenharmony_ci (void)GetCmdResultFromPopen("param set persist.sys.hilog.stats.tag true"); 6602498b56bSopenharmony_ci (void)GetCmdResultFromPopen("service_control stop hilogd"); 6612498b56bSopenharmony_ci (void)GetCmdResultFromPopen("service_control start hilogd"); 6622498b56bSopenharmony_ci sleep(10); 6632498b56bSopenharmony_ci str = "report"; 6642498b56bSopenharmony_ci EXPECT_TRUE(IsExistInCmdResult(cmd, str)); 6652498b56bSopenharmony_ci EXPECT_TRUE(IsStatsEnable()); 6662498b56bSopenharmony_ci EXPECT_TRUE(IsTagStatsEnable()); 6672498b56bSopenharmony_ci 6682498b56bSopenharmony_ci cmd = "hilog -S"; 6692498b56bSopenharmony_ci str = "Statistic info clear successfully\n"; 6702498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 6712498b56bSopenharmony_ci} 6722498b56bSopenharmony_ci 6732498b56bSopenharmony_ci/** 6742498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_017 6752498b56bSopenharmony_ci * @tc.desc: FormatHandler. 6762498b56bSopenharmony_ci * @tc.type: FUNC 6772498b56bSopenharmony_ci */ 6782498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_017, TestSize.Level1) 6792498b56bSopenharmony_ci{ 6802498b56bSopenharmony_ci /** 6812498b56bSopenharmony_ci * @tc.steps: step1. log format time. 6822498b56bSopenharmony_ci * @tc.steps: step2. log format epoch. 6832498b56bSopenharmony_ci * @tc.steps: step3. log format monotonic. 6842498b56bSopenharmony_ci * @tc.steps: step4. log format msec. 6852498b56bSopenharmony_ci * @tc.steps: step5. log format usec. 6862498b56bSopenharmony_ci * @tc.steps: step6. log format nsec. 6872498b56bSopenharmony_ci * @tc.steps: step7. log format year. 6882498b56bSopenharmony_ci * @tc.steps: step8. log format zone. 6892498b56bSopenharmony_ci * @tc.steps: step9. invalid log format. 6902498b56bSopenharmony_ci */ 6912498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_017: start."; 6922498b56bSopenharmony_ci std::string cmd = "hilog -v time -z 5"; 6932498b56bSopenharmony_ci std::regex pattern("(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])\\s(0\\d{1}|1\\d{1}|2[0-3])" 6942498b56bSopenharmony_ci ":[0-5]\\d{1}:([0-5]\\d{1})(\\.(\\d){0,3})?$"); 6952498b56bSopenharmony_ci std::string res = GetCmdResultFromPopen(cmd); 6962498b56bSopenharmony_ci vector<string> vec; 6972498b56bSopenharmony_ci Split(res, vec, "\n"); 6982498b56bSopenharmony_ci for (auto& it : vec) { 6992498b56bSopenharmony_ci EXPECT_TRUE(regex_match(it.substr(0, 18), pattern)); 7002498b56bSopenharmony_ci } 7012498b56bSopenharmony_ci 7022498b56bSopenharmony_ci cmd = "hilog -v epoch -z 5"; 7032498b56bSopenharmony_ci pattern = ("\\d{0,10}.\\d{3}$"); 7042498b56bSopenharmony_ci res = GetCmdResultFromPopen(cmd); 7052498b56bSopenharmony_ci Split(res, vec, "\n"); 7062498b56bSopenharmony_ci for (auto& it : vec) { 7072498b56bSopenharmony_ci EXPECT_TRUE(regex_match(it.substr(0, 14), pattern)); 7082498b56bSopenharmony_ci } 7092498b56bSopenharmony_ci 7102498b56bSopenharmony_ci cmd = "hilog -v monotonic -z 5"; 7112498b56bSopenharmony_ci pattern = ("\\d{0,8}.\\d{3}$"); 7122498b56bSopenharmony_ci res = GetCmdResultFromPopen(cmd); 7132498b56bSopenharmony_ci std::string initStr = "HiLog: ========Zeroth log of type"; 7142498b56bSopenharmony_ci Split(res, vec, "\n"); 7152498b56bSopenharmony_ci for (auto& it : vec) { 7162498b56bSopenharmony_ci if (it.find(initStr) == string::npos) { 7172498b56bSopenharmony_ci std::string str = it.substr(0, 12); 7182498b56bSopenharmony_ci // remove the head blank space 7192498b56bSopenharmony_ci str.erase(0, str.find_first_not_of(" ")); 7202498b56bSopenharmony_ci EXPECT_TRUE(regex_match(str, pattern)); 7212498b56bSopenharmony_ci } 7222498b56bSopenharmony_ci } 7232498b56bSopenharmony_ci 7242498b56bSopenharmony_ci cmd = "hilog -v msec -z 5"; 7252498b56bSopenharmony_ci pattern = ("(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])\\s(0\\d{1}|1\\d{1}|2[0-3])" 7262498b56bSopenharmony_ci ":[0-5]\\d{1}:([0-5]\\d{1})(\\.(\\d){0,3})?$"); 7272498b56bSopenharmony_ci res = GetCmdResultFromPopen(cmd); 7282498b56bSopenharmony_ci Split(res, vec, "\n"); 7292498b56bSopenharmony_ci for (auto& it : vec) { 7302498b56bSopenharmony_ci EXPECT_TRUE(regex_match(it.substr(0, 18), pattern)); 7312498b56bSopenharmony_ci } 7322498b56bSopenharmony_ci 7332498b56bSopenharmony_ci cmd = "hilog -v usec -z 5"; 7342498b56bSopenharmony_ci pattern = "(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])\\s(0\\d{1}|1\\d{1}|2[0-3])" 7352498b56bSopenharmony_ci ":[0-5]\\d{1}:([0-5]\\d{1})(\\.(\\d){0,6})?$"; 7362498b56bSopenharmony_ci res = GetCmdResultFromPopen(cmd); 7372498b56bSopenharmony_ci Split(res, vec, "\n"); 7382498b56bSopenharmony_ci for (auto& it : vec) { 7392498b56bSopenharmony_ci EXPECT_TRUE(regex_match(it.substr(0, 21), pattern)); 7402498b56bSopenharmony_ci } 7412498b56bSopenharmony_ci 7422498b56bSopenharmony_ci cmd = "hilog -v nsec -z 5"; 7432498b56bSopenharmony_ci pattern = "(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])\\s(0\\d{1}|1\\d{1}|2[0-3])" 7442498b56bSopenharmony_ci ":[0-5]\\d{1}:([0-5]\\d{1})(\\.(\\d){0,9})?$"; 7452498b56bSopenharmony_ci res = GetCmdResultFromPopen(cmd); 7462498b56bSopenharmony_ci Split(res, vec, "\n"); 7472498b56bSopenharmony_ci for (auto& it : vec) { 7482498b56bSopenharmony_ci EXPECT_TRUE(regex_match(it.substr(0, 24), pattern)); 7492498b56bSopenharmony_ci } 7502498b56bSopenharmony_ci 7512498b56bSopenharmony_ci cmd = "hilog -v year -z 5"; 7522498b56bSopenharmony_ci pattern = "(\\d{4})-(0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])\\s(0\\d{1}|1\\d{1}|2[0-3])" 7532498b56bSopenharmony_ci ":[0-5]\\d{1}:([0-5]\\d{1})(\\.(\\d){0,3})?$"; 7542498b56bSopenharmony_ci res = GetCmdResultFromPopen(cmd); 7552498b56bSopenharmony_ci Split(res, vec, "\n"); 7562498b56bSopenharmony_ci for (auto& it : vec) { 7572498b56bSopenharmony_ci EXPECT_TRUE(regex_match(it.substr(0, 23), pattern)); 7582498b56bSopenharmony_ci } 7592498b56bSopenharmony_ci 7602498b56bSopenharmony_ci cmd = "hilog -v zone -z 5"; 7612498b56bSopenharmony_ci std::regex gmtPattern("GMT (0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])\\s(0\\d{1}|1\\d{1}|2[0-3])" 7622498b56bSopenharmony_ci ":[0-5]\\d{1}:([0-5]\\d{1})(\\.(\\d){0,3})?$"); 7632498b56bSopenharmony_ci std::regex cstPattern("CST (0\\d{1}|1[0-2])-(0\\d{1}|[12]\\d{1}|3[01])\\s(0\\d{1}|1\\d{1}|2[0-3])" 7642498b56bSopenharmony_ci ":[0-5]\\d{1}:([0-5]\\d{1})(\\.(\\d){0,3})?$"); 7652498b56bSopenharmony_ci res = GetCmdResultFromPopen(cmd); 7662498b56bSopenharmony_ci Split(res, vec, "\n"); 7672498b56bSopenharmony_ci for (auto& it : vec) { 7682498b56bSopenharmony_ci EXPECT_TRUE(regex_match(it.substr(0, 22), gmtPattern) || regex_match(it.substr(0, 22), cstPattern)); 7692498b56bSopenharmony_ci } 7702498b56bSopenharmony_ci 7712498b56bSopenharmony_ci cmd = "hilog -v test 2>&1"; 7722498b56bSopenharmony_ci std::string errMsg = ErrorCode2Str(ERR_INVALID_ARGUMENT) + "\n"; 7732498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 7742498b56bSopenharmony_ci 7752498b56bSopenharmony_ci cmd = "hilog -v time -v epoch 2>&1"; 7762498b56bSopenharmony_ci errMsg = ErrorCode2Str(ERR_DUPLICATE_OPTION) + "\n"; 7772498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 7782498b56bSopenharmony_ci 7792498b56bSopenharmony_ci cmd = "hilog -v msec -v usec 2>&1"; 7802498b56bSopenharmony_ci errMsg = ErrorCode2Str(ERR_DUPLICATE_OPTION) + "\n"; 7812498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), errMsg); 7822498b56bSopenharmony_ci 7832498b56bSopenharmony_ci cmd = "hilog -x -v color"; 7842498b56bSopenharmony_ci EXPECT_GT(GetCmdLinesFromPopen(cmd), 0); 7852498b56bSopenharmony_ci} 7862498b56bSopenharmony_ci 7872498b56bSopenharmony_ci/** 7882498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_018 7892498b56bSopenharmony_ci * @tc.desc: QueryLogHandler. 7902498b56bSopenharmony_ci * @tc.type: FUNC 7912498b56bSopenharmony_ci */ 7922498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_018, TestSize.Level1) 7932498b56bSopenharmony_ci{ 7942498b56bSopenharmony_ci /** 7952498b56bSopenharmony_ci * @tc.steps: step1. query log of specific pid. 7962498b56bSopenharmony_ci * @tc.steps: step2. query log of specific domain. 7972498b56bSopenharmony_ci * @tc.steps: step3. query log of specific tag. 7982498b56bSopenharmony_ci */ 7992498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_018: start."; 8002498b56bSopenharmony_ci std::string res = GetCmdResultFromPopen("hilog -z 1"); 8012498b56bSopenharmony_ci std::string pid = res.substr(19, 5); 8022498b56bSopenharmony_ci std::string domain = res.substr(34, 5); 8032498b56bSopenharmony_ci int tagLen = res.substr(40).find(":") + 1; 8042498b56bSopenharmony_ci std::string tag = res.substr(40, tagLen); 8052498b56bSopenharmony_ci std::string queryDomainCmd = "hilog -x -D d0" + domain; 8062498b56bSopenharmony_ci std::string queryPidCmd = "hilog -x -P " + pid; 8072498b56bSopenharmony_ci std::string queryTagCmd = "hilog -x -T " + tag; 8082498b56bSopenharmony_ci vector<string> vec; 8092498b56bSopenharmony_ci 8102498b56bSopenharmony_ci res = GetCmdResultFromPopen(queryPidCmd); 8112498b56bSopenharmony_ci if (res != "") { 8122498b56bSopenharmony_ci Split(res, vec, "\n"); 8132498b56bSopenharmony_ci for (auto& it : vec) { 8142498b56bSopenharmony_ci std::string logPid = it.substr(19, 5); 8152498b56bSopenharmony_ci EXPECT_EQ(logPid, pid); 8162498b56bSopenharmony_ci } 8172498b56bSopenharmony_ci } 8182498b56bSopenharmony_ci 8192498b56bSopenharmony_ci res = GetCmdResultFromPopen(queryDomainCmd); 8202498b56bSopenharmony_ci if (res != "") { 8212498b56bSopenharmony_ci Split(res, vec, "\n"); 8222498b56bSopenharmony_ci for (auto& it : vec) { 8232498b56bSopenharmony_ci std::string logDomain = it.substr(34, 5); 8242498b56bSopenharmony_ci EXPECT_EQ(logDomain, domain); 8252498b56bSopenharmony_ci } 8262498b56bSopenharmony_ci } 8272498b56bSopenharmony_ci 8282498b56bSopenharmony_ci res = GetCmdResultFromPopen(queryTagCmd); 8292498b56bSopenharmony_ci if (res != "") { 8302498b56bSopenharmony_ci Split(res, vec, "\n"); 8312498b56bSopenharmony_ci for (auto& it : vec) { 8322498b56bSopenharmony_ci std::string logTag = it.substr(40, tagLen); 8332498b56bSopenharmony_ci EXPECT_EQ(logTag, tag); 8342498b56bSopenharmony_ci } 8352498b56bSopenharmony_ci } 8362498b56bSopenharmony_ci} 8372498b56bSopenharmony_ci 8382498b56bSopenharmony_ci/** 8392498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_HandleTest_019 8402498b56bSopenharmony_ci * @tc.desc: tag & domain level ctl. 8412498b56bSopenharmony_ci * @tc.type: FUNC 8422498b56bSopenharmony_ci */ 8432498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_019, TestSize.Level1) 8442498b56bSopenharmony_ci{ 8452498b56bSopenharmony_ci /** 8462498b56bSopenharmony_ci * @tc.steps: step1. tag level ctl. 8472498b56bSopenharmony_ci * @tc.steps: step2. domain level ctl. 8482498b56bSopenharmony_ci */ 8492498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_019: start."; 8502498b56bSopenharmony_ci std::string res = GetCmdResultFromPopen("hilog -z 1"); 8512498b56bSopenharmony_ci uint32_t domain = std::stoi(res.substr(34, 5)); 8522498b56bSopenharmony_ci int tagLen = res.substr(40).find(":") + 1; 8532498b56bSopenharmony_ci std::string tag = res.substr(40, tagLen); 8542498b56bSopenharmony_ci 8552498b56bSopenharmony_ci // Priority: TagLevel > DomainLevel > GlobalLevel 8562498b56bSopenharmony_ci SetTagLevel(tag, LOG_ERROR); 8572498b56bSopenharmony_ci SetDomainLevel(domain, LOG_INFO); 8582498b56bSopenharmony_ci EXPECT_FALSE(HiLogIsLoggable(domain, tag.c_str(), LOG_INFO)); 8592498b56bSopenharmony_ci EXPECT_TRUE(HiLogIsLoggable(domain, tag.c_str(), LOG_ERROR)); 8602498b56bSopenharmony_ci 8612498b56bSopenharmony_ci SetTagLevel(tag, LOG_INFO); 8622498b56bSopenharmony_ci SetDomainLevel(domain, LOG_ERROR); 8632498b56bSopenharmony_ci EXPECT_TRUE(HiLogIsLoggable(domain, tag.c_str(), LOG_INFO)); 8642498b56bSopenharmony_ci EXPECT_TRUE(HiLogIsLoggable(domain, tag.c_str(), LOG_ERROR)); 8652498b56bSopenharmony_ci 8662498b56bSopenharmony_ci // restore log level 8672498b56bSopenharmony_ci SetDomainLevel(domain, LOG_INFO); 8682498b56bSopenharmony_ci} 8692498b56bSopenharmony_ci 8702498b56bSopenharmony_ci/** 8712498b56bSopenharmony_ci * @tc.name: Dfx_HilogToolTest_ClearTest_020 8722498b56bSopenharmony_ci * @tc.desc: hilog -w clear can delete /data/log/hilog/hilog*.gz. 8732498b56bSopenharmony_ci * @tc.type: FUNC 8742498b56bSopenharmony_ci */ 8752498b56bSopenharmony_ciHWTEST_F(HilogToolTest, HandleTest_020, TestSize.Level1) 8762498b56bSopenharmony_ci{ 8772498b56bSopenharmony_ci GTEST_LOG_(INFO) << "HandleTest_020: start."; 8782498b56bSopenharmony_ci (void)GetCmdResultFromPopen("hilog -w stop"); 8792498b56bSopenharmony_ci std::string cmd = "hilog -w clear"; 8802498b56bSopenharmony_ci std::string str = "Persist log /data/log/hilog clear successfully\n"; 8812498b56bSopenharmony_ci EXPECT_EQ(GetCmdResultFromPopen(cmd), str); 8822498b56bSopenharmony_ci 8832498b56bSopenharmony_ci std::regex hilogFilePattern("^hilog.*gz$"); 8842498b56bSopenharmony_ci DIR *dir = nullptr; 8852498b56bSopenharmony_ci struct dirent *ent = nullptr; 8862498b56bSopenharmony_ci if ((dir = opendir("/data/log/hilog")) != nullptr) { 8872498b56bSopenharmony_ci while ((ent = readdir(dir)) != nullptr) { 8882498b56bSopenharmony_ci EXPECT_FALSE(std::regex_match(ent->d_name, hilogFilePattern)); 8892498b56bSopenharmony_ci } 8902498b56bSopenharmony_ci } 8912498b56bSopenharmony_ci if (dir != nullptr) { 8922498b56bSopenharmony_ci closedir(dir); 8932498b56bSopenharmony_ci } 8942498b56bSopenharmony_ci (void)GetCmdResultFromPopen("hilog -w start"); 8952498b56bSopenharmony_ci} 8962498b56bSopenharmony_ci} // namespace 897