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