1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci
16800b99b8Sopenharmony_ci#include <cstring>
17800b99b8Sopenharmony_ci#include <gtest/gtest.h>
18800b99b8Sopenharmony_ci#include <unistd.h>
19800b99b8Sopenharmony_ci#include <vector>
20800b99b8Sopenharmony_ci#ifdef HISYSEVENT_ENABLE
21800b99b8Sopenharmony_ci#include "hisysevent_manager.h"
22800b99b8Sopenharmony_ci#include "rustpanic_listener.h"
23800b99b8Sopenharmony_ci
24800b99b8Sopenharmony_ciusing namespace testing;
25800b99b8Sopenharmony_ciusing namespace testing::ext;
26800b99b8Sopenharmony_ci
27800b99b8Sopenharmony_cinamespace OHOS {
28800b99b8Sopenharmony_cinamespace HiviewDFX {
29800b99b8Sopenharmony_cinamespace {
30800b99b8Sopenharmony_cistatic std::shared_ptr<RustPanicListener> panicListener = nullptr;
31800b99b8Sopenharmony_ci}
32800b99b8Sopenharmony_ciclass PanicHandlerTest : public testing::Test {
33800b99b8Sopenharmony_cipublic:
34800b99b8Sopenharmony_ci    static void SetUpTestCase();
35800b99b8Sopenharmony_ci    static void TearDownTestCase();
36800b99b8Sopenharmony_ci    void SetUp();
37800b99b8Sopenharmony_ci    void TearDown();
38800b99b8Sopenharmony_ci};
39800b99b8Sopenharmony_ci
40800b99b8Sopenharmony_civoid PanicHandlerTest::SetUpTestCase()
41800b99b8Sopenharmony_ci{}
42800b99b8Sopenharmony_ci
43800b99b8Sopenharmony_civoid PanicHandlerTest::TearDownTestCase()
44800b99b8Sopenharmony_ci{}
45800b99b8Sopenharmony_ci
46800b99b8Sopenharmony_civoid PanicHandlerTest::SetUp()
47800b99b8Sopenharmony_ci{
48800b99b8Sopenharmony_ci    chmod("/data/rustpanic_maker", 0755); // 0755 : -rwxr-xr-x
49800b99b8Sopenharmony_ci}
50800b99b8Sopenharmony_ci
51800b99b8Sopenharmony_civoid PanicHandlerTest::TearDown()
52800b99b8Sopenharmony_ci{
53800b99b8Sopenharmony_ci    panicListener = nullptr;
54800b99b8Sopenharmony_ci}
55800b99b8Sopenharmony_ci
56800b99b8Sopenharmony_cistatic void ForkAndTriggerRustPanic(bool ismain)
57800b99b8Sopenharmony_ci{
58800b99b8Sopenharmony_ci    pid_t pid = fork();
59800b99b8Sopenharmony_ci    if (pid < 0) {
60800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "ForkAndTriggerRustPanic: Failed to fork panic process.";
61800b99b8Sopenharmony_ci        return;
62800b99b8Sopenharmony_ci    } else if (pid == 0) {
63800b99b8Sopenharmony_ci        if (ismain) {
64800b99b8Sopenharmony_ci            execl("/data/rustpanic_maker", "rustpanic_maker", "main", nullptr);
65800b99b8Sopenharmony_ci        } else {
66800b99b8Sopenharmony_ci            execl("/data/rustpanic_maker", "rustpanic_maker", "child", nullptr);
67800b99b8Sopenharmony_ci        }
68800b99b8Sopenharmony_ci    }
69800b99b8Sopenharmony_ci}
70800b99b8Sopenharmony_ci
71800b99b8Sopenharmony_cistatic void ListenAndCheckHiSysevent()
72800b99b8Sopenharmony_ci{
73800b99b8Sopenharmony_ci    panicListener = std::make_shared<RustPanicListener>();
74800b99b8Sopenharmony_ci    ListenerRule tagRule("RELIABILITY", "RUST_PANIC", RuleType::WHOLE_WORD);
75800b99b8Sopenharmony_ci    std::vector<ListenerRule> sysRules;
76800b99b8Sopenharmony_ci    sysRules.push_back(tagRule);
77800b99b8Sopenharmony_ci    HiSysEventManager::AddListener(panicListener, sysRules);
78800b99b8Sopenharmony_ci}
79800b99b8Sopenharmony_ci
80800b99b8Sopenharmony_ci/**
81800b99b8Sopenharmony_ci * @tc.name: PanicHandlerTest001
82800b99b8Sopenharmony_ci * @tc.desc: test panic in main thread
83800b99b8Sopenharmony_ci * @tc.type: FUNC
84800b99b8Sopenharmony_ci * @tc.require: issueI6HM7C
85800b99b8Sopenharmony_ci */
86800b99b8Sopenharmony_ciHWTEST_F(PanicHandlerTest, PanicHandlerTest001, TestSize.Level2)
87800b99b8Sopenharmony_ci{
88800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "PanicHandlerTest001: start.";
89800b99b8Sopenharmony_ci    ListenAndCheckHiSysevent();
90800b99b8Sopenharmony_ci    panicListener->SetKeyWord("panic in main thread");
91800b99b8Sopenharmony_ci    ForkAndTriggerRustPanic(true);
92800b99b8Sopenharmony_ci    if (panicListener != nullptr) {
93800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "PanicHandlerTest001: ready to check hisysevent reason keyword.";
94800b99b8Sopenharmony_ci        ASSERT_TRUE(panicListener->CheckKeywordInReasons());
95800b99b8Sopenharmony_ci    }
96800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "PanicHandlerTest001: end.";
97800b99b8Sopenharmony_ci}
98800b99b8Sopenharmony_ci
99800b99b8Sopenharmony_ci/**
100800b99b8Sopenharmony_ci * @tc.name: PanicHandlerTest002
101800b99b8Sopenharmony_ci * @tc.desc: test panic in child thread
102800b99b8Sopenharmony_ci * @tc.type: FUNC
103800b99b8Sopenharmony_ci * @tc.require: issueI6HM7C
104800b99b8Sopenharmony_ci */
105800b99b8Sopenharmony_ciHWTEST_F(PanicHandlerTest, PanicHandlerTest002, TestSize.Level2)
106800b99b8Sopenharmony_ci{
107800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "PanicHandlerTest002: start.";
108800b99b8Sopenharmony_ci    ListenAndCheckHiSysevent();
109800b99b8Sopenharmony_ci    panicListener->SetKeyWord("panic in child thread");
110800b99b8Sopenharmony_ci    ForkAndTriggerRustPanic(false);
111800b99b8Sopenharmony_ci    if (panicListener != nullptr) {
112800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << "PanicHandlerTest002: ready to check hisysevent reason keyword.";
113800b99b8Sopenharmony_ci        ASSERT_TRUE(panicListener->CheckKeywordInReasons());
114800b99b8Sopenharmony_ci    }
115800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "PanicHandlerTest002: end.";
116800b99b8Sopenharmony_ci}
117800b99b8Sopenharmony_ci} // namespace HiviewDFX
118800b99b8Sopenharmony_ci} // namespace OHOS
119800b99b8Sopenharmony_ci#endif