1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
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 <grp.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include "test.h"
20
21 #define GROUPS_MAX 2
22
23 /**
24 * @tc.name : setgroups_0100
25 * @tc.desc : Sets the supplementary group IDs for the calling process.
26 * @tc.level : Level 0
27 */
setgroups_0100(void)28 void setgroups_0100(void)
29 {
30 gid_t groups[GROUPS_MAX];
31 int count = GROUPS_MAX;
32 gid_t groupslist[GROUPS_MAX];
33 int size = GROUPS_MAX;
34 groups[0] = 0;
35 groups[1] = 1;
36 int result = setgroups(count, groups);
37 if (result == -1) {
38 t_error("%s setgroups failed", __func__);
39 }
40 int num = getgroups(size, groupslist);
41 if (groupslist[0] != groups[0] && groupslist[1] != groups[1]) {
42 t_error("%s setgroups invalid", __func__);
43 }
44 }
45
46 /**
47 * @tc.name : setgroups_0200
48 * @tc.desc : Drop all of its supplementary groups.
49 * @tc.level : Level 0
50 */
setgroups_0200(void)51 void setgroups_0200(void)
52 {
53 gid_t groups[GROUPS_MAX];
54 int count = GROUPS_MAX;
55 gid_t groupslist[GROUPS_MAX];
56 int size = GROUPS_MAX;
57 groups[0] = 0;
58 groups[1] = 1;
59 int result = setgroups(count, groups);
60 if (result == -1) {
61 t_error("%s setgroups failed", __func__);
62 }
63 if (setgroups(0, NULL) == -1) {
64 t_error("%s drop all of its supplementary groups failed", __func__);
65 return;
66 }
67 int num = getgroups(size, groupslist);
68 if (num != 0) {
69 t_error("%s drop failed", __func__);
70 }
71 }
72
main(int argc,char * argv[])73 int main(int argc, char *argv[])
74 {
75 setgroups_0100();
76 setgroups_0200();
77 return t_status;
78 }