blob: ed4692915e62f0c26f58d472f0eff2c6b4e36237 [file] [log] [blame]
Zeng Linggangf9113612014-02-26 17:35:13 +08001/*
vapier032481e2006-08-24 00:24:45 +00002 * Copyright (c) International Business Machines Corp., 2006
Zeng Linggangf9113612014-02-26 17:35:13 +08003 * AUTHOR: Yi Yang <yyangcdl@cn.ibm.com>
vapier032481e2006-08-24 00:24:45 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Zeng Linggangf9113612014-02-26 17:35:13 +080016 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19/*
vapier032481e2006-08-24 00:24:45 +000020 * DESCRIPTION
21 * This test case will verify basic function of fchownat
22 * added by kernel 2.6.16 or up.
Zeng Linggangf9113612014-02-26 17:35:13 +080023 */
vapier032481e2006-08-24 00:24:45 +000024
25#define _GNU_SOURCE
26
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <fcntl.h>
30#include <unistd.h>
31#include <error.h>
32#include <stdlib.h>
33#include <errno.h>
34#include <string.h>
35#include <signal.h>
Zeng Linggangf9113612014-02-26 17:35:13 +080036
vapier032481e2006-08-24 00:24:45 +000037#include "test.h"
Zeng Linggangf9113612014-02-26 17:35:13 +080038#include "safe_macros.h"
39#include "fchownat.h"
40#include "lapi/fcntl.h"
vapier032481e2006-08-24 00:24:45 +000041
Zeng Linggangf9113612014-02-26 17:35:13 +080042#define TESTFILE "testfile"
43
44static void setup(void);
45static void cleanup(void);
46
47static int dirfd;
48static int fd;
49static int no_fd = -1;
50static int cu_fd = AT_FDCWD;
51
52static struct test_case_t {
53 int exp_ret;
54 int exp_errno;
55 int flag;
56 int *fds;
57 char *filenames;
58} test_cases[] = {
59 {0, 0, 0, &dirfd, TESTFILE},
60 {-1, ENOTDIR, 0, &fd, TESTFILE},
61 {-1, EBADF, 0, &no_fd, TESTFILE},
62 {-1, EINVAL, 9999, &dirfd, TESTFILE},
63 {0, 0, 0, &cu_fd, TESTFILE},
64};
vapier032481e2006-08-24 00:24:45 +000065
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020066char *TCID = "fchownat01";
Zeng Linggangf9113612014-02-26 17:35:13 +080067int TST_TOTAL = ARRAY_SIZE(test_cases);
68static void fchownat_verify(const struct test_case_t *);
vapier032481e2006-08-24 00:24:45 +000069
70int main(int ac, char **av)
71{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020072 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020073 const char *msg;
vapier032481e2006-08-24 00:24:45 +000074 int i;
75
Garrett Cooper45e285d2010-11-22 12:19:25 -080076 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080077 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
vapier032481e2006-08-24 00:24:45 +000078
vapier032481e2006-08-24 00:24:45 +000079 setup();
80
vapier032481e2006-08-24 00:24:45 +000081 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080082 tst_count = 0;
Zeng Linggangf9113612014-02-26 17:35:13 +080083 for (i = 0; i < TST_TOTAL; i++)
84 fchownat_verify(&test_cases[i]);
Garrett Cooper2c282152010-12-16 00:55:50 -080085 }
vapier032481e2006-08-24 00:24:45 +000086
vapier032481e2006-08-24 00:24:45 +000087 cleanup();
Zeng Linggangf9113612014-02-26 17:35:13 +080088 tst_exit();
Garrett Cooper2c282152010-12-16 00:55:50 -080089}
vapier032481e2006-08-24 00:24:45 +000090
Zeng Linggangf9113612014-02-26 17:35:13 +080091static void setup(void)
vapier032481e2006-08-24 00:24:45 +000092{
Zeng Linggangf9113612014-02-26 17:35:13 +080093 if ((tst_kvercmp(2, 6, 16)) < 0)
94 tst_brkm(TCONF, NULL, "This test needs kernel 2.6.16 or newer");
vapier032481e2006-08-24 00:24:45 +000095
vapier032481e2006-08-24 00:24:45 +000096 tst_sig(NOFORK, DEF_HANDLER, cleanup);
97
vapier032481e2006-08-24 00:24:45 +000098 TEST_PAUSE;
Zeng Linggangf9113612014-02-26 17:35:13 +080099
100 tst_tmpdir();
101
102 dirfd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
103
104 SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
105
106 fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
Garrett Cooper2c282152010-12-16 00:55:50 -0800107}
vapier032481e2006-08-24 00:24:45 +0000108
Zeng Linggangf9113612014-02-26 17:35:13 +0800109static void fchownat_verify(const struct test_case_t *test)
vapier032481e2006-08-24 00:24:45 +0000110{
Zeng Linggangf9113612014-02-26 17:35:13 +0800111 TEST(fchownat(*(test->fds), test->filenames, geteuid(),
112 getegid(), test->flag));
subrata_modak381cb392008-03-14 19:23:15 +0000113
Zeng Linggangf9113612014-02-26 17:35:13 +0800114 if (TEST_RETURN != test->exp_ret) {
115 tst_resm(TFAIL | TTERRNO,
116 "fchownat() returned %ld, expected %d, errno=%d",
117 TEST_RETURN, test->exp_ret, test->exp_errno);
118 return;
119 }
vapier032481e2006-08-24 00:24:45 +0000120
Zeng Linggangf9113612014-02-26 17:35:13 +0800121 if (TEST_ERRNO == test->exp_errno) {
122 tst_resm(TPASS | TTERRNO,
123 "fchownat() returned the expected errno %d: %s",
124 test->exp_ret, strerror(test->exp_errno));
125 } else {
126 tst_resm(TFAIL | TTERRNO,
127 "fchownat() failed unexpectedly; expected: %d - %s",
128 test->exp_errno, strerror(test->exp_errno));
129 }
130}
131
132static void cleanup(void)
133{
134 if (fd > 0)
135 close(fd);
136
137 if (dirfd > 0)
138 close(dirfd);
139
140 tst_rmdir();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700141}