1f6603c60Sopenharmony_ci/*
2f6603c60Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License.
5f6603c60Sopenharmony_ci * You may obtain a copy of the License at
6f6603c60Sopenharmony_ci *
7f6603c60Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f6603c60Sopenharmony_ci *
9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and
13f6603c60Sopenharmony_ci * limitations under the License.
14f6603c60Sopenharmony_ci */
15f6603c60Sopenharmony_ci
16f6603c60Sopenharmony_ci#include "UidGidTest.h"
17f6603c60Sopenharmony_ci#include <unistd.h>
18f6603c60Sopenharmony_ci#include <sys/types.h>
19f6603c60Sopenharmony_ci#include "log.h"
20f6603c60Sopenharmony_ci#include "utils.h"
21f6603c60Sopenharmony_ci
22f6603c60Sopenharmony_ciusing namespace testing::ext;
23f6603c60Sopenharmony_ci
24f6603c60Sopenharmony_ci/**
25f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetUID_0100
26f6603c60Sopenharmony_ci * @tc.name   setuid and getuid basic test
27f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
28f6603c60Sopenharmony_ci */
29f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetGetuid, Function | MediumTest | Level1)
30f6603c60Sopenharmony_ci{
31f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
32f6603c60Sopenharmony_ci    uid_t uid = getuid();
33f6603c60Sopenharmony_ci    ASSERT_EQ(uid, SHELL_UID);
34f6603c60Sopenharmony_ci
35f6603c60Sopenharmony_ci    uid_t newUid = GetRandom(100);
36f6603c60Sopenharmony_ci    LOG("new uid = %d", newUid);
37f6603c60Sopenharmony_ci    int rt = setuid(newUid);
38f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
39f6603c60Sopenharmony_ci    uid = getuid();
40f6603c60Sopenharmony_ci    ASSERT_EQ(uid, newUid);
41f6603c60Sopenharmony_ci
42f6603c60Sopenharmony_ci    pid_t pid = fork();
43f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
44f6603c60Sopenharmony_ci    if (pid > 0) { // parent
45f6603c60Sopenharmony_ci        Msleep(20);
46f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
47f6603c60Sopenharmony_ci    } else { // child
48f6603c60Sopenharmony_ci        uid = getuid();
49f6603c60Sopenharmony_ci        if (uid != newUid) {
50f6603c60Sopenharmony_ci            LOG("child uid check fail, child uid=%d", uid);
51f6603c60Sopenharmony_ci            exit(1);
52f6603c60Sopenharmony_ci        }
53f6603c60Sopenharmony_ci        LOG("child uid check OK");
54f6603c60Sopenharmony_ci        exit(0);
55f6603c60Sopenharmony_ci    }
56f6603c60Sopenharmony_ci    rt = setuid(0);
57f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
58f6603c60Sopenharmony_ci    uid = getuid();
59f6603c60Sopenharmony_ci    ASSERT_EQ(uid, 0);
60f6603c60Sopenharmony_ci}
61f6603c60Sopenharmony_ci/**
62f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetUID_0200
63f6603c60Sopenharmony_ci * @tc.name   test setuid fail if input uid is negative
64f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
65f6603c60Sopenharmony_ci */
66f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetuidFail, Function | MediumTest | Level3)
67f6603c60Sopenharmony_ci{
68f6603c60Sopenharmony_ci    uid_t uid = getuid();
69f6603c60Sopenharmony_ci    ASSERT_EQ(uid, SHELL_UID);
70f6603c60Sopenharmony_ci
71f6603c60Sopenharmony_ci    int newUid = -GetRandID();
72f6603c60Sopenharmony_ci    LOG("new uid = %d", newUid);
73f6603c60Sopenharmony_ci    int rt = setuid(newUid);
74f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
75f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
76f6603c60Sopenharmony_ci
77f6603c60Sopenharmony_ci    uid = getuid();
78f6603c60Sopenharmony_ci    ASSERT_EQ(uid, SHELL_UID);
79f6603c60Sopenharmony_ci}
80f6603c60Sopenharmony_ci
81f6603c60Sopenharmony_ci/**
82f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetGID_0100
83f6603c60Sopenharmony_ci * @tc.name   setgid and getgid basic test
84f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
85f6603c60Sopenharmony_ci */
86f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetGetgid, Function | MediumTest | Level1)
87f6603c60Sopenharmony_ci{
88f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
89f6603c60Sopenharmony_ci    gid_t gid = getgid();
90f6603c60Sopenharmony_ci    ASSERT_EQ(gid, SHELL_GID);
91f6603c60Sopenharmony_ci
92f6603c60Sopenharmony_ci    uid_t newgid = GetRandom(100);
93f6603c60Sopenharmony_ci    LOG("new gid = %d", newgid);
94f6603c60Sopenharmony_ci    int rt = setgid(newgid);
95f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
96f6603c60Sopenharmony_ci    gid = getgid();
97f6603c60Sopenharmony_ci    ASSERT_EQ(gid, newgid);
98f6603c60Sopenharmony_ci
99f6603c60Sopenharmony_ci    pid_t pid = fork();
100f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
101f6603c60Sopenharmony_ci    if (pid > 0) { // parent
102f6603c60Sopenharmony_ci        Msleep(20);
103f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
104f6603c60Sopenharmony_ci    } else { // child
105f6603c60Sopenharmony_ci        gid = getgid();
106f6603c60Sopenharmony_ci        if (gid != newgid) {
107f6603c60Sopenharmony_ci            LOG("child gid check fail, child gid=%d", gid);
108f6603c60Sopenharmony_ci            exit(1);
109f6603c60Sopenharmony_ci        }
110f6603c60Sopenharmony_ci        LOG("child guid check OK");
111f6603c60Sopenharmony_ci        exit(0);
112f6603c60Sopenharmony_ci    }
113f6603c60Sopenharmony_ci    rt = setgid(0);
114f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
115f6603c60Sopenharmony_ci    gid = getgid();
116f6603c60Sopenharmony_ci    ASSERT_EQ(gid, 0);
117f6603c60Sopenharmony_ci}
118f6603c60Sopenharmony_ci/**
119f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetGID_0200
120f6603c60Sopenharmony_ci * @tc.name   test setgid fail if input gid is negative
121f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
122f6603c60Sopenharmony_ci */
123f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetgidFail, Function | MediumTest | Level3)
124f6603c60Sopenharmony_ci{
125f6603c60Sopenharmony_ci    gid_t gid = getgid();
126f6603c60Sopenharmony_ci    ASSERT_EQ(gid, SHELL_GID);
127f6603c60Sopenharmony_ci
128f6603c60Sopenharmony_ci    int newgid = -GetRandID();
129f6603c60Sopenharmony_ci    LOG("new gid = %d", newgid);
130f6603c60Sopenharmony_ci    int rt = setgid(newgid);
131f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
132f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
133f6603c60Sopenharmony_ci
134f6603c60Sopenharmony_ci    gid = getgid();
135f6603c60Sopenharmony_ci    ASSERT_EQ(gid, SHELL_GID);
136f6603c60Sopenharmony_ci}
137f6603c60Sopenharmony_ci
138f6603c60Sopenharmony_ci
139f6603c60Sopenharmony_ci/**
140f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetEUID_0100
141f6603c60Sopenharmony_ci * @tc.name   seteuid and geteuid basic test
142f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
143f6603c60Sopenharmony_ci */
144f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetGeteuid, Function | MediumTest | Level1)
145f6603c60Sopenharmony_ci{
146f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
147f6603c60Sopenharmony_ci
148f6603c60Sopenharmony_ci    uid_t newEuid = GetRandID();
149f6603c60Sopenharmony_ci    LOG("new euid = %d", newEuid);
150f6603c60Sopenharmony_ci    int rt = seteuid(newEuid);
151f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
152f6603c60Sopenharmony_ci    uid_t euid = geteuid();
153f6603c60Sopenharmony_ci    ASSERT_EQ(euid, newEuid);
154f6603c60Sopenharmony_ci    AssertAllUid(newEuid);
155f6603c60Sopenharmony_ci
156f6603c60Sopenharmony_ci    pid_t pid = fork();
157f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
158f6603c60Sopenharmony_ci    if (pid > 0) { // parent
159f6603c60Sopenharmony_ci        Msleep(20);
160f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
161f6603c60Sopenharmony_ci    } else { // child
162f6603c60Sopenharmony_ci        uid_t uid, euid, suid;
163f6603c60Sopenharmony_ci        getresuid(&uid, &euid, &suid);
164f6603c60Sopenharmony_ci        if (uid != newEuid || euid != newEuid || suid != newEuid) {
165f6603c60Sopenharmony_ci            LOG("child resuid check fail, actual uid=%d,euid=%d,suid=%d,", uid, euid, suid);
166f6603c60Sopenharmony_ci            exit(1);
167f6603c60Sopenharmony_ci        }
168f6603c60Sopenharmony_ci        LOG("child uid check OK");
169f6603c60Sopenharmony_ci        exit(0);
170f6603c60Sopenharmony_ci    }
171f6603c60Sopenharmony_ci    rt = seteuid(SHELL_UID);
172f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
173f6603c60Sopenharmony_ci    euid = geteuid();
174f6603c60Sopenharmony_ci    ASSERT_EQ(euid, SHELL_UID);
175f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
176f6603c60Sopenharmony_ci}
177f6603c60Sopenharmony_ci/**
178f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetEUID_0200
179f6603c60Sopenharmony_ci * @tc.name   test seteuid fail if input uid is negative
180f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
181f6603c60Sopenharmony_ci */
182f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSeteuidFail, Function | MediumTest | Level3)
183f6603c60Sopenharmony_ci{
184f6603c60Sopenharmony_ci    uid_t uid = getuid();
185f6603c60Sopenharmony_ci    ASSERT_EQ(uid, SHELL_UID);
186f6603c60Sopenharmony_ci
187f6603c60Sopenharmony_ci    int newEuid = -GetRandID();
188f6603c60Sopenharmony_ci    LOG("new euid = %d", newEuid);
189f6603c60Sopenharmony_ci    int rt = seteuid(newEuid);
190f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
191f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
192f6603c60Sopenharmony_ci
193f6603c60Sopenharmony_ci    uid = getuid();
194f6603c60Sopenharmony_ci    ASSERT_EQ(uid, SHELL_UID);
195f6603c60Sopenharmony_ci    uid_t euid = geteuid();
196f6603c60Sopenharmony_ci    ASSERT_EQ(euid, SHELL_UID);
197f6603c60Sopenharmony_ci}
198f6603c60Sopenharmony_ci
199f6603c60Sopenharmony_ci/**
200f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetEGID_0100
201f6603c60Sopenharmony_ci * @tc.name   setegid and getegid basic test
202f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
203f6603c60Sopenharmony_ci */
204f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetGetegid, Function | MediumTest | Level1)
205f6603c60Sopenharmony_ci{
206f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
207f6603c60Sopenharmony_ci
208f6603c60Sopenharmony_ci    uid_t newEgid = GetRandID();
209f6603c60Sopenharmony_ci    LOG("new egid = %d", newEgid);
210f6603c60Sopenharmony_ci    int rt = setegid(newEgid);
211f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
212f6603c60Sopenharmony_ci    gid_t egid = getegid();
213f6603c60Sopenharmony_ci    ASSERT_EQ(egid, newEgid);
214f6603c60Sopenharmony_ci    AssertAllGid(newEgid);
215f6603c60Sopenharmony_ci
216f6603c60Sopenharmony_ci    pid_t pid = fork();
217f6603c60Sopenharmony_ci    ASSERT_TRUE(pid >= 0) << "======== Fork Error! =========";
218f6603c60Sopenharmony_ci    if (pid > 0) { // parent
219f6603c60Sopenharmony_ci        Msleep(20);
220f6603c60Sopenharmony_ci        WaitProcExitedOK(pid);
221f6603c60Sopenharmony_ci    } else { // child
222f6603c60Sopenharmony_ci        gid_t gid, egid, sgid;
223f6603c60Sopenharmony_ci        getresgid(&gid, &egid, &sgid);
224f6603c60Sopenharmony_ci        if (gid != newEgid || egid != newEgid || sgid != newEgid) {
225f6603c60Sopenharmony_ci            LOG("child resgid check fail, actual gid=%d,egid=%d,sgid=%d,", gid, egid, sgid);
226f6603c60Sopenharmony_ci            exit(1);
227f6603c60Sopenharmony_ci        }
228f6603c60Sopenharmony_ci        LOG("child gid check OK");
229f6603c60Sopenharmony_ci        exit(0);
230f6603c60Sopenharmony_ci    }
231f6603c60Sopenharmony_ci    rt = setegid(SHELL_GID);
232f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
233f6603c60Sopenharmony_ci    egid = getegid();
234f6603c60Sopenharmony_ci    ASSERT_EQ(egid, SHELL_GID);
235f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
236f6603c60Sopenharmony_ci}
237f6603c60Sopenharmony_ci/**
238f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetEGID_0200
239f6603c60Sopenharmony_ci * @tc.name   test setegid fail if input gid is negative
240f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
241f6603c60Sopenharmony_ci */
242f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetegidFail, Function | MediumTest | Level3)
243f6603c60Sopenharmony_ci{
244f6603c60Sopenharmony_ci    gid_t gid = getgid();
245f6603c60Sopenharmony_ci    ASSERT_EQ(gid, SHELL_GID);
246f6603c60Sopenharmony_ci
247f6603c60Sopenharmony_ci    int newEgid = -GetRandID();
248f6603c60Sopenharmony_ci    LOG("new egid = %d", newEgid);
249f6603c60Sopenharmony_ci    int rt = setegid(newEgid);
250f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
251f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
252f6603c60Sopenharmony_ci
253f6603c60Sopenharmony_ci    gid = getgid();
254f6603c60Sopenharmony_ci    ASSERT_EQ(gid, SHELL_GID);
255f6603c60Sopenharmony_ci    gid_t egid = getegid();
256f6603c60Sopenharmony_ci    ASSERT_EQ(egid, SHELL_GID);
257f6603c60Sopenharmony_ci}
258f6603c60Sopenharmony_ci
259f6603c60Sopenharmony_ci
260f6603c60Sopenharmony_ci/**
261f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetREUID_0100
262f6603c60Sopenharmony_ci * @tc.name   setreuid basic test
263f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
264f6603c60Sopenharmony_ci */
265f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetreuid, Function | MediumTest | Level1)
266f6603c60Sopenharmony_ci{
267f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
268f6603c60Sopenharmony_ci
269f6603c60Sopenharmony_ci    int newUid = GetRandID();
270f6603c60Sopenharmony_ci    LOG("new uid1 = %d", newUid);
271f6603c60Sopenharmony_ci    int rt = setreuid(newUid, newUid);
272f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
273f6603c60Sopenharmony_ci    AssertAllUid(newUid);
274f6603c60Sopenharmony_ci
275f6603c60Sopenharmony_ci    newUid = GetRandID();
276f6603c60Sopenharmony_ci    LOG("new uid2 = %d", newUid);
277f6603c60Sopenharmony_ci    rt = setreuid(newUid, -1);
278f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
279f6603c60Sopenharmony_ci    AssertAllUid(newUid);
280f6603c60Sopenharmony_ci
281f6603c60Sopenharmony_ci    newUid = GetRandID();
282f6603c60Sopenharmony_ci    LOG("new uid3 = %d", newUid);
283f6603c60Sopenharmony_ci    rt = setreuid(-1, newUid);
284f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
285f6603c60Sopenharmony_ci    AssertAllUid(newUid);
286f6603c60Sopenharmony_ci
287f6603c60Sopenharmony_ci    LOG("use all -1");
288f6603c60Sopenharmony_ci    rt = setreuid(-1, -1);
289f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
290f6603c60Sopenharmony_ci    AssertAllUid(newUid);
291f6603c60Sopenharmony_ci
292f6603c60Sopenharmony_ci    LOG("restore to 0");
293f6603c60Sopenharmony_ci    rt = setreuid(SHELL_UID, SHELL_UID);
294f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
295f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
296f6603c60Sopenharmony_ci}
297f6603c60Sopenharmony_ci/**
298f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetREUID_0200
299f6603c60Sopenharmony_ci * @tc.name   test setreuid fail if input uid is negative, or ruid and euid is not equal
300f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
301f6603c60Sopenharmony_ci */
302f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetreuidFail, Function | MediumTest | Level2)
303f6603c60Sopenharmony_ci{
304f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
305f6603c60Sopenharmony_ci
306f6603c60Sopenharmony_ci    int newUid = GetRandID();
307f6603c60Sopenharmony_ci    LOG("new uid = %d", -newUid);
308f6603c60Sopenharmony_ci    int rt = setreuid(-newUid, -newUid);
309f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
310f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
311f6603c60Sopenharmony_ci
312f6603c60Sopenharmony_ci    rt = setreuid(-newUid, newUid);
313f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
314f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
315f6603c60Sopenharmony_ci
316f6603c60Sopenharmony_ci    rt = setreuid(-newUid, -1);
317f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
318f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
319f6603c60Sopenharmony_ci
320f6603c60Sopenharmony_ci    int newUid2 = GetRandID();
321f6603c60Sopenharmony_ci    LOG("new uid2 = %d", -newUid2);
322f6603c60Sopenharmony_ci    rt = setreuid(newUid, -newUid2);
323f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
324f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
325f6603c60Sopenharmony_ci    rt = setreuid(newUid2, -newUid);
326f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
327f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
328f6603c60Sopenharmony_ci
329f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
330f6603c60Sopenharmony_ci
331f6603c60Sopenharmony_ci    LOG("check uids of setreuid should equal");
332f6603c60Sopenharmony_ci    rt = setreuid(newUid, newUid2);
333f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
334f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
335f6603c60Sopenharmony_ci
336f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
337f6603c60Sopenharmony_ci}
338f6603c60Sopenharmony_ci
339f6603c60Sopenharmony_ci/**
340f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetREGID_0100
341f6603c60Sopenharmony_ci * @tc.name   setregid basic test
342f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
343f6603c60Sopenharmony_ci */
344f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetregid, Function | MediumTest | Level1)
345f6603c60Sopenharmony_ci{
346f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
347f6603c60Sopenharmony_ci
348f6603c60Sopenharmony_ci    int newGid = GetRandID();
349f6603c60Sopenharmony_ci    LOG("new gid1 = %d", newGid);
350f6603c60Sopenharmony_ci    int rt = setregid(newGid, newGid);
351f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
352f6603c60Sopenharmony_ci    AssertAllGid(newGid);
353f6603c60Sopenharmony_ci
354f6603c60Sopenharmony_ci    newGid = GetRandID();
355f6603c60Sopenharmony_ci    LOG("new gid2 = %d", newGid);
356f6603c60Sopenharmony_ci    rt = setregid(newGid, -1);
357f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
358f6603c60Sopenharmony_ci    AssertAllGid(newGid);
359f6603c60Sopenharmony_ci
360f6603c60Sopenharmony_ci    newGid = GetRandID();
361f6603c60Sopenharmony_ci    LOG("new gid3 = %d", newGid);
362f6603c60Sopenharmony_ci    rt = setregid(-1, newGid);
363f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
364f6603c60Sopenharmony_ci    AssertAllGid(newGid);
365f6603c60Sopenharmony_ci
366f6603c60Sopenharmony_ci    LOG("use all -1");
367f6603c60Sopenharmony_ci    rt = setregid(-1, -1);
368f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
369f6603c60Sopenharmony_ci    AssertAllGid(newGid);
370f6603c60Sopenharmony_ci
371f6603c60Sopenharmony_ci    LOG("restore to 0");
372f6603c60Sopenharmony_ci    rt = setregid(SHELL_GID, SHELL_GID);
373f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
374f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
375f6603c60Sopenharmony_ci}
376f6603c60Sopenharmony_ci/**
377f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetREGID_0200
378f6603c60Sopenharmony_ci * @tc.name   test setregid fail if input gid is negative, or rgid and egid is not equal
379f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
380f6603c60Sopenharmony_ci */
381f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetregidFail, Function | MediumTest | Level2)
382f6603c60Sopenharmony_ci{
383f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
384f6603c60Sopenharmony_ci
385f6603c60Sopenharmony_ci    int newGid = GetRandID();
386f6603c60Sopenharmony_ci    LOG("new gid = %d", -newGid);
387f6603c60Sopenharmony_ci    int rt = setregid(-newGid, -newGid);
388f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
389f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
390f6603c60Sopenharmony_ci
391f6603c60Sopenharmony_ci    rt = setregid(-newGid, newGid);
392f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
393f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
394f6603c60Sopenharmony_ci
395f6603c60Sopenharmony_ci    rt = setregid(-newGid, -1);
396f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
397f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
398f6603c60Sopenharmony_ci
399f6603c60Sopenharmony_ci    int newGid2 = GetRandID();
400f6603c60Sopenharmony_ci    LOG("new gid2 = %d", -newGid2);
401f6603c60Sopenharmony_ci    rt = setregid(newGid, -newGid2);
402f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
403f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
404f6603c60Sopenharmony_ci    rt = setregid(newGid2, -newGid);
405f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
406f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
407f6603c60Sopenharmony_ci
408f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
409f6603c60Sopenharmony_ci
410f6603c60Sopenharmony_ci    LOG("check gids of setregid should equal");
411f6603c60Sopenharmony_ci    rt = setregid(newGid, newGid2);
412f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
413f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
414f6603c60Sopenharmony_ci
415f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
416f6603c60Sopenharmony_ci}
417f6603c60Sopenharmony_ci
418f6603c60Sopenharmony_ci// ================================== res ===========================================
419f6603c60Sopenharmony_ci/**
420f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetRESUID_0100
421f6603c60Sopenharmony_ci * @tc.name   setresuid and getresuid basic test
422f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
423f6603c60Sopenharmony_ci */
424f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetGetresuid, Function | MediumTest | Level1)
425f6603c60Sopenharmony_ci{
426f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
427f6603c60Sopenharmony_ci
428f6603c60Sopenharmony_ci    int newUid = GetRandID();
429f6603c60Sopenharmony_ci    LOG("new uid1 = %d", newUid);
430f6603c60Sopenharmony_ci    int rt = setresuid(newUid, newUid, newUid);
431f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
432f6603c60Sopenharmony_ci    uid_t uid = getuid();
433f6603c60Sopenharmony_ci    ASSERT_EQ(uid, newUid);
434f6603c60Sopenharmony_ci    AssertAllUid(newUid);
435f6603c60Sopenharmony_ci
436f6603c60Sopenharmony_ci    newUid = GetRandID();
437f6603c60Sopenharmony_ci    LOG("new uid2 = %d", newUid);
438f6603c60Sopenharmony_ci    rt = setresuid(newUid, -1, -1);
439f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
440f6603c60Sopenharmony_ci    AssertAllUid(newUid);
441f6603c60Sopenharmony_ci
442f6603c60Sopenharmony_ci    newUid = GetRandID();
443f6603c60Sopenharmony_ci    LOG("new uid3 = %d", newUid);
444f6603c60Sopenharmony_ci    rt = setresuid(-1, newUid, -1);
445f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
446f6603c60Sopenharmony_ci    AssertAllUid(newUid);
447f6603c60Sopenharmony_ci
448f6603c60Sopenharmony_ci    newUid = GetRandID();
449f6603c60Sopenharmony_ci    LOG("new uid4 = %d", newUid);
450f6603c60Sopenharmony_ci    rt = setresuid(-1, -1, newUid);
451f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
452f6603c60Sopenharmony_ci    AssertAllUid(newUid);
453f6603c60Sopenharmony_ci
454f6603c60Sopenharmony_ci    newUid = GetRandID();
455f6603c60Sopenharmony_ci    LOG("new uid5 = %d", newUid);
456f6603c60Sopenharmony_ci    rt = setresuid(-1, newUid, newUid);
457f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
458f6603c60Sopenharmony_ci    AssertAllUid(newUid);
459f6603c60Sopenharmony_ci
460f6603c60Sopenharmony_ci    newUid = GetRandID();
461f6603c60Sopenharmony_ci    LOG("new uid6 = %d", newUid);
462f6603c60Sopenharmony_ci    rt = setresuid(newUid, -1, newUid);
463f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
464f6603c60Sopenharmony_ci    AssertAllUid(newUid);
465f6603c60Sopenharmony_ci
466f6603c60Sopenharmony_ci    newUid = GetRandID();
467f6603c60Sopenharmony_ci    LOG("new uid7 = %d", newUid);
468f6603c60Sopenharmony_ci    rt = setresuid(newUid, newUid, -1);
469f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
470f6603c60Sopenharmony_ci    AssertAllUid(newUid);
471f6603c60Sopenharmony_ci
472f6603c60Sopenharmony_ci    LOG("use all -1");
473f6603c60Sopenharmony_ci    rt = setresuid(-1, -1, -1);
474f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
475f6603c60Sopenharmony_ci    AssertAllUid(newUid);
476f6603c60Sopenharmony_ci
477f6603c60Sopenharmony_ci    LOG("restore to %d", SHELL_UID);
478f6603c60Sopenharmony_ci    rt = setresuid(SHELL_UID, SHELL_UID, SHELL_UID);
479f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
480f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
481f6603c60Sopenharmony_ci}
482f6603c60Sopenharmony_ci/**
483f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetRESUID_0200
484f6603c60Sopenharmony_ci * @tc.name   test setresuid fail if input uid is negative, or ruid,euid,suid is not equal to each other.
485f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
486f6603c60Sopenharmony_ci */
487f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetresuidFail, Function | MediumTest | Level2)
488f6603c60Sopenharmony_ci{
489f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
490f6603c60Sopenharmony_ci
491f6603c60Sopenharmony_ci    int newUid = GetRandID();
492f6603c60Sopenharmony_ci    int newUid2 = GetRandID();
493f6603c60Sopenharmony_ci    LOG("new uid = %d, new uid2 = %d", newUid, newUid2);
494f6603c60Sopenharmony_ci    int rt = setresuid(-newUid, newUid2, newUid2);
495f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
496f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
497f6603c60Sopenharmony_ci    rt = setresuid(newUid2, -newUid, newUid2);
498f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
499f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
500f6603c60Sopenharmony_ci    rt = setresuid(newUid2, -1, -newUid);
501f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
502f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
503f6603c60Sopenharmony_ci    rt = setresuid(-1, -1, -newUid);
504f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
505f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
506f6603c60Sopenharmony_ci
507f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
508f6603c60Sopenharmony_ci
509f6603c60Sopenharmony_ci    LOG("check uids of setresuid should all equal to each other");
510f6603c60Sopenharmony_ci    rt = setresuid(newUid, newUid2, newUid2);
511f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
512f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
513f6603c60Sopenharmony_ci    rt = setresuid(newUid2, newUid, newUid2);
514f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
515f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
516f6603c60Sopenharmony_ci    rt = setresuid(newUid2, -1, newUid);
517f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
518f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
519f6603c60Sopenharmony_ci    rt = setresuid(-1, newUid, newUid2);
520f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
521f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
522f6603c60Sopenharmony_ci
523f6603c60Sopenharmony_ci    AssertAllUid(SHELL_UID);
524f6603c60Sopenharmony_ci}
525f6603c60Sopenharmony_ci
526f6603c60Sopenharmony_ci/**
527f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetRESGID_0100
528f6603c60Sopenharmony_ci * @tc.name   setresgid and getresgid basic test
529f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
530f6603c60Sopenharmony_ci */
531f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetGetresgid, Function | MediumTest | Level1)
532f6603c60Sopenharmony_ci{
533f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
534f6603c60Sopenharmony_ci
535f6603c60Sopenharmony_ci    int newGid = GetRandID();
536f6603c60Sopenharmony_ci    LOG("new gid1 = %d", newGid);
537f6603c60Sopenharmony_ci    int rt = setresgid(newGid, newGid, newGid);
538f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
539f6603c60Sopenharmony_ci    gid_t gid = getgid();
540f6603c60Sopenharmony_ci    ASSERT_EQ(gid, newGid);
541f6603c60Sopenharmony_ci    AssertAllGid(newGid);
542f6603c60Sopenharmony_ci
543f6603c60Sopenharmony_ci    newGid = GetRandID();
544f6603c60Sopenharmony_ci    LOG("new gid2 = %d", newGid);
545f6603c60Sopenharmony_ci    rt = setresgid(newGid, -1, -1);
546f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
547f6603c60Sopenharmony_ci    AssertAllGid(newGid);
548f6603c60Sopenharmony_ci
549f6603c60Sopenharmony_ci    newGid = GetRandID();
550f6603c60Sopenharmony_ci    LOG("new gid3 = %d", newGid);
551f6603c60Sopenharmony_ci    rt = setresgid(-1, newGid, -1);
552f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
553f6603c60Sopenharmony_ci    AssertAllGid(newGid);
554f6603c60Sopenharmony_ci
555f6603c60Sopenharmony_ci    newGid = GetRandID();
556f6603c60Sopenharmony_ci    LOG("new gid4 = %d", newGid);
557f6603c60Sopenharmony_ci    rt = setresgid(-1, -1, newGid);
558f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
559f6603c60Sopenharmony_ci    AssertAllGid(newGid);
560f6603c60Sopenharmony_ci
561f6603c60Sopenharmony_ci    newGid = GetRandID();
562f6603c60Sopenharmony_ci    LOG("new gid5 = %d", newGid);
563f6603c60Sopenharmony_ci    rt = setresgid(-1, newGid, newGid);
564f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
565f6603c60Sopenharmony_ci    AssertAllGid(newGid);
566f6603c60Sopenharmony_ci
567f6603c60Sopenharmony_ci    newGid = GetRandID();
568f6603c60Sopenharmony_ci    LOG("new gid6 = %d", newGid);
569f6603c60Sopenharmony_ci    rt = setresgid(newGid, -1, newGid);
570f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
571f6603c60Sopenharmony_ci    AssertAllGid(newGid);
572f6603c60Sopenharmony_ci
573f6603c60Sopenharmony_ci    newGid = GetRandID();
574f6603c60Sopenharmony_ci    LOG("new gid7 = %d", newGid);
575f6603c60Sopenharmony_ci    rt = setresgid(newGid, newGid, -1);
576f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
577f6603c60Sopenharmony_ci    AssertAllGid(newGid);
578f6603c60Sopenharmony_ci
579f6603c60Sopenharmony_ci    LOG("use all -1");
580f6603c60Sopenharmony_ci    rt = setresgid(-1, -1, -1);
581f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
582f6603c60Sopenharmony_ci    AssertAllGid(newGid);
583f6603c60Sopenharmony_ci
584f6603c60Sopenharmony_ci    LOG("restore to %d", SHELL_GID);
585f6603c60Sopenharmony_ci    rt = setresgid(SHELL_GID, SHELL_GID, SHELL_GID);
586f6603c60Sopenharmony_ci    ASSERT_EQ(rt, 0);
587f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
588f6603c60Sopenharmony_ci}
589f6603c60Sopenharmony_ci/**
590f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGetRESGID_0200
591f6603c60Sopenharmony_ci * @tc.name   test setresgid fail if input ids is negative, or rgid,egid,sgid is not equal to each other.
592f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
593f6603c60Sopenharmony_ci */
594f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetresgidFail, Function | MediumTest | Level2)
595f6603c60Sopenharmony_ci{
596f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
597f6603c60Sopenharmony_ci
598f6603c60Sopenharmony_ci    int newGid = GetRandID();
599f6603c60Sopenharmony_ci    int newGid2 = GetRandID();
600f6603c60Sopenharmony_ci    LOG("new gid = %d, new gid2 = %d", newGid, newGid2);
601f6603c60Sopenharmony_ci    int rt = setresgid(-newGid, newGid2, newGid2);
602f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
603f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
604f6603c60Sopenharmony_ci    rt = setresgid(newGid2, -newGid, newGid2);
605f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
606f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
607f6603c60Sopenharmony_ci    rt = setresgid(newGid2, -1, -newGid);
608f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
609f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
610f6603c60Sopenharmony_ci    rt = setresgid(-1, -1, -newGid);
611f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
612f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EINVAL);
613f6603c60Sopenharmony_ci
614f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
615f6603c60Sopenharmony_ci
616f6603c60Sopenharmony_ci    LOG("check gids of setresgid should all equal to each other");
617f6603c60Sopenharmony_ci    rt = setresgid(newGid, newGid2, newGid2);
618f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
619f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
620f6603c60Sopenharmony_ci    rt = setresgid(newGid2, newGid, newGid2);
621f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
622f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
623f6603c60Sopenharmony_ci    rt = setresgid(newGid2, -1, newGid);
624f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
625f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
626f6603c60Sopenharmony_ci    rt = setresgid(-1, newGid, newGid2);
627f6603c60Sopenharmony_ci    ASSERT_EQ(rt, -1);
628f6603c60Sopenharmony_ci    ASSERT_EQ(errno, EPERM);
629f6603c60Sopenharmony_ci
630f6603c60Sopenharmony_ci    AssertAllGid(SHELL_GID);
631f6603c60Sopenharmony_ci}
632f6603c60Sopenharmony_ci
633f6603c60Sopenharmony_ci/**
634f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_GetGroups_0100
635f6603c60Sopenharmony_ci * @tc.name   getgroups basic test
636f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
637f6603c60Sopenharmony_ci */
638f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testGetgroups, Function | MediumTest | Level1)
639f6603c60Sopenharmony_ci{
640f6603c60Sopenharmony_ci    const int testSize = 10;
641f6603c60Sopenharmony_ci    gid_t gidList[testSize] = {0};
642f6603c60Sopenharmony_ci    int n = getgroups(0, gidList);
643f6603c60Sopenharmony_ci    ASSERT_EQ(n, 1);
644f6603c60Sopenharmony_ci    int rt = getgroups(n, gidList);
645f6603c60Sopenharmony_ci    ASSERT_EQ(gidList[0], SHELL_GID);
646f6603c60Sopenharmony_ci    ASSERT_EQ(rt, n);
647f6603c60Sopenharmony_ci    n = getgroups(testSize, gidList);
648f6603c60Sopenharmony_ci    ASSERT_EQ(n, 1);
649f6603c60Sopenharmony_ci    ASSERT_EQ(gidList[0], SHELL_GID);
650f6603c60Sopenharmony_ci
651f6603c60Sopenharmony_ci}
652f6603c60Sopenharmony_ci
653f6603c60Sopenharmony_ci/**
654f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGroups_0100
655f6603c60Sopenharmony_ci * @tc.name   setgroups function test. also test invalid size of getroups, and clear groups function.
656f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
657f6603c60Sopenharmony_ci */
658f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetgroups1, Function | MediumTest | Level1)
659f6603c60Sopenharmony_ci{
660f6603c60Sopenharmony_ci    int groupSize = GetRandom(MAX_PROCESS_GROUPS - 3) + 3;
661f6603c60Sopenharmony_ci    gid_t *gidListIn  = (gid_t*)malloc(groupSize * sizeof(gid_t));
662f6603c60Sopenharmony_ci    if (!gidListIn) {
663f6603c60Sopenharmony_ci        LOG("gidListIn malloc fail!");
664f6603c60Sopenharmony_ci        FAIL();
665f6603c60Sopenharmony_ci    }
666f6603c60Sopenharmony_ci    gid_t *gidListOut = (gid_t*)malloc(groupSize * sizeof(gid_t));
667f6603c60Sopenharmony_ci    if (!gidListOut) {
668f6603c60Sopenharmony_ci        LOG("gidListOut malloc fail!");
669f6603c60Sopenharmony_ci        free(gidListIn);
670f6603c60Sopenharmony_ci        FAIL();
671f6603c60Sopenharmony_ci    }
672f6603c60Sopenharmony_ci    gidListIn[0]  = SHELL_GID;
673f6603c60Sopenharmony_ci    for (int i=1; i<groupSize; i++) {
674f6603c60Sopenharmony_ci        gidListIn[i]  = GetRandID();
675f6603c60Sopenharmony_ci        gidListOut[i] = 0;
676f6603c60Sopenharmony_ci    }
677f6603c60Sopenharmony_ci
678f6603c60Sopenharmony_ci    int rt = setgroups(groupSize, gidListIn);
679f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
680f6603c60Sopenharmony_ci
681f6603c60Sopenharmony_ci    int n = getgroups(1, gidListOut);
682f6603c60Sopenharmony_ci    EXPECT_EQ(n, -1) << "input size less than the number of group IDs should fail";
683f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
684f6603c60Sopenharmony_ci
685f6603c60Sopenharmony_ci    n = getgroups(groupSize, gidListOut);
686f6603c60Sopenharmony_ci    EXPECT_EQ(n, groupSize);
687f6603c60Sopenharmony_ci    for (int i=0; i<groupSize; i++) {
688f6603c60Sopenharmony_ci        EXPECT_EQ(gidListIn[i], gidListOut[i]) << " groups not equal, i=" << i << std::endl;
689f6603c60Sopenharmony_ci    }
690f6603c60Sopenharmony_ci
691f6603c60Sopenharmony_ci    LOG("clear groups");
692f6603c60Sopenharmony_ci    rt = setgroups(0, NULL);
693f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
694f6603c60Sopenharmony_ci    n = getgroups(2, gidListOut);
695f6603c60Sopenharmony_ci    EXPECT_EQ(n, 1);
696f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[0], SHELL_GID);
697f6603c60Sopenharmony_ci    free(gidListIn);
698f6603c60Sopenharmony_ci    free(gidListOut);
699f6603c60Sopenharmony_ci}
700f6603c60Sopenharmony_ci
701f6603c60Sopenharmony_ci/**
702f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGroups_0200
703f6603c60Sopenharmony_ci * @tc.name   setgroups function test: gid not in groups will automaticlly add by kernel
704f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
705f6603c60Sopenharmony_ci */
706f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetgroups2, Function | MediumTest | Level1)
707f6603c60Sopenharmony_ci{
708f6603c60Sopenharmony_ci    gid_t gidListIn[2] = {GetRandID(), GetRandID()};
709f6603c60Sopenharmony_ci    gid_t gidListOut[4] = {0};
710f6603c60Sopenharmony_ci
711f6603c60Sopenharmony_ci    LOG("Init: make sure groups not changed by other test.");
712f6603c60Sopenharmony_ci    int n = getgroups(0, gidListOut);
713f6603c60Sopenharmony_ci    EXPECT_EQ(n, 1);
714f6603c60Sopenharmony_ci    int rt = getgroups(n, gidListOut);
715f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[0], SHELL_GID);
716f6603c60Sopenharmony_ci    EXPECT_EQ(rt, n);
717f6603c60Sopenharmony_ci
718f6603c60Sopenharmony_ci    LOG("add 2 groups");
719f6603c60Sopenharmony_ci    rt = setgroups(2, gidListIn);
720f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
721f6603c60Sopenharmony_ci    n = getgroups(4, gidListOut);
722f6603c60Sopenharmony_ci    EXPECT_EQ(n, 3);
723f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[0], gidListIn[0]);
724f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[1], gidListIn[1]);
725f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[2], SHELL_GID);
726f6603c60Sopenharmony_ci
727f6603c60Sopenharmony_ci    LOG("clear groups");
728f6603c60Sopenharmony_ci    rt = setgroups(0, NULL);
729f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
730f6603c60Sopenharmony_ci    n = getgroups(0, gidListOut);
731f6603c60Sopenharmony_ci    EXPECT_EQ(n, 1);
732f6603c60Sopenharmony_ci    rt = getgroups(n, gidListOut);
733f6603c60Sopenharmony_ci    EXPECT_EQ(rt, n);
734f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[0], SHELL_GID);
735f6603c60Sopenharmony_ci}
736f6603c60Sopenharmony_ci
737f6603c60Sopenharmony_ci/**
738f6603c60Sopenharmony_ci * @tc.number SUB_KERNEL_DAC_SetGroups_0300
739f6603c60Sopenharmony_ci * @tc.name   setgroups function test: input size is grater than 'MAX_PROCESS_GROUPS'
740f6603c60Sopenharmony_ci * @tc.desc   [C- SOFTWARE -0200]
741f6603c60Sopenharmony_ci */
742f6603c60Sopenharmony_ciHWTEST_F(UidGidTest, testSetgroupsFail, Function | MediumTest | Level3)
743f6603c60Sopenharmony_ci{
744f6603c60Sopenharmony_ci    LOG("add 2 groups");
745f6603c60Sopenharmony_ci    gid_t gidListIn[2] = {GetRandID(), GetRandID()};
746f6603c60Sopenharmony_ci    gid_t gidListOut[4] = {0};
747f6603c60Sopenharmony_ci
748f6603c60Sopenharmony_ci    int rt = setgroups(2, gidListIn);
749f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
750f6603c60Sopenharmony_ci
751f6603c60Sopenharmony_ci    LOG("setgroups fail");
752f6603c60Sopenharmony_ci    rt = setgroups(MAX_PROCESS_GROUPS+1, gidListIn);
753f6603c60Sopenharmony_ci    EXPECT_EQ(rt, -1);
754f6603c60Sopenharmony_ci    EXPECT_EQ(errno, EINVAL);
755f6603c60Sopenharmony_ci
756f6603c60Sopenharmony_ci    LOG("check groups not changed");
757f6603c60Sopenharmony_ci    int n = getgroups(4, gidListOut);
758f6603c60Sopenharmony_ci    EXPECT_EQ(n, 3);
759f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[0], gidListIn[0]);
760f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[1], gidListIn[1]);
761f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[2], SHELL_GID);
762f6603c60Sopenharmony_ci
763f6603c60Sopenharmony_ci    LOG("clear groups");
764f6603c60Sopenharmony_ci    rt = setgroups(0, NULL);
765f6603c60Sopenharmony_ci    EXPECT_EQ(rt, 0);
766f6603c60Sopenharmony_ci    n = getgroups(2, gidListOut);
767f6603c60Sopenharmony_ci    EXPECT_EQ(n, 1);
768f6603c60Sopenharmony_ci    EXPECT_EQ(gidListOut[0], SHELL_GID);
769f6603c60Sopenharmony_ci}
770