19762338dSopenharmony_ci/*
29762338dSopenharmony_ci * Copyright (C) 2024 HiHope Open Source Organization.
39762338dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49762338dSopenharmony_ci * you may not use this file except in compliance with the License.
59762338dSopenharmony_ci * You may obtain a copy of the License at
69762338dSopenharmony_ci *
79762338dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89762338dSopenharmony_ci *
99762338dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109762338dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119762338dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129762338dSopenharmony_ci * See the License for the specific language governing permissions and
139762338dSopenharmony_ci * limitations under the License.
149762338dSopenharmony_ci */
159762338dSopenharmony_ci
169762338dSopenharmony_ci#include <cerrno>
179762338dSopenharmony_ci#include <cstdio>
189762338dSopenharmony_ci#include <cstdlib>
199762338dSopenharmony_ci#include <csignal>
209762338dSopenharmony_ci#include <string>
219762338dSopenharmony_ci#include <vector>
229762338dSopenharmony_ci#include <fcntl.h>
239762338dSopenharmony_ci#include <unistd.h>
249762338dSopenharmony_ci#include <malloc.h>
259762338dSopenharmony_ci#include <poll.h>
269762338dSopenharmony_ci#include <arpa/inet.h>
279762338dSopenharmony_ci#include <gtest/gtest.h>
289762338dSopenharmony_ci#include <netinet/in.h>
299762338dSopenharmony_ci#include <sys/stat.h>
309762338dSopenharmony_ci#include <sys/mman.h>
319762338dSopenharmony_ci#include <sys/wait.h>
329762338dSopenharmony_ci#include <sys/socket.h>
339762338dSopenharmony_ci#include <sys/syscall.h>
349762338dSopenharmony_ci#include <sys/timerfd.h>
359762338dSopenharmony_ci#include <sys/types.h>
369762338dSopenharmony_ci#include "securec.h"
379762338dSopenharmony_ci
389762338dSopenharmony_ciusing namespace testing::ext;
399762338dSopenharmony_ci
409762338dSopenharmony_cistatic const int DELAY_TIME = 10 * 1000;
419762338dSopenharmony_ci
429762338dSopenharmony_ciclass HatsWait4Test : public testing::Test {
439762338dSopenharmony_cipublic:
449762338dSopenharmony_ci    static void SetUpTestCase();
459762338dSopenharmony_ci    static void TearDownTestCase();
469762338dSopenharmony_ci    void SetUp();
479762338dSopenharmony_ci    void TearDown();
489762338dSopenharmony_ciprivate:
499762338dSopenharmony_ci};
509762338dSopenharmony_civoid HatsWait4Test::SetUp()
519762338dSopenharmony_ci{
529762338dSopenharmony_ci}
539762338dSopenharmony_ci
549762338dSopenharmony_civoid HatsWait4Test::TearDown()
559762338dSopenharmony_ci{
569762338dSopenharmony_ci}
579762338dSopenharmony_ci
589762338dSopenharmony_civoid HatsWait4Test::SetUpTestCase()
599762338dSopenharmony_ci{
609762338dSopenharmony_ci}
619762338dSopenharmony_ci
629762338dSopenharmony_civoid HatsWait4Test::TearDownTestCase()
639762338dSopenharmony_ci{
649762338dSopenharmony_ci}
659762338dSopenharmony_ci
669762338dSopenharmony_ci/*
679762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_WAIT4_0100
689762338dSopenharmony_ci * @tc.name   : Wait4Failed_0001
699762338dSopenharmony_ci * @tc.desc   : Wait4 wait continued failed.
709762338dSopenharmony_ci * @tc.size   : MediumTest
719762338dSopenharmony_ci * @tc.type   : Function
729762338dSopenharmony_ci * @tc.level  : Level 2
739762338dSopenharmony_ci */
749762338dSopenharmony_ciHWTEST_F(HatsWait4Test, Wait4Failed_0001, Function | MediumTest | Level2)
759762338dSopenharmony_ci{
769762338dSopenharmony_ci    int ret;
779762338dSopenharmony_ci    pid_t childPid = -1;
789762338dSopenharmony_ci
799762338dSopenharmony_ci    sigset_t sigMask;
809762338dSopenharmony_ci    ret = sigemptyset(&sigMask);
819762338dSopenharmony_ci    EXPECT_TRUE(ret >= 0);
829762338dSopenharmony_ci    ret = sigaddset(&sigMask, SIGCHLD);
839762338dSopenharmony_ci    EXPECT_TRUE(ret >= 0);
849762338dSopenharmony_ci
859762338dSopenharmony_ci    struct sigaction sa;
869762338dSopenharmony_ci    sa.sa_sigaction = nullptr;
879762338dSopenharmony_ci    sa.sa_flags = SA_SIGINFO;
889762338dSopenharmony_ci    ret = sigemptyset(&sa.sa_mask);
899762338dSopenharmony_ci    EXPECT_TRUE(ret >= 0);
909762338dSopenharmony_ci    ret = sigaction(SIGCHLD, &sa, nullptr);
919762338dSopenharmony_ci    EXPECT_EQ(ret, 0);
929762338dSopenharmony_ci
939762338dSopenharmony_ci    int wstatus = 0;
949762338dSopenharmony_ci    int options = WCONTINUED;
959762338dSopenharmony_ci    int retPid = wait4(childPid, &wstatus, options, nullptr);
969762338dSopenharmony_ci    EXPECT_EQ(retPid, -1);
979762338dSopenharmony_ci}
989762338dSopenharmony_ci
999762338dSopenharmony_ci/*
1009762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_WAIT4_0200
1019762338dSopenharmony_ci * @tc.name   : Wait4ChildProcessSuccess_0002
1029762338dSopenharmony_ci * @tc.desc   : wait4 child process and WIFEXITED success.
1039762338dSopenharmony_ci * @tc.size   : MediumTest
1049762338dSopenharmony_ci * @tc.type   : Function
1059762338dSopenharmony_ci * @tc.level  : Level 1
1069762338dSopenharmony_ci */
1079762338dSopenharmony_ciHWTEST_F(HatsWait4Test, Wait4ChildProcessSuccess_0002, Function | MediumTest | Level1)
1089762338dSopenharmony_ci{
1099762338dSopenharmony_ci    pid_t pid = fork();
1109762338dSopenharmony_ci    if (pid == 0) {
1119762338dSopenharmony_ci        printf("Child process (PID: %d) is running...\n", getpid());
1129762338dSopenharmony_ci        usleep(DELAY_TIME);
1139762338dSopenharmony_ci        exit(0);
1149762338dSopenharmony_ci    }
1159762338dSopenharmony_ci    int status;
1169762338dSopenharmony_ci    struct rusage usage;
1179762338dSopenharmony_ci    pid_t tpid = wait4(pid, &status, 0, &usage);
1189762338dSopenharmony_ci    EXPECT_TRUE(tpid > 0);
1199762338dSopenharmony_ci    EXPECT_EQ(WIFEXITED(status), 1);
1209762338dSopenharmony_ci    EXPECT_EQ(WEXITSTATUS(status), 0);
1219762338dSopenharmony_ci    EXPECT_EQ(WIFSIGNALED(status), 0);
1229762338dSopenharmony_ci    EXPECT_EQ(WTERMSIG(status), 0);
1239762338dSopenharmony_ci    EXPECT_EQ(WCOREDUMP(status), 0);
1249762338dSopenharmony_ci    EXPECT_EQ(WIFSTOPPED(status), 0);
1259762338dSopenharmony_ci    EXPECT_EQ(WSTOPSIG(status), 0);
1269762338dSopenharmony_ci    EXPECT_EQ(WIFCONTINUED(status), 0);
1279762338dSopenharmony_ci}
128