1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
3*49cdfc7eSAndroid Build Coastguard Worker *
4*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
5*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
6*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
7*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker *
9*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
10*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker *
14*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
16*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*49cdfc7eSAndroid Build Coastguard Worker */
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker /*
20*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
21*49cdfc7eSAndroid Build Coastguard Worker * Verify that, lchown(2) succeeds to change the owner and group of a file
22*49cdfc7eSAndroid Build Coastguard Worker * specified by path to any numeric owner(uid)/group(gid) values when invoked
23*49cdfc7eSAndroid Build Coastguard Worker * by super-user.
24*49cdfc7eSAndroid Build Coastguard Worker *
25*49cdfc7eSAndroid Build Coastguard Worker * Expected Result:
26*49cdfc7eSAndroid Build Coastguard Worker * lchown(2) should return 0 and the ownership set on the file should match
27*49cdfc7eSAndroid Build Coastguard Worker * the numeric values contained in owner and group respectively.
28*49cdfc7eSAndroid Build Coastguard Worker *
29*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
30*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
31*49cdfc7eSAndroid Build Coastguard Worker * 11/2010 Code cleanup by Cyril Hrubis [email protected]
32*49cdfc7eSAndroid Build Coastguard Worker */
33*49cdfc7eSAndroid Build Coastguard Worker
34*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <sys/stat.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
43*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker /*
46*49cdfc7eSAndroid Build Coastguard Worker * Don't forget to remove USE_LEGACY_COMPAT_16_H from Makefile after
47*49cdfc7eSAndroid Build Coastguard Worker * rewriting all tests to the new API.
48*49cdfc7eSAndroid Build Coastguard Worker */
49*49cdfc7eSAndroid Build Coastguard Worker #include "compat_16.h"
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE (S_IFREG|S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)
52*49cdfc7eSAndroid Build Coastguard Worker #define TESTFILE "testfile"
53*49cdfc7eSAndroid Build Coastguard Worker #define SFILE "slink_file"
54*49cdfc7eSAndroid Build Coastguard Worker
55*49cdfc7eSAndroid Build Coastguard Worker TCID_DEFINE(lchown01);
56*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 5;
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker struct test_case_t {
59*49cdfc7eSAndroid Build Coastguard Worker char *desc;
60*49cdfc7eSAndroid Build Coastguard Worker uid_t user_id;
61*49cdfc7eSAndroid Build Coastguard Worker gid_t group_id;
62*49cdfc7eSAndroid Build Coastguard Worker };
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker static struct test_case_t test_cases[] = {
65*49cdfc7eSAndroid Build Coastguard Worker {"Change Owner/Group ids", 700, 701},
66*49cdfc7eSAndroid Build Coastguard Worker {"Change Owner id only", 702, -1},
67*49cdfc7eSAndroid Build Coastguard Worker {"Change Owner/Group ids", 703, 701},
68*49cdfc7eSAndroid Build Coastguard Worker {"Change Group id only", -1, 704},
69*49cdfc7eSAndroid Build Coastguard Worker {"Change Group/Group ids", 703, 705},
70*49cdfc7eSAndroid Build Coastguard Worker {"Change none", -1, -1},
71*49cdfc7eSAndroid Build Coastguard Worker {NULL, 0, 0}
72*49cdfc7eSAndroid Build Coastguard Worker };
73*49cdfc7eSAndroid Build Coastguard Worker
74*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
75*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
76*49cdfc7eSAndroid Build Coastguard Worker
main(int argc,char * argv[])77*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
78*49cdfc7eSAndroid Build Coastguard Worker {
79*49cdfc7eSAndroid Build Coastguard Worker struct stat stat_buf;
80*49cdfc7eSAndroid Build Coastguard Worker int lc;
81*49cdfc7eSAndroid Build Coastguard Worker int i;
82*49cdfc7eSAndroid Build Coastguard Worker
83*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(argc, argv, NULL, NULL);
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker setup();
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; test_cases[i].desc != NULL; i++) {
92*49cdfc7eSAndroid Build Coastguard Worker uid_t user_id = test_cases[i].user_id;
93*49cdfc7eSAndroid Build Coastguard Worker gid_t group_id = test_cases[i].group_id;
94*49cdfc7eSAndroid Build Coastguard Worker char *test_desc = test_cases[i].desc;
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker /*
97*49cdfc7eSAndroid Build Coastguard Worker * Call lchown(2) with different user id and
98*49cdfc7eSAndroid Build Coastguard Worker * group id (numeric values) to set it on
99*49cdfc7eSAndroid Build Coastguard Worker * symlink of testfile.
100*49cdfc7eSAndroid Build Coastguard Worker */
101*49cdfc7eSAndroid Build Coastguard Worker TEST(LCHOWN(cleanup, SFILE, user_id, group_id));
102*49cdfc7eSAndroid Build Coastguard Worker
103*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
104*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
105*49cdfc7eSAndroid Build Coastguard Worker "lchown() Fails to %s, errno %d",
106*49cdfc7eSAndroid Build Coastguard Worker test_desc, TEST_ERRNO);
107*49cdfc7eSAndroid Build Coastguard Worker continue;
108*49cdfc7eSAndroid Build Coastguard Worker }
109*49cdfc7eSAndroid Build Coastguard Worker
110*49cdfc7eSAndroid Build Coastguard Worker if (lstat(SFILE, &stat_buf) < 0) {
111*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TFAIL, cleanup, "lstat(2) "
112*49cdfc7eSAndroid Build Coastguard Worker "%s failed, errno %d",
113*49cdfc7eSAndroid Build Coastguard Worker SFILE, TEST_ERRNO);
114*49cdfc7eSAndroid Build Coastguard Worker }
115*49cdfc7eSAndroid Build Coastguard Worker
116*49cdfc7eSAndroid Build Coastguard Worker if ((int)user_id == -1) {
117*49cdfc7eSAndroid Build Coastguard Worker if (i > 0)
118*49cdfc7eSAndroid Build Coastguard Worker user_id =
119*49cdfc7eSAndroid Build Coastguard Worker test_cases[i - 1].user_id;
120*49cdfc7eSAndroid Build Coastguard Worker else
121*49cdfc7eSAndroid Build Coastguard Worker user_id = geteuid();
122*49cdfc7eSAndroid Build Coastguard Worker }
123*49cdfc7eSAndroid Build Coastguard Worker
124*49cdfc7eSAndroid Build Coastguard Worker if ((int)group_id == -1) {
125*49cdfc7eSAndroid Build Coastguard Worker if (i > 0)
126*49cdfc7eSAndroid Build Coastguard Worker group_id =
127*49cdfc7eSAndroid Build Coastguard Worker test_cases[i - 1].group_id;
128*49cdfc7eSAndroid Build Coastguard Worker else
129*49cdfc7eSAndroid Build Coastguard Worker group_id = getegid();
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker /*
133*49cdfc7eSAndroid Build Coastguard Worker * Check for expected Ownership ids
134*49cdfc7eSAndroid Build Coastguard Worker * set on testfile.
135*49cdfc7eSAndroid Build Coastguard Worker */
136*49cdfc7eSAndroid Build Coastguard Worker if ((stat_buf.st_uid != user_id) ||
137*49cdfc7eSAndroid Build Coastguard Worker (stat_buf.st_gid != group_id)) {
138*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL,
139*49cdfc7eSAndroid Build Coastguard Worker "%s: incorrect ownership set, "
140*49cdfc7eSAndroid Build Coastguard Worker "Expected %d %d", SFILE,
141*49cdfc7eSAndroid Build Coastguard Worker user_id, group_id);
142*49cdfc7eSAndroid Build Coastguard Worker } else {
143*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "lchown() succeeds to "
144*49cdfc7eSAndroid Build Coastguard Worker "%s of %s", test_desc, SFILE);
145*49cdfc7eSAndroid Build Coastguard Worker }
146*49cdfc7eSAndroid Build Coastguard Worker }
147*49cdfc7eSAndroid Build Coastguard Worker }
148*49cdfc7eSAndroid Build Coastguard Worker
149*49cdfc7eSAndroid Build Coastguard Worker cleanup();
150*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
151*49cdfc7eSAndroid Build Coastguard Worker }
152*49cdfc7eSAndroid Build Coastguard Worker
setup(void)153*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
154*49cdfc7eSAndroid Build Coastguard Worker {
155*49cdfc7eSAndroid Build Coastguard Worker int fd;
156*49cdfc7eSAndroid Build Coastguard Worker
157*49cdfc7eSAndroid Build Coastguard Worker tst_sig(NOFORK, DEF_HANDLER, cleanup);
158*49cdfc7eSAndroid Build Coastguard Worker
159*49cdfc7eSAndroid Build Coastguard Worker tst_require_root();
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
162*49cdfc7eSAndroid Build Coastguard Worker tst_tmpdir();
163*49cdfc7eSAndroid Build Coastguard Worker
164*49cdfc7eSAndroid Build Coastguard Worker if ((fd = open(TESTFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
165*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "open failed");
166*49cdfc7eSAndroid Build Coastguard Worker }
167*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(cleanup, fd);
168*49cdfc7eSAndroid Build Coastguard Worker
169*49cdfc7eSAndroid Build Coastguard Worker SAFE_SYMLINK(cleanup, TESTFILE, SFILE);
170*49cdfc7eSAndroid Build Coastguard Worker }
171*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)172*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
173*49cdfc7eSAndroid Build Coastguard Worker {
174*49cdfc7eSAndroid Build Coastguard Worker tst_rmdir();
175*49cdfc7eSAndroid Build Coastguard Worker }
176