1 /*
2 * Copyright (c) 2024 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 <ctime>
17 #include <gtest/gtest.h>
18 #include <securec.h>
19 #include <string>
20 #include <vector>
21
22 #include "crash_exception.h"
23 #include "crash_exception_listener.h"
24 #include "dfx_errors.h"
25 #include "dfx_util.h"
26 #include "hisysevent_manager.h"
27
28 using namespace OHOS::HiviewDFX;
29 using namespace testing::ext;
30 using namespace std;
31
32 namespace OHOS {
33 namespace HiviewDFX {
34 class CrashExceptionTest : public testing::Test {
35 public:
SetUpTestCase(void)36 static void SetUpTestCase(void) {}
TearDownTestCase(void)37 static void TearDownTestCase(void) {}
SetUp()38 void SetUp() {}
TearDown()39 void TearDown() {}
40 };
41
42 static constexpr int32_t TEST_PROCESS_ID = 1234;
43 static constexpr int32_t TEST_UID = 5678;
44
45 /**
46 * @tc.name: CrashExceptionTest001
47 * @tc.desc: test ReportCrashException
48 * @tc.type: FUNC
49 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest001, TestSize.Level2)50 HWTEST_F(CrashExceptionTest, CrashExceptionTest001, TestSize.Level2)
51 {
52 GTEST_LOG_(INFO) << "CrashExceptionTest001: start.";
53 char testProcessName[] = "process_name_ptr";
54 std::shared_ptr<CrashExceptionListener> crashListener = std::make_shared<CrashExceptionListener>();
55 ListenerRule tagRule("RELIABILITY", "CPP_CRASH_EXCEPTION", RuleType::WHOLE_WORD);
56 std::vector<ListenerRule> sysRules;
57 sysRules.push_back(tagRule);
58 HiSysEventManager::AddListener(crashListener, sysRules);
59 crashListener->SetKeyWord(testProcessName);
60 ReportCrashException(testProcessName, TEST_PROCESS_ID, TEST_UID, CrashExceptionCode::CRASH_UNKNOWN);
61 ASSERT_TRUE(crashListener->CheckKeywordInReasons());
62 HiSysEventManager::RemoveListener(crashListener);
63 GTEST_LOG_(INFO) << "CrashExceptionTest001: end.";
64 }
65
66 /**
67 * @tc.name: CrashExceptionTest002
68 * @tc.desc: test ReportCrashException, error code is success.
69 * @tc.type: FUNC
70 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest002, TestSize.Level2)71 HWTEST_F(CrashExceptionTest, CrashExceptionTest002, TestSize.Level2)
72 {
73 GTEST_LOG_(INFO) << "CrashExceptionTest002: start.";
74 char testProcessName[] = "process_name_ptr";
75 std::shared_ptr<CrashExceptionListener> crashListener = std::make_shared<CrashExceptionListener>();
76 ListenerRule tagRule("RELIABILITY", "CPP_CRASH_EXCEPTION", RuleType::WHOLE_WORD);
77 std::vector<ListenerRule> sysRules;
78 sysRules.push_back(tagRule);
79 HiSysEventManager::AddListener(crashListener, sysRules);
80 crashListener->SetKeyWord(testProcessName);
81 ReportCrashException(testProcessName, TEST_PROCESS_ID, TEST_UID, CrashExceptionCode::CRASH_ESUCCESS);
82 ASSERT_FALSE(crashListener->CheckKeywordInReasons());
83 HiSysEventManager::RemoveListener(crashListener);
84 GTEST_LOG_(INFO) << "CrashExceptionTest002: end.";
85 }
86
87 /**
88 * @tc.name: CrashExceptionTest003
89 * @tc.desc: test ReportCrashException, process name length is more than 128 bytes.
90 * @tc.type: FUNC
91 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest003, TestSize.Level2)92 HWTEST_F(CrashExceptionTest, CrashExceptionTest003, TestSize.Level2)
93 {
94 GTEST_LOG_(INFO) << "CrashExceptionTest003: start.";
95 char testProcessName[] = "process_name_ptr_1111111111111111111111111111111111111"
96 "11111111111111111111111111111111111111111111111111111111111111111111111111111111"
97 "11111111111111111111111111111111111111111111111111111111111111111111111111111111";
98 std::shared_ptr<CrashExceptionListener> crashListener = std::make_shared<CrashExceptionListener>();
99 ListenerRule tagRule("RELIABILITY", "CPP_CRASH_EXCEPTION", RuleType::WHOLE_WORD);
100 std::vector<ListenerRule> sysRules;
101 sysRules.push_back(tagRule);
102 HiSysEventManager::AddListener(crashListener, sysRules);
103 crashListener->SetKeyWord(testProcessName);
104 ReportCrashException(testProcessName, TEST_PROCESS_ID, TEST_UID, CrashExceptionCode::CRASH_UNKNOWN);
105 ASSERT_FALSE(crashListener->CheckKeywordInReasons());
106 HiSysEventManager::RemoveListener(crashListener);
107 GTEST_LOG_(INFO) << "CrashExceptionTest003: end.";
108 }
109
110 /**
111 * @tc.name: CrashExceptionTest004
112 * @tc.desc: test ReportCrashException
113 * @tc.type: FUNC
114 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest004, TestSize.Level2)115 HWTEST_F(CrashExceptionTest, CrashExceptionTest004, TestSize.Level2)
116 {
117 GTEST_LOG_(INFO) << "CrashExceptionTest004: start.";
118 std::string keyWord = "process_name_string";
119 std::shared_ptr<CrashExceptionListener> crashListener = std::make_shared<CrashExceptionListener>();
120 ListenerRule tagRule("RELIABILITY", "CPP_CRASH_EXCEPTION", RuleType::WHOLE_WORD);
121 std::vector<ListenerRule> sysRules;
122 sysRules.push_back(tagRule);
123 HiSysEventManager::AddListener(crashListener, sysRules);
124 crashListener->SetKeyWord(keyWord);
125 ReportCrashException(keyWord, TEST_PROCESS_ID, TEST_UID, CrashExceptionCode::CRASH_UNKNOWN);
126 ASSERT_TRUE(crashListener->CheckKeywordInReasons());
127 HiSysEventManager::RemoveListener(crashListener);
128 GTEST_LOG_(INFO) << "CrashExceptionTest004: end.";
129 }
130
131 /**
132 * @tc.name: CrashExceptionTest005
133 * @tc.desc: test ReportCrashException, error code is success.
134 * @tc.type: FUNC
135 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest005, TestSize.Level2)136 HWTEST_F(CrashExceptionTest, CrashExceptionTest005, TestSize.Level2)
137 {
138 GTEST_LOG_(INFO) << "CrashExceptionTest005: start.";
139 std::string keyWord = "process_name_string";
140 std::shared_ptr<CrashExceptionListener> crashListener = std::make_shared<CrashExceptionListener>();
141 ListenerRule tagRule("RELIABILITY", "CPP_CRASH_EXCEPTION", RuleType::WHOLE_WORD);
142 std::vector<ListenerRule> sysRules;
143 sysRules.push_back(tagRule);
144 HiSysEventManager::AddListener(crashListener, sysRules);
145 crashListener->SetKeyWord(keyWord);
146 ReportCrashException(keyWord, TEST_PROCESS_ID, TEST_UID, CrashExceptionCode::CRASH_ESUCCESS);
147 ASSERT_FALSE(crashListener->CheckKeywordInReasons());
148 HiSysEventManager::RemoveListener(crashListener);
149 GTEST_LOG_(INFO) << "CrashExceptionTest005: end.";
150 }
151
152 /**
153 * @tc.name: CrashExceptionTest006
154 * @tc.desc: test ReportUnwinderException
155 * @tc.type: FUNC
156 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest006, TestSize.Level2)157 HWTEST_F(CrashExceptionTest, CrashExceptionTest006, TestSize.Level2)
158 {
159 GTEST_LOG_(INFO) << "CrashExceptionTest006: start.";
160 std::string keyWord = "process_name_unwind";
161 std::shared_ptr<CrashExceptionListener> crashListener = std::make_shared<CrashExceptionListener>();
162 ListenerRule tagRule("RELIABILITY", "CPP_CRASH_EXCEPTION", RuleType::WHOLE_WORD);
163 std::vector<ListenerRule> sysRules;
164 sysRules.push_back(tagRule);
165 HiSysEventManager::AddListener(crashListener, sysRules);
166 crashListener->SetKeyWord(keyWord);
167 SetCrashProcInfo(keyWord, TEST_PROCESS_ID, TEST_UID);
168 ReportUnwinderException(UnwindErrorCode::UNW_ERROR_STEP_ARK_FRAME);
169 ASSERT_TRUE(crashListener->CheckKeywordInReasons());
170 HiSysEventManager::RemoveListener(crashListener);
171 GTEST_LOG_(INFO) << "CrashExceptionTest006: end.";
172 }
173
174 /**
175 * @tc.name: CrashExceptionTest007
176 * @tc.desc: test ReportUnwinderException, invalid unwind error code.
177 * @tc.type: FUNC
178 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest007, TestSize.Level2)179 HWTEST_F(CrashExceptionTest, CrashExceptionTest007, TestSize.Level2)
180 {
181 GTEST_LOG_(INFO) << "CrashExceptionTest007: start.";
182 std::string keyWord = "process_name_unwind";
183 std::shared_ptr<CrashExceptionListener> crashListener = std::make_shared<CrashExceptionListener>();
184 ListenerRule tagRule("RELIABILITY", "CPP_CRASH_EXCEPTION", RuleType::WHOLE_WORD);
185 std::vector<ListenerRule> sysRules;
186 sysRules.push_back(tagRule);
187 HiSysEventManager::AddListener(crashListener, sysRules);
188 crashListener->SetKeyWord(keyWord);
189 SetCrashProcInfo(keyWord, TEST_PROCESS_ID, TEST_UID);
190 ReportUnwinderException(0);
191 ASSERT_FALSE(crashListener->CheckKeywordInReasons());
192 HiSysEventManager::RemoveListener(crashListener);
193 GTEST_LOG_(INFO) << "CrashExceptionTest007: end.";
194 }
195
196 /**
197 * @tc.name: CrashExceptionTest08
198 * @tc.desc: test CheckFaultSummaryValid, valid Fault Summary.
199 * @tc.type: FUNC
200 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest08, TestSize.Level2)201 HWTEST_F(CrashExceptionTest, CrashExceptionTest08, TestSize.Level2)
202 {
203 GTEST_LOG_(INFO) << "CrashExceptionTest08: start.";
204 std::string summary = std::string("Thread name:sensors\n") +
205 "#00 pc 000c5738 /system/lib/ld-musl-arm.so.1(ioctl+72)(b985d2b9b22c3e542388f5803bac6a56)\n" +
206 "#01 pc 00007fcf /system/lib/chipset-pub-sdk/libipc_common.z.so(OHOS::BinderConnector::WriteBinder(\n" +
207 "#02 pc 00034f35 /system/lib/platformsdk/libipc_core.z.so(OHOS::BinderInvoker::TransactWithDriver(\n" +
208 "#03 pc 000350a5 /system/lib/platformsdk/libipc_core.z.so(OHOS::BinderInvoker::StartWorkLoop(\n" +
209 "#04 pc 000363df /system/lib/platformsdk/libipc_core.z.so(OHOS::BinderInvoker::JoinThread(\n" +
210 "#05 pc 00010955 /system/lib/platformsdk/libsystem_ability_fwk.z.so(\n" +
211 "#06 pc 0000391b /system/bin/sa_main(main.cfi+1986)(c626ef160394bf644c17e6769318dc7d)\n" +
212 "#07 pc 00073560 /system/lib/ld-musl-arm.so.1(libc_start_main_stage2+56)(\n" +
213 "#08 pc 00003078 /system/bin/sa_main(_start_c+84)(c626ef160394bf644c17e6769318dc7d)\n" +
214 "#09 pc 0000301c /system/bin/sa_main(c626ef160394bf644c17e6769318dc7d)\n";
215 ASSERT_TRUE(CheckFaultSummaryValid(summary));
216 GTEST_LOG_(INFO) << "CrashExceptionTest08: end.";
217 }
218
219 /**
220 * @tc.name: CrashExceptionTest09
221 * @tc.desc: test CheckFaultSummaryValid, valid Fault Summary.
222 * @tc.type: FUNC
223 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest09, TestSize.Level2)224 HWTEST_F(CrashExceptionTest, CrashExceptionTest09, TestSize.Level2)
225 {
226 GTEST_LOG_(INFO) << "CrashExceptionTest09: start.";
227 std::string summary = std::string("Thread name:sensors\n") +
228 "#00 pc 000c5738 /system/lib/ld-musl-arm.so.1(ioctl+72)(b985d2b9b22c3e542388f5803bac6a56)\n" +
229 "#01 pc 00007fcf Not mapped\n" +
230 "#02 pc 00034f35 /system/lib/platformsdk/libipc_core.z.so(OHOS::BinderInvoker::TransactWithDriver(\n" +
231 "#03 pc 000350a5 /system/lib/platformsdk/libipc_core.z.so(OHOS::BinderInvoker::StartWorkLoop(\n" +
232 "#04 pc 000363df /system/lib/platformsdk/libipc_core.z.so(OHOS::BinderInvoker::JoinThread(\n" +
233 "#05 pc 00010955 /system/lib/platformsdk/libsystem_ability_fwk.z.so(\n" +
234 "#06 pc 0000391b /system/bin/sa_main(main.cfi+1986)(c626ef160394bf644c17e6769318dc7d)\n" +
235 "#07 pc 00073560 /system/lib/ld-musl-arm.so.1(libc_start_main_stage2+56)(\n" +
236 "#08 pc 00003078 /system/bin/sa_main(_start_c+84)(c626ef160394bf644c17e6769318dc7d)\n" +
237 "#09 pc 0000301c /system/bin/sa_main(c626ef160394bf644c17e6769318dc7d)\n";
238 ASSERT_TRUE(CheckFaultSummaryValid(summary));
239 GTEST_LOG_(INFO) << "CrashExceptionTest09: end.";
240 }
241
242 /**
243 * @tc.name: CrashExceptionTest010
244 * @tc.desc: test CheckFaultSummaryValid, invalid Fault Summary.
245 * @tc.type: FUNC
246 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest010, TestSize.Level2)247 HWTEST_F(CrashExceptionTest, CrashExceptionTest010, TestSize.Level2)
248 {
249 GTEST_LOG_(INFO) << "CrashExceptionTest010: start.";
250 std::string summary = std::string("Thread name:sensors\n") +
251 "#00 pc 000c5738 /system/lib/ld-musl-arm.so.1(ioctl+72)(b985d2b9b22c3e542388f5803bac6a56)\n" +
252 "#01 pc 00007fcf /system/lib/chipset-pub-sdk/libipc_common.z.so(OHOS::BinderConnector::WriteBinder(\n";
253 ASSERT_FALSE(CheckFaultSummaryValid(summary));
254 GTEST_LOG_(INFO) << "CrashExceptionTest010: end.";
255 }
256
257 /**
258 * @tc.name: CrashExceptionTest011
259 * @tc.desc: test SetCrashProcInfo functions
260 * @tc.type: FUNC
261 */
HWTEST_F(CrashExceptionTest, CrashExceptionTest011, TestSize.Level2)262 HWTEST_F(CrashExceptionTest, CrashExceptionTest011, TestSize.Level2)
263 {
264 GTEST_LOG_(INFO) << "CrashExceptionTest011: start.";
265 std::string name = "";
266 const int32_t pid = -1;
267 SetCrashProcInfo(name, pid, 0);
268 ReportUnwinderException(0);
269 ASSERT_EQ(name, "");
270 GTEST_LOG_(INFO) << "CrashExceptionTest011: end.";
271 }
272 } // namespace HiviewDFX
273 } // namespace OHOS