aboutsummaryrefslogtreecommitdiff
path: root/testcases/kernel/syscalls/fchownat/fchownat01.c
blob: 3b29f1e7598bcd3b73f4cc18f67ab43875e97ec8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 *   Copyright (c) International Business Machines  Corp., 2006
 *   AUTHOR: Yi Yang <yyangcdl@cn.ibm.com>
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software Foundation,
 *   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
/*
 * DESCRIPTION
 *	This test case will verify basic function of fchownat
 *	added by kernel 2.6.16 or up.
 */

#define _GNU_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>

#include "test.h"
#include "safe_macros.h"
#include "fchownat.h"
#include "lapi/fcntl.h"

#define TESTFILE	"testfile"

static void setup(void);
static void cleanup(void);

static int dir_fd;
static int fd;
static int no_fd = -1;
static int cu_fd = AT_FDCWD;

static struct test_case_t {
	int exp_ret;
	int exp_errno;
	int flag;
	int *fds;
	char *filenames;
} test_cases[] = {
	{0, 0, 0, &dir_fd, TESTFILE},
	{-1, ENOTDIR, 0, &fd, TESTFILE},
	{-1, EBADF, 0, &no_fd, TESTFILE},
	{-1, EINVAL, 9999, &dir_fd, TESTFILE},
	{0, 0, 0, &cu_fd, TESTFILE},
};

char *TCID = "fchownat01";
int TST_TOTAL = ARRAY_SIZE(test_cases);
static void fchownat_verify(const struct test_case_t *);

int main(int ac, char **av)
{
	int lc;
	int i;

	tst_parse_opts(ac, av, NULL, NULL);

	setup();

	for (lc = 0; TEST_LOOPING(lc); lc++) {
		tst_count = 0;
		for (i = 0; i < TST_TOTAL; i++)
			fchownat_verify(&test_cases[i]);
	}

	cleanup();
	tst_exit();
}

static void setup(void)
{
	tst_sig(NOFORK, DEF_HANDLER, cleanup);

	TEST_PAUSE;

	tst_tmpdir();

	dir_fd = SAFE_OPEN(cleanup, "./", O_DIRECTORY);

	SAFE_TOUCH(cleanup, TESTFILE, 0600, NULL);

	fd = SAFE_OPEN(cleanup, "testfile2", O_CREAT | O_RDWR, 0600);
}

static void fchownat_verify(const struct test_case_t *test)
{
	TEST(fchownat(*(test->fds), test->filenames, geteuid(),
		      getegid(), test->flag));

	if (TEST_RETURN != test->exp_ret) {
		tst_resm(TFAIL | TTERRNO,
			 "fchownat() returned %ld, expected %d, errno=%d",
			 TEST_RETURN, test->exp_ret, test->exp_errno);
		return;
	}

	if (TEST_ERRNO == test->exp_errno) {
		tst_resm(TPASS | TTERRNO,
			 "fchownat() returned the expected errno %d: %s",
			 test->exp_ret, strerror(test->exp_errno));
	} else {
		tst_resm(TFAIL | TTERRNO,
			 "fchownat() failed unexpectedly; expected: %d - %s",
			 test->exp_errno, strerror(test->exp_errno));
	}
}

static void cleanup(void)
{
	if (fd > 0)
		close(fd);

	if (dir_fd > 0)
		close(dir_fd);

	tst_rmdir();
}