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 <cstdio>
18#include <cstdlib>
19#include <string>
20#include <vector>
21#include <unistd.h>
22#include <gtest/gtest.h>
23#include <sys/fsuid.h>
24#include <sys/personality.h>
25#include <sys/stat.h>
26#include <sys/types.h>
27
28
29using namespace testing::ext;
30using namespace std;
31
32static const int MAX_GROUPS = 1024;
33const unsigned long RETRIEVE_PERSONALITY = 0xffffffff;
34const unsigned long DEFAULT_PERSONALITY = 0;
35
36class UserApiTest : public testing::Test {
37public:
38    static void SetUpTestCase();
39    static void TearDownTestCase();
40    void SetUp();
41    void TearDown();
42private:
43};
44void UserApiTest::SetUp()
45{
46}
47void UserApiTest::TearDown()
48{
49}
50void UserApiTest::SetUpTestCase()
51{
52}
53void UserApiTest::TearDownTestCase()
54{
55}
56
57/*
58 * @tc.number : SUB_KERNEL_SYSCALL_USER_0100
59 * @tc.name   : SetpGidSetGroupIDSuccess_0001
60 * @tc.desc   : SetpGid Set group ID success.
61 * @tc.size   : MediumTest
62 * @tc.type   : Function
63 * @tc.level  : Level 1
64 */
65HWTEST_F(UserApiTest, SetpGidSetGroupIDSuccess_0001, Function | MediumTest | Level1)
66{
67    int ret;
68    gid_t gid = getgid();
69    EXPECT_TRUE(gid >= 0);
70
71    ret = setpgid(gid, gid);
72    EXPECT_EQ(ret, 0);
73
74    ret = setpgid(0, gid);
75    EXPECT_EQ(ret, 0);
76
77    ret = setpgid(gid, 0);
78    EXPECT_EQ(ret, 0);
79}
80
81/*
82 * @tc.number : SUB_KERNEL_SYSCALL_USER_0200
83 * @tc.name   : SetpGidSetGroupIDFail_0002
84 * @tc.desc   : SetpGid Set group ID fail.
85 * @tc.size   : MediumTest
86 * @tc.type   : Function
87 * @tc.level  : Level 2
88 */
89HWTEST_F(UserApiTest, SetpGidSetGroupIDFail_0002, Function | MediumTest | Level2)
90{
91    gid_t invalidGid = -1;
92    errno = 0;
93    int ret = setpgid(0, invalidGid);
94    EXPECT_EQ(ret, -1);
95    EXPECT_EQ(errno, EINVAL);
96}
97
98/*
99 * @tc.number : SUB_KERNEL_SYSCALL_USER_0300
100 * @tc.name   : GetgroupsGetGroupIDListSuccess_0003
101 * @tc.desc   : Getgroups get list of supplementary group IDs success.
102 * @tc.size   : MediumTest
103 * @tc.type   : Function
104 * @tc.level  : Level 1
105 */
106HWTEST_F(UserApiTest, GetgroupsGetGroupIDListSuccess_0003, Function | MediumTest | Level1)
107{
108    int numGroups;
109    gid_t groups[MAX_GROUPS];
110
111    numGroups = getgroups(MAX_GROUPS, groups);
112    EXPECT_TRUE(numGroups >= 0);
113}
114
115/*
116 * @tc.number : SUB_KERNEL_SYSCALL_USER_0400
117 * @tc.name   : GetgroupsGetGroupIDInNullFail_0004
118 * @tc.desc   : Getgroups get list of supplementary group IDs in nullptr fail, errno EFAULT.
119 * @tc.size   : MediumTest
120 * @tc.type   : Function
121 * @tc.level  : Level 2
122 */
123HWTEST_F(UserApiTest, GetgroupsGetGroupIDInNullFail_0004, Function | MediumTest | Level2)
124{
125    errno = 0;
126    int numGroups = getgroups(MAX_GROUPS, nullptr);
127    EXPECT_EQ(numGroups, -1);
128    EXPECT_EQ(errno, EFAULT);
129}
130
131/*
132 * @tc.number : SUB_KERNEL_SYSCALL_USER_0500
133 * @tc.name   : GetpgidAcquireSpecifyProcessGroupIDSuccess_0005
134 * @tc.desc   : Getpgid acquire specify process group ID success.
135 * @tc.size   : MediumTest
136 * @tc.type   : Function
137 * @tc.level  : Level 1
138 */
139HWTEST_F(UserApiTest, GetpgidAcquireSpecifyProcessGroupIDSuccess_0005, Function | MediumTest | Level1)
140{
141    pid_t pid = getpid();
142    pid_t pgid = getpgid(pid);
143    EXPECT_TRUE(pgid >= 0);
144}
145
146/*
147 * @tc.number : SUB_KERNEL_SYSCALL_USER_0600
148 * @tc.name   : GetpgidAcquireInvalidProcessGroupIDFail_0006
149 * @tc.desc   : Getpgid acquire group ID of invalid process ID fail, errno ESRCH.
150 * @tc.size   : MediumTest
151 * @tc.type   : Function
152 * @tc.level  : Level 2
153 */
154HWTEST_F(UserApiTest, GetpgidAcquireInvalidProcessGroupIDFail_0006, Function | MediumTest | Level2)
155{
156    int invalidPID = -1;
157    errno = 0;
158    pid_t pgid = getpgid(invalidPID);
159    EXPECT_EQ(pgid, -1);
160    EXPECT_EQ(errno, ESRCH);
161}
162
163/*
164 * @tc.number : SUB_KERNEL_SYSCALL_USER_0700
165 * @tc.name   : GetresuidGetReadEffectiveSavedUIDSuccess_0007
166 * @tc.desc   : Getresuid get real uid effective uid and saved uid success.
167 * @tc.size   : MediumTest
168 * @tc.type   : Function
169 * @tc.level  : Level 1
170 */
171HWTEST_F(UserApiTest, GetresuidGetReadEffectiveSavedUIDSuccess_0007, Function | MediumTest | Level1)
172{
173    int ret;
174    uid_t getRUid, getEUid, savedUid;
175    uid_t realUid = getuid();
176    uid_t effectiveUid = geteuid();
177    ret = getresuid(&getRUid, &getEUid, &savedUid);
178    EXPECT_EQ(ret, 0);
179    EXPECT_EQ(realUid, getRUid);
180    EXPECT_EQ(effectiveUid, getEUid);
181    EXPECT_TRUE(savedUid >= 0);
182}
183
184/*
185 * @tc.number : SUB_KERNEL_SYSCALL_USER_0800
186 * @tc.name   : GetresuidGetReadEffectiveSavedUIDInNullptrFail_0008
187 * @tc.desc   : Getresuid get real uid effective uid and saved uid in nullptr fail.
188 * @tc.size   : MediumTest
189 * @tc.type   : Function
190 * @tc.level  : Level 2
191 */
192HWTEST_F(UserApiTest, GetresuidGetReadEffectiveSavedUIDInNullptrFail_0008, Function | MediumTest | Level2)
193{
194    int ret;
195    uid_t realUid, effectiveUid, savedUid;
196
197    errno = 0;
198    ret = getresuid(nullptr, &effectiveUid, &savedUid);
199    EXPECT_EQ(ret, -1);
200    EXPECT_EQ(errno, EFAULT);
201    errno = 0;
202    ret = getresuid(&realUid, nullptr, &savedUid);
203    EXPECT_EQ(ret, -1);
204    EXPECT_EQ(errno, EFAULT);
205    errno = 0;
206    ret = getresuid(&realUid, &effectiveUid, nullptr);
207    EXPECT_EQ(ret, -1);
208    EXPECT_EQ(errno, EFAULT);
209}
210
211
212/*
213 * @tc.number : SUB_KERNEL_SYSCALL_USER_0900
214 * @tc.name   : GetuidAcquireUIDSuccess_0009
215 * @tc.desc   : Getuid acquire user ID success.
216 * @tc.size   : MediumTest
217 * @tc.type   : Function
218 * @tc.level  : Level 1
219 */
220HWTEST_F(UserApiTest, GetuidAcquireUIDSuccess_0009, Function | MediumTest | Level1)
221{
222    int ret;
223    uid_t uid = getuid();
224    EXPECT_TRUE(uid >= 0);
225
226    uid_t setUid = uid + 1;
227    ret = setuid(setUid);
228    EXPECT_EQ(ret, 0);
229
230    uid_t newUid = getuid();
231    EXPECT_EQ(newUid, setUid);
232}
233
234/*
235 * @tc.number : SUB_KERNEL_SYSCALL_USER_1000
236 * @tc.name   : PersonalitySetExecutionDomainSuccess_0010
237 * @tc.desc   : Personality set the process execution domain success.
238 * @tc.size   : MediumTest
239 * @tc.type   : Function
240 * @tc.level  : Level 1
241 */
242HWTEST_F(UserApiTest, PersonalitySetExecutionDomainSuccess_0010, Function | MediumTest | Level1)
243{
244    int ret = personality(PER_LINUX32);
245    EXPECT_EQ(ret, 0);
246    unsigned long currentPersonality = static_cast<unsigned long>(personality(RETRIEVE_PERSONALITY));
247    EXPECT_EQ(currentPersonality, PER_LINUX32);
248
249    ret = personality(DEFAULT_PERSONALITY);
250    EXPECT_GE(ret, 0);
251}
252
253/*
254 * @tc.number : SUB_KERNEL_SYSCALL_USER_1100
255 * @tc.name   : SetfsgidCurrentSuccess_0011
256 * @tc.desc   : Setfsgid set current gid.
257 * @tc.size   : MediumTest
258 * @tc.type   : Function
259 * @tc.level  : Level 1
260 */
261HWTEST_F(UserApiTest, SetfsgidCurrentSuccess_0011, Function | MediumTest | Level1)
262{
263    int ret;
264    ret = setfsgid(0);
265    EXPECT_GE(ret, 0);
266}
267
268/*
269 * @tc.number : SUB_KERNEL_SYSCALL_USER_1200
270 * @tc.name   : SetfsgidInvalidFsgidFailed_0012
271 * @tc.desc   : Setfsgid set user ID success.
272 * @tc.size   : MediumTest
273 * @tc.type   : Function
274 * @tc.level  : Level 2
275 */
276HWTEST_F(UserApiTest, SetfsgidInvalidFsgidFailed_0012, Function | MediumTest | Level2)
277{
278    int ret;
279    ret = setfsgid(-1);
280    EXPECT_GE(ret, -1);
281    EXPECT_EQ(errno, EFAULT);
282}
283
284/*
285 * @tc.number : SUB_KERNEL_SYSCALL_USER_1300
286 * @tc.name   : SetfsuidSetFSUserIDSuccess_0013
287 * @tc.desc   : Setfsuid set user ID used for file system checks success.
288 * @tc.size   : MediumTest
289 * @tc.type   : Function
290 * @tc.level  : Level 1
291 */
292HWTEST_F(UserApiTest, SetfsuidSetFSUserIDSuccess_0013, Function | MediumTest | Level1)
293{
294    int ret;
295    uid_t effectiveUid = geteuid();
296    EXPECT_TRUE(effectiveUid >= 0);
297
298    uid_t fsUid = effectiveUid + 1;
299    ret = setfsuid(fsUid);
300    EXPECT_GE(ret, 0);
301}
302
303/*
304 * @tc.number : SUB_KERNEL_SYSCALL_USER_1400
305 * @tc.name   : SetgidSetGroupIDSuccess_0014
306 * @tc.desc   : Setgid Set group ID success.
307 * @tc.size   : MediumTest
308 * @tc.type   : Function
309 * @tc.level  : Level 1
310 */
311HWTEST_F(UserApiTest, SetgidSetGroupIDSuccess_0014, Function | MediumTest | Level1)
312{
313    int ret;
314    gid_t gid = getgid();
315    EXPECT_TRUE(gid >= 0);
316
317    gid_t setGid = gid + 1;
318    ret = setgid(setGid);
319    EXPECT_EQ(ret, 0);
320
321    gid_t newGid = getgid();
322    EXPECT_EQ(newGid, setGid);
323}
324
325/*
326 * @tc.number : SUB_KERNEL_SYSCALL_USER_1500
327 * @tc.name   : SetgidSetGroupIDFail_0015
328 * @tc.desc   : Setgid Set group ID fail.
329 * @tc.size   : MediumTest
330 * @tc.type   : Function
331 * @tc.level  : Level 2
332 */
333HWTEST_F(UserApiTest, SetgidSetGroupIDFail_0015, Function | MediumTest | Level2)
334{
335    gid_t invalidGid = -1;
336    errno = 0;
337    int ret = setgid(invalidGid);
338    EXPECT_EQ(ret, -1);
339    EXPECT_EQ(errno, EINVAL);
340}
341
342/*
343 * @tc.number : SUB_KERNEL_SYSCALL_USER_1600
344 * @tc.name   : SetgroupsSetGroupIDListSuccess_0016
345 * @tc.desc   : Setgroups set list of supplementary group IDs success.
346 * @tc.size   : MediumTest
347 * @tc.type   : Function
348 * @tc.level  : Level 1
349 */
350HWTEST_F(UserApiTest, SetgroupsSetGroupIDListSuccess_0016, Function | MediumTest | Level1)
351{
352    int numGroups;
353    int ret;
354    const int groupLength = 3;
355    gid_t groups[groupLength] = { 1001, 1002, 1003 };
356    gid_t groupsCheck[MAX_GROUPS];
357
358    ret = setgroups(groupLength, groups);
359    EXPECT_EQ(ret, 0);
360    numGroups = getgroups(MAX_GROUPS, groupsCheck);
361    EXPECT_EQ(numGroups, groupLength);
362    for (int i = 0; i < numGroups; i++) {
363        EXPECT_EQ(groups[i], groupsCheck[i]);
364    }
365}
366
367/*
368 * @tc.number : SUB_KERNEL_SYSCALL_USER_1700
369 * @tc.name   : SetgroupsSetGroupIDNullptrFail_0017
370 * @tc.desc   : Setgroups set list of supplementary group IDs in nullptr fail, errno EFAULT.
371 * @tc.size   : MediumTest
372 * @tc.type   : Function
373 * @tc.level  : Level 2
374 */
375HWTEST_F(UserApiTest, SetgroupsSetGroupIDNullptrFail_0017, Function | MediumTest | Level2)
376{
377    errno = 0;
378    int ret = setgroups(MAX_GROUPS, nullptr);
379    EXPECT_EQ(ret, -1);
380    EXPECT_EQ(errno, EFAULT);
381
382    errno = 0;
383    ret = getgroups(MAX_GROUPS, nullptr);
384    EXPECT_EQ(ret, -1);
385    EXPECT_EQ(errno, EFAULT);
386}
387
388/*
389 * @tc.number : SUB_KERNEL_SYSCALL_USER_1800
390 * @tc.name   : SetregidSetRealEffectiveGIDSuccess_0018
391 * @tc.desc   : Setregid Set real and effective group ID success.
392 * @tc.size   : MediumTest
393 * @tc.type   : Function
394 * @tc.level  : Level 1
395 */
396HWTEST_F(UserApiTest, SetregidSetRealEffectiveGIDSuccess_0018, Function | MediumTest | Level1)
397{
398    int ret;
399    gid_t realGid = getgid();
400    EXPECT_TRUE(realGid >= 0);
401    gid_t effectiveGid = getegid();
402    EXPECT_TRUE(effectiveGid >= 0);
403
404    gid_t setRealGid = realGid + 1;
405    gid_t setEffectiveGid = effectiveGid + 1;
406    ret = setregid(setRealGid, setEffectiveGid);
407    EXPECT_EQ(ret, 0);
408
409    gid_t newRealGid = getgid();
410    EXPECT_EQ(newRealGid, setRealGid);
411    gid_t newEffectiveGid = getegid();
412    EXPECT_EQ(newEffectiveGid, setEffectiveGid);
413}
414
415/*
416 * @tc.number : SUB_KERNEL_SYSCALL_USER_1900
417 * @tc.name   : SetresgidSetRealEffectiveSavedGIDSuccess_0019
418 * @tc.desc   : Setresgid Set real effective and saved group ID success.
419 * @tc.size   : MediumTest
420 * @tc.type   : Function
421 * @tc.level  : Level 1
422 */
423HWTEST_F(UserApiTest, SetresgidSetRealEffectiveSavedGIDSuccess_0019, Function | MediumTest | Level1)
424{
425    int ret;
426    pid_t pid = getpid();
427    gid_t realGid = getgid();
428    EXPECT_TRUE(realGid >= 0);
429    gid_t effectiveGid = getegid();
430    EXPECT_TRUE(effectiveGid >= 0);
431    gid_t savedGid = getsid(pid);
432    EXPECT_TRUE(savedGid >= 0);
433
434    gid_t setRealGid = realGid + 1;
435    gid_t setEffectiveGid = effectiveGid + 1;
436    gid_t notModifySavedGid = -1;
437    ret = setresgid(setRealGid, setEffectiveGid, notModifySavedGid);
438    EXPECT_EQ(ret, 0);
439
440    gid_t newRealGid = getgid();
441    EXPECT_EQ(newRealGid, setRealGid);
442    gid_t newEffectiveGid = getegid();
443    EXPECT_EQ(newEffectiveGid, setEffectiveGid);
444    gid_t newSavedGid = getsid(pid);
445    EXPECT_EQ(newSavedGid, savedGid);
446}
447
448/*
449 * @tc.number : SUB_KERNEL_SYSCALL_USER_2000
450 * @tc.name   : SetresuidSuccess_0020
451 * @tc.desc   : Setresuid set current uid.
452 * @tc.size   : MediumTest
453 * @tc.type   : Function
454 * @tc.level  : Level 1
455 */
456HWTEST_F(UserApiTest, SetresuidSuccess_0020, Function | MediumTest | Level1)
457{
458    int ret;
459    ret = setresuid(0, 0, 0);
460    EXPECT_EQ(ret, 0);
461}
462
463/*
464 * @tc.number : SUB_KERNEL_SYSCALL_USER_2100
465 * @tc.name   : SetreuidSuccess_0021
466 * @tc.desc   : Setresuid Set current uid.
467 * @tc.size   : MediumTest
468 * @tc.type   : Function
469 * @tc.level  : Level 1
470 */
471HWTEST_F(UserApiTest, SetreuidSuccess_0021, Function | MediumTest | Level1)
472{
473    int ret;
474    ret = setreuid(0, 0);
475    EXPECT_EQ(ret, 0);
476}
477
478/*
479 * @tc.number : SUB_KERNEL_SYSCALL_USER_2200
480 * @tc.name   : SetuidSetUserIDSuccess_0022
481 * @tc.desc   : Setuid set user ID success.
482 * @tc.size   : MediumTest
483 * @tc.type   : Function
484 * @tc.level  : Level 1
485 */
486HWTEST_F(UserApiTest, SetuidSetUserIDSuccess_0022, Function | MediumTest | Level1)
487{
488    int ret;
489    uid_t uid = getuid();
490    EXPECT_TRUE(uid >= 0);
491
492    uid_t setUid = uid + 1;
493    ret = setuid(setUid);
494    EXPECT_EQ(ret, 0);
495
496    uid_t newUid = getuid();
497    EXPECT_EQ(newUid, setUid);
498}
499
500/*
501 * @tc.number : SUB_KERNEL_SYSCALL_USER_2300
502 * @tc.name   : SetgidSetInvalidUIDFail_0023
503 * @tc.desc   : Setgid set invalid user ID fail.
504 * @tc.size   : MediumTest
505 * @tc.type   : Function
506 * @tc.level  : Level 2
507 */
508HWTEST_F(UserApiTest, SetgidSetInvalidUIDFail_0023, Function | MediumTest | Level2)
509{
510    uid_t invalidUid = -1;
511    errno = 0;
512    int ret = setuid(invalidUid);
513    EXPECT_EQ(ret, -1);
514    EXPECT_EQ(errno, EINVAL);
515}