1f08c3bdfSopenharmony_ci/*
2f08c3bdfSopenharmony_ci * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com>
3f08c3bdfSopenharmony_ci *
4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or
5f08c3bdfSopenharmony_ci * modify it under the terms of the GNU General Public License as
6f08c3bdfSopenharmony_ci * published by the Free Software Foundation; either version 2 of
7f08c3bdfSopenharmony_ci * the License, or (at your option) any later version.
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful,
10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of
11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12f08c3bdfSopenharmony_ci * GNU General Public License for more details.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License
15f08c3bdfSopenharmony_ci * along with this program; if not, write the Free Software Foundation,
16f08c3bdfSopenharmony_ci * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17f08c3bdfSopenharmony_ci */
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci /* Description:
20f08c3bdfSopenharmony_ci *   Verify that:
21f08c3bdfSopenharmony_ci *   1) renameat2(2) returns -1 and sets errno to EEXIST because newpath
22f08c3bdfSopenharmony_ci *		already exists and the flag RENAME_NOREPLACE is used.
23f08c3bdfSopenharmony_ci *   2) renameat2(2) returns 0.
24f08c3bdfSopenharmony_ci *   3) renameat2(2) returns -1 and sets errno to ENOENT because the flag
25f08c3bdfSopenharmony_ci *		RENAME_EXCHANGE is used and newpath does not exist.
26f08c3bdfSopenharmony_ci *   4) renameat2(2) returns 0 because the flag RENAME_NOREPLACE is used,
27f08c3bdfSopenharmony_ci *		both olddirfd and newdirfd are valid and oldpath exists and
28f08c3bdfSopenharmony_ci *		newpath does not exist.
29f08c3bdfSopenharmony_ci *   5) renameat2(2) returns -1 and sets errno to EINVAL because
30f08c3bdfSopenharmony_ci *		RENAME_NOREPLACE and RENAME_EXCHANGE are used together
31f08c3bdfSopenharmony_ci *   6) renameat2(2) returns -1 and sets errno to EINVAL because
32f08c3bdfSopenharmony_ci *		RENAME_WHITEOUT and RENAME_EXCHANGE are used together
33f08c3bdfSopenharmony_ci */
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_ci#define _GNU_SOURCE
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci#include "test.h"
38f08c3bdfSopenharmony_ci#include "safe_macros.h"
39f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
40f08c3bdfSopenharmony_ci#include "renameat2.h"
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_ci#define TEST_DIR "test_dir/"
43f08c3bdfSopenharmony_ci#define TEST_DIR2 "test_dir2/"
44f08c3bdfSopenharmony_ci
45f08c3bdfSopenharmony_ci#define TEST_FILE "test_file"
46f08c3bdfSopenharmony_ci#define TEST_FILE2 "test_file2"
47f08c3bdfSopenharmony_ci#define TEST_FILE3 "test_file3"
48f08c3bdfSopenharmony_ci#define NON_EXIST "non_exist"
49f08c3bdfSopenharmony_ci
50f08c3bdfSopenharmony_cichar *TCID = "renameat201";
51f08c3bdfSopenharmony_ci
52f08c3bdfSopenharmony_cistatic int olddirfd;
53f08c3bdfSopenharmony_cistatic int newdirfd;
54f08c3bdfSopenharmony_cistatic long fs_type;
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_cistatic struct test_case {
57f08c3bdfSopenharmony_ci	int *olddirfd;
58f08c3bdfSopenharmony_ci	const char *oldpath;
59f08c3bdfSopenharmony_ci	int *newdirfd;
60f08c3bdfSopenharmony_ci	const char *newpath;
61f08c3bdfSopenharmony_ci	int flags;
62f08c3bdfSopenharmony_ci	int exp_errno;
63f08c3bdfSopenharmony_ci} test_cases[] = {
64f08c3bdfSopenharmony_ci	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE2, RENAME_NOREPLACE, EEXIST},
65f08c3bdfSopenharmony_ci	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE2, RENAME_EXCHANGE, 0},
66f08c3bdfSopenharmony_ci	{&olddirfd, TEST_FILE, &newdirfd, NON_EXIST, RENAME_EXCHANGE, ENOENT},
67f08c3bdfSopenharmony_ci	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE3, RENAME_NOREPLACE, 0},
68f08c3bdfSopenharmony_ci	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE2, RENAME_NOREPLACE
69f08c3bdfSopenharmony_ci				| RENAME_EXCHANGE, EINVAL},
70f08c3bdfSopenharmony_ci	{&olddirfd, TEST_FILE, &newdirfd, TEST_FILE2, RENAME_WHITEOUT
71f08c3bdfSopenharmony_ci				| RENAME_EXCHANGE, EINVAL}
72f08c3bdfSopenharmony_ci};
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ciint TST_TOTAL = ARRAY_SIZE(test_cases);
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_cistatic void setup(void);
77f08c3bdfSopenharmony_cistatic void cleanup(void);
78f08c3bdfSopenharmony_cistatic void renameat2_verify(const struct test_case *test);
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci
81f08c3bdfSopenharmony_ciint main(int ac, char **av)
82f08c3bdfSopenharmony_ci{
83f08c3bdfSopenharmony_ci	int i;
84f08c3bdfSopenharmony_ci	int lc;
85f08c3bdfSopenharmony_ci
86f08c3bdfSopenharmony_ci	tst_parse_opts(ac, av, NULL, NULL);
87f08c3bdfSopenharmony_ci
88f08c3bdfSopenharmony_ci	setup();
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	for (lc = 0; lc < TEST_LOOPING(lc); lc++) {
91f08c3bdfSopenharmony_ci		tst_count = 0;
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci		for (i = 0; i < TST_TOTAL; i++)
94f08c3bdfSopenharmony_ci			renameat2_verify(&test_cases[i]);
95f08c3bdfSopenharmony_ci	}
96f08c3bdfSopenharmony_ci
97f08c3bdfSopenharmony_ci	cleanup();
98f08c3bdfSopenharmony_ci	tst_exit();
99f08c3bdfSopenharmony_ci}
100f08c3bdfSopenharmony_ci
101f08c3bdfSopenharmony_cistatic void setup(void)
102f08c3bdfSopenharmony_ci{
103f08c3bdfSopenharmony_ci	if ((tst_kvercmp(3, 15, 0)) < 0) {
104f08c3bdfSopenharmony_ci		tst_brkm(TCONF, NULL,
105f08c3bdfSopenharmony_ci			"This test can only run on kernels that are 3.15. and higher");
106f08c3bdfSopenharmony_ci	}
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci	tst_tmpdir();
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci	fs_type = tst_fs_type(cleanup, ".");
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, TEST_DIR, 0700);
113f08c3bdfSopenharmony_ci	SAFE_MKDIR(cleanup, TEST_DIR2, 0700);
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE, 0600, NULL);
116f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, TEST_DIR2 TEST_FILE2, 0600, NULL);
117f08c3bdfSopenharmony_ci	SAFE_TOUCH(cleanup, TEST_DIR TEST_FILE3, 0600, NULL);
118f08c3bdfSopenharmony_ci
119f08c3bdfSopenharmony_ci	olddirfd = SAFE_OPEN(cleanup, TEST_DIR, O_DIRECTORY);
120f08c3bdfSopenharmony_ci	newdirfd = SAFE_OPEN(cleanup, TEST_DIR2, O_DIRECTORY);
121f08c3bdfSopenharmony_ci}
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_cistatic void cleanup(void)
124f08c3bdfSopenharmony_ci{
125f08c3bdfSopenharmony_ci	if (olddirfd > 0 && close(olddirfd) < 0)
126f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "close olddirfd failed");
127f08c3bdfSopenharmony_ci
128f08c3bdfSopenharmony_ci	if (newdirfd > 0 && close(newdirfd) < 0)
129f08c3bdfSopenharmony_ci		tst_resm(TWARN | TERRNO, "close newdirfd failed");
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci	tst_rmdir();
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci}
134f08c3bdfSopenharmony_ci
135f08c3bdfSopenharmony_cistatic void renameat2_verify(const struct test_case *test)
136f08c3bdfSopenharmony_ci{
137f08c3bdfSopenharmony_ci	TEST(renameat2(*(test->olddirfd), test->oldpath,
138f08c3bdfSopenharmony_ci			*(test->newdirfd), test->newpath, test->flags));
139f08c3bdfSopenharmony_ci
140f08c3bdfSopenharmony_ci	if ((test->flags & RENAME_EXCHANGE) && EINVAL == TEST_ERRNO
141f08c3bdfSopenharmony_ci		&& fs_type == TST_BTRFS_MAGIC) {
142f08c3bdfSopenharmony_ci		tst_resm(TCONF,
143f08c3bdfSopenharmony_ci			"RENAME_EXCHANGE flag is not implemeted on %s",
144f08c3bdfSopenharmony_ci			tst_fs_type_name(fs_type));
145f08c3bdfSopenharmony_ci		return;
146f08c3bdfSopenharmony_ci	}
147f08c3bdfSopenharmony_ci
148f08c3bdfSopenharmony_ci	if (test->exp_errno && TEST_RETURN != -1) {
149f08c3bdfSopenharmony_ci		tst_resm(TFAIL, "renameat2() succeeded unexpectedly");
150f08c3bdfSopenharmony_ci		return;
151f08c3bdfSopenharmony_ci	}
152f08c3bdfSopenharmony_ci
153f08c3bdfSopenharmony_ci	if (test->exp_errno == 0 && TEST_RETURN != 0) {
154f08c3bdfSopenharmony_ci		tst_resm(TFAIL | TTERRNO, "renameat2() failed unexpectedly");
155f08c3bdfSopenharmony_ci		return;
156f08c3bdfSopenharmony_ci	}
157f08c3bdfSopenharmony_ci
158f08c3bdfSopenharmony_ci	if (test->exp_errno == TEST_ERRNO) {
159f08c3bdfSopenharmony_ci		tst_resm(TPASS | TTERRNO,
160f08c3bdfSopenharmony_ci		"renameat2() returned the expected value");
161f08c3bdfSopenharmony_ci		return;
162f08c3bdfSopenharmony_ci	}
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_ci	tst_resm(TFAIL | TTERRNO,
165f08c3bdfSopenharmony_ci		"renameat2() got unexpected return value: expected: %d - %s",
166f08c3bdfSopenharmony_ci			test->exp_errno, tst_strerrno(test->exp_errno));
167f08c3bdfSopenharmony_ci
168f08c3bdfSopenharmony_ci}
169