12498b56bSopenharmony_ci/* 22498b56bSopenharmony_ci * Copyright (c) 2021 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 162498b56bSopenharmony_ci#include <array> 172498b56bSopenharmony_ci#include <cstdlib> 182498b56bSopenharmony_ci#include <ctime> 192498b56bSopenharmony_ci#include <iostream> 202498b56bSopenharmony_ci#include <sstream> 212498b56bSopenharmony_ci#include <string> 222498b56bSopenharmony_ci 232498b56bSopenharmony_ci#include <unistd.h> 242498b56bSopenharmony_ci 252498b56bSopenharmony_ci#include <gtest/gtest.h> 262498b56bSopenharmony_ci 272498b56bSopenharmony_ci#include "hilog/log.h" 282498b56bSopenharmony_ci#include "parameters.h" 292498b56bSopenharmony_ci 302498b56bSopenharmony_ci#undef LOG_DOMAIN 312498b56bSopenharmony_ci#define LOG_DOMAIN 0xD002D00 322498b56bSopenharmony_ci 332498b56bSopenharmony_ci#undef LOG_TAG 342498b56bSopenharmony_ci#define LOG_TAG "HILOGTEST_C" 352498b56bSopenharmony_ci 362498b56bSopenharmony_ciusing namespace testing::ext; 372498b56bSopenharmony_ci 382498b56bSopenharmony_cinamespace OHOS { 392498b56bSopenharmony_cinamespace HiviewDFX { 402498b56bSopenharmony_cinamespace HiLogTest { 412498b56bSopenharmony_ciconst HiLogLabel APP_LABEL = { LOG_APP, 0x002a, "HILOGTEST_CPP" }; 422498b56bSopenharmony_ciconst HiLogLabel LABEL = { LOG_CORE, 0xD002D00, "HILOGTEST_CPP" }; 432498b56bSopenharmony_ciconst HiLogLabel ILLEGAL_DOMAIN_LABEL = { LOG_CORE, 0xD00EEEE, "HILOGTEST_CPP" }; 442498b56bSopenharmony_cistatic constexpr unsigned int SOME_LOGS = 10; 452498b56bSopenharmony_cistatic constexpr unsigned int MORE_LOGS = 100; 462498b56bSopenharmony_cistatic constexpr unsigned int OVER_LOGS = 1000; 472498b56bSopenharmony_ci 482498b56bSopenharmony_cienum LogInterfaceType { 492498b56bSopenharmony_ci DEBUG_METHOD = 0, 502498b56bSopenharmony_ci INFO_METHOD = 1, 512498b56bSopenharmony_ci WARN_METHOD = 2, 522498b56bSopenharmony_ci ERROR_METHOD = 3, 532498b56bSopenharmony_ci FATAL_METHOD = 4, 542498b56bSopenharmony_ci METHODS_NUMBER = 5, 552498b56bSopenharmony_ci}; 562498b56bSopenharmony_ci 572498b56bSopenharmony_ciusing LogMethodFunc = std::function<void(const std::string &msg)>; 582498b56bSopenharmony_ci 592498b56bSopenharmony_cistatic const std::array<LogMethodFunc, METHODS_NUMBER> LOG_C_METHODS = { 602498b56bSopenharmony_ci [] (const std::string &msg) { 612498b56bSopenharmony_ci HILOG_DEBUG(LOG_CORE, "%{public}s", msg.c_str()); 622498b56bSopenharmony_ci }, 632498b56bSopenharmony_ci [] (const std::string &msg) { 642498b56bSopenharmony_ci HILOG_INFO(LOG_CORE, "%{public}s", msg.c_str()); 652498b56bSopenharmony_ci }, 662498b56bSopenharmony_ci [] (const std::string &msg) { 672498b56bSopenharmony_ci HILOG_WARN(LOG_CORE, "%{public}s", msg.c_str()); 682498b56bSopenharmony_ci }, 692498b56bSopenharmony_ci [] (const std::string &msg) { 702498b56bSopenharmony_ci HILOG_ERROR(LOG_CORE, "%{public}s", msg.c_str()); 712498b56bSopenharmony_ci }, 722498b56bSopenharmony_ci [] (const std::string &msg) { 732498b56bSopenharmony_ci HILOG_FATAL(LOG_CORE, "%{public}s", msg.c_str()); 742498b56bSopenharmony_ci }, 752498b56bSopenharmony_ci}; 762498b56bSopenharmony_ci 772498b56bSopenharmony_cistatic const std::array<LogMethodFunc, METHODS_NUMBER> LOG_CPP_METHODS = { 782498b56bSopenharmony_ci [] (const std::string &msg) { 792498b56bSopenharmony_ci HiLog::Debug(LABEL, "%{public}s", msg.c_str()); 802498b56bSopenharmony_ci }, 812498b56bSopenharmony_ci [] (const std::string &msg) { 822498b56bSopenharmony_ci HiLog::Info(LABEL, "%{public}s", msg.c_str()); 832498b56bSopenharmony_ci }, 842498b56bSopenharmony_ci [] (const std::string &msg) { 852498b56bSopenharmony_ci HiLog::Warn(LABEL, "%{public}s", msg.c_str()); 862498b56bSopenharmony_ci }, 872498b56bSopenharmony_ci [] (const std::string &msg) { 882498b56bSopenharmony_ci HiLog::Error(LABEL, "%{public}s", msg.c_str()); 892498b56bSopenharmony_ci }, 902498b56bSopenharmony_ci [] (const std::string &msg) { 912498b56bSopenharmony_ci HiLog::Fatal(LABEL, "%{public}s", msg.c_str()); 922498b56bSopenharmony_ci }, 932498b56bSopenharmony_ci}; 942498b56bSopenharmony_ci 952498b56bSopenharmony_cistatic std::string PopenToString(const std::string &command) 962498b56bSopenharmony_ci{ 972498b56bSopenharmony_ci std::string str; 982498b56bSopenharmony_ci constexpr int bufferSize = 1024; 992498b56bSopenharmony_ci FILE *fp = popen(command.c_str(), "re"); 1002498b56bSopenharmony_ci if (fp != nullptr) { 1012498b56bSopenharmony_ci char buf[bufferSize] = {0}; 1022498b56bSopenharmony_ci size_t n = fread(buf, 1, sizeof(buf), fp); 1032498b56bSopenharmony_ci while (n > 0) { 1042498b56bSopenharmony_ci str.append(buf, n); 1052498b56bSopenharmony_ci n = fread(buf, 1, sizeof(buf), fp); 1062498b56bSopenharmony_ci } 1072498b56bSopenharmony_ci pclose(fp); 1082498b56bSopenharmony_ci } 1092498b56bSopenharmony_ci std::cout << "PopenToString res: " << str << std::endl; 1102498b56bSopenharmony_ci return str; 1112498b56bSopenharmony_ci} 1122498b56bSopenharmony_ci 1132498b56bSopenharmony_ciclass HiLogNDKTest : public testing::Test { 1142498b56bSopenharmony_cipublic: 1152498b56bSopenharmony_ci static void SetUpTestCase(); 1162498b56bSopenharmony_ci static void TearDownTestCase() {} 1172498b56bSopenharmony_ci void SetUp(); 1182498b56bSopenharmony_ci void TearDown() {} 1192498b56bSopenharmony_ci}; 1202498b56bSopenharmony_ci 1212498b56bSopenharmony_civoid HiLogNDKTest::SetUpTestCase() 1222498b56bSopenharmony_ci{ 1232498b56bSopenharmony_ci (void)PopenToString("hilog -Q pidoff"); 1242498b56bSopenharmony_ci (void)PopenToString("hilog -Q domainoff"); 1252498b56bSopenharmony_ci} 1262498b56bSopenharmony_ci 1272498b56bSopenharmony_civoid HiLogNDKTest::SetUp() 1282498b56bSopenharmony_ci{ 1292498b56bSopenharmony_ci (void)PopenToString("hilog -r"); 1302498b56bSopenharmony_ci} 1312498b56bSopenharmony_ci 1322498b56bSopenharmony_cistatic std::string RandomStringGenerator() 1332498b56bSopenharmony_ci{ 1342498b56bSopenharmony_ci std::string str; 1352498b56bSopenharmony_ci int logLen = 16; 1362498b56bSopenharmony_ci char index; 1372498b56bSopenharmony_ci for (int i = 0; i < logLen; ++i) { 1382498b56bSopenharmony_ci index = rand() % ('z' - 'a') + 'a'; 1392498b56bSopenharmony_ci str.append(1, index); 1402498b56bSopenharmony_ci } 1412498b56bSopenharmony_ci return str; 1422498b56bSopenharmony_ci} 1432498b56bSopenharmony_ci 1442498b56bSopenharmony_cistatic void HiLogWriteTest(LogInterfaceType methodType, unsigned int count, 1452498b56bSopenharmony_ci const std::array<LogMethodFunc, METHODS_NUMBER> &logMethods) 1462498b56bSopenharmony_ci{ 1472498b56bSopenharmony_ci std::string logMsg(RandomStringGenerator()); 1482498b56bSopenharmony_ci for (unsigned int i = 0; i < count; ++i) { 1492498b56bSopenharmony_ci logMethods.at(methodType)(logMsg + std::to_string(i)); 1502498b56bSopenharmony_ci } 1512498b56bSopenharmony_ci usleep(1000); /* 1000: sleep 1 ms */ 1522498b56bSopenharmony_ci std::string logMsgs = PopenToString("/system/bin/hilog -x"); 1532498b56bSopenharmony_ci unsigned int realCount = 0; 1542498b56bSopenharmony_ci std::stringstream ss(logMsgs); 1552498b56bSopenharmony_ci std::string str; 1562498b56bSopenharmony_ci while (!ss.eof()) { 1572498b56bSopenharmony_ci getline(ss, str); 1582498b56bSopenharmony_ci if (str.find(logMsg) != std::string::npos) { 1592498b56bSopenharmony_ci ++realCount; 1602498b56bSopenharmony_ci } 1612498b56bSopenharmony_ci } 1622498b56bSopenharmony_ci unsigned int allowedLeastLogCount = count - count * 1 / 10; /* 1 / 10: loss rate less than 10% */ 1632498b56bSopenharmony_ci if (methodType == DEBUG_METHOD) { 1642498b56bSopenharmony_ci allowedLeastLogCount = 0; /* 0: debug log is allowed to be closed */ 1652498b56bSopenharmony_ci } 1662498b56bSopenharmony_ci EXPECT_GE(realCount, allowedLeastLogCount); 1672498b56bSopenharmony_ci} 1682498b56bSopenharmony_ci 1692498b56bSopenharmony_cistatic void FlowCtlTest(const HiLogLabel &label, const std::string keyWord) 1702498b56bSopenharmony_ci{ 1712498b56bSopenharmony_ci const std::string str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 1722498b56bSopenharmony_ci for (unsigned int i = 0; i < OVER_LOGS; ++i) { 1732498b56bSopenharmony_ci HiLog::Info(label, "%{public}s:%{public}d", str.c_str(), i); 1742498b56bSopenharmony_ci } 1752498b56bSopenharmony_ci sleep(1); /* 1: sleep 1 s */ 1762498b56bSopenharmony_ci HiLog::Info(label, "%{public}s", str.c_str()); 1772498b56bSopenharmony_ci std::string logMsgs = PopenToString("hilog -x -T LOGLIMIT"); 1782498b56bSopenharmony_ci EXPECT_TRUE(logMsgs.find(keyWord) != std::string::npos); 1792498b56bSopenharmony_ci} 1802498b56bSopenharmony_ci 1812498b56bSopenharmony_ci/** 1822498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintDebugLog_001 1832498b56bSopenharmony_ci * @tc.desc: Call HILOG_DEBUG to print logs. 1842498b56bSopenharmony_ci * @tc.type: FUNC 1852498b56bSopenharmony_ci */ 1862498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintDebugLog_001, TestSize.Level1) 1872498b56bSopenharmony_ci{ 1882498b56bSopenharmony_ci /** 1892498b56bSopenharmony_ci * @tc.steps: step1. Call HILOG_DEBUG to print logs and call hilog to read it 1902498b56bSopenharmony_ci * @tc.expected: step1. Logs can be printed only if hilog.debug is enabled. 1912498b56bSopenharmony_ci */ 1922498b56bSopenharmony_ci HiLogWriteTest(DEBUG_METHOD, SOME_LOGS, LOG_C_METHODS); 1932498b56bSopenharmony_ci} 1942498b56bSopenharmony_ci 1952498b56bSopenharmony_ci/** 1962498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintInfoLog_001 1972498b56bSopenharmony_ci * @tc.desc: Call HILOG_INFO to print logs. 1982498b56bSopenharmony_ci * @tc.type: FUNC 1992498b56bSopenharmony_ci */ 2002498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintInfoLog_001, TestSize.Level1) 2012498b56bSopenharmony_ci{ 2022498b56bSopenharmony_ci /** 2032498b56bSopenharmony_ci * @tc.steps: step1. Call HILOG_INFO to print logs and call hilog to read it 2042498b56bSopenharmony_ci * @tc.expected: step1. Logs printed without loss. 2052498b56bSopenharmony_ci */ 2062498b56bSopenharmony_ci HiLogWriteTest(INFO_METHOD, SOME_LOGS, LOG_C_METHODS); 2072498b56bSopenharmony_ci} 2082498b56bSopenharmony_ci 2092498b56bSopenharmony_ci/** 2102498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintWarnLog_001 2112498b56bSopenharmony_ci * @tc.desc: Call HILOG_WARN to print logs. 2122498b56bSopenharmony_ci * @tc.type: FUNC 2132498b56bSopenharmony_ci */ 2142498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintWarnLog_001, TestSize.Level1) 2152498b56bSopenharmony_ci{ 2162498b56bSopenharmony_ci /** 2172498b56bSopenharmony_ci * @tc.steps: step1. Call HILOG_WARN to print logs and call hilog to read it 2182498b56bSopenharmony_ci * @tc.expected: step1. Logs printed without loss. 2192498b56bSopenharmony_ci */ 2202498b56bSopenharmony_ci HiLogWriteTest(WARN_METHOD, SOME_LOGS, LOG_C_METHODS); 2212498b56bSopenharmony_ci} 2222498b56bSopenharmony_ci 2232498b56bSopenharmony_ci/** 2242498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintErrorLog_001 2252498b56bSopenharmony_ci * @tc.desc: Call HILOG_ERROR to print logs. 2262498b56bSopenharmony_ci * @tc.type: FUNC 2272498b56bSopenharmony_ci */ 2282498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintErrorLog_001, TestSize.Level1) 2292498b56bSopenharmony_ci{ 2302498b56bSopenharmony_ci /** 2312498b56bSopenharmony_ci * @tc.steps: step1. Call HILOG_ERROR to print logs and call hilog to read it 2322498b56bSopenharmony_ci * @tc.expected: step1. Logs printed without loss. 2332498b56bSopenharmony_ci */ 2342498b56bSopenharmony_ci HiLogWriteTest(ERROR_METHOD, SOME_LOGS, LOG_C_METHODS); 2352498b56bSopenharmony_ci} 2362498b56bSopenharmony_ci 2372498b56bSopenharmony_ci/** 2382498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintFatalLog_001 2392498b56bSopenharmony_ci * @tc.desc: Call HILOG_FATAL to print logs. 2402498b56bSopenharmony_ci * @tc.type: FUNC 2412498b56bSopenharmony_ci */ 2422498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintFatalLog_001, TestSize.Level1) 2432498b56bSopenharmony_ci{ 2442498b56bSopenharmony_ci /** 2452498b56bSopenharmony_ci * @tc.steps: step1. Call HILOG_FATAL to print logs and call hilog to read it 2462498b56bSopenharmony_ci * @tc.expected: step1. Logs printed without loss. 2472498b56bSopenharmony_ci */ 2482498b56bSopenharmony_ci HiLogWriteTest(FATAL_METHOD, SOME_LOGS, LOG_C_METHODS); 2492498b56bSopenharmony_ci} 2502498b56bSopenharmony_ci 2512498b56bSopenharmony_ci/** 2522498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_LogLossCheck_001 2532498b56bSopenharmony_ci * @tc.desc: HiLog log loss rate must less than 10%. 2542498b56bSopenharmony_ci * @tc.type: FUNC 2552498b56bSopenharmony_ci */ 2562498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, LogLossCheck_001, TestSize.Level1) 2572498b56bSopenharmony_ci{ 2582498b56bSopenharmony_ci /** 2592498b56bSopenharmony_ci * @tc.steps: step1. Call HILOG_INFO to print logs and call hilog to read it 2602498b56bSopenharmony_ci * @tc.expected: step1. Calculate log loss rate and it should less than 10% 2612498b56bSopenharmony_ci */ 2622498b56bSopenharmony_ci HiLogWriteTest(INFO_METHOD, MORE_LOGS, LOG_C_METHODS); 2632498b56bSopenharmony_ci} 2642498b56bSopenharmony_ci 2652498b56bSopenharmony_ci/** 2662498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintDebugLog_002 2672498b56bSopenharmony_ci * @tc.desc: Call HiLog::Debug to print logs. 2682498b56bSopenharmony_ci * @tc.type: FUNC 2692498b56bSopenharmony_ci */ 2702498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintDebugLog_002, TestSize.Level1) 2712498b56bSopenharmony_ci{ 2722498b56bSopenharmony_ci /** 2732498b56bSopenharmony_ci * @tc.steps: step1. Call HiLog::Debug to print logs and call hilog to read it 2742498b56bSopenharmony_ci * @tc.expected: step1. Logs can be printed only if hilog.debug is enabled. 2752498b56bSopenharmony_ci */ 2762498b56bSopenharmony_ci HiLogWriteTest(DEBUG_METHOD, SOME_LOGS, LOG_CPP_METHODS); 2772498b56bSopenharmony_ci} 2782498b56bSopenharmony_ci 2792498b56bSopenharmony_ci/** 2802498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintInfoLog_002 2812498b56bSopenharmony_ci * @tc.desc: Call HiLog::Info to print logs. 2822498b56bSopenharmony_ci * @tc.type: FUNC 2832498b56bSopenharmony_ci */ 2842498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintInfoLog_002, TestSize.Level1) 2852498b56bSopenharmony_ci{ 2862498b56bSopenharmony_ci /** 2872498b56bSopenharmony_ci * @tc.steps: step1. Call HiLog::Info to print logs and call hilog to read it 2882498b56bSopenharmony_ci * @tc.expected: step1. Logs printed without loss. 2892498b56bSopenharmony_ci */ 2902498b56bSopenharmony_ci HiLogWriteTest(INFO_METHOD, SOME_LOGS, LOG_CPP_METHODS); 2912498b56bSopenharmony_ci} 2922498b56bSopenharmony_ci 2932498b56bSopenharmony_ci/** 2942498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintWarnLog_002 2952498b56bSopenharmony_ci * @tc.desc: Call HiLog::Warn print logs. 2962498b56bSopenharmony_ci * @tc.type: FUNC 2972498b56bSopenharmony_ci */ 2982498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintWarnLog_002, TestSize.Level1) 2992498b56bSopenharmony_ci{ 3002498b56bSopenharmony_ci /** 3012498b56bSopenharmony_ci * @tc.steps: step1. Call HiLog::Warn to print logs and call hilog to read it 3022498b56bSopenharmony_ci * @tc.expected: step1. Logs printed without loss. 3032498b56bSopenharmony_ci */ 3042498b56bSopenharmony_ci HiLogWriteTest(WARN_METHOD, SOME_LOGS, LOG_CPP_METHODS); 3052498b56bSopenharmony_ci} 3062498b56bSopenharmony_ci 3072498b56bSopenharmony_ci/** 3082498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintErrorLog_002 3092498b56bSopenharmony_ci * @tc.desc: Call HiLog::Error to print logs. 3102498b56bSopenharmony_ci * @tc.type: FUNC 3112498b56bSopenharmony_ci */ 3122498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintErrorLog_002, TestSize.Level1) 3132498b56bSopenharmony_ci{ 3142498b56bSopenharmony_ci /** 3152498b56bSopenharmony_ci * @tc.steps: step1. Call HiLog::Error to print logs and call hilog to read it 3162498b56bSopenharmony_ci * @tc.expected: step1. Logs printed without loss. 3172498b56bSopenharmony_ci */ 3182498b56bSopenharmony_ci HiLogWriteTest(ERROR_METHOD, SOME_LOGS, LOG_CPP_METHODS); 3192498b56bSopenharmony_ci} 3202498b56bSopenharmony_ci 3212498b56bSopenharmony_ci/** 3222498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_PrintFatalLog_002 3232498b56bSopenharmony_ci * @tc.desc: Call HiLog::Fatal to print logs. 3242498b56bSopenharmony_ci * @tc.type: FUNC 3252498b56bSopenharmony_ci */ 3262498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, PrintFatalLog_002, TestSize.Level1) 3272498b56bSopenharmony_ci{ 3282498b56bSopenharmony_ci /** 3292498b56bSopenharmony_ci * @tc.steps: step1. Call HiLog::Fatal to print logs and call hilog to read it 3302498b56bSopenharmony_ci * @tc.expected: step1. Logs printed without loss. 3312498b56bSopenharmony_ci */ 3322498b56bSopenharmony_ci HiLogWriteTest(FATAL_METHOD, SOME_LOGS, LOG_CPP_METHODS); 3332498b56bSopenharmony_ci} 3342498b56bSopenharmony_ci 3352498b56bSopenharmony_ci/** 3362498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_LogLossCheck_002 3372498b56bSopenharmony_ci * @tc.desc: HiLog log loss rate must less than 10% 3382498b56bSopenharmony_ci * @tc.type: FUNC 3392498b56bSopenharmony_ci */ 3402498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, LogLossCheck_002, TestSize.Level1) 3412498b56bSopenharmony_ci{ 3422498b56bSopenharmony_ci /** 3432498b56bSopenharmony_ci * @tc.steps: step1. Call HiLog::Info to print logs and call hilog to read it 3442498b56bSopenharmony_ci * @tc.expected: step1. Calculate log loss rate and it should less than 10% 3452498b56bSopenharmony_ci */ 3462498b56bSopenharmony_ci HiLogWriteTest(INFO_METHOD, MORE_LOGS, LOG_CPP_METHODS); 3472498b56bSopenharmony_ci} 3482498b56bSopenharmony_ci 3492498b56bSopenharmony_ci/** 3502498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_IsLoggable_001 3512498b56bSopenharmony_ci * @tc.desc: Check whether is loggable for each log level 3522498b56bSopenharmony_ci * @tc.type: FUNC 3532498b56bSopenharmony_ci */ 3542498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, IsLoggable_001, TestSize.Level1) 3552498b56bSopenharmony_ci{ 3562498b56bSopenharmony_ci /** 3572498b56bSopenharmony_ci * @tc.steps: step1. Call HiLogIsLoggable to check whether is loggable for each log level. 3582498b56bSopenharmony_ci * @tc.expected: step1. LOG_DEBUG and lower level should return false in release version, and others return true. 3592498b56bSopenharmony_ci */ 3602498b56bSopenharmony_ci if (OHOS::system::GetParameter("hilog.loggable.global", "D") == "D") { 3612498b56bSopenharmony_ci EXPECT_TRUE(HiLogIsLoggable(0xD002D00, LOG_TAG, LOG_DEBUG)); 3622498b56bSopenharmony_ci } else { 3632498b56bSopenharmony_ci EXPECT_FALSE(HiLogIsLoggable(0xD002D00, LOG_TAG, LOG_DEBUG)); 3642498b56bSopenharmony_ci } 3652498b56bSopenharmony_ci EXPECT_TRUE(HiLogIsLoggable(0xD002D00, LOG_TAG, LOG_INFO)); 3662498b56bSopenharmony_ci EXPECT_TRUE(HiLogIsLoggable(0xD002D00, LOG_TAG, LOG_WARN)); 3672498b56bSopenharmony_ci EXPECT_TRUE(HiLogIsLoggable(0xD002D00, LOG_TAG, LOG_ERROR)); 3682498b56bSopenharmony_ci EXPECT_TRUE(HiLogIsLoggable(0xD002D00, LOG_TAG, LOG_FATAL)); 3692498b56bSopenharmony_ci EXPECT_TRUE(HiLogIsLoggable(0xD002D00, "abc", LOG_WARN)); 3702498b56bSopenharmony_ci} 3712498b56bSopenharmony_ci 3722498b56bSopenharmony_ci/** 3732498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_DomainCheck_001 3742498b56bSopenharmony_ci * @tc.desc: test illegal domainID 3752498b56bSopenharmony_ci * @tc.type: FUNC 3762498b56bSopenharmony_ci * @tc.require:issueI5NU4L 3772498b56bSopenharmony_ci */ 3782498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, DomainCheck_001, TestSize.Level1) 3792498b56bSopenharmony_ci{ 3802498b56bSopenharmony_ci (void)PopenToString("param set hilog.debug.on false"); 3812498b56bSopenharmony_ci (void)PopenToString("param set persist.sys.hilog.debug.on false"); 3822498b56bSopenharmony_ci std::string logMsg(RandomStringGenerator()); 3832498b56bSopenharmony_ci for (unsigned int i = 0; i < SOME_LOGS; ++i) { 3842498b56bSopenharmony_ci HiLog::Info(ILLEGAL_DOMAIN_LABEL, "%{public}s", logMsg.c_str()); 3852498b56bSopenharmony_ci } 3862498b56bSopenharmony_ci usleep(1000); /* 1000: sleep 1 ms */ 3872498b56bSopenharmony_ci std::string logMsgs = PopenToString("/system/bin/hilog -x"); 3882498b56bSopenharmony_ci unsigned int realCount = 0; 3892498b56bSopenharmony_ci std::stringstream ss(logMsgs); 3902498b56bSopenharmony_ci std::string str; 3912498b56bSopenharmony_ci while (!ss.eof()) { 3922498b56bSopenharmony_ci getline(ss, str); 3932498b56bSopenharmony_ci if (str.find(logMsg) != std::string::npos) { 3942498b56bSopenharmony_ci ++realCount; 3952498b56bSopenharmony_ci } 3962498b56bSopenharmony_ci } 3972498b56bSopenharmony_ci EXPECT_EQ(realCount, 0); 3982498b56bSopenharmony_ci} 3992498b56bSopenharmony_ci 4002498b56bSopenharmony_ci/** 4012498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_hilogSocketTest 4022498b56bSopenharmony_ci * @tc.desc: Query hilog socket rights 4032498b56bSopenharmony_ci * @tc.type: FUNC 4042498b56bSopenharmony_ci * @tc.require:issueI5NU7F 4052498b56bSopenharmony_ci */ 4062498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, hilogSocketTest, TestSize.Level1) 4072498b56bSopenharmony_ci{ 4082498b56bSopenharmony_ci std::string str; 4092498b56bSopenharmony_ci std::string hilogControlRights = "srw-rw----"; 4102498b56bSopenharmony_ci std::string logMsgs = PopenToString("ls -al //dev/unix/socket/hilogControl"); 4112498b56bSopenharmony_ci std::stringstream ss(logMsgs); 4122498b56bSopenharmony_ci getline(ss, str); 4132498b56bSopenharmony_ci EXPECT_TRUE(str.find(hilogControlRights) != std::string::npos); 4142498b56bSopenharmony_ci} 4152498b56bSopenharmony_ci 4162498b56bSopenharmony_ci/** 4172498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_pidFlowCtrlTest 4182498b56bSopenharmony_ci * @tc.desc: hilog pidFlowCtrlTest 4192498b56bSopenharmony_ci * @tc.type: FUNC 4202498b56bSopenharmony_ci */ 4212498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, pidFlowCtrlTest, TestSize.Level1) 4222498b56bSopenharmony_ci{ 4232498b56bSopenharmony_ci (void)PopenToString("hilog -Q pidon"); 4242498b56bSopenharmony_ci const std::string pidCtrlLog = "DROPPED"; 4252498b56bSopenharmony_ci FlowCtlTest(APP_LABEL, pidCtrlLog); 4262498b56bSopenharmony_ci (void)PopenToString("hilog -Q pidoff"); 4272498b56bSopenharmony_ci} 4282498b56bSopenharmony_ci 4292498b56bSopenharmony_ci/** 4302498b56bSopenharmony_ci * @tc.name: Dfx_HiLogNDKTest_domainFlowCtrlTest 4312498b56bSopenharmony_ci * @tc.desc: hilog domainFlowCtrlTest 4322498b56bSopenharmony_ci * @tc.type: FUNC 4332498b56bSopenharmony_ci */ 4342498b56bSopenharmony_ciHWTEST_F(HiLogNDKTest, domainFlowCtrlTest, TestSize.Level1) 4352498b56bSopenharmony_ci{ 4362498b56bSopenharmony_ci (void)PopenToString("hilog -Q domainon"); 4372498b56bSopenharmony_ci const std::string domainCtrlLog = "dropped"; 4382498b56bSopenharmony_ci FlowCtlTest(LABEL, domainCtrlLog); 4392498b56bSopenharmony_ci (void)PopenToString("hilog -Q domainoff"); 4402498b56bSopenharmony_ci} 4412498b56bSopenharmony_ci} // namespace HiLogTest 4422498b56bSopenharmony_ci} // namespace HiviewDFX 4432498b56bSopenharmony_ci} // namespace OHOS 444