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 #include <sys/tgkill.h>
20 using namespace testing::ext;
21 
22 class HatsTgkillTest : 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 HatsTgkillTest::SetUp()
31 {
32 }
TearDown()33 void HatsTgkillTest::TearDown()
34 {
35 }
SetUpTestCase()36 void HatsTgkillTest::SetUpTestCase()
37 {
38 }
TearDownTestCase()39 void HatsTgkillTest::TearDownTestCase()
40 {
41 }
42 
Handle(int sig)43 void Handle(int sig)
44 {
45 }
46 
47 /*
48  * @tc.number : SUB_KERNEL_SYSCALL_TGKILL_0100
49  * @tc.name   : TgkillSendSignalToExistingProcessSuccess_0001
50  * @tc.desc   : tgkill send signal to existing process success.
51  * @tc.size   : MediumTest
52  * @tc.type   : Function
53  * @tc.level  : Level 1
54  */
HWTEST_F(HatsTgkillTest, TgkillSendSignalToExistingProcessSuccess_0001, Function | MediumTest | Level1)55 HWTEST_F(HatsTgkillTest, TgkillSendSignalToExistingProcessSuccess_0001, Function | MediumTest | Level1)
56 {
57     int ret;
58     pid_t tid;
59     struct sigaction sa;
60 
61     sa.sa_handler = &Handle;
62     ret = sigemptyset(&sa.sa_mask);
63     EXPECT_EQ(ret, 0);
64     sa.sa_flags = 0;
65     ret = sigaction(SIGUSR1, &sa, NULL);
66     EXPECT_EQ(ret, 0);
67     tid = gettid();
68     ret = tgkill(getpid(), tid, SIGUSR1);
69     EXPECT_EQ(ret, 0);
70 }
71 
72 /*
73  * @tc.number : SUB_KERNEL_SYSCALL_TGKILL_0200
74  * @tc.name   : TgkillSendSignalToNonExistingProcessFail_0002
75  * @tc.desc   : tgkill send signal to nonExisting process fail, return -1, and set errno.
76  * @tc.size   : MediumTest
77  * @tc.type   : Function
78  * @tc.level  : Level 2
79  */
HWTEST_F(HatsTgkillTest, TgkillSendSignalToNonExistingProcessFail_0002, Function | MediumTest | Level2)80 HWTEST_F(HatsTgkillTest, TgkillSendSignalToNonExistingProcessFail_0002, Function | MediumTest | Level2)
81 {
82     errno = 0;
83     int ret = tgkill(-1, -1, SIGUSR1);
84     EXPECT_EQ(ret, -1);
85     EXPECT_EQ(errno, EINVAL);
86 }
87