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 */
HWTEST_F(DfxFuncHookUnitTest, FuncHookTest001, TestSize.Level3)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 return;
59 }
60 if (pid == 0) {
61 exit(retCode);
62 } else {
63 int status;
64 int ret = waitpid(pid, &status, 0);
65 printf("child ret with pid:%d status:%d\n", ret, status);
66 if (WIFEXITED(status)) {
67 int code = WEXITSTATUS(status);
68 printf("Exit code was %d\n", code);
69 EXPECT_EQ(code, retCode);
70 }
71 }
72 GTEST_LOG_(INFO) << "FuncHookTest001: end.";
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 */
HWTEST_F(DfxFuncHookUnitTest, FuncHookTest002, TestSize.Level3)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 return;
88 }
89 if (pid == 0) {
90 while (true) {
91 sleep(1);
92 }
93 } else {
94 printf("child pid:%d\n", pid);
95 kill(pid, SIGKILL);
96 int status;
97 int ret = waitpid(pid, &status, 0);
98 printf("child ret with pid:%d status:%d\n", ret, status);
99 if (WIFSIGNALED(status)) {
100 int signal = WTERMSIG(status);
101 printf("Exit signal was %d\n", signal);
102 EXPECT_EQ(signal, SIGKILL);
103 }
104 }
105 GTEST_LOG_(INFO) << "FuncHookTest002: end.";
106 }
107 }
108