• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
29 using namespace testing::ext;
30 using namespace std;
31 
32 static const int MAX_GROUPS = 1024;
33 const unsigned long RETRIEVE_PERSONALITY = 0xffffffff;
34 const unsigned long DEFAULT_PERSONALITY = 0;
35 
36 class UserApiTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42 private:
43 };
SetUp()44 void UserApiTest::SetUp()
45 {
46 }
TearDown()47 void UserApiTest::TearDown()
48 {
49 }
SetUpTestCase()50 void UserApiTest::SetUpTestCase()
51 {
52 }
TearDownTestCase()53 void 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  */
65 HWTEST_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  */
89 HWTEST_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  */
106 HWTEST_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  */
123 HWTEST_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  */
139 HWTEST_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  */
154 HWTEST_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  */
171 HWTEST_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  */
192 HWTEST_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  */
220 HWTEST_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  */
242 HWTEST_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  */
261 HWTEST_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  */
276 HWTEST_F(UserApiTest, SetfsgidInvalidFsgidFailed_0012, Function | MediumTest | Level2)
277 {
278     int ret;
279     ret = setfsgid(-1);
280     EXPECT_GE(ret, -1);
281 }
282 
283 /*
284  * @tc.number : SUB_KERNEL_SYSCALL_USER_1300
285  * @tc.name   : SetfsuidSetFSUserIDSuccess_0013
286  * @tc.desc   : Setfsuid set user ID used for file system checks success.
287  * @tc.size   : MediumTest
288  * @tc.type   : Function
289  * @tc.level  : Level 1
290  */
291 HWTEST_F(UserApiTest, SetfsuidSetFSUserIDSuccess_0013, Function | MediumTest | Level1)
292 {
293     int ret;
294     uid_t effectiveUid = geteuid();
295     EXPECT_TRUE(effectiveUid >= 0);
296 
297     uid_t fsUid = effectiveUid + 1;
298     ret = setfsuid(fsUid);
299     EXPECT_GE(ret, 0);
300 }
301 
302 /*
303  * @tc.number : SUB_KERNEL_SYSCALL_USER_1400
304  * @tc.name   : SetgidSetGroupIDSuccess_0014
305  * @tc.desc   : Setgid Set group ID success.
306  * @tc.size   : MediumTest
307  * @tc.type   : Function
308  * @tc.level  : Level 1
309  */
310 HWTEST_F(UserApiTest, SetgidSetGroupIDSuccess_0014, Function | MediumTest | Level1)
311 {
312     int ret;
313     gid_t gid = getgid();
314     EXPECT_TRUE(gid >= 0);
315 
316     gid_t setGid = gid + 1;
317     ret = setgid(setGid);
318     EXPECT_EQ(ret, 0);
319 
320     gid_t newGid = getgid();
321     EXPECT_EQ(newGid, setGid);
322 }
323 
324 /*
325  * @tc.number : SUB_KERNEL_SYSCALL_USER_1500
326  * @tc.name   : SetgidSetGroupIDFail_0015
327  * @tc.desc   : Setgid Set group ID fail.
328  * @tc.size   : MediumTest
329  * @tc.type   : Function
330  * @tc.level  : Level 2
331  */
332 HWTEST_F(UserApiTest, SetgidSetGroupIDFail_0015, Function | MediumTest | Level2)
333 {
334     gid_t invalidGid = -1;
335     errno = 0;
336     int ret = setgid(invalidGid);
337     EXPECT_EQ(ret, -1);
338     EXPECT_EQ(errno, EINVAL);
339 }
340 
341 /*
342  * @tc.number : SUB_KERNEL_SYSCALL_USER_1600
343  * @tc.name   : SetgroupsSetGroupIDListSuccess_0016
344  * @tc.desc   : Setgroups set list of supplementary group IDs success.
345  * @tc.size   : MediumTest
346  * @tc.type   : Function
347  * @tc.level  : Level 1
348  */
349 HWTEST_F(UserApiTest, SetgroupsSetGroupIDListSuccess_0016, Function | MediumTest | Level1)
350 {
351     int numGroups;
352     int ret;
353     const int groupLength = 3;
354     gid_t groups[groupLength] = { 1001, 1002, 1003 };
355     gid_t groupsCheck[MAX_GROUPS];
356 
357     ret = setgroups(groupLength, groups);
358     EXPECT_EQ(ret, 0);
359     numGroups = getgroups(MAX_GROUPS, groupsCheck);
360     EXPECT_EQ(numGroups, groupLength);
361     for (int i = 0; i < numGroups; i++) {
362         EXPECT_EQ(groups[i], groupsCheck[i]);
363     }
364 }
365 
366 /*
367  * @tc.number : SUB_KERNEL_SYSCALL_USER_1700
368  * @tc.name   : SetgroupsSetGroupIDNullptrFail_0017
369  * @tc.desc   : Setgroups set list of supplementary group IDs in nullptr fail, errno EFAULT.
370  * @tc.size   : MediumTest
371  * @tc.type   : Function
372  * @tc.level  : Level 2
373  */
374 HWTEST_F(UserApiTest, SetgroupsSetGroupIDNullptrFail_0017, Function | MediumTest | Level2)
375 {
376     errno = 0;
377     int ret = setgroups(MAX_GROUPS, nullptr);
378     EXPECT_EQ(ret, -1);
379     EXPECT_EQ(errno, EFAULT);
380 
381     errno = 0;
382     ret = getgroups(MAX_GROUPS, nullptr);
383     EXPECT_EQ(ret, -1);
384     EXPECT_EQ(errno, EFAULT);
385 }
386 
387 /*
388  * @tc.number : SUB_KERNEL_SYSCALL_USER_1800
389  * @tc.name   : SetregidSetRealEffectiveGIDSuccess_0018
390  * @tc.desc   : Setregid Set real and effective group ID success.
391  * @tc.size   : MediumTest
392  * @tc.type   : Function
393  * @tc.level  : Level 1
394  */
395 HWTEST_F(UserApiTest, SetregidSetRealEffectiveGIDSuccess_0018, Function | MediumTest | Level1)
396 {
397     int ret;
398     gid_t realGid = getgid();
399     EXPECT_TRUE(realGid >= 0);
400     gid_t effectiveGid = getegid();
401     EXPECT_TRUE(effectiveGid >= 0);
402 
403     gid_t setRealGid = realGid + 1;
404     gid_t setEffectiveGid = effectiveGid + 1;
405     ret = setregid(setRealGid, setEffectiveGid);
406     EXPECT_EQ(ret, 0);
407 
408     gid_t newRealGid = getgid();
409     EXPECT_EQ(newRealGid, setRealGid);
410     gid_t newEffectiveGid = getegid();
411     EXPECT_EQ(newEffectiveGid, setEffectiveGid);
412 }
413 
414 /*
415  * @tc.number : SUB_KERNEL_SYSCALL_USER_1900
416  * @tc.name   : SetresgidSetRealEffectiveSavedGIDSuccess_0019
417  * @tc.desc   : Setresgid Set real effective and saved group ID success.
418  * @tc.size   : MediumTest
419  * @tc.type   : Function
420  * @tc.level  : Level 1
421  */
422 HWTEST_F(UserApiTest, SetresgidSetRealEffectiveSavedGIDSuccess_0019, Function | MediumTest | Level1)
423 {
424     int ret;
425     pid_t pid = getpid();
426     gid_t realGid = getgid();
427     EXPECT_TRUE(realGid >= 0);
428     gid_t effectiveGid = getegid();
429     EXPECT_TRUE(effectiveGid >= 0);
430     gid_t savedGid = getsid(pid);
431     EXPECT_TRUE(savedGid >= 0);
432 
433     gid_t setRealGid = realGid + 1;
434     gid_t setEffectiveGid = effectiveGid + 1;
435     gid_t notModifySavedGid = -1;
436     ret = setresgid(setRealGid, setEffectiveGid, notModifySavedGid);
437     EXPECT_EQ(ret, 0);
438 
439     gid_t newRealGid = getgid();
440     EXPECT_EQ(newRealGid, setRealGid);
441     gid_t newEffectiveGid = getegid();
442     EXPECT_EQ(newEffectiveGid, setEffectiveGid);
443     gid_t newSavedGid = getsid(pid);
444     EXPECT_EQ(newSavedGid, savedGid);
445 }
446 
447 /*
448  * @tc.number : SUB_KERNEL_SYSCALL_USER_2000
449  * @tc.name   : SetresuidSuccess_0020
450  * @tc.desc   : Setresuid set current uid.
451  * @tc.size   : MediumTest
452  * @tc.type   : Function
453  * @tc.level  : Level 1
454  */
455 HWTEST_F(UserApiTest, SetresuidSuccess_0020, Function | MediumTest | Level1)
456 {
457     int ret;
458     ret = setresuid(0, 0, 0);
459     EXPECT_EQ(ret, 0);
460 }
461 
462 /*
463  * @tc.number : SUB_KERNEL_SYSCALL_USER_2100
464  * @tc.name   : SetreuidSuccess_0021
465  * @tc.desc   : Setresuid Set current uid.
466  * @tc.size   : MediumTest
467  * @tc.type   : Function
468  * @tc.level  : Level 1
469  */
470 HWTEST_F(UserApiTest, SetreuidSuccess_0021, Function | MediumTest | Level1)
471 {
472     int ret;
473     ret = setreuid(0, 0);
474     EXPECT_EQ(ret, 0);
475 }
476 
477 /*
478  * @tc.number : SUB_KERNEL_SYSCALL_USER_2200
479  * @tc.name   : SetuidSetUserIDSuccess_0022
480  * @tc.desc   : Setuid set user ID success.
481  * @tc.size   : MediumTest
482  * @tc.type   : Function
483  * @tc.level  : Level 1
484  */
485 HWTEST_F(UserApiTest, SetuidSetUserIDSuccess_0022, Function | MediumTest | Level1)
486 {
487     int ret;
488     uid_t uid = getuid();
489     EXPECT_TRUE(uid >= 0);
490 
491     uid_t setUid = uid + 1;
492     ret = setuid(setUid);
493     EXPECT_EQ(ret, 0);
494 
495     uid_t newUid = getuid();
496     EXPECT_EQ(newUid, setUid);
497 }
498 
499 /*
500  * @tc.number : SUB_KERNEL_SYSCALL_USER_2300
501  * @tc.name   : SetgidSetInvalidUIDFail_0023
502  * @tc.desc   : Setgid set invalid user ID fail.
503  * @tc.size   : MediumTest
504  * @tc.type   : Function
505  * @tc.level  : Level 2
506  */
507 HWTEST_F(UserApiTest, SetgidSetInvalidUIDFail_0023, Function | MediumTest | Level2)
508 {
509     uid_t invalidUid = -1;
510     errno = 0;
511     int ret = setuid(invalidUid);
512     EXPECT_EQ(ret, -1);
513     EXPECT_EQ(errno, EINVAL);
514 }