1 /*
2 * Copyright (c) 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
16 #include "smart_parser_module_test.h"
17
18 #include <cstdio>
19 #include <ctime>
20 #include <errors.h>
21 #include <map>
22 #include <string>
23
24 #include "feature_analysis.h"
25 #include "file_util.h"
26 #include "log_util.h"
27 #include "smart_parser.h"
28 #include "string_util.h"
29
30 using namespace std;
31 namespace OHOS {
32 namespace HiviewDFX {
33 using namespace testing::ext;
34 static const std::string TEST_CONFIG = "/data/test/test_data/SmartParser/common/";
35
SetUpTestCase(void)36 void SmartParserModuleTest::SetUpTestCase(void) {}
37
TearDownTestCase(void)38 void SmartParserModuleTest::TearDownTestCase(void) {}
39
SetUp(void)40 void SmartParserModuleTest::SetUp(void) {}
41
TearDown(void)42 void SmartParserModuleTest::TearDown(void) {}
43
44 /**
45 * @tc.name: SmartParserTest001
46 * @tc.desc: process cpp_crash fault, this case match FeatureListForReliability.Json
47 * @tc.type: FUNC
48 * @tc.require:
49 * @tc.author: liuwei
50 */
51 HWTEST_F(SmartParserModuleTest, SmartParserTest001, TestSize.Level1)
52 {
53 /**
54 * @tc.steps: step1. Set taskSheet fault log path and eventid.
55 */
56 std::string faultFile = LogUtil::SMART_PARSER_TEST_DIR +
57 "/SmartParserTest001/cppcrash-com.ohos.launcher-20010025-19700324235211";
58 std::string trustStack = LogUtil::SMART_PARSER_TEST_DIR + "/SmartParserTest001/trace.txt";
59
60 ASSERT_EQ(FileUtil::FileExists(faultFile), true);
61 ASSERT_EQ(FileUtil::FileExists(trustStack), true);
62
63 /**
64 * @tc.steps: step2. smart parser process fault log
65 */
66 auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "CPP_CRASH");
67
68 /**
69 * @tc.steps: step3. check the result of eventinfo for fault.
70 * @tc.expected: step3. equal to correct answer.
71 */
72 EXPECT_STREQ(eventInfos["PNAME"].c_str(), "com.ohos.launcher");
73 EXPECT_EQ(eventInfos["END_STACK"].size() > 0, true);
74 std::stringstream buff;
75 LogUtil::ReadFileBuff(trustStack, buff);
76 std::vector<std::string> trace;
77 StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
78 std::string line;
79 size_t num = 0;
80 while (getline(buff, line) && num < trace.size()) {
81 EXPECT_STREQ(line.c_str(), trace[num++].c_str());
82 }
83 }
84
85 /**
86 * @tc.name: SmartParserTest002
87 * @tc.desc: process JS_ERROR fault, this case match FeatureListForReliability.Json
88 * @tc.type: FUNC
89 * @tc.require:
90 * @tc.author: liuwei
91 */
92 HWTEST_F(SmartParserModuleTest, SmartParserTest002, TestSize.Level1)
93 {
94 /**
95 * @tc.steps: step1. Set taskSheet fault log path and eventid.
96 */
97 std::string faultFile = LogUtil::SMART_PARSER_TEST_DIR +
98 "/SmartParserTest002/jscrash-com.example.jsinject-20010041-19700424183123";
99 std::string trustStack = LogUtil::SMART_PARSER_TEST_DIR + "/SmartParserTest002/trace.txt";
100 ASSERT_EQ(FileUtil::FileExists(faultFile), true);
101 ASSERT_EQ(FileUtil::FileExists(trustStack), true);
102
103 /**
104 * @tc.steps: step2. smart parser process fault log
105 */
106 auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "JS_ERROR");
107
108 /**
109 * @tc.steps: step3. check the result of eventinfo for fault.
110 * @tc.expected: step3. equal to correct answer.
111 */
112 EXPECT_STREQ(eventInfos["PNAME"].c_str(), "com.example.jsinject");
113 EXPECT_EQ(eventInfos["END_STACK"].size() > 0, true);
114 std::stringstream buff;
115 LogUtil::ReadFileBuff(trustStack, buff);
116 std::vector<std::string> trace;
117 StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
118 std::string line;
119 size_t num = 0;
120 while (getline(buff, line) && num < trace.size()) {
121 EXPECT_STREQ(line.c_str(), trace[num++].c_str());
122 }
123 }
124
125 /**
126 * @tc.name: SmartParserTest003
127 * @tc.desc: process freeze fault, this case match FeatureListForReliability.Json
128 * @tc.type: FUNC
129 * @tc.require:
130 * @tc.author: liuwei
131 */
132 HWTEST_F(SmartParserModuleTest, SmartParserTest003, TestSize.Level1)
133 {
134 /**
135 * @tc.steps: step1. Set taskSheet fault log path and eventid.
136 */
137 std::string faultFile = LogUtil::SMART_PARSER_TEST_DIR +
138 "/SmartParserTest003/appfreeze-com.example.jsinject-20010039-19700326211815";
139 std::string traceFile = LogUtil::SMART_PARSER_TEST_DIR + "/SmartParserTest003/trace.txt";
140 ASSERT_EQ(FileUtil::FileExists(faultFile), true);
141 ASSERT_EQ(FileUtil::FileExists(traceFile), true);
142
143 /**
144 * @tc.steps: step2. smart parser process crash fault log
145 */
146 auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "APP_FREEZE");
147
148 /**
149 * @tc.steps: step3. check the result of eventinfo for fault.
150 * @tc.expected: step3. equal to correct answer.
151 */
152 EXPECT_EQ(eventInfos["END_STACK"].size() > 0, true);
153 std::string content;
154 if (!FileUtil::LoadStringFromFile(traceFile, content)) {
155 printf("read logFile: %s failed", traceFile.c_str());
156 return;
157 }
158 std::stringstream buff(content);
159 std::vector<std::string> trace;
160 StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
161 std::string line;
162 size_t num = 0;
163 while (getline(buff, line) && num < trace.size()) {
164 EXPECT_STREQ(trace[num++].c_str(), line.c_str());
165 }
166 }
167
168 /**
169 * @tc.name: SmartParserTest004
170 * @tc.desc: process PANIC fault, this case match FeatureAnalysisForRebootsys.Json.
171 * 1. fault log should can be read;
172 * 2. FeatureAnalysisForRebootsys.Json should match the json file in perforce.
173 * @tc.type: FUNC
174 * @tc.require:
175 * @tc.author: liuwei
176 */
177 HWTEST_F(SmartParserModuleTest, SmartParserTest004, TestSize.Level1)
178 {
179 /**
180 * @tc.steps: step1. Set taskSheet fault log path and eventid.
181 */
182 std::string faultFile = LogUtil::SMART_PARSER_TEST_DIR + "/SmartParserTest004/dmesg-ramoops-0";
183 std::string traceFile = LogUtil::SMART_PARSER_TEST_DIR + "/SmartParserTest004/trace.txt";
184 ASSERT_EQ(FileUtil::FileExists(faultFile), true);
185 ASSERT_EQ(FileUtil::FileExists(traceFile), true);
186 std::stringstream buff;
187 LogUtil::ReadFileBuff(traceFile, buff);
188
189 /**
190 * @tc.steps: step2. smart parser process crash fault log
191 */
192 auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "PANIC");
193
194 std::vector<std::string> trace;
195 StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
196 std::string line;
197 size_t num = 0;
198 while (getline(buff, line) && num < trace.size()) {
199 EXPECT_STREQ(trace[num++].c_str(), line.c_str());
200 }
201 }
202
203 /**
204 * @tc.name: SmartParserTest005
205 * @tc.desc: process HWWATCHDOG fault, this case match FeatureAnalysisForRebootsys.Json.
206 * 1. fault log should can be read;
207 * 2. FeatureAnalysisForRebootsys.Json should match the json file in perforce.
208 * @tc.type: FUNC
209 * @tc.require:
210 * @tc.author: liuwei
211 */
212 HWTEST_F(SmartParserModuleTest, SmartParserTest005, TestSize.Level1)
213 {
214 /**
215 * @tc.steps: step1. Set taskSheet fault log path and eventid.
216 */
217 std::string faultFile = LogUtil::SMART_PARSER_TEST_DIR + "/SmartParserTest005/dmesg-ramoops-0";
218 std::string traceFile = LogUtil::SMART_PARSER_TEST_DIR + "/SmartParserTest005/trace.txt";
219 ASSERT_EQ(FileUtil::FileExists(faultFile), true);
220 ASSERT_EQ(FileUtil::FileExists(traceFile), true);
221 std::stringstream buff;
222 LogUtil::ReadFileBuff(traceFile, buff);
223
224 /**
225 * @tc.steps: step2. smart parser process crash fault log
226 */
227 auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "HWWATCHDOG");
228
229 std::vector<std::string> trace;
230 StringUtil::SplitStr(eventInfos["END_STACK"], LogUtil::SPLIT_PATTERN, trace, false, false);
231 std::string line;
232 size_t num = 0;
233 while (getline(buff, line) && num < trace.size()) {
234 EXPECT_STREQ(trace[num++].c_str(), line.c_str());
235 }
236 }
237
238 /**
239 * @tc.name: SmartParserTest006
240 * @tc.desc: process HWWATCHDOG fault, this case match FeatureAnalysisForRebootsys.Json.
241 * 1. fault log should can be read;
242 * 2. FeatureAnalysisForRebootsys.Json should match the json file in perforce.
243 * @tc.type: FUNC
244 * @tc.require:
245 * @tc.author: liuwei
246 */
247 HWTEST_F(SmartParserModuleTest, SmartParserTest006, TestSize.Level1)
248 {
249 /**
250 * @tc.steps: step1. Set taskSheet fault log path and eventid.
251 */
252 std::string faultFile = LogUtil::SMART_PARSER_TEST_DIR + "/SmartParserTest005/dmesg-ramoops-1";
253 ASSERT_EQ(FileUtil::FileExists(faultFile), false);
254
255 /**
256 * @tc.steps: step2. smart parser process crash fault log
257 */
258 auto eventInfos = SmartParser::Analysis(faultFile, TEST_CONFIG, "HWWATCHDOG");
259 ASSERT_EQ(eventInfos.empty(), true);
260 }
261
262 /**
263 * @tc.name: SmartParserTest007
264 * @tc.desc: process test fault, this case match FeatureAnalysisForRebootsys.Json.
265 * 1. fault log should can be read;
266 * 2. FeatureAnalysisForRebootsys.Json should match the json file in perforce.
267 * @tc.type: FUNC
268 * @tc.require:
269 * @tc.author: liuwei
270 */
271 HWTEST_F(SmartParserModuleTest, SmartParserTest007, TestSize.Level1)
272 {
273 /**
274 * @tc.steps: step1. Set taskSheet fault log path and eventid.
275 */
276 auto eventInfos = SmartParser::Analysis("", "/system/etc/hiview/reliability", "TEST");
277 ASSERT_EQ(eventInfos.empty(), true);
278 eventInfos = SmartParser::Analysis("", "", "TEST");
279 ASSERT_EQ(eventInfos.empty(), true);
280 eventInfos = SmartParser::Analysis("", "", "");
281 ASSERT_EQ(eventInfos.empty(), true);
282 eventInfos = SmartParser::Analysis("test", "test", "test");
283 ASSERT_EQ(eventInfos.empty(), true);
284 }
285 } // namespace HiviewDFX
286 } // namespace OHOS
287