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 : setregid_0100
22 * @tc.desc : set real and/or effective group ID
23 * @tc.level : Level 0
24 */
setregid_0100(void)25 void setregid_0100(void)
26 {
27 gid_t cgid = getgid();
28 gid_t cegid = getegid();
29
30 gid_t srgid = 1;
31 gid_t segid = 2;
32 int result = setregid(srgid, segid);
33 if (result != 0) {
34 t_error("%s failed: result = %d\n", __func__, result);
35 }
36
37 gid_t gid = getgid();
38 gid_t egid = getegid();
39 if (gid != srgid || egid != segid) {
40 t_error("%s failed: gid = %d\n", __func__, gid);
41 t_error("%s failed: egid = %d\n", __func__, egid);
42 }
43
44 result = setregid(cgid, cegid);
45 if (result != 0) {
46 t_error("%s failed: result = %d\n", __func__, result);
47 }
48 }
49
50 /**
51 * @tc.name : setregid_0200
52 * @tc.desc : set real and/or effective group ID with the current effective group ID
53 * @tc.level : Level 1
54 */
setregid_0200(void)55 void setregid_0200(void)
56 {
57 gid_t cgid = getgid();
58 gid_t cegid = getegid();
59
60 gid_t srgid = cgid;
61 gid_t segid = cegid;
62 int result = setregid(srgid, segid);
63 if (result != 0) {
64 t_error("%s failed: result = %d\n", __func__, result);
65 }
66
67 gid_t gid = getgid();
68 gid_t egid = getegid();
69 if (gid != srgid || egid != segid) {
70 t_error("%s failed: gid = %d\n", __func__, gid);
71 t_error("%s failed: egid = %d\n", __func__, egid);
72 }
73 }
74
main(int argc,char * argv[])75 int main(int argc, char *argv[])
76 {
77 setregid_0100();
78 setregid_0200();
79
80 return t_status;
81 }
82