• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 <cstring>
17 #include <gtest/gtest.h>
18 #include <unistd.h>
19 #include <vector>
20 #ifdef HISYSEVENT_ENABLE
21 #include "hisysevent_manager.h"
22 #include "rustpanic_listener.h"
23 #include "dfx_test_util.h"
24 
25 using namespace testing;
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace {
31 static std::shared_ptr<RustPanicListener> panicListener = nullptr;
32 }
33 class PanicHandlerTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase()41 void PanicHandlerTest::SetUpTestCase()
42 {}
43 
TearDownTestCase()44 void PanicHandlerTest::TearDownTestCase()
45 {}
46 
SetUp()47 void PanicHandlerTest::SetUp()
48 {
49     chmod("/data/rustpanic_maker", 0755); // 0755 : -rwxr-xr-x
50 }
51 
TearDown()52 void PanicHandlerTest::TearDown()
53 {
54     panicListener = nullptr;
55 }
56 
ForkAndTriggerRustPanic(bool ismain,uint32_t hilogCnt=0)57 static void ForkAndTriggerRustPanic(bool ismain, uint32_t hilogCnt = 0)
58 {
59     pid_t pid = fork();
60     if (pid < 0) {
61         GTEST_LOG_(ERROR) << "ForkAndTriggerRustPanic: Failed to fork panic process.";
62         return;
63     } else if (pid == 0) {
64         if (ismain) {
65             execl("/data/rustpanic_maker", "rustpanic_maker", "main", std::to_string(hilogCnt).c_str(), nullptr);
66         } else {
67             execl("/data/rustpanic_maker", "rustpanic_maker", "child", std::to_string(hilogCnt).c_str(), nullptr);
68         }
69     }
70 }
71 
ListenAndCheckHiSysevent()72 static void ListenAndCheckHiSysevent()
73 {
74     panicListener = std::make_shared<RustPanicListener>();
75     ListenerRule tagRule("RELIABILITY", "RUST_PANIC", RuleType::WHOLE_WORD);
76     std::vector<ListenerRule> sysRules;
77     sysRules.push_back(tagRule);
78     HiSysEventManager::AddListener(panicListener, sysRules);
79 }
80 
WaitFaultloggerFile()81 static std::string WaitFaultloggerFile()
82 {
83     std::regex reg(R"(rustpanic-rustpanic_maker-0-\d{17}.log)");
84     const std::string folder = "/data/log/faultlog/faultlogger/";
85     return WaitCreateFile(folder, reg);
86 }
87 
CheckRustPanicFaultlog(const string & filePath,bool isMain)88 static bool CheckRustPanicFaultlog(const string& filePath, bool isMain)
89 {
90     if (filePath.empty()) {
91         return false;
92     }
93     std::list<LineRule> rules;
94     rules.push_back(LineRule(R"(^Build info:.*$)"));
95     rules.push_back(LineRule(R"(^Module name:rustpanic_maker$)"));
96     if (isMain) {
97         rules.push_back(LineRule(R"(^Reason:.*message:panic in main thread$)"));
98     } else {
99         rules.push_back(LineRule(R"(^Reason:.*message:panic in child thread$)"));
100     }
101     rules.push_back(LineRule(R"(^#\d+ pc [0-9a-f]+ .*$)"));
102     rules.push_back(LineRule(R"(^HiLog:$)"));
103     return CheckLineMatch(filePath, rules);
104 }
105 
106 /**
107  * @tc.name: PanicHandlerTest001
108  * @tc.desc: test panic in main thread
109  * @tc.type: FUNC
110  * @tc.require: issueI6HM7C
111  */
112 HWTEST_F(PanicHandlerTest, PanicHandlerTest001, TestSize.Level0)
113 {
114     GTEST_LOG_(INFO) << "PanicHandlerTest001: start.";
115     ListenAndCheckHiSysevent();
116     panicListener->SetKeyWord("panic in main thread");
117     ForkAndTriggerRustPanic(true);
118     if (panicListener != nullptr) {
119         GTEST_LOG_(INFO) << "PanicHandlerTest001: ready to check hisysevent reason keyword.";
120         ASSERT_TRUE(panicListener->CheckKeywordInReasons());
121     }
122     GTEST_LOG_(INFO) << "PanicHandlerTest001: end.";
123 }
124 
125 /**
126  * @tc.name: PanicHandlerTest002
127  * @tc.desc: test panic in child thread
128  * @tc.type: FUNC
129  * @tc.require: issueI6HM7C
130  */
131 HWTEST_F(PanicHandlerTest, PanicHandlerTest002, TestSize.Level2)
132 {
133     GTEST_LOG_(INFO) << "PanicHandlerTest002: start.";
134     ListenAndCheckHiSysevent();
135     panicListener->SetKeyWord("panic in child thread");
136     ForkAndTriggerRustPanic(false);
137     if (panicListener != nullptr) {
138         GTEST_LOG_(INFO) << "PanicHandlerTest002: ready to check hisysevent reason keyword.";
139         ASSERT_TRUE(panicListener->CheckKeywordInReasons());
140     }
141     GTEST_LOG_(INFO) << "PanicHandlerTest002: end.";
142 }
143 
144 /**
145  * @tc.name: PanicHandlerTest003
146  * @tc.desc: test panic in main thread
147  * @tc.type: FUNC
148  */
149 HWTEST_F(PanicHandlerTest, PanicHandlerTest003, TestSize.Level2)
150 {
__anon27ec648e0202(std::string& data) 151     AsyncWorker<string> listenFileCreate([](std::string& data) { data.clear(); });
152     listenFileCreate.Start(WaitFaultloggerFile);
153     ForkAndTriggerRustPanic(true, 1000); // print hilog 1000 lines
154     auto fileName = listenFileCreate.GetResult(std::chrono::seconds(10)); // 10s timeout
155     ASSERT_TRUE(CheckRustPanicFaultlog(fileName, true));
156 }
157 
158 /**
159  * @tc.name: PanicHandlerTest004
160  * @tc.desc: test panic in child thread
161  * @tc.type: FUNC
162  */
163 HWTEST_F(PanicHandlerTest, PanicHandlerTest004, TestSize.Level2)
164 {
__anon27ec648e0302(std::string& data) 165     AsyncWorker<string> listenFileCreate([](std::string& data) { data.clear(); });
166     listenFileCreate.Start(WaitFaultloggerFile);
167     ForkAndTriggerRustPanic(false, 1000); // print hilog 1000 lines
168     auto fileName = listenFileCreate.GetResult(std::chrono::seconds(10)); // 10s timeout
169     ASSERT_TRUE(CheckRustPanicFaultlog(fileName, false));
170 }
171 } // namespace HiviewDFX
172 } // namespace OHOS
173 #endif
174