diff --git a/test/BUILD.gn b/test/BUILD.gn index 426db23fea4cff32b3fddded26abd011367932bf..2c78ff78ff38ebd490eb2aea0865ebb13f369635 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -60,3 +60,10 @@ ohos_moduletest("HiLogAdapterTest") { "//base/hiviewdfx/hilog/frameworks/libhilog/utils/include/", ] } + +group("unittest") { + testonly = true + deps = [] + + deps += [ "unittest/common:unittest" ] +} diff --git a/test/unittest/common/BUILD.gn b/test/unittest/common/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0ad4ee14adda4968ea0c887e5d7ae318b407efa8 --- /dev/null +++ b/test/unittest/common/BUILD.gn @@ -0,0 +1,43 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +module_output_path = "hiviewdfx/hilog" + +config("module_private_config") { + visibility = [ ":*" ] +} + +ohos_unittest("HilogToolTest") { + module_out_path = module_output_path + + sources = [ "hilogtool_test.cpp" ] + + configs = [ + ":module_private_config", + "//base/hiviewdfx/hilog/frameworks/libhilog:libhilog_config", + ] + + deps = [ "//third_party/googletest:gtest_main" ] + + external_deps = [ + "c_utils:utils", + "hilog_native:libhilog", + ] +} + +group("unittest") { + testonly = true + deps = [ ":HilogToolTest" ] +} diff --git a/test/unittest/common/hilogtool_test.cpp b/test/unittest/common/hilogtool_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..914fd9196f8bfc757dd486b1954518a1bac36b83 --- /dev/null +++ b/test/unittest/common/hilogtool_test.cpp @@ -0,0 +1,401 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "hilogtool_test.h" +#include +#include + +using namespace std; +using namespace testing::ext; +using namespace OHOS; +using namespace OHOS::HiviewDFX; + +int GetCmdLinesFromPopen(const std::string& cmd) +{ + if (cmd.empty()) { + return 0; + } + FILE* fp = popen(cmd.c_str(), "r"); + if (fp == nullptr) { + return 0; + } + int ret = 0; + char* buffer = nullptr; + size_t len = 0; + while (getline(&buffer, &len, fp) != -1) { + ret++; + } + if (buffer != nullptr) { + free(buffer); + buffer = nullptr; + } + pclose(fp); + return ret; +} + +std::string GetCmdResultFromPopen(const std::string& cmd) +{ + if (cmd.empty()) { + return ""; + } + FILE* fp = popen(cmd.c_str(), "r"); + if (fp == nullptr) { + return ""; + } + std::string ret = ""; + char* buffer = nullptr; + size_t len = 0; + while (getline(&buffer, &len, fp) != -1) { + std::string line = buffer; + ret += line; + } + if (buffer != nullptr) { + free(buffer); + buffer = nullptr; + } + pclose(fp); + return ret; +} + +bool IsExistInCmdResult(const std::string &cmd, const std::string &str) +{ + if (cmd.empty()) { + return false; + } + FILE* fp = popen(cmd.c_str(), "r"); + if (fp == nullptr) { + return false; + } + bool ret = false; + char* buffer = nullptr; + size_t len = 0; + while (getline(&buffer, &len, fp) != -1) { + std::string line = buffer; + if (line.find(str) != string::npos) { + ret = true; + break; + } + } + if (buffer != nullptr) { + free(buffer); + buffer = nullptr; + } + pclose(fp); + return ret; +} + +const std::list> helperList = { + /* help cmd suffix, information key word */ + {"", "Usage"}, + {"query", "Query"}, + {"clear", "Remove"}, + {"buffer", "buffer"}, + {"stats", "statistics"}, + {"persist", "persistance"}, + {"private", "privacy"}, + {"kmsg", "kmsg"}, + {"flowcontrol", "flow-control"}, + {"baselevel", "baselevel"}, + {"combo", "combination"}, + {"domain", "domain"}, +}; + +/** + * @tc.name: Dfx_HilogToolTest_HelperTest_001 + * @tc.desc: hilog help information. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HelperTest_001, TestSize.Level1) +{ + /** + * @tc.steps: step1. show hilog help information. + */ + GTEST_LOG_(INFO) << "HelperTest_001: start."; + std::string prefix = "hilog -h "; + std::string cmd = ""; + for (auto &it : helperList) { + cmd = prefix + it.first; + EXPECT_TRUE(IsExistInCmdResult(cmd, it.second)); + } + + prefix = "hilog --help "; + for (auto &it : helperList) { + cmd = prefix + it.first; + EXPECT_TRUE(IsExistInCmdResult(cmd, it.second)); + } +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_001 + * @tc.desc: BaseLogLevelHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_001, TestSize.Level1) +{ + /** + * @tc.steps: step1. set global log level to INFO. + */ + GTEST_LOG_(INFO) << "HandleTest_001: start."; + std::string cmd = "hilog -b I"; + std::string str = "Set global log level to I successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_002 + * @tc.desc: DomainHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_002, TestSize.Level1) +{ + /** + * @tc.steps: step1. set domain xxx log level to INFO. + */ + GTEST_LOG_(INFO) << "HandleTest_002: start."; + uint32_t domain = 0x02d00; + std::string cmd = "hilog -b I -D " + Uint2HexStr(domain); + std::string str = "Set domain 0x" + Uint2HexStr(domain) + " log level to I successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_003 + * @tc.desc: TagHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_003, TestSize.Level1) +{ + /** + * @tc.steps: step1. set tag xxx log level to INFO. + */ + GTEST_LOG_(INFO) << "HandleTest_003: start."; + std::string tag = "test"; + std::string cmd = "hilog -b I -T " + tag; + std::string str = "Set tag " + tag + " log level to I successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_004 + * @tc.desc: BufferSizeSetHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_004, TestSize.Level1) +{ + /** + * @tc.steps: step1. set app,init.core buffer size [valid]. + * @tc.expected: step1. set app,init.core buffer size successfully. + * @tc.steps: step2. set app,init.core buffer size [invalid]. + * @tc.expected: step2 set app,init.core buffer size failed. + * buffer size should be in range [64.0K, 512.0M]. + */ + GTEST_LOG_(INFO) << "HandleTest_004: start."; + std::string validSizeCmd = "hilog -G 256K"; + std::string str = "Set log type app buffer size to 256.0K successfully\n" + "Set log type init buffer size to 256.0K successfully\n" + "Set log type core buffer size to 256.0K successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(validSizeCmd), str); + + std::string inValidSizeCmd = "hilog -G 256G"; + str = "failed"; + EXPECT_TRUE(IsExistInCmdResult(inValidSizeCmd, str)); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_005 + * @tc.desc: BufferSizeGetHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_005, TestSize.Level1) +{ + /** + * @tc.steps: step1. get app,init.core valid buffer size. + */ + GTEST_LOG_(INFO) << "HandleTest_005: start."; + std::string cmd = "hilog -g"; + std::string str = "Log type app buffer size is 256.0K\n" + "Log type init buffer size is 256.0K\n" + "Log type core buffer size is 256.0K\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_006 + * @tc.desc: KmsgFeatureSetHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_006, TestSize.Level1) +{ + /** + * @tc.steps: step1. set hilogd storing kmsg log feature on. + * @tc.steps: step1. set hilogd storing kmsg log feature off. + */ + GTEST_LOG_(INFO) << "HandleTest_006: start."; + std::string cmd = "hilog -k on"; + std::string str = "Set hilogd storing kmsg log on successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); + + cmd = "hilog -k off"; + str = "Set hilogd storing kmsg log off successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_007 + * @tc.desc: PrivateFeatureSetHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_007, TestSize.Level1) +{ + /** + * @tc.steps: step1. set hilog api privacy formatter feature on. + * @tc.steps: step1. set hilog api privacy formatter feature off. + */ + GTEST_LOG_(INFO) << "HandleTest_007: start."; + std::string cmd = "hilog -p on"; + std::string str = "Set hilog privacy format on successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); + + cmd = "hilog -p off"; + str = "Set hilog privacy format off successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_008 + * @tc.desc: FlowControlFeatureSetHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_008, TestSize.Level1) +{ + /** + * @tc.steps: step1. set process flow control on. + * @tc.steps: step2. set process flow control off. + * @tc.steps: step3. set domain flow control on. + * @tc.steps: step4. set domain flow control off. + */ + GTEST_LOG_(INFO) << "HandleTest_008: start."; + std::string cmd = "hilog -Q pidon"; + std::string str = "Set flow control by process to enabled, result: Success [CODE: 0]\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); + + cmd = "hilog -Q pidoff"; + str = "Set flow control by process to disabled, result: Success [CODE: 0]\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); + + cmd = "hilog -Q domainon"; + str = "Set flow control by domain to enabled, result: Success [CODE: 0]\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); + + cmd = "hilog -Q domainoff"; + str = "Set flow control by domain to disabled, result: Success [CODE: 0]\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_009 + * @tc.desc: HeadHandler & TailHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_009, TestSize.Level1) +{ + /** + * @tc.steps: step1. show n lines logs on head of buffer. + * @tc.steps: step2. show n lines logs on tail of buffer. + */ + GTEST_LOG_(INFO) << "HandleTest_009: start."; + int lines = 5; + std::string cmd = "hilog -a " + std::to_string(lines); + EXPECT_EQ(GetCmdLinesFromPopen(cmd), lines); + + cmd = "hilog -z "+ std::to_string(lines); + EXPECT_EQ(GetCmdLinesFromPopen(cmd), lines); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_010 + * @tc.desc: RemoveHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_010, TestSize.Level1) +{ + /** + * @tc.steps: step1. remove all logs in hilogd buffer. + */ + GTEST_LOG_(INFO) << "HandleTest_010: start."; + std::string cmd = "hilog -r"; + std::string str = "Log type core,app buffer clear successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_011 + * @tc.desc: TypeHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_011, TestSize.Level1) +{ + /** + * @tc.steps: step1. remove app logs in hilogd buffer. + * @tc.steps: step2. remove core logs in hilogd buffer. + */ + GTEST_LOG_(INFO) << "HandleTest_011: start."; + std::string cmd = "hilog -r -t app"; + std::string str = "Log type app buffer clear successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); + + cmd = "hilog -r -t core"; + str = "Log type core buffer clear successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} + +/** + * @tc.name: Dfx_HilogToolTest_HandleTest_012 + * @tc.desc: PersistTaskHandler. + * @tc.type: FUNC + */ +HWTEST_F(HilogToolTest, HandleTest_012, TestSize.Level1) +{ + /** + * @tc.steps: step1. start hilog persistance task control. + * @tc.steps: step2. stop hilog persistance task control. + * @tc.steps: step3. start hilog persistance task control with advanced options. + * @tc.steps: step4. query tasks informations. + */ + GTEST_LOG_(INFO) << "HandleTest_012: start."; + (void)GetCmdResultFromPopen("hilog -w stop"); + std::string cmd = "hilog -w start"; + std::string str = "Persist task [jobid:1] start successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); + + cmd = "hilog -w stop"; + str = "Persist task [jobid:1] stop successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); + + std::string filename = "test"; + uint64_t length = 2 * 1024 * 1024; + std::string unit = "B"; + std::string compress = "zlib"; + int num = 25; + int jobid = 200; + cmd = "hilog -w start -f " + filename + " -l " + std::to_string(length) + unit + + " -n " + std::to_string(num) + " -m " + compress + " -j " + std::to_string(jobid); + str = "Persist task [jobid:" + std::to_string(jobid) + "] start successfully\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); + + cmd = "hilog -w query"; + str = std::to_string(jobid) + " init,core,app " + compress + " /data/log/hilog/" + filename + + " " + Size2Str(length) + " " + std::to_string(num) + "\n"; + EXPECT_EQ(GetCmdResultFromPopen(cmd), str); +} diff --git a/test/unittest/common/hilogtool_test.h b/test/unittest/common/hilogtool_test.h new file mode 100644 index 0000000000000000000000000000000000000000..bc7e6b64dd5ba66111609c80fe61ac8ce1eb5a3c --- /dev/null +++ b/test/unittest/common/hilogtool_test.h @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef HILOGTOOL_TEST_H +#define HILOGTOOL_TEST_H + +#include + +namespace OHOS { +namespace HiviewDFX { +class HilogToolTest : public testing::Test { +public: + static void SetUpTestCase() {} + static void TearDownTestCase() {} + void SetUp() {} + void TearDown() {}; +}; +} // namespace HiviewDFX +} // namespace OHOS +#endif // HILOGTOOL_TEST_H