1 /*
2  * Copyright (c) 2022-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 "dfx_func_hook_unittest.h"
17 
18 #include <csignal>
19 #include <sys/wait.h>
20 #include <unistd.h>
21 
22 #include "dfx_exit_hook.h"
23 
24 using namespace OHOS::HiviewDFX;
25 using namespace testing::ext;
26 using namespace std;
SetUpTestCase(void)27 void DfxFuncHookUnitTest::SetUpTestCase(void)
28 {
29 }
30 
TearDownTestCase(void)31 void DfxFuncHookUnitTest::TearDownTestCase(void)
32 {
33 }
34 
SetUp(void)35 void DfxFuncHookUnitTest::SetUp(void)
36 {
37     StartHookExitFunc();
38 }
39 
TearDown(void)40 void DfxFuncHookUnitTest::TearDown(void)
41 {
42 }
43 
44 namespace {
45 /**
46  * @tc.name: FuncHookTest001
47  * @tc.desc: fork a child process and exit with calling _exit
48  * @tc.type: FUNC
49  */
50 HWTEST_F(DfxFuncHookUnitTest, FuncHookTest001, TestSize.Level3)
51 {
52     GTEST_LOG_(INFO) << "FuncHookTest001: start.";
53     const int retCode = 99;
54     pid_t pid = fork();
55     if (pid < 0) {
56         return;
57     }
58 
59     if (pid == 0) {
60         exit(retCode);
61     } else {
62         int status;
63         int ret = waitpid(pid, &status, 0);
64         printf("child ret with pid:%d status:%d\n", ret, status);
65         if (WIFEXITED(status)) {
66             int code = WEXITSTATUS(status);
67             printf("Exit code was %d\n", code);
68             EXPECT_EQ(code, retCode);
69         }
70     }
71     GTEST_LOG_(INFO) << "FuncHookTest001: end.";
72 }
73 
74 /**
75  * @tc.name: FuncHookTest002
76  * @tc.desc: fork a child process and kill it in parent process
77  * @tc.type: FUNC
78  */
79 HWTEST_F(DfxFuncHookUnitTest, FuncHookTest002, TestSize.Level3)
80 {
81     GTEST_LOG_(INFO) << "FuncHookTest002: start.";
82     pid_t pid = fork();
83     if (pid < 0) {
84         return;
85     }
86 
87     if (pid == 0) {
88         while (true) {
89             sleep(1);
90         }
91     } else {
92         printf("child pid:%d\n", pid);
93         kill(pid, SIGKILL);
94         int status;
95         int ret = waitpid(pid, &status, 0);
96         printf("child ret with pid:%d status:%d\n", ret, status);
97         if (WIFSIGNALED(status)) {
98             int signal = WTERMSIG(status);
99             printf("Exit signal was %d\n", signal);
100             EXPECT_EQ(signal, SIGKILL);
101         }
102     }
103     GTEST_LOG_(INFO) << "FuncHookTest002: end.";
104 }
105 }
106