1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2024 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 <gtest/gtest.h>
17800b99b8Sopenharmony_ci#include <csignal>
18800b99b8Sopenharmony_ci#include <map>
19800b99b8Sopenharmony_ci#include <securec.h>
20800b99b8Sopenharmony_ci#include <string>
21800b99b8Sopenharmony_ci#include <thread>
22800b99b8Sopenharmony_ci#include <unistd.h>
23800b99b8Sopenharmony_ci#include <vector>
24800b99b8Sopenharmony_ci#include <sys/prctl.h>
25800b99b8Sopenharmony_ci
26800b99b8Sopenharmony_ci#include "dfx_define.h"
27800b99b8Sopenharmony_ci#include "dfx_test_util.h"
28800b99b8Sopenharmony_ci#include "dfx_dump_catcher.h"
29800b99b8Sopenharmony_ci#include "dfx_sigdump_handler.h"
30800b99b8Sopenharmony_ci
31800b99b8Sopenharmony_ciusing namespace testing;
32800b99b8Sopenharmony_ciusing namespace testing::ext;
33800b99b8Sopenharmony_ciusing namespace std;
34800b99b8Sopenharmony_ci
35800b99b8Sopenharmony_cinamespace OHOS {
36800b99b8Sopenharmony_cinamespace HiviewDFX {
37800b99b8Sopenharmony_ciclass DfxSigDumpHandlerTest : public testing::Test {
38800b99b8Sopenharmony_cipublic:
39800b99b8Sopenharmony_ci    static void SetUpTestCase();
40800b99b8Sopenharmony_ci    static void TearDownTestCase();
41800b99b8Sopenharmony_ci    void SetUp();
42800b99b8Sopenharmony_ci    void TearDown();
43800b99b8Sopenharmony_ci    static void TestThreadRunTask();
44800b99b8Sopenharmony_ci};
45800b99b8Sopenharmony_ci
46800b99b8Sopenharmony_cistatic const int TEST_THREAD_NUM = 10;
47800b99b8Sopenharmony_cistatic const int TEST_SLEEP_SEC = 5;
48800b99b8Sopenharmony_ci
49800b99b8Sopenharmony_civoid DfxSigDumpHandlerTest::SetUpTestCase()
50800b99b8Sopenharmony_ci{}
51800b99b8Sopenharmony_ci
52800b99b8Sopenharmony_civoid DfxSigDumpHandlerTest::TearDownTestCase()
53800b99b8Sopenharmony_ci{}
54800b99b8Sopenharmony_ci
55800b99b8Sopenharmony_civoid DfxSigDumpHandlerTest::SetUp()
56800b99b8Sopenharmony_ci{}
57800b99b8Sopenharmony_ci
58800b99b8Sopenharmony_civoid DfxSigDumpHandlerTest::TearDown()
59800b99b8Sopenharmony_ci{}
60800b99b8Sopenharmony_ci
61800b99b8Sopenharmony_civoid DfxSigDumpHandlerTest::TestThreadRunTask()
62800b99b8Sopenharmony_ci{
63800b99b8Sopenharmony_ci    std::this_thread::sleep_for(std::chrono::seconds(TEST_SLEEP_SEC));
64800b99b8Sopenharmony_ci}
65800b99b8Sopenharmony_ci/**
66800b99b8Sopenharmony_ci * @tc.name: DfxSigDumpHandlerTest
67800b99b8Sopenharmony_ci * @tc.desc: test DfxSigDumpHandler
68800b99b8Sopenharmony_ci * @tc.type: FUNC
69800b99b8Sopenharmony_ci */
70800b99b8Sopenharmony_ciHWTEST_F(DfxSigDumpHandlerTest, DfxSigDumpHandlerTest001, TestSize.Level2)
71800b99b8Sopenharmony_ci{
72800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxSigDumpHandlerTest001: start.";
73800b99b8Sopenharmony_ci    pid_t targetPid = getpid();
74800b99b8Sopenharmony_ci    pid_t pid = fork();
75800b99b8Sopenharmony_ci    if (pid < 0) {
76800b99b8Sopenharmony_ci        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
77800b99b8Sopenharmony_ci    } else if (pid == 0) {
78800b99b8Sopenharmony_ci        sleep(1);
79800b99b8Sopenharmony_ci        DfxDumpCatcher dumplog;
80800b99b8Sopenharmony_ci        std::string msg = "";
81800b99b8Sopenharmony_ci        bool ret = dumplog.DumpCatch(targetPid, 0, msg);
82800b99b8Sopenharmony_ci        EXPECT_EQ(ret, true) << "DfxSigDumpHandlerTest001 Failed";
83800b99b8Sopenharmony_ci        std::string log[] = {"Pid:", "Timestamp", "test_sigdump_handler", "#00"};
84800b99b8Sopenharmony_ci        log[0] = log[0] + std::to_string(targetPid);
85800b99b8Sopenharmony_ci        int len = sizeof(log) / sizeof(log[0]);
86800b99b8Sopenharmony_ci        int count = GetKeywordsNum(msg, log, len);
87800b99b8Sopenharmony_ci        GTEST_LOG_(INFO) << msg;
88800b99b8Sopenharmony_ci        EXPECT_EQ(count, len) << "DfxSigDumpHandlerTest001 Failed";
89800b99b8Sopenharmony_ci    } else {
90800b99b8Sopenharmony_ci        InitSigDumpHandler();
91800b99b8Sopenharmony_ci        std::thread testThread[TEST_THREAD_NUM];
92800b99b8Sopenharmony_ci        for (int i = 0; i < TEST_THREAD_NUM; i++) {
93800b99b8Sopenharmony_ci            testThread[i] = std::thread(&DfxSigDumpHandlerTest::TestThreadRunTask);
94800b99b8Sopenharmony_ci        }
95800b99b8Sopenharmony_ci        for (int i = 0; i < TEST_THREAD_NUM; i++) {
96800b99b8Sopenharmony_ci            testThread[i].join();
97800b99b8Sopenharmony_ci        }
98800b99b8Sopenharmony_ci        DeinitSigDumpHandler();
99800b99b8Sopenharmony_ci    }
100800b99b8Sopenharmony_ci    GTEST_LOG_(INFO) << "DfxSigDumpHandlerTest001: end.";
101800b99b8Sopenharmony_ci}
102800b99b8Sopenharmony_ci
103800b99b8Sopenharmony_ci} // namespace HiviewDFX
104800b99b8Sopenharmony_ci} // namepsace OHOS
105