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 25using namespace testing::ext; 26using namespace std; 27 28static const int INVALID_VERSION = 0; 29static const int INVALID_PID = -1; 30static const int UNUSED_PID = 15464648; 31static pid_t g_pid; 32 33class CapSetApiTest : public testing::Test { 34public: 35 static void SetUpTestCase(); 36 static void TearDownTestCase(); 37 void SetUp(); 38 void TearDown(); 39private: 40}; 41 42void CapSetApiTest::SetUp() 43{ 44 g_pid = getpid(); 45} 46 47void CapSetApiTest::TearDown() 48{ 49} 50 51void CapSetApiTest::SetUpTestCase() 52{ 53} 54 55void CapSetApiTest::TearDownTestCase() 56{ 57} 58 59/* 60 * @tc.number : SUB_KERNEL_SYSCALL_CAPSET_0100 61 * @tc.name : CapSetVersionSuccess_0001 62 * @tc.desc : capset version 1 2 3 success. 63 * @tc.size : MediumTest 64 * @tc.type : Function 65 * @tc.level : Level 1 66 */ 67HWTEST_F(CapSetApiTest, CapSetVersionSuccess_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 = capset(&hdr, data); 75 EXPECT_TRUE(ret >= 0); 76 77 ret = -1; 78 hdr.version = _LINUX_CAPABILITY_VERSION_2; 79 ret = capset(&hdr, data); 80 EXPECT_TRUE(ret >= 0); 81 82 ret = -1; 83 hdr.version = _LINUX_CAPABILITY_VERSION_3; 84 ret = capset(&hdr, data); 85 EXPECT_TRUE(ret >= 0); 86} 87 88/* 89 * @tc.number : SUB_KERNEL_SYSCALL_CAPSET_0200 90 * @tc.name : CapSetInvalidVersionFailed_0002 91 * @tc.desc : capset invalid version return -1 errno EINVAL. 92 * @tc.size : MediumTest 93 * @tc.type : Function 94 * @tc.level : Level 2 95 */ 96HWTEST_F(CapSetApiTest, CapSetInvalidVersionFailed_0002, Function | MediumTest | Level2) 97{ 98 struct __user_cap_header_struct hdr; 99 struct __user_cap_data_struct data[2]; 100 hdr.pid = g_pid; 101 102 hdr.version = INVALID_VERSION; 103 errno = 0; 104 int ret = capset(&hdr, data); 105 EXPECT_EQ(ret, -1); 106 EXPECT_EQ(errno, EINVAL); 107} 108 109/* 110 * @tc.number : SUB_KERNEL_SYSCALL_CAPSET_0300 111 * @tc.name : CapSetInvalidPidFailed_0003 112 * @tc.desc : capset invalid g_pid return -1 errno EINVAL. 113 * @tc.size : MediumTest 114 * @tc.type : Function 115 * @tc.level : Level 2 116 */ 117HWTEST_F(CapSetApiTest, CapSetInvalidPidFailed_0003, Function | MediumTest | Level2) 118{ 119 struct __user_cap_header_struct hdr; 120 struct __user_cap_data_struct data[2]; 121 hdr.pid = INVALID_PID; 122 hdr.version = _LINUX_CAPABILITY_VERSION_2; 123 124 errno = 0; 125 int ret = capset(&hdr, data); 126 EXPECT_EQ(ret, -1); 127 EXPECT_EQ(errno, EPERM); 128} 129 130/* 131 * @tc.number : SUB_KERNEL_SYSCALL_CAPSET_0400 132 * @tc.name : CapSetUnUsedPidFailed_0004 133 * @tc.desc : capset unused g_pid return -1 errno ESRCH. 134 * @tc.size : MediumTest 135 * @tc.type : Function 136 * @tc.level : Level 2 137 */ 138HWTEST_F(CapSetApiTest, CapSetUnUsedPidFailed_0004, Function | MediumTest | Level2) 139{ 140 struct __user_cap_header_struct hdr; 141 struct __user_cap_data_struct data[2]; 142 hdr.pid = UNUSED_PID; 143 hdr.version = _LINUX_CAPABILITY_VERSION_2; 144 145 errno = 0; 146 int ret = capset(&hdr, data); 147 EXPECT_EQ(ret, -1); 148 EXPECT_EQ(errno, EPERM); 149} 150 151/* 152 * @tc.number : SUB_KERNEL_SYSCALL_CAPSET_0500 153 * @tc.name : CapSetInvalidAddressFailed_0005 154 * @tc.desc : capset invalid address return -1 errno EFAULT. 155 * @tc.size : MediumTest 156 * @tc.type : Function 157 * @tc.level : Level 2 158 */ 159HWTEST_F(CapSetApiTest, CapSetInvalidAddressFailed_0005, Function | MediumTest | Level2) 160{ 161 struct __user_cap_data_struct data[2]; 162 163 errno = 0; 164 int ret = capset(nullptr, data); 165 EXPECT_EQ(ret, -1); 166 EXPECT_EQ(errno, EFAULT); 167}