blob: 25c9e360936e44d82e80086d1bdb617d7b982e56 [file] [log] [blame]
Zeng Linggangc1dcdca2014-02-18 21:10:05 +08001/*
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"
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080041#include "safe_macros.h"
Han Pingtian75fb0572014-11-28 16:33:41 +080042#include "compat_16.h"
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080043
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] = ".";
Cyril Hrubisbbffc172014-06-19 17:03:55 +020049static const char *device;
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080050static int mount_flag;
51
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080052static struct test_case_t {
53 char *pathname;
54 int exp_errno;
55} test_cases[] = {
56 {test_eloop, ELOOP},
57 {TEST_EROFS, EROFS},
58};
59
Han Pingtian75fb0572014-11-28 16:33:41 +080060TCID_DEFINE(lchown03);
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080061int TST_TOTAL = ARRAY_SIZE(test_cases);
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080062
63static void setup(void);
64static void lchown_verify(const struct test_case_t *);
65static void cleanup(void);
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080066
67int main(int argc, char *argv[])
68{
69 int lc;
70 int i;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020071 const char *msg;
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080072
Cyril Hrubisbbffc172014-06-19 17:03:55 +020073 msg = parse_opts(argc, argv, NULL, NULL);
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080074 if (msg != NULL)
75 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
76
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080077 setup();
78
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080079 for (lc = 0; TEST_LOOPING(lc); lc++) {
80 tst_count = 0;
81 for (i = 0; i < TST_TOTAL; i++)
82 lchown_verify(&test_cases[i]);
83 }
84
85 cleanup();
86 tst_exit();
87}
88
89static void setup(void)
90{
91 int i;
Cyril Hrubisbbffc172014-06-19 17:03:55 +020092 const char *fs_type;
Zeng Linggangc1dcdca2014-02-18 21:10:05 +080093
94 tst_require_root(NULL);
95
96 tst_sig(NOFORK, DEF_HANDLER, cleanup);
97
98 TEST_PAUSE;
99
100 tst_tmpdir();
101
Cyril Hrubisbbffc172014-06-19 17:03:55 +0200102 fs_type = tst_dev_fs_type();
103 device = tst_acquire_device(cleanup);
104
105 if (!device)
106 tst_brkm(TCONF, cleanup, "Failed to acquire device");
107
Zeng Linggangc1dcdca2014-02-18 21:10:05 +0800108 SAFE_MKDIR(cleanup, "test_eloop", DIR_MODE);
109 SAFE_SYMLINK(cleanup, "../test_eloop", "test_eloop/test_eloop");
110 for (i = 0; i < 43; i++)
111 strcat(test_eloop, "/test_eloop");
112
Cyril Hrubisbbffc172014-06-19 17:03:55 +0200113 tst_mkfs(cleanup, device, fs_type, NULL);
Zeng Linggangc1dcdca2014-02-18 21:10:05 +0800114 SAFE_MKDIR(cleanup, TEST_EROFS, DIR_MODE);
Cyril Hrubisbbffc172014-06-19 17:03:55 +0200115 if (mount(device, TEST_EROFS, fs_type, MS_RDONLY, NULL) < 0) {
Zeng Linggangc1dcdca2014-02-18 21:10:05 +0800116 tst_brkm(TBROK | TERRNO, cleanup,
117 "mount device:%s failed", device);
118 }
119 mount_flag = 1;
120}
121
122static void lchown_verify(const struct test_case_t *test)
123{
Han Pingtian75fb0572014-11-28 16:33:41 +0800124 UID16_CHECK(geteuid(), "lchown", cleanup)
125 GID16_CHECK(getegid(), "lchown", cleanup)
126
127 TEST(LCHOWN(cleanup, test->pathname, geteuid(), getegid()));
Zeng Linggangc1dcdca2014-02-18 21:10:05 +0800128
129 if (TEST_RETURN != -1) {
130 tst_resm(TFAIL, "lchown() returned %ld, expected -1, errno=%d",
131 TEST_RETURN, test->exp_errno);
132 return;
133 }
134
Zeng Linggangc1dcdca2014-02-18 21:10:05 +0800135 if (TEST_ERRNO == test->exp_errno) {
136 tst_resm(TPASS | TTERRNO, "lchown() failed as expected");
137 } else {
138 tst_resm(TFAIL | TTERRNO,
139 "lchown() failed unexpectedly; expected: %d - %s",
140 test->exp_errno,
141 strerror(test->exp_errno));
142 }
143}
144
145static void cleanup(void)
146{
Cyril Hrubisc60a2302015-01-29 16:28:24 +0100147 if (mount_flag && tst_umount(TEST_EROFS) < 0)
Zeng Linggangc1dcdca2014-02-18 21:10:05 +0800148 tst_resm(TWARN | TERRNO, "umount device:%s failed", device);
149
Cyril Hrubisbbffc172014-06-19 17:03:55 +0200150 if (device)
151 tst_release_device(NULL, device);
Zeng Linggangc1dcdca2014-02-18 21:10:05 +0800152
Cyril Hrubisbbffc172014-06-19 17:03:55 +0200153 tst_rmdir();
Zeng Linggangc1dcdca2014-02-18 21:10:05 +0800154}