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 <unistd.h>
17
18#include "test.h"
19
20/**
21 * @tc.name      : setresgid_0100
22 * @tc.desc      : set real, effective, and saved group ID
23 * @tc.level     : Level 0
24 */
25void setresgid_0100(void)
26{
27    gid_t crgid = 0;
28    gid_t cegid = 0;
29    gid_t csgid = 0;
30    int result = getresgid(&crgid, &cegid, &csgid);
31    if (result != 0) {
32        t_error("%s failed: result = %d\n", __func__, result);
33    }
34
35    gid_t srgid = 1;
36    gid_t segid = 2;
37    gid_t ssgid = 3;
38    result = setresgid(srgid, segid, ssgid);
39    if (result != 0) {
40        t_error("%s failed: result = %d\n", __func__, result);
41    }
42
43    gid_t rgid = 0;
44    gid_t egid = 0;
45    gid_t sgid = 0;
46    result = getresgid(&rgid, &egid, &sgid);
47    if (result != 0) {
48        t_error("%s failed: result = %d\n", __func__, result);
49    }
50
51    if ((rgid != srgid) || (egid != segid) || (sgid != ssgid)) {
52        t_error("%s failed: rgid = %d\n", __func__, rgid);
53        t_error("%s failed: egid = %d\n", __func__, egid);
54        t_error("%s failed: sgid = %d\n", __func__, sgid);
55    }
56
57    result = setresgid(crgid, cegid, csgid);
58    if (result != 0) {
59        t_error("%s failed: result = %d\n", __func__, result);
60    }
61}
62
63/**
64 * @tc.name      : setresgid_0200
65 * @tc.desc      : set real, effective, and saved group ID with the current values
66 * @tc.level     : Level 1
67 */
68void setresgid_0200(void)
69{
70    gid_t crgid = 0;
71    gid_t cegid = 0;
72    gid_t csgid = 0;
73    int result = getresgid(&crgid, &cegid, &csgid);
74    if (result != 0) {
75        t_error("%s failed: result = %d\n", __func__, result);
76    }
77
78    gid_t srgid = crgid;
79    gid_t segid = crgid;
80    gid_t ssgid = crgid;
81    result = setresgid(srgid, segid, ssgid);
82    if (result != 0) {
83        t_error("%s failed: result = %d\n", __func__, result);
84    }
85
86    gid_t rgid = 0;
87    gid_t egid = 0;
88    gid_t sgid = 0;
89    result = getresgid(&rgid, &egid, &sgid);
90    if (result != 0) {
91        t_error("%s failed: result = %d\n", __func__, result);
92    }
93
94    if ((rgid != srgid) || (egid != segid) || (sgid != ssgid)) {
95        t_error("%s failed: rgid = %d\n", __func__, rgid);
96        t_error("%s failed: egid = %d\n", __func__, egid);
97        t_error("%s failed: sgid = %d\n", __func__, sgid);
98    }
99}
100
101int main(int argc, char *argv[])
102{
103    setresgid_0100();
104    setresgid_0200();
105
106    return t_status;
107}
108