• 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 <gtest/gtest.h>
20 #include <random>
21 
22 #include <hilog/log.h>
23 
24 using namespace testing::ext;
25 namespace OHOS {
26 namespace Developtools {
27 namespace HiPerf {
28 class OptionDebugTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34     const std::string TEST_LOG_MESSAGE = "<HELLO_TEST_LOG_MESSAGE>";
35     void LogLevelTest(std::vector<std::string> args, DebugLevel level, bool result = true);
36     std::default_random_engine rnd_;
37 };
38 
SetUpTestCase()39 void OptionDebugTest::SetUpTestCase()
40 {
41     DebugLogger::GetInstance()->Reset();
42 }
43 
TearDownTestCase()44 void OptionDebugTest::TearDownTestCase()
45 {
46     DebugLogger::GetInstance()->Reset();
47 }
48 
SetUp()49 void OptionDebugTest::SetUp()
50 {
51     Option::ClearMainOptions();
52     SubCommand::RegisterSubCommand(TEST_CMD_NOTHING,
53                                    std::make_unique<SubCommandTest>(TEST_CMD_NOTHING));
54     RegisterMainCommandDebug();
55 }
56 
TearDown()57 void OptionDebugTest::TearDown()
58 {
59     SubCommand::ClearSubCommands();
60     Option::ClearMainOptions();
61 }
62 
LogLevelTest(std::vector<std::string> args,const DebugLevel testlevel,bool result)63 void OptionDebugTest::LogLevelTest(std::vector<std::string> args, const DebugLevel testlevel, bool result)
64 {
65     // backup
66     DebugLevel oldLevel = DebugLogger::GetInstance()->GetLogLevel();
67     EXPECT_EQ(Command::DispatchCommands(args), result);
68     if (!result) {
69         return;
70     }
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: undebug
144  * @tc.desc:
145  * @tc.type: FUNC
146  */
147 HWTEST_F(OptionDebugTest, undebug, TestSize.Level1)
148 {
149     LogLevelTest({"--debug"}, LEVEL_DEBUG, false);
150 }
151 
152 /**
153  * @tc.name: unverbose
154  * @tc.desc:
155  * @tc.type: FUNC
156  */
157 HWTEST_F(OptionDebugTest, unverbose, TestSize.Level1)
158 {
159     LogLevelTest({"--verbose"}, LEVEL_VERBOSE, false);
160 }
161 
162 /**
163  * @tc.name: unmuch
164  * @tc.desc:
165  * @tc.type: FUNC
166  */
167 HWTEST_F(OptionDebugTest, unmuch, TestSize.Level1)
168 {
169     LogLevelTest({"--much"}, LEVEL_MUCH, false);
170 }
171 
172 /**
173  * @tc.name: mixlog
174  * @tc.desc:
175  * @tc.type: FUNC
176  */
177 HWTEST_F(OptionDebugTest, mixlog, TestSize.Level1)
178 {
179     StdoutRecord stdoutRecord;
180     stdoutRecord.Start();
181     EXPECT_EQ(Command::DispatchCommands({"--mixlog", TEST_CMD_NOTHING}), true);
182 
183     const std::string logMessage = TEST_LOG_MESSAGE + std::to_string(rnd_());
184     HLOGD("%s", logMessage.c_str());
185     std::string stringOut = stdoutRecord.Stop();
186     EXPECT_NE(stringOut.find(logMessage), std::string::npos);
187 
188     // close it
189     DebugLogger::GetInstance()->SetMixLogOutput(false);
190 }
191 
192 /**
193  * @tc.name: logpath
194  * @tc.desc:
195  * @tc.type: FUNC
196  */
197 HWTEST_F(OptionDebugTest, logpath, TestSize.Level1)
198 {
199     EXPECT_EQ(Command::DispatchCommands({"--logpath", "./log.temp.txt", TEST_CMD_NOTHING}), true);
200     EXPECT_EQ(Command::DispatchCommands({"--logpath", DEFAULT_LOG_PATH, TEST_CMD_NOTHING}), true);
201 }
202 
203 /**
204  * @tc.name: unlogpath
205  * @tc.desc:
206  * @tc.type: FUNC
207  */
208 HWTEST_F(OptionDebugTest, unlogpath, TestSize.Level1)
209 {
210     EXPECT_EQ(Command::DispatchCommands({"--logpath"}), false);
211 }
212 
213 /**
214  * @tc.name: logtag
215  * @tc.desc:
216  * @tc.type: FUNC
217  */
218 HWTEST_F(OptionDebugTest, logtag, TestSize.Level1)
219 {
220     LogLevelTest({"--logtag", "OptionDebugTest", TEST_CMD_NOTHING}, LEVEL_MUCH);
221     LogLevelTest({"--logtag", "123", TEST_CMD_NOTHING}, DebugLogger::GetInstance()->GetLogLevel());
222 }
223 
224 /**
225  * @tc.name: unlogtag
226  * @tc.desc:
227  * @tc.type: FUNC
228  */
229 HWTEST_F(OptionDebugTest, unlogtag, TestSize.Level1)
230 {
231     LogLevelTest({"--logtag"}, LEVEL_MUCH, false);
232 }
233 
234 /**
235  * @tc.name: logDisabled
236  * @tc.desc:
237  * @tc.type: FUNC
238  */
239 HWTEST_F(OptionDebugTest, logDisabled, TestSize.Level1)
240 {
241     // no log will save in log file.
242     LogLevelTest({"--nodebug", TEST_CMD_NOTHING}, LEVEL_FATAL);
243     DebugLogger::GetInstance()->Disable(false);
244 }
245 
246 /**
247  * @tc.name: unlogDisabled
248  * @tc.desc:
249  * @tc.type: FUNC
250  */
251 HWTEST_F(OptionDebugTest, unlogDisabled, TestSize.Level1)
252 {
253     // no log will save in log file.
254     LogLevelTest({"--nodebug"}, LEVEL_FATAL, false);
255     DebugLogger::GetInstance()->Disable(false);
256 }
257 
258 /**
259  * @tc.name: hilog
260  * @tc.desc:
261  * @tc.type: FUNC
262  */
263 HWTEST_F(OptionDebugTest, hilog, TestSize.Level1)
264 {
265 #if is_ohos
266     // no log will save in log file.
267     LogLevelTest({"--hilog", TEST_CMD_NOTHING}, LEVEL_FATAL);
268 
269     DebugLogger::GetInstance()->EnableHiLog(false);
270 #endif
271 }
272 } // namespace HiPerf
273 } // namespace Developtools
274 } // namespace OHOS
275