1 /*
2 * Copyright (C) 2024 HiHope Open Source Organization.
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 <cerrno>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <csignal>
20 #include <string>
21 #include <vector>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include <malloc.h>
25 #include <poll.h>
26 #include <arpa/inet.h>
27 #include <gtest/gtest.h>
28 #include <netinet/in.h>
29 #include <sys/stat.h>
30 #include <sys/mman.h>
31 #include <sys/wait.h>
32 #include <sys/socket.h>
33 #include <sys/syscall.h>
34 #include <sys/timerfd.h>
35 #include <sys/types.h>
36 #include "securec.h"
37
38 using namespace testing::ext;
39
40 static const int DELAY_TIME = 10 * 1000;
41
42 class HatsWait4Test : public testing::Test {
43 public:
44 static void SetUpTestCase();
45 static void TearDownTestCase();
46 void SetUp();
47 void TearDown();
48 private:
49 };
SetUp()50 void HatsWait4Test::SetUp()
51 {
52 }
53
TearDown()54 void HatsWait4Test::TearDown()
55 {
56 }
57
SetUpTestCase()58 void HatsWait4Test::SetUpTestCase()
59 {
60 }
61
TearDownTestCase()62 void HatsWait4Test::TearDownTestCase()
63 {
64 }
65
66 /*
67 * @tc.number : SUB_KERNEL_SYSCALL_WAIT4_0100
68 * @tc.name : Wait4Failed_0001
69 * @tc.desc : Wait4 wait continued failed.
70 * @tc.size : MediumTest
71 * @tc.type : Function
72 * @tc.level : Level 2
73 */
74 HWTEST_F(HatsWait4Test, Wait4Failed_0001, Function | MediumTest | Level2)
75 {
76 int ret;
77 pid_t childPid = -1;
78
79 sigset_t sigMask;
80 ret = sigemptyset(&sigMask);
81 EXPECT_TRUE(ret >= 0);
82 ret = sigaddset(&sigMask, SIGCHLD);
83 EXPECT_TRUE(ret >= 0);
84
85 struct sigaction sa;
86 sa.sa_sigaction = nullptr;
87 sa.sa_flags = SA_SIGINFO;
88 ret = sigemptyset(&sa.sa_mask);
89 EXPECT_TRUE(ret >= 0);
90 ret = sigaction(SIGCHLD, &sa, nullptr);
91 EXPECT_EQ(ret, 0);
92
93 int wstatus = 0;
94 int options = WCONTINUED;
95 int retPid = wait4(childPid, &wstatus, options, nullptr);
96 EXPECT_EQ(retPid, -1);
97 }
98
99 /*
100 * @tc.number : SUB_KERNEL_SYSCALL_WAIT4_0200
101 * @tc.name : Wait4ChildProcessSuccess_0002
102 * @tc.desc : wait4 child process and WIFEXITED success.
103 * @tc.size : MediumTest
104 * @tc.type : Function
105 * @tc.level : Level 1
106 */
107 HWTEST_F(HatsWait4Test, Wait4ChildProcessSuccess_0002, Function | MediumTest | Level1)
108 {
109 pid_t pid = fork();
110 if (pid == 0) {
111 printf("Child process (PID: %d) is running...\n", getpid());
112 usleep(DELAY_TIME);
113 exit(0);
114 }
115 int status;
116 struct rusage usage;
117 pid_t tpid = wait4(pid, &status, 0, &usage);
118 EXPECT_TRUE(tpid > 0);
119 EXPECT_EQ(WIFEXITED(status), 1);
120 EXPECT_EQ(WEXITSTATUS(status), 0);
121 EXPECT_EQ(WIFSIGNALED(status), 0);
122 EXPECT_EQ(WTERMSIG(status), 0);
123 EXPECT_EQ(WCOREDUMP(status), 0);
124 EXPECT_EQ(WIFSTOPPED(status), 0);
125 EXPECT_EQ(WSTOPSIG(status), 0);
126 EXPECT_EQ(WIFCONTINUED(status), 0);
127 }
128