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 #include "running_status_logger_test.h"
16 
17 #include <chrono>
18 #include <ctime>
19 #include <gmock/gmock.h>
20 #include <iostream>
21 #include <memory>
22 #include <thread>
23 
24 #include "file_util.h"
25 #include "hiview_global.h"
26 #include "plugin.h"
27 #include "running_status_logger.h"
28 #include "time_util.h"
29 
30 namespace OHOS {
31 namespace HiviewDFX {
32 namespace {
33 constexpr char TEST_LOG_DIR[] = "/data/log/hiview/sys_event_test";
34 constexpr int TIME_STAMP_OFFSET = 24 * 60 * 60; // one day
35 const char TIME_FORMAT[] = "%Y%m%d";
36 constexpr int SIZE_512 = 512; // test value
37 constexpr int SIZE_10 = 10; // test value
38 constexpr int SIZE_10240 = 10240; // test value
FormatTimeStamp()39 std::string FormatTimeStamp()
40 {
41     time_t lt;
42     (void)time(&lt);
43     return TimeUtil::TimestampFormatToDate(lt - TIME_STAMP_OFFSET, TIME_FORMAT);
44 }
45 
GetLogDir()46 std::string GetLogDir()
47 {
48     std::string workPath = HiviewGlobal::GetInstance()->GetHiViewDirectory(
49         HiviewContext::DirectoryType::WORK_DIRECTORY);
50     if (workPath.back() != '/') {
51         workPath = workPath + "/";
52     }
53     std::string logDestDir = workPath + "sys_event/";
54     if (!FileUtil::FileExists(logDestDir)) {
55         FileUtil::ForceCreateDirectory(logDestDir, FileUtil::FILE_PERM_770);
56     }
57     return logDestDir;
58 }
59 
GenerateInvalidFileName(int index)60 std::string GenerateInvalidFileName(int index)
61 {
62     return GetLogDir() + "runningstatus_2024_0" + std::to_string(index);
63 }
64 
GenerateYesterdayLogFileName()65 std::string GenerateYesterdayLogFileName()
66 {
67     return GetLogDir() + "runningstatus_" + FormatTimeStamp() + "_01";
68 }
69 
70 class HiviewTestContext : public HiviewContext {
71 public:
GetHiViewDirectory(DirectoryType type __UNUSED)72     std::string GetHiViewDirectory(DirectoryType type __UNUSED)
73     {
74         return TEST_LOG_DIR;
75     }
76 };
77 }
SetUpTestCase()78 void RunningStatusLoggerTest::SetUpTestCase()
79 {
80 }
81 
TearDownTestCase()82 void RunningStatusLoggerTest::TearDownTestCase()
83 {
84 }
85 
SetUp()86 void RunningStatusLoggerTest::SetUp()
87 {
88 }
89 
TearDown()90 void RunningStatusLoggerTest::TearDown()
91 {
92     (void)FileUtil::ForceRemoveDirectory(TEST_LOG_DIR);
93 }
94 
95 /**
96  * @tc.name: RunningStatusLoggerTest_001
97  * @tc.desc: write logs with size less than 2M into a local file
98  * @tc.type: FUNC
99  * @tc.require: issueI62PQY
100  */
101 HWTEST_F(RunningStatusLoggerTest, RunningStatusLoggerTest_001, testing::ext::TestSize.Level3)
102 {
103     /**
104      * @tc.steps: step1. init hiview global with customized hiview context
105      * @tc.steps: step2. init string with 2k size
106      * @tc.steps: step3. log string with total size less than 2M to local file
107      */
108     FileUtil::ForceRemoveDirectory(TEST_LOG_DIR);
109     ASSERT_TRUE(true);
110     HiviewTestContext hiviewTestContext;
111     HiviewGlobal::CreateInstance(hiviewTestContext);
112     std::string singleLine;
113     for (int index = 0; index < SIZE_512; index++) {
114         singleLine += "ohos";
115     }
116     for (int index = 0; index < SIZE_512; index++) {
117         RunningStatusLogger::GetInstance().Log(singleLine);
118     }
119     ASSERT_TRUE(true);
120 }
121 
122 /**
123  * @tc.name: RunningStatusLoggerTest_002
124  * @tc.desc: write logs with size less more 2M into local files
125  * @tc.type: FUNC
126  * @tc.require: issueI64Q4L
127  */
128 HWTEST_F(RunningStatusLoggerTest, RunningStatusLoggerTest_002, testing::ext::TestSize.Level3)
129 {
130     /**
131      * @tc.steps: step1. init hiview global with customized hiview context
132      * @tc.steps: step2. init string with 2K size
133      * @tc.steps: step3. log string with total size less than 2M to local file
134      * @tc.steps: step4. keep logging string with size more than 2M to local file
135      */
136     FileUtil::ForceRemoveDirectory(TEST_LOG_DIR);
137     ASSERT_TRUE(true);
138     HiviewTestContext hiviewTestContext;
139     HiviewGlobal::CreateInstance(hiviewTestContext);
140     std::string singleLine;
141     for (int index = 0; index < SIZE_512; index++) {
142         singleLine += "ohos";
143     }
144     for (int index = 0; index < SIZE_512; index++) {
145         RunningStatusLogger::GetInstance().Log(singleLine);
146     }
147     ASSERT_TRUE(true);
148     for (int index = 0; index < SIZE_512; index++) {
149         RunningStatusLogger::GetInstance().Log(singleLine);
150     }
151     ASSERT_TRUE(true);
152 }
153 
154 
155 /**
156  * @tc.name: RunningStatusLoggerTest_003
157  * @tc.desc: write logs with size more than 20M into local files
158  * @tc.type: FUNC
159  * @tc.require: issueI64Q4L
160  */
161 HWTEST_F(RunningStatusLoggerTest, RunningStatusLoggerTest_003, testing::ext::TestSize.Level3)
162 {
163     /**
164      * @tc.steps: step1. init hiview global with customized hiview context
165      * @tc.steps: step2. init string with 2K size
166      * @tc.steps: step3. log string with size more than 20M to local files
167      * @tc.steps: step4. keep logging string to local file
168      */
169     FileUtil::ForceRemoveDirectory(TEST_LOG_DIR);
170     ASSERT_TRUE(true);
171     HiviewTestContext hiviewTestContext;
172     HiviewGlobal::CreateInstance(hiviewTestContext);
173     std::string singleLine;
174     for (int index = 0; index < SIZE_512; index++) {
175         singleLine += "ohos";
176     }
177     for (int index = 0; index < SIZE_10240; index++) {
178         RunningStatusLogger::GetInstance().Log(singleLine);
179     }
180     ASSERT_TRUE(true);
181     for (int index = 0; index < SIZE_512; index++) {
182         RunningStatusLogger::GetInstance().Log(singleLine);
183     }
184     ASSERT_TRUE(true);
185 }
186 
187 /**
188  * @tc.name: RunningStatusLoggerTest_004
189  * @tc.desc: write logs with newer timestamp
190  * @tc.type: FUNC
191  * @tc.require: issueI64Q4L
192  */
193 HWTEST_F(RunningStatusLoggerTest, RunningStatusLoggerTest_004, testing::ext::TestSize.Level3)
194 {
195     /**
196      * @tc.steps: step1. init hiview global with customized hiview context
197      * @tc.steps: step2. init string with 2K size
198      * @tc.steps: step3. log string to a local file with yesterday's time stamp
199      * @tc.steps: step4. keep logging string to local file
200      */
201     FileUtil::ForceRemoveDirectory(TEST_LOG_DIR);
202     ASSERT_TRUE(true);
203     HiviewTestContext hiviewTestContext;
204     HiviewGlobal::CreateInstance(hiviewTestContext);
205     std::string singleLine;
206     for (int index = 0; index < SIZE_512; index++) {
207         singleLine += "ohos";
208     }
209     (void)FileUtil::SaveStringToFile(GenerateYesterdayLogFileName(), singleLine);
210     for (int index = 0; index < SIZE_10; index++) {
211         RunningStatusLogger::GetInstance().Log(singleLine);
212     }
213     ASSERT_TRUE(true);
214 }
215 
216 /**
217  * @tc.name: RunningStatusLoggerTest_005
218  * @tc.desc: time stamp format
219  * @tc.type: FUNC
220  * @tc.require: issueI62PQY
221  */
222 HWTEST_F(RunningStatusLoggerTest, RunningStatusLoggerTest_005, testing::ext::TestSize.Level3)
223 {
224     auto simpleTimeStamp = RunningStatusLogger::GetInstance().FormatTimeStamp(true);
225     ASSERT_TRUE(!simpleTimeStamp.empty());
226     auto normalTimeStamp = RunningStatusLogger::GetInstance().FormatTimeStamp(false);
227     ASSERT_TRUE(!normalTimeStamp.empty() && normalTimeStamp.find(':') != std::string::npos &&
228         normalTimeStamp.find('/') != std::string::npos);
229 }
230 
231 /**
232  * @tc.name: RunningStatusLoggerTest_007
233  * @tc.desc: write logs with newer timestamp
234  * @tc.type: FUNC
235  * @tc.require: issueI64Q4L
236  */
237 HWTEST_F(RunningStatusLoggerTest, RunningStatusLoggerTest_007, testing::ext::TestSize.Level3)
238 {
239     /**
240      * @tc.steps: step1. init hiview global with customized hiview context
241       * @tc.steps: step2. init string with 2K size
242      * @tc.steps: step3. init 3 local files with invalid name
243      * @tc.steps: step4. log string to a local file with yesterday's time stamp
244      * @tc.steps: step5. keep logging string to local file
245      */
246     FileUtil::ForceRemoveDirectory(TEST_LOG_DIR);
247     ASSERT_TRUE(true);
248     HiviewTestContext hiviewTestContext;
249     HiviewGlobal::CreateInstance(hiviewTestContext);
250     std::string singleLine;
251     for (int index = 0; index < SIZE_512; index++) {
252         singleLine += "ohos";
253     }
254     for (int index = 0; index < SIZE_10; index++) {
255         (void)FileUtil::SaveStringToFile(GenerateInvalidFileName(index), singleLine);
256     }
257     RunningStatusLogger::GetInstance().Log(singleLine);
258     ASSERT_TRUE(true);
259 }
260 
261 } // namespace HiviewDFX
262 } // namespace OHOS