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 <csignal>
17#include <unistd.h>
18#include <gtest/gtest.h>
19#include <sys/wait.h>
20
21using namespace testing::ext;
22
23class HatsKillTest : public testing::Test {
24public:
25    static void SetUpTestCase();
26    static void TearDownTestCase();
27    void SetUp();
28    void TearDown();
29private:
30};
31void HatsKillTest::SetUp()
32{
33}
34void HatsKillTest::TearDown()
35{
36}
37void HatsKillTest::SetUpTestCase()
38{
39}
40void HatsKillTest::TearDownTestCase()
41{
42}
43
44/*
45 * @tc.number : SUB_KERNEL_SYSCALL_KILL_0100
46 * @tc.name   : KillNormalTerminateProcessSuccess_0001
47 * @tc.desc   : kill normal terminate process success.
48 * @tc.size   : MediumTest
49 * @tc.type   : Function
50 * @tc.level  : Level 1
51 */
52HWTEST_F(HatsKillTest, KillNormalTerminateProcessSuccess_0001, Function | MediumTest | Level1)
53{
54    ssize_t killRet = 0;
55    pid_t waitpidRet = 0;
56    bool signalRet = false;
57
58    pid_t childPid = fork();
59    if (childPid == 0) {
60        usleep(5);
61    } else {
62        killRet = kill(childPid, SIGTERM);
63        EXPECT_EQ(killRet, 0);
64        int status;
65        waitpidRet = waitpid(childPid, &status, WUNTRACED);
66        EXPECT_EQ(waitpidRet, childPid);
67        signalRet = WIFSIGNALED(status);
68        EXPECT_TRUE(signalRet);
69    }
70}
71
72/*
73 * @tc.number : SUB_KERNEL_SYSCALL_KILL_0200
74 * @tc.name   : KillSendInvalidSignalFail_0002
75 * @tc.desc   : Kill send invalid signal fail, return -1, and set errno.
76 * @tc.size   : MediumTest
77 * @tc.type   : Function
78 * @tc.level  : Level 2
79 */
80HWTEST_F(HatsKillTest, KillSendInvalidSignalFail_0002, Function | MediumTest | Level2)
81{
82    ssize_t killRet = 0;
83
84    pid_t pid = getpid();
85    errno = 0;
86    killRet = kill(pid, -1);
87    EXPECT_EQ(killRet, -1);
88    EXPECT_EQ(errno, EINVAL);
89}
90