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 bool isSuccess = pid >= 0;
56 if (!isSuccess) {
57 ASSERT_FALSE(isSuccess);
58 } else {
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 /**
76 * @tc.name: FuncHookTest002
77 * @tc.desc: fork a child process and kill it in parent process
78 * @tc.type: FUNC
79 */
80 HWTEST_F(DfxFuncHookUnitTest, FuncHookTest002, TestSize.Level3)
81 {
82 GTEST_LOG_(INFO) << "FuncHookTest002: start.";
83 pid_t pid = fork();
84 bool isSuccess = pid >= 0;
85 if (!isSuccess) {
86 ASSERT_FALSE(isSuccess);
87 } else {
88 if (pid == 0) {
89 while (true) {
90 sleep(1);
91 }
92 } else {
93 printf("child pid:%d\n", pid);
94 kill(pid, SIGKILL);
95 int status;
96 int ret = waitpid(pid, &status, 0);
97 printf("child ret with pid:%d status:%d\n", ret, status);
98 if (WIFSIGNALED(status)) {
99 int signal = WTERMSIG(status);
100 printf("Exit signal was %d\n", signal);
101 EXPECT_EQ(signal, SIGKILL);
102 }
103 }
104 GTEST_LOG_(INFO) << "FuncHookTest002: end.";
105 }
106 }
107 }
108