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 <gtest/gtest.h>
17#include <csignal>
18#include <map>
19#include <securec.h>
20#include <string>
21#include <thread>
22#include <unistd.h>
23#include <vector>
24#include <sys/prctl.h>
25
26#include "dfx_define.h"
27#include "dfx_test_util.h"
28#include "dfx_dump_catcher.h"
29#include "dfx_sigdump_handler.h"
30
31using namespace testing;
32using namespace testing::ext;
33using namespace std;
34
35namespace OHOS {
36namespace HiviewDFX {
37class DfxSigDumpHandlerTest : public testing::Test {
38public:
39    static void SetUpTestCase();
40    static void TearDownTestCase();
41    void SetUp();
42    void TearDown();
43    static void TestThreadRunTask();
44};
45
46static const int TEST_THREAD_NUM = 10;
47static const int TEST_SLEEP_SEC = 5;
48
49void DfxSigDumpHandlerTest::SetUpTestCase()
50{}
51
52void DfxSigDumpHandlerTest::TearDownTestCase()
53{}
54
55void DfxSigDumpHandlerTest::SetUp()
56{}
57
58void DfxSigDumpHandlerTest::TearDown()
59{}
60
61void DfxSigDumpHandlerTest::TestThreadRunTask()
62{
63    std::this_thread::sleep_for(std::chrono::seconds(TEST_SLEEP_SEC));
64}
65/**
66 * @tc.name: DfxSigDumpHandlerTest
67 * @tc.desc: test DfxSigDumpHandler
68 * @tc.type: FUNC
69 */
70HWTEST_F(DfxSigDumpHandlerTest, DfxSigDumpHandlerTest001, TestSize.Level2)
71{
72    GTEST_LOG_(INFO) << "DfxSigDumpHandlerTest001: start.";
73    pid_t targetPid = getpid();
74    pid_t pid = fork();
75    if (pid < 0) {
76        GTEST_LOG_(ERROR) << "Failed to fork new test process.";
77    } else if (pid == 0) {
78        sleep(1);
79        DfxDumpCatcher dumplog;
80        std::string msg = "";
81        bool ret = dumplog.DumpCatch(targetPid, 0, msg);
82        EXPECT_EQ(ret, true) << "DfxSigDumpHandlerTest001 Failed";
83        std::string log[] = {"Pid:", "Timestamp", "test_sigdump_handler", "#00"};
84        log[0] = log[0] + std::to_string(targetPid);
85        int len = sizeof(log) / sizeof(log[0]);
86        int count = GetKeywordsNum(msg, log, len);
87        GTEST_LOG_(INFO) << msg;
88        EXPECT_EQ(count, len) << "DfxSigDumpHandlerTest001 Failed";
89    } else {
90        InitSigDumpHandler();
91        std::thread testThread[TEST_THREAD_NUM];
92        for (int i = 0; i < TEST_THREAD_NUM; i++) {
93            testThread[i] = std::thread(&DfxSigDumpHandlerTest::TestThreadRunTask);
94        }
95        for (int i = 0; i < TEST_THREAD_NUM; i++) {
96            testThread[i].join();
97        }
98        DeinitSigDumpHandler();
99    }
100    GTEST_LOG_(INFO) << "DfxSigDumpHandlerTest001: end.";
101}
102
103} // namespace HiviewDFX
104} // namepsace OHOS
105