1 /*
2 * Copyright (c) International Business Machines Corp., 2006
3 * AUTHOR: Yi Yang <yyangcdl@cn.ibm.com>
4 *
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
16 * 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 /*
20 * DESCRIPTION
21 * This test case will verify basic function of fchownat
22 * added by kernel 2.6.16 or up.
23 */
24
25 #define _GNU_SOURCE
26
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <string.h>
33 #include <signal.h>
34
35 #include "test.h"
36 #include "safe_macros.h"
37 #include "fchownat.h"
38 #include "lapi/fcntl.h"
39
40 #define TESTFILE "testfile"
41
42 static void setup(void);
43 static void cleanup(void);
44
45 static int dir_fd;
46 static int fd;
47 static int no_fd = -1;
48 static int cu_fd = AT_FDCWD;
49
50 static struct test_case_t {
51 int exp_ret;
52 int exp_errno;
53 int flag;
54 int *fds;
55 char *filenames;
56 } test_cases[] = {
57 {0, 0, 0, &dir_fd, TESTFILE},
58 {-1, ENOTDIR, 0, &fd, TESTFILE},
59 {-1, EBADF, 0, &no_fd, TESTFILE},
60 {-1, EINVAL, 9999, &dir_fd, TESTFILE},
61 {0, 0, 0, &cu_fd, TESTFILE},
62 };
63
64 char *TCID = "fchownat01";
65 int TST_TOTAL = ARRAY_SIZE(test_cases);
66 static void fchownat_verify(const struct test_case_t *);
67
main(int ac,char ** av)68 int main(int ac, char **av)
69 {
70 int lc;
71 int i;
72
73 tst_parse_opts(ac, av, NULL, NULL);
74
75 setup();
76
77 for (lc = 0; TEST_LOOPING(lc); lc++) {
78 tst_count = 0;
79 for (i = 0; i < TST_TOTAL; i++)
80 fchownat_verify(&test_cases[i]);
81 }
82
83 cleanup();
84 tst_exit();
85 }
86
setup(void)87 static void setup(void)
88 {
89 tst_sig(NOFORK, DEF_HANDLER, cleanup);
90
91 TEST_PAUSE;
92
93 tst_tmpdir();
94
95 dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);
96
97 SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);
98
99 fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
100 }
101
fchownat_verify(const struct test_case_t * test)102 static void fchownat_verify(const struct test_case_t *test)
103 {
104 TEST(fchownat(*(test->fds), test->filenames, geteuid(),
105 getegid(), test->flag));
106
107 if (TEST_RETURN != test->exp_ret) {
108 tst_resm(TFAIL | TTERRNO,
109 "fchownat() returned %ld, expected %d, errno=%d",
110 TEST_RETURN, test->exp_ret, test->exp_errno);
111 return;
112 }
113
114 if (TEST_ERRNO == test->exp_errno) {
115 tst_resm(TPASS | TTERRNO,
116 "fchownat() returned the expected errno %d: %s",
117 test->exp_ret, strerror(test->exp_errno));
118 } else {
119 tst_resm(TFAIL | TTERRNO,
120 "fchownat() failed unexpectedly; expected: %d - %s",
121 test->exp_errno, strerror(test->exp_errno));
122 }
123 }
124
cleanup(void)125 static void cleanup(void)
126 {
127 if (fd > 0)
128 close(fd);
129
130 if (dir_fd > 0)
131 close(dir_fd);
132
133 tst_rmdir();
134 }
135