• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #define HILOG_TAG "OptionDebugTest"
16 
17 #include "option_debug_test.h"
18 
19 #include <gmock/gmock.h>
20 #include <gtest/gtest.h>
21 #include <random>
22 
23 #include <hilog/log.h>
24 
25 using namespace testing::ext;
26 using namespace std;
27 using namespace OHOS::HiviewDFX;
28 namespace OHOS {
29 namespace Developtools {
30 namespace HiPerf {
31 class OptionDebugTest : public testing::Test {
32 public:
33     static void SetUpTestCase(void);
34     static void TearDownTestCase(void);
35     void SetUp();
36     void TearDown();
37     const std::string TEST_LOG_MESSAGE = "<HELLO_TEST_LOG_MESSAGE>";
38     void LogLevelTest(std::vector<std::string> args, DebugLevel level);
39     default_random_engine rnd_;
40 };
41 
SetUpTestCase()42 void OptionDebugTest::SetUpTestCase()
43 {
44     DebugLogger::GetInstance()->Reset();
45 }
46 
TearDownTestCase()47 void OptionDebugTest::TearDownTestCase()
48 {
49     DebugLogger::GetInstance()->Reset();
50 }
51 
SetUp()52 void OptionDebugTest::SetUp()
53 {
54     Option::ClearMainOptions();
55     SubCommand::RegisterSubCommand(TEST_CMD_NOTHING,
56                                    std::make_unique<SubCommandTest>(TEST_CMD_NOTHING));
57     RegisterMainCommandDebug();
58 }
59 
TearDown()60 void OptionDebugTest::TearDown()
61 {
62     SubCommand::ClearSubCommands();
63     Option::ClearMainOptions();
64 }
65 
LogLevelTest(std::vector<std::string> args,const DebugLevel testlevel)66 void OptionDebugTest::LogLevelTest(std::vector<std::string> args, const DebugLevel testlevel)
67 {
68     // backup
69     DebugLevel oldLevel = DebugLogger::GetInstance()->GetLogLevel();
70     EXPECT_EQ(Command::DispatchCommands(args), true);
71 
72     const std::string logMessage =
73         TEST_LOG_MESSAGE + std::to_string(rnd_()) + "_" + std::to_string(testlevel);
74     HLOGE("%s", logMessage.c_str());
75     HLOGW("%s", logMessage.c_str());
76     HLOGI("%s", logMessage.c_str());
77     HLOGD("%s", logMessage.c_str());
78     HLOGV("%s", logMessage.c_str());
79     HLOGM("%s", logMessage.c_str());
80 
81     if (fflush(DebugLogger::GetInstance()->file_) != 0) {
82         HLOGD("fflush failed.");
83     }
84     std::string log = ReadFileToString(DebugLogger::GetInstance()->logPath_);
85     ASSERT_EQ(log.empty(), false);
86     // we have 6 level log
87     // so the logout line is : (all log level - curr log level) + curr log level self
88     EXPECT_EQ(SubStringCount(log, logMessage),
89               static_cast<size_t>(LEVEL_ERROR) - static_cast<size_t>(testlevel) + 1u);
90     if (HasFailure()) {
91         HLOGD("LogLevelTest failed.");
92     }
93     // restore
94     DebugLogger::GetInstance()->SetLogLevel(oldLevel);
95 }
96 
97 /**
98  * @tc.name: TestRegisterMainCommandDebug
99  * @tc.desc:
100  * @tc.type: FUNC
101  */
102 HWTEST_F(OptionDebugTest, TestRegisterMainCommandDebug, TestSize.Level1)
103 {
104     // see RegisterMainCommandDebug
105 #if is_ohos
106     EXPECT_EQ(Option::GetMainOptions().size(), 8u);
107 #else
108     EXPECT_EQ(Option::GetMainOptions().size(), 7u);
109 #endif
110 }
111 
112 /**
113  * @tc.name: debug
114  * @tc.desc:
115  * @tc.type: FUNC
116  */
117 HWTEST_F(OptionDebugTest, debug, TestSize.Level1)
118 {
119     LogLevelTest({"--debug", TEST_CMD_NOTHING}, LEVEL_DEBUG);
120 }
121 
122 /**
123  * @tc.name: verbose
124  * @tc.desc:
125  * @tc.type: FUNC
126  */
127 HWTEST_F(OptionDebugTest, verbose, TestSize.Level1)
128 {
129     LogLevelTest({"--verbose", TEST_CMD_NOTHING}, LEVEL_VERBOSE);
130 }
131 
132 /**
133  * @tc.name: verbose
134  * @tc.desc:
135  * @tc.type: FUNC
136  */
137 HWTEST_F(OptionDebugTest, much, TestSize.Level1)
138 {
139     LogLevelTest({"--much", TEST_CMD_NOTHING}, LEVEL_MUCH);
140 }
141 
142 /**
143  * @tc.name: mixlog
144  * @tc.desc:
145  * @tc.type: FUNC
146  */
147 HWTEST_F(OptionDebugTest, mixlog, TestSize.Level1)
148 {
149     StdoutRecord stdoutRecord;
150     stdoutRecord.Start();
151     EXPECT_EQ(Command::DispatchCommands({"--mixlog", TEST_CMD_NOTHING}), true);
152 
153     const std::string logMessage = TEST_LOG_MESSAGE + std::to_string(rnd_());
154     HLOGD("%s", logMessage.c_str());
155     std::string stringOut = stdoutRecord.Stop();
156     EXPECT_NE(stringOut.find(logMessage), std::string::npos);
157 
158     // close it
159     DebugLogger::GetInstance()->SetMixLogOutput(false);
160 }
161 
162 /**
163  * @tc.name: logpath
164  * @tc.desc:
165  * @tc.type: FUNC
166  */
167 HWTEST_F(OptionDebugTest, logpath, TestSize.Level1)
168 {
169     EXPECT_EQ(Command::DispatchCommands({"--logpath", "./log.temp.txt", TEST_CMD_NOTHING}), true);
170     EXPECT_EQ(Command::DispatchCommands({"--logpath", DEFAULT_LOG_PATH, TEST_CMD_NOTHING}), true);
171 }
172 
173 /**
174  * @tc.name: logtag
175  * @tc.desc:
176  * @tc.type: FUNC
177  */
178 HWTEST_F(OptionDebugTest, logtag, TestSize.Level1)
179 {
180     LogLevelTest({"--logtag", "OptionDebugTest", TEST_CMD_NOTHING}, LEVEL_MUCH);
181     LogLevelTest({"--logtag", "123", TEST_CMD_NOTHING}, DebugLogger::GetInstance()->GetLogLevel());
182 }
183 
184 /**
185  * @tc.name: logDisabled
186  * @tc.desc:
187  * @tc.type: FUNC
188  */
189 HWTEST_F(OptionDebugTest, logDisabled, TestSize.Level1)
190 {
191     // no log will save in log file.
192     LogLevelTest({"--nodebug", TEST_CMD_NOTHING}, LEVEL_FATAL);
193     DebugLogger::GetInstance()->Disable(false);
194 }
195 
196 /**
197  * @tc.name: hilog
198  * @tc.desc:
199  * @tc.type: FUNC
200  */
201 HWTEST_F(OptionDebugTest, hilog, TestSize.Level1)
202 {
203 #if is_ohos
204     // no log will save in log file.
205     LogLevelTest({"--hilog", TEST_CMD_NOTHING}, LEVEL_FATAL);
206 
207     DebugLogger::GetInstance()->EnableHiLog(false);
208 #endif
209 }
210 } // namespace HiPerf
211 } // namespace Developtools
212 } // namespace OHOS
213