lchown03.c revision d1e794d62b1bf619df8390535e4c2a58899b1145
1/*
2 * Copyright (c) 2014 Fujitsu Ltd.
3 * Author: Zeng Linggang <zenglg.jy@cn.fujitsu.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17/*
18 * Test Description:
19 *  Verify that,
20 *   1. lchown() fails with -1 return value and sets errno to ELOOP
21 *      if too many symbolic links were encountered in resolving path.
22 *   2. lchown() fails with -1 return value and sets errno to EROFS
23 *      if the file is on a read-only file system.
24 */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <fcntl.h>
30#include <errno.h>
31#include <string.h>
32#include <signal.h>
33#include <grp.h>
34#include <pwd.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/mman.h>
38#include <sys/mount.h>
39
40#include "test.h"
41#include "safe_macros.h"
42#include "compat_16.h"
43
44#define DIR_MODE	(S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP| \
45			 S_IXGRP|S_IROTH|S_IXOTH)
46#define TEST_EROFS	"mntpoint"
47
48static char test_eloop[PATH_MAX] = ".";
49static const char *device;
50static int mount_flag;
51
52static struct test_case_t {
53	char *pathname;
54	int exp_errno;
55} test_cases[] = {
56	{test_eloop, ELOOP},
57	{TEST_EROFS, EROFS},
58};
59
60TCID_DEFINE(lchown03);
61int TST_TOTAL = ARRAY_SIZE(test_cases);
62
63static void setup(void);
64static void lchown_verify(const struct test_case_t *);
65static void cleanup(void);
66
67int main(int argc, char *argv[])
68{
69	int lc;
70	int i;
71
72	tst_parse_opts(argc, argv, NULL, NULL);
73
74	setup();
75
76	for (lc = 0; TEST_LOOPING(lc); lc++) {
77		tst_count = 0;
78		for (i = 0; i < TST_TOTAL; i++)
79			lchown_verify(&test_cases[i]);
80	}
81
82	cleanup();
83	tst_exit();
84}
85
86static void setup(void)
87{
88	int i;
89	const char *fs_type;
90
91	tst_require_root();
92
93	tst_sig(NOFORK, DEF_HANDLER, cleanup);
94
95	TEST_PAUSE;
96
97	tst_tmpdir();
98
99	fs_type = tst_dev_fs_type();
100	device = tst_acquire_device(cleanup);
101
102	if (!device)
103		tst_brkm(TCONF, cleanup, "Failed to acquire device");
104
105	SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
106	SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
107	for (i = 0; i < 43; i++)
108		strcat(test_eloop, "/test_eloop");
109
110	tst_mkfs(cleanup, device, fs_type, NULL);
111	SAFE_MKDIR(cleanup, TEST_EROFS, DIR_MODE);
112	if (mount(device, TEST_EROFS, fs_type, MS_RDONLY, NULL) < 0) {
113		tst_brkm(TBROK | TERRNO, cleanup,
114			 "mount device:%s failed", device);
115	}
116	mount_flag = 1;
117}
118
119static void lchown_verify(const struct test_case_t *test)
120{
121	UID16_CHECK(geteuid(), "lchown", cleanup)
122	GID16_CHECK(getegid(), "lchown", cleanup)
123
124	TEST(LCHOWN(cleanup, test->pathname, geteuid(), getegid()));
125
126	if (TEST_RETURN != -1) {
127		tst_resm(TFAIL, "lchown() returned %ld, expected -1, errno=%d",
128			 TEST_RETURN, test->exp_errno);
129		return;
130	}
131
132	if (TEST_ERRNO == test->exp_errno) {
133		tst_resm(TPASS | TTERRNO, "lchown() failed as expected");
134	} else {
135		tst_resm(TFAIL | TTERRNO,
136			 "lchown() failed unexpectedly; expected: %d - %s",
137			 test->exp_errno,
138			 strerror(test->exp_errno));
139	}
140}
141
142static void cleanup(void)
143{
144	if (mount_flag && tst_umount(TEST_EROFS) < 0)
145		tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
146
147	if (device)
148		tst_release_device(NULL, device);
149
150	tst_rmdir();
151}
152