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 <unistd.h>
179762338dSopenharmony_ci#include <gtest/gtest.h>
189762338dSopenharmony_ci#include <sys/syscall.h>
199762338dSopenharmony_ci
209762338dSopenharmony_ciusing namespace testing::ext;
219762338dSopenharmony_ci
229762338dSopenharmony_ciclass HatsTkillTest : public testing::Test {
239762338dSopenharmony_cipublic:
249762338dSopenharmony_ci    static void SetUpTestCase();
259762338dSopenharmony_ci    static void TearDownTestCase();
269762338dSopenharmony_ci    void SetUp();
279762338dSopenharmony_ci    void TearDown();
289762338dSopenharmony_ciprivate:
299762338dSopenharmony_ci};
309762338dSopenharmony_civoid HatsTkillTest::SetUp()
319762338dSopenharmony_ci{
329762338dSopenharmony_ci}
339762338dSopenharmony_civoid HatsTkillTest::TearDown()
349762338dSopenharmony_ci{
359762338dSopenharmony_ci}
369762338dSopenharmony_civoid HatsTkillTest::SetUpTestCase()
379762338dSopenharmony_ci{
389762338dSopenharmony_ci}
399762338dSopenharmony_civoid HatsTkillTest::TearDownTestCase()
409762338dSopenharmony_ci{
419762338dSopenharmony_ci}
429762338dSopenharmony_ci
439762338dSopenharmony_ci/*
449762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_TKILL_0100
459762338dSopenharmony_ci * @tc.name   : TkillSendSignalToExistingProcessSuccess_0001
469762338dSopenharmony_ci * @tc.desc   : tkill send signal to existing process success.
479762338dSopenharmony_ci * @tc.size   : MediumTest
489762338dSopenharmony_ci * @tc.type   : Function
499762338dSopenharmony_ci * @tc.level  : Level 1
509762338dSopenharmony_ci */
519762338dSopenharmony_ciHWTEST_F(HatsTkillTest, TkillSendSignalToExistingProcessSuccess_0001, Function | MediumTest | Level1)
529762338dSopenharmony_ci{
539762338dSopenharmony_ci    bool signalRet = false;
549762338dSopenharmony_ci    int signalNumber = 0;
559762338dSopenharmony_ci
569762338dSopenharmony_ci    pid_t childPid = fork();
579762338dSopenharmony_ci    if (childPid == 0) {
589762338dSopenharmony_ci        usleep(5);
599762338dSopenharmony_ci    } else {
609762338dSopenharmony_ci        int ret = syscall(SYS_tkill, childPid, SIGTERM);
619762338dSopenharmony_ci        EXPECT_EQ(ret, 0);
629762338dSopenharmony_ci        int status;
639762338dSopenharmony_ci        pid_t waitret = waitpid(childPid, &status, 0);
649762338dSopenharmony_ci        EXPECT_EQ(waitret, childPid);
659762338dSopenharmony_ci        signalRet = WIFSIGNALED(status);
669762338dSopenharmony_ci        signalNumber = WTERMSIG(status);
679762338dSopenharmony_ci        EXPECT_TRUE(signalRet && (SIGTERM == signalNumber));
689762338dSopenharmony_ci    }
699762338dSopenharmony_ci}
709762338dSopenharmony_ci
719762338dSopenharmony_ci/*
729762338dSopenharmony_ci * @tc.number : SUB_KERNEL_SYSCALL_TKILL_0200
739762338dSopenharmony_ci * @tc.name   : TkillSendSignalToNonExistingProcessFail_0002
749762338dSopenharmony_ci * @tc.desc   : tkill send signal to nonExisting process fail, return -1, and set errno.
759762338dSopenharmony_ci * @tc.size   : MediumTest
769762338dSopenharmony_ci * @tc.type   : Function
779762338dSopenharmony_ci * @tc.level  : Level 2
789762338dSopenharmony_ci */
799762338dSopenharmony_ciHWTEST_F(HatsTkillTest, TkillSendSignalToNonExistingProcessFail_0002, Function | MediumTest | Level2)
809762338dSopenharmony_ci{
819762338dSopenharmony_ci    errno = 0;
829762338dSopenharmony_ci    int ret = syscall(SYS_tkill, -1, SIGTERM);
839762338dSopenharmony_ci    EXPECT_EQ(ret, -1);
849762338dSopenharmony_ci    EXPECT_EQ(errno, EINVAL);
859762338dSopenharmony_ci}
86