xref: /xnu-10002.81.5/tests/rename_excl.c (revision 5e3eaea39dcf651e66cb99ba7d70e32cc4a99587)
1*5e3eaea3SApple OSS Distributions #include <darwintest.h>
2*5e3eaea3SApple OSS Distributions #include <darwintest_utils.h>
3*5e3eaea3SApple OSS Distributions #include <dirent.h>
4*5e3eaea3SApple OSS Distributions #include <errno.h>
5*5e3eaea3SApple OSS Distributions #include <fcntl.h>
6*5e3eaea3SApple OSS Distributions #include <stdio.h>
7*5e3eaea3SApple OSS Distributions #include <stdlib.h>
8*5e3eaea3SApple OSS Distributions #include <unistd.h>
9*5e3eaea3SApple OSS Distributions #include <sys/stat.h>
10*5e3eaea3SApple OSS Distributions 
11*5e3eaea3SApple OSS Distributions 
12*5e3eaea3SApple OSS Distributions T_GLOBAL_META(
13*5e3eaea3SApple OSS Distributions 	T_META_NAMESPACE("xnu.vfs"),
14*5e3eaea3SApple OSS Distributions 	T_META_CHECK_LEAKS(false)
15*5e3eaea3SApple OSS Distributions 	);
16*5e3eaea3SApple OSS Distributions 
17*5e3eaea3SApple OSS Distributions #define TEST_DIR         "rename_dir"
18*5e3eaea3SApple OSS Distributions #define TEST_FILE1       TEST_DIR "/file1"
19*5e3eaea3SApple OSS Distributions #define TEST_FILE1_UC    TEST_DIR "/FILE1"
20*5e3eaea3SApple OSS Distributions #define TEST_FILE2       TEST_DIR "/file2"
21*5e3eaea3SApple OSS Distributions #define TEST_FILE3_HL    TEST_DIR "/file3"
22*5e3eaea3SApple OSS Distributions 
23*5e3eaea3SApple OSS Distributions static void
cleanup(void)24*5e3eaea3SApple OSS Distributions cleanup(void)
25*5e3eaea3SApple OSS Distributions {
26*5e3eaea3SApple OSS Distributions 	(void) remove(TEST_FILE1);
27*5e3eaea3SApple OSS Distributions 	(void) remove(TEST_FILE1_UC);
28*5e3eaea3SApple OSS Distributions 	(void) remove(TEST_FILE2);
29*5e3eaea3SApple OSS Distributions 	(void) remove(TEST_FILE3_HL);
30*5e3eaea3SApple OSS Distributions 	(void) rmdir(TEST_DIR);
31*5e3eaea3SApple OSS Distributions }
32*5e3eaea3SApple OSS Distributions 
33*5e3eaea3SApple OSS Distributions /*
34*5e3eaea3SApple OSS Distributions  * This unit-test validates the behavior of renamex_np() with RENAME_EXCL flag.
35*5e3eaea3SApple OSS Distributions  * On either a case-insensitve/case-sensitive volume:
36*5e3eaea3SApple OSS Distributions  * 1. rename from source to existing target should succeed when the change is
37*5e3eaea3SApple OSS Distributions  *    only case-variant (for e.g rename_dir/file1 -> rename_dir/FILE1)
38*5e3eaea3SApple OSS Distributions  * 2. rename from source to existing target should fail with EEXIST
39*5e3eaea3SApple OSS Distributions  * 3. rename from source to existing target which is a hardlink of the source
40*5e3eaea3SApple OSS Distributions  *    should fail with EEXIST
41*5e3eaea3SApple OSS Distributions  *
42*5e3eaea3SApple OSS Distributions  * On case-insensitive volume:
43*5e3eaea3SApple OSS Distributions  * 1. rename from source to itself should succeed
44*5e3eaea3SApple OSS Distributions  *    (rename_dir/file1 -> rename_dir/file1)
45*5e3eaea3SApple OSS Distributions  *
46*5e3eaea3SApple OSS Distributions  * On case-sensitive volume:
47*5e3eaea3SApple OSS Distributions  * 1. rename from source to itself should fail with EEXIST
48*5e3eaea3SApple OSS Distributions  *    (rename_dir/file1 -> rename_dir/file1)
49*5e3eaea3SApple OSS Distributions  */
50*5e3eaea3SApple OSS Distributions 
51*5e3eaea3SApple OSS Distributions T_DECL(rename_excl_with_case_variant,
52*5e3eaea3SApple OSS Distributions     "test renamex_np() with RENAME_EXCL flag for files with case variants")
53*5e3eaea3SApple OSS Distributions {
54*5e3eaea3SApple OSS Distributions 	const char *tmpdir = dt_tmpdir();
55*5e3eaea3SApple OSS Distributions 	long case_sensitive_vol;
56*5e3eaea3SApple OSS Distributions 	int err, saved_errno;
57*5e3eaea3SApple OSS Distributions 	int fd;
58*5e3eaea3SApple OSS Distributions 
59*5e3eaea3SApple OSS Distributions 	T_SETUPBEGIN;
60*5e3eaea3SApple OSS Distributions 
61*5e3eaea3SApple OSS Distributions 	atexit(cleanup);
62*5e3eaea3SApple OSS Distributions 
63*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_ZERO(chdir(tmpdir),
64*5e3eaea3SApple OSS Distributions 	    "Setup: changing to tmpdir: %s", tmpdir);
65*5e3eaea3SApple OSS Distributions 
66*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(mkdir(TEST_DIR, 0777),
67*5e3eaea3SApple OSS Distributions 	    "Setup: creating test dir: %s", TEST_DIR);
68*5e3eaea3SApple OSS Distributions 
69*5e3eaea3SApple OSS Distributions 	T_WITH_ERRNO;
70*5e3eaea3SApple OSS Distributions 	fd = open(TEST_FILE1, O_CREAT | O_RDWR, 0666);
71*5e3eaea3SApple OSS Distributions 	T_ASSERT_TRUE(fd != -1, "Creating test file1: %s", TEST_FILE1);
72*5e3eaea3SApple OSS Distributions 
73*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(fd), "Closing test file1: %s",
74*5e3eaea3SApple OSS Distributions 	    TEST_FILE1);
75*5e3eaea3SApple OSS Distributions 
76*5e3eaea3SApple OSS Distributions 	T_WITH_ERRNO;
77*5e3eaea3SApple OSS Distributions 	fd = open(TEST_FILE2, O_CREAT | O_RDWR, 0666);
78*5e3eaea3SApple OSS Distributions 	T_ASSERT_TRUE(fd != -1, "Creating test file2: %s", TEST_FILE2);
79*5e3eaea3SApple OSS Distributions 
80*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(fd), "Closing test file2: %s",
81*5e3eaea3SApple OSS Distributions 	    TEST_FILE2);
82*5e3eaea3SApple OSS Distributions 
83*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(link(TEST_FILE1, TEST_FILE3_HL),
84*5e3eaea3SApple OSS Distributions 	    "Creating hardlink for %s from source: %s",
85*5e3eaea3SApple OSS Distributions 	    TEST_FILE3_HL, TEST_FILE1);
86*5e3eaea3SApple OSS Distributions 
87*5e3eaea3SApple OSS Distributions 	case_sensitive_vol = pathconf(TEST_DIR, _PC_CASE_SENSITIVE);
88*5e3eaea3SApple OSS Distributions 	T_ASSERT_TRUE(case_sensitive_vol != -1,
89*5e3eaea3SApple OSS Distributions 	    "Checking if target volume is case-sensitive, is_case_sensitive: %ld",
90*5e3eaea3SApple OSS Distributions 	    case_sensitive_vol);
91*5e3eaea3SApple OSS Distributions 
92*5e3eaea3SApple OSS Distributions 	T_SETUPEND;
93*5e3eaea3SApple OSS Distributions 
94*5e3eaea3SApple OSS Distributions 	err = renamex_np(TEST_FILE1, TEST_FILE2, RENAME_EXCL);
95*5e3eaea3SApple OSS Distributions 	saved_errno = errno;
96*5e3eaea3SApple OSS Distributions 	T_ASSERT_TRUE((err == -1 && saved_errno == EEXIST),
97*5e3eaea3SApple OSS Distributions 	    "Renaming with RENAME_EXCL from source: %s to target: %s",
98*5e3eaea3SApple OSS Distributions 	    TEST_FILE1, TEST_FILE2);
99*5e3eaea3SApple OSS Distributions 
100*5e3eaea3SApple OSS Distributions 	err = renamex_np(TEST_FILE1, TEST_FILE3_HL, RENAME_EXCL);
101*5e3eaea3SApple OSS Distributions 	saved_errno = errno;
102*5e3eaea3SApple OSS Distributions 	T_ASSERT_TRUE((err == -1 && saved_errno == EEXIST),
103*5e3eaea3SApple OSS Distributions 	    "Renaming with RENAME_EXCL from source: %s to hardlink target: %s",
104*5e3eaea3SApple OSS Distributions 	    TEST_FILE1, TEST_FILE3_HL);
105*5e3eaea3SApple OSS Distributions 
106*5e3eaea3SApple OSS Distributions 	if (case_sensitive_vol) {
107*5e3eaea3SApple OSS Distributions 		err = renamex_np(TEST_FILE1, TEST_FILE1, RENAME_EXCL);
108*5e3eaea3SApple OSS Distributions 		saved_errno = errno;
109*5e3eaea3SApple OSS Distributions 		T_ASSERT_TRUE((err == -1 && saved_errno == EEXIST),
110*5e3eaea3SApple OSS Distributions 		    "Renaming with RENAME_EXCL from source: %s to target: %s",
111*5e3eaea3SApple OSS Distributions 		    TEST_FILE1, TEST_FILE1);
112*5e3eaea3SApple OSS Distributions 	} else {
113*5e3eaea3SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(renamex_np(TEST_FILE1, TEST_FILE1, RENAME_EXCL),
114*5e3eaea3SApple OSS Distributions 		    "Renaming with RENAME_EXCL from source: %s to target: %s",
115*5e3eaea3SApple OSS Distributions 		    TEST_FILE1, TEST_FILE1);
116*5e3eaea3SApple OSS Distributions 	}
117*5e3eaea3SApple OSS Distributions 
118*5e3eaea3SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(renamex_np(TEST_FILE1, TEST_FILE1_UC, RENAME_EXCL),
119*5e3eaea3SApple OSS Distributions 	    "Renaming with RENAME_EXCL from source: %s to target: %s",
120*5e3eaea3SApple OSS Distributions 	    TEST_FILE1, TEST_FILE1_UC);
121*5e3eaea3SApple OSS Distributions }
122