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