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 <cerrno>
17 #include <fcntl.h>
18 #include <unistd.h>
19 #include <gtest/gtest.h>
20 #include <linux/capability.h>
21 #include <sys/capability.h>
22 #include <sys/mman.h>
23 #include <sys/types.h>
24
25 using namespace testing::ext;
26 using namespace std;
27
28 static const int INVALID_VERSION = 0;
29 static const int INVALID_PID = -1;
30 static const int UNUSED_PID = 15464648;
31 static pid_t g_pid;
32
33 class CapGetApiTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 private:
40 };
41
SetUp()42 void CapGetApiTest::SetUp()
43 {
44 g_pid = getpid();
45 }
46
TearDown()47 void CapGetApiTest::TearDown()
48 {
49 }
50
SetUpTestCase()51 void CapGetApiTest::SetUpTestCase()
52 {
53 }
54
TearDownTestCase()55 void CapGetApiTest::TearDownTestCase()
56 {
57 }
58
59 /*
60 * @tc.number : SUB_KERNEL_SYSCALL_CAPGET_0100
61 * @tc.name : CapGetVersionSuccess_0001
62 * @tc.desc : capget version 1 2 3 and data success.
63 * @tc.size : MediumTest
64 * @tc.type : Function
65 * @tc.level : Level 1
66 */
67 HWTEST_F(CapGetApiTest, CapGetVersionSuccess_0001, Function | MediumTest | Level1)
68 {
69 struct __user_cap_header_struct hdr;
70 struct __user_cap_data_struct data[2];
71 hdr.pid = g_pid;
72
73 hdr.version = _LINUX_CAPABILITY_VERSION_1;
74 int ret = capget(&hdr, data);
75 EXPECT_TRUE(ret >= 0);
76
77 hdr.version = _LINUX_CAPABILITY_VERSION_2;
78 ret = capget(&hdr, data);
79 EXPECT_TRUE(ret >= 0);
80
81 hdr.version = _LINUX_CAPABILITY_VERSION_3;
82 ret = capget(&hdr, data);
83 EXPECT_TRUE(ret >= 0);
84
85 hdr.version = _LINUX_CAPABILITY_VERSION_2;
86 ret = capget(&hdr, nullptr);
87 EXPECT_TRUE(ret >= 0);
88 }
89
90 /*
91 * @tc.number : SUB_KERNEL_SYSCALL_CAPGET_0200
92 * @tc.name : CapGetInvalidVersionFailed_0002
93 * @tc.desc : capget invalid version return -1 errno EINVAL.
94 * @tc.size : MediumTest
95 * @tc.type : Function
96 * @tc.level : Level 2
97 */
98 HWTEST_F(CapGetApiTest, CapGetInvalidVersionFailed_0002, Function | MediumTest | Level2)
99 {
100 struct __user_cap_header_struct hdr;
101 struct __user_cap_data_struct data[2];
102 hdr.pid = g_pid;
103
104 hdr.version = INVALID_VERSION;
105 errno = 0;
106 int ret = capget(&hdr, data);
107 EXPECT_EQ(ret, -1);
108 EXPECT_EQ(errno, EINVAL);
109 }
110
111 /*
112 * @tc.number : SUB_KERNEL_SYSCALL_CAPGET_0300
113 * @tc.name : CapGetInvalidPidFailed_0003
114 * @tc.desc : capget invalid g_pid return -1 errno EINVAL.
115 * @tc.size : MediumTest
116 * @tc.type : Function
117 * @tc.level : Level 2
118 */
119 HWTEST_F(CapGetApiTest, CapGetInvalidPidFailed_0003, Function | MediumTest | Level2)
120 {
121 struct __user_cap_header_struct hdr;
122 struct __user_cap_data_struct data[2];
123 hdr.pid = INVALID_PID;
124 hdr.version = _LINUX_CAPABILITY_VERSION_2;
125
126 errno = 0;
127 int ret = capget(&hdr, data);
128 EXPECT_EQ(ret, -1);
129 EXPECT_EQ(errno, EINVAL);
130 }
131
132 /*
133 * @tc.number : SUB_KERNEL_SYSCALL_CAPGET_0400
134 * @tc.name : CapGetUnUsedPidFailed_0004
135 * @tc.desc : capget unused g_pid return -1 errno ESRCH.
136 * @tc.size : MediumTest
137 * @tc.type : Function
138 * @tc.level : Level 2
139 */
140 HWTEST_F(CapGetApiTest, CapGetUnUsedPidFailed_0004, Function | MediumTest | Level2)
141 {
142 struct __user_cap_header_struct hdr;
143 struct __user_cap_data_struct data[2];
144 hdr.pid = UNUSED_PID;
145 hdr.version = _LINUX_CAPABILITY_VERSION_2;
146
147 errno = 0;
148 int ret = capget(&hdr, data);
149 EXPECT_EQ(ret, -1);
150 EXPECT_EQ(errno, ESRCH);
151 }
152
153 /*
154 * @tc.number : SUB_KERNEL_SYSCALL_CAPGET_0500
155 * @tc.name : CapGetInvalidAddressFailed_0005
156 * @tc.desc : capget invalid address return -1 errno EFAULT.
157 * @tc.size : MediumTest
158 * @tc.type : Function
159 * @tc.level : Level 2
160 */
161 HWTEST_F(CapGetApiTest, CapGetInvalidAddressFailed_0005, Function | MediumTest | Level2)
162 {
163 struct __user_cap_data_struct data[2];
164
165 errno = 0;
166 int ret = capget(nullptr, data);
167 EXPECT_EQ(ret, -1);
168 EXPECT_EQ(errno, EFAULT);
169 }