148f512ceSopenharmony_ci/*
248f512ceSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
348f512ceSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
448f512ceSopenharmony_ci * you may not use this file except in compliance with the License.
548f512ceSopenharmony_ci * You may obtain a copy of the License at
648f512ceSopenharmony_ci *
748f512ceSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
848f512ceSopenharmony_ci *
948f512ceSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1048f512ceSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1148f512ceSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1248f512ceSopenharmony_ci * See the License for the specific language governing permissions and
1348f512ceSopenharmony_ci * limitations under the License.
1448f512ceSopenharmony_ci */
1548f512ceSopenharmony_ci#define HILOG_TAG "OptionDebugTest"
1648f512ceSopenharmony_ci
1748f512ceSopenharmony_ci#include "option_debug_test.h"
1848f512ceSopenharmony_ci
1948f512ceSopenharmony_ci#include <gmock/gmock.h>
2048f512ceSopenharmony_ci#include <gtest/gtest.h>
2148f512ceSopenharmony_ci#include <random>
2248f512ceSopenharmony_ci
2348f512ceSopenharmony_ci#include <hilog/log.h>
2448f512ceSopenharmony_ci
2548f512ceSopenharmony_ciusing namespace testing::ext;
2648f512ceSopenharmony_ciusing namespace std;
2748f512ceSopenharmony_ciusing namespace OHOS::HiviewDFX;
2848f512ceSopenharmony_cinamespace OHOS {
2948f512ceSopenharmony_cinamespace Developtools {
3048f512ceSopenharmony_cinamespace HiPerf {
3148f512ceSopenharmony_ciclass OptionDebugTest : public testing::Test {
3248f512ceSopenharmony_cipublic:
3348f512ceSopenharmony_ci    static void SetUpTestCase(void);
3448f512ceSopenharmony_ci    static void TearDownTestCase(void);
3548f512ceSopenharmony_ci    void SetUp();
3648f512ceSopenharmony_ci    void TearDown();
3748f512ceSopenharmony_ci    const std::string TEST_LOG_MESSAGE = "<HELLO_TEST_LOG_MESSAGE>";
3848f512ceSopenharmony_ci    void LogLevelTest(std::vector<std::string> args, DebugLevel level, bool result = true);
3948f512ceSopenharmony_ci    default_random_engine rnd_;
4048f512ceSopenharmony_ci};
4148f512ceSopenharmony_ci
4248f512ceSopenharmony_civoid OptionDebugTest::SetUpTestCase()
4348f512ceSopenharmony_ci{
4448f512ceSopenharmony_ci    DebugLogger::GetInstance()->Reset();
4548f512ceSopenharmony_ci}
4648f512ceSopenharmony_ci
4748f512ceSopenharmony_civoid OptionDebugTest::TearDownTestCase()
4848f512ceSopenharmony_ci{
4948f512ceSopenharmony_ci    DebugLogger::GetInstance()->Reset();
5048f512ceSopenharmony_ci}
5148f512ceSopenharmony_ci
5248f512ceSopenharmony_civoid OptionDebugTest::SetUp()
5348f512ceSopenharmony_ci{
5448f512ceSopenharmony_ci    Option::ClearMainOptions();
5548f512ceSopenharmony_ci    SubCommand::RegisterSubCommand(TEST_CMD_NOTHING,
5648f512ceSopenharmony_ci                                   std::make_unique<SubCommandTest>(TEST_CMD_NOTHING));
5748f512ceSopenharmony_ci    RegisterMainCommandDebug();
5848f512ceSopenharmony_ci}
5948f512ceSopenharmony_ci
6048f512ceSopenharmony_civoid OptionDebugTest::TearDown()
6148f512ceSopenharmony_ci{
6248f512ceSopenharmony_ci    SubCommand::ClearSubCommands();
6348f512ceSopenharmony_ci    Option::ClearMainOptions();
6448f512ceSopenharmony_ci}
6548f512ceSopenharmony_ci
6648f512ceSopenharmony_civoid OptionDebugTest::LogLevelTest(std::vector<std::string> args, const DebugLevel testlevel, bool result)
6748f512ceSopenharmony_ci{
6848f512ceSopenharmony_ci    // backup
6948f512ceSopenharmony_ci    DebugLevel oldLevel = DebugLogger::GetInstance()->GetLogLevel();
7048f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommands(args), result);
7148f512ceSopenharmony_ci    if (!result) {
7248f512ceSopenharmony_ci        return;
7348f512ceSopenharmony_ci    }
7448f512ceSopenharmony_ci
7548f512ceSopenharmony_ci    const std::string logMessage =
7648f512ceSopenharmony_ci        TEST_LOG_MESSAGE + std::to_string(rnd_()) + "_" + std::to_string(testlevel);
7748f512ceSopenharmony_ci    HLOGE("%s", logMessage.c_str());
7848f512ceSopenharmony_ci    HLOGW("%s", logMessage.c_str());
7948f512ceSopenharmony_ci    HLOGI("%s", logMessage.c_str());
8048f512ceSopenharmony_ci    HLOGD("%s", logMessage.c_str());
8148f512ceSopenharmony_ci    HLOGV("%s", logMessage.c_str());
8248f512ceSopenharmony_ci    HLOGM("%s", logMessage.c_str());
8348f512ceSopenharmony_ci
8448f512ceSopenharmony_ci    if (fflush(DebugLogger::GetInstance()->file_) != 0) {
8548f512ceSopenharmony_ci        HLOGD("fflush failed.");
8648f512ceSopenharmony_ci    }
8748f512ceSopenharmony_ci    std::string log = ReadFileToString(DebugLogger::GetInstance()->logPath_);
8848f512ceSopenharmony_ci    ASSERT_EQ(log.empty(), false);
8948f512ceSopenharmony_ci    // we have 6 level log
9048f512ceSopenharmony_ci    // so the logout line is : (all log level - curr log level) + curr log level self
9148f512ceSopenharmony_ci    EXPECT_EQ(SubStringCount(log, logMessage),
9248f512ceSopenharmony_ci              static_cast<size_t>(LEVEL_ERROR) - static_cast<size_t>(testlevel) + 1u);
9348f512ceSopenharmony_ci    if (HasFailure()) {
9448f512ceSopenharmony_ci        HLOGD("LogLevelTest failed.");
9548f512ceSopenharmony_ci    }
9648f512ceSopenharmony_ci    // restore
9748f512ceSopenharmony_ci    DebugLogger::GetInstance()->SetLogLevel(oldLevel);
9848f512ceSopenharmony_ci}
9948f512ceSopenharmony_ci
10048f512ceSopenharmony_ci/**
10148f512ceSopenharmony_ci * @tc.name: TestRegisterMainCommandDebug
10248f512ceSopenharmony_ci * @tc.desc:
10348f512ceSopenharmony_ci * @tc.type: FUNC
10448f512ceSopenharmony_ci */
10548f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, TestRegisterMainCommandDebug, TestSize.Level1)
10648f512ceSopenharmony_ci{
10748f512ceSopenharmony_ci    // see RegisterMainCommandDebug
10848f512ceSopenharmony_ci#if is_ohos
10948f512ceSopenharmony_ci    EXPECT_EQ(Option::GetMainOptions().size(), 8u);
11048f512ceSopenharmony_ci#else
11148f512ceSopenharmony_ci    EXPECT_EQ(Option::GetMainOptions().size(), 7u);
11248f512ceSopenharmony_ci#endif
11348f512ceSopenharmony_ci}
11448f512ceSopenharmony_ci
11548f512ceSopenharmony_ci/**
11648f512ceSopenharmony_ci * @tc.name: debug
11748f512ceSopenharmony_ci * @tc.desc:
11848f512ceSopenharmony_ci * @tc.type: FUNC
11948f512ceSopenharmony_ci */
12048f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, debug, TestSize.Level1)
12148f512ceSopenharmony_ci{
12248f512ceSopenharmony_ci    LogLevelTest({"--debug", TEST_CMD_NOTHING}, LEVEL_DEBUG);
12348f512ceSopenharmony_ci}
12448f512ceSopenharmony_ci
12548f512ceSopenharmony_ci/**
12648f512ceSopenharmony_ci * @tc.name: verbose
12748f512ceSopenharmony_ci * @tc.desc:
12848f512ceSopenharmony_ci * @tc.type: FUNC
12948f512ceSopenharmony_ci */
13048f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, verbose, TestSize.Level1)
13148f512ceSopenharmony_ci{
13248f512ceSopenharmony_ci    LogLevelTest({"--verbose", TEST_CMD_NOTHING}, LEVEL_VERBOSE);
13348f512ceSopenharmony_ci}
13448f512ceSopenharmony_ci
13548f512ceSopenharmony_ci/**
13648f512ceSopenharmony_ci * @tc.name: verbose
13748f512ceSopenharmony_ci * @tc.desc:
13848f512ceSopenharmony_ci * @tc.type: FUNC
13948f512ceSopenharmony_ci */
14048f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, much, TestSize.Level1)
14148f512ceSopenharmony_ci{
14248f512ceSopenharmony_ci    LogLevelTest({"--much", TEST_CMD_NOTHING}, LEVEL_MUCH);
14348f512ceSopenharmony_ci}
14448f512ceSopenharmony_ci
14548f512ceSopenharmony_ci/**
14648f512ceSopenharmony_ci * @tc.name: undebug
14748f512ceSopenharmony_ci * @tc.desc:
14848f512ceSopenharmony_ci * @tc.type: FUNC
14948f512ceSopenharmony_ci */
15048f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, undebug, TestSize.Level1)
15148f512ceSopenharmony_ci{
15248f512ceSopenharmony_ci    LogLevelTest({"--debug"}, LEVEL_DEBUG, false);
15348f512ceSopenharmony_ci}
15448f512ceSopenharmony_ci
15548f512ceSopenharmony_ci/**
15648f512ceSopenharmony_ci * @tc.name: unverbose
15748f512ceSopenharmony_ci * @tc.desc:
15848f512ceSopenharmony_ci * @tc.type: FUNC
15948f512ceSopenharmony_ci */
16048f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, unverbose, TestSize.Level1)
16148f512ceSopenharmony_ci{
16248f512ceSopenharmony_ci    LogLevelTest({"--verbose"}, LEVEL_VERBOSE, false);
16348f512ceSopenharmony_ci}
16448f512ceSopenharmony_ci
16548f512ceSopenharmony_ci/**
16648f512ceSopenharmony_ci * @tc.name: unmuch
16748f512ceSopenharmony_ci * @tc.desc:
16848f512ceSopenharmony_ci * @tc.type: FUNC
16948f512ceSopenharmony_ci */
17048f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, unmuch, TestSize.Level1)
17148f512ceSopenharmony_ci{
17248f512ceSopenharmony_ci    LogLevelTest({"--much"}, LEVEL_MUCH, false);
17348f512ceSopenharmony_ci}
17448f512ceSopenharmony_ci
17548f512ceSopenharmony_ci/**
17648f512ceSopenharmony_ci * @tc.name: mixlog
17748f512ceSopenharmony_ci * @tc.desc:
17848f512ceSopenharmony_ci * @tc.type: FUNC
17948f512ceSopenharmony_ci */
18048f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, mixlog, TestSize.Level1)
18148f512ceSopenharmony_ci{
18248f512ceSopenharmony_ci    StdoutRecord stdoutRecord;
18348f512ceSopenharmony_ci    stdoutRecord.Start();
18448f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommands({"--mixlog", TEST_CMD_NOTHING}), true);
18548f512ceSopenharmony_ci
18648f512ceSopenharmony_ci    const std::string logMessage = TEST_LOG_MESSAGE + std::to_string(rnd_());
18748f512ceSopenharmony_ci    HLOGD("%s", logMessage.c_str());
18848f512ceSopenharmony_ci    std::string stringOut = stdoutRecord.Stop();
18948f512ceSopenharmony_ci    EXPECT_NE(stringOut.find(logMessage), std::string::npos);
19048f512ceSopenharmony_ci
19148f512ceSopenharmony_ci    // close it
19248f512ceSopenharmony_ci    DebugLogger::GetInstance()->SetMixLogOutput(false);
19348f512ceSopenharmony_ci}
19448f512ceSopenharmony_ci
19548f512ceSopenharmony_ci/**
19648f512ceSopenharmony_ci * @tc.name: logpath
19748f512ceSopenharmony_ci * @tc.desc:
19848f512ceSopenharmony_ci * @tc.type: FUNC
19948f512ceSopenharmony_ci */
20048f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, logpath, TestSize.Level1)
20148f512ceSopenharmony_ci{
20248f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommands({"--logpath", "./log.temp.txt", TEST_CMD_NOTHING}), true);
20348f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommands({"--logpath", DEFAULT_LOG_PATH, TEST_CMD_NOTHING}), true);
20448f512ceSopenharmony_ci}
20548f512ceSopenharmony_ci
20648f512ceSopenharmony_ci/**
20748f512ceSopenharmony_ci * @tc.name: unlogpath
20848f512ceSopenharmony_ci * @tc.desc:
20948f512ceSopenharmony_ci * @tc.type: FUNC
21048f512ceSopenharmony_ci */
21148f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, unlogpath, TestSize.Level1)
21248f512ceSopenharmony_ci{
21348f512ceSopenharmony_ci    EXPECT_EQ(Command::DispatchCommands({"--logpath"}), false);
21448f512ceSopenharmony_ci}
21548f512ceSopenharmony_ci
21648f512ceSopenharmony_ci/**
21748f512ceSopenharmony_ci * @tc.name: logtag
21848f512ceSopenharmony_ci * @tc.desc:
21948f512ceSopenharmony_ci * @tc.type: FUNC
22048f512ceSopenharmony_ci */
22148f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, logtag, TestSize.Level1)
22248f512ceSopenharmony_ci{
22348f512ceSopenharmony_ci    LogLevelTest({"--logtag", "OptionDebugTest", TEST_CMD_NOTHING}, LEVEL_MUCH);
22448f512ceSopenharmony_ci    LogLevelTest({"--logtag", "123", TEST_CMD_NOTHING}, DebugLogger::GetInstance()->GetLogLevel());
22548f512ceSopenharmony_ci}
22648f512ceSopenharmony_ci
22748f512ceSopenharmony_ci/**
22848f512ceSopenharmony_ci * @tc.name: unlogtag
22948f512ceSopenharmony_ci * @tc.desc:
23048f512ceSopenharmony_ci * @tc.type: FUNC
23148f512ceSopenharmony_ci */
23248f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, unlogtag, TestSize.Level1)
23348f512ceSopenharmony_ci{
23448f512ceSopenharmony_ci    LogLevelTest({"--logtag"}, LEVEL_MUCH, false);
23548f512ceSopenharmony_ci}
23648f512ceSopenharmony_ci
23748f512ceSopenharmony_ci/**
23848f512ceSopenharmony_ci * @tc.name: logDisabled
23948f512ceSopenharmony_ci * @tc.desc:
24048f512ceSopenharmony_ci * @tc.type: FUNC
24148f512ceSopenharmony_ci */
24248f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, logDisabled, TestSize.Level1)
24348f512ceSopenharmony_ci{
24448f512ceSopenharmony_ci    // no log will save in log file.
24548f512ceSopenharmony_ci    LogLevelTest({"--nodebug", TEST_CMD_NOTHING}, LEVEL_FATAL);
24648f512ceSopenharmony_ci    DebugLogger::GetInstance()->Disable(false);
24748f512ceSopenharmony_ci}
24848f512ceSopenharmony_ci
24948f512ceSopenharmony_ci/**
25048f512ceSopenharmony_ci * @tc.name: unlogDisabled
25148f512ceSopenharmony_ci * @tc.desc:
25248f512ceSopenharmony_ci * @tc.type: FUNC
25348f512ceSopenharmony_ci */
25448f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, unlogDisabled, TestSize.Level1)
25548f512ceSopenharmony_ci{
25648f512ceSopenharmony_ci    // no log will save in log file.
25748f512ceSopenharmony_ci    LogLevelTest({"--nodebug"}, LEVEL_FATAL, false);
25848f512ceSopenharmony_ci    DebugLogger::GetInstance()->Disable(false);
25948f512ceSopenharmony_ci}
26048f512ceSopenharmony_ci
26148f512ceSopenharmony_ci/**
26248f512ceSopenharmony_ci * @tc.name: hilog
26348f512ceSopenharmony_ci * @tc.desc:
26448f512ceSopenharmony_ci * @tc.type: FUNC
26548f512ceSopenharmony_ci */
26648f512ceSopenharmony_ciHWTEST_F(OptionDebugTest, hilog, TestSize.Level1)
26748f512ceSopenharmony_ci{
26848f512ceSopenharmony_ci#if is_ohos
26948f512ceSopenharmony_ci    // no log will save in log file.
27048f512ceSopenharmony_ci    LogLevelTest({"--hilog", TEST_CMD_NOTHING}, LEVEL_FATAL);
27148f512ceSopenharmony_ci
27248f512ceSopenharmony_ci    DebugLogger::GetInstance()->EnableHiLog(false);
27348f512ceSopenharmony_ci#endif
27448f512ceSopenharmony_ci}
27548f512ceSopenharmony_ci} // namespace HiPerf
27648f512ceSopenharmony_ci} // namespace Developtools
27748f512ceSopenharmony_ci} // namespace OHOS
278