xref: /xnu-12377.41.6/tests/vfs/linkat_flags.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2024 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 /* compile: xcrun -sdk macosx.internal clang -ldarwintest -o linkat_flags linkat_flags.c -g -Weverything */
30 
31 #include <fcntl.h>
32 #include <unistd.h>
33 #include <sys/stat.h>
34 #include <sys/syslimits.h>
35 
36 #include <darwintest.h>
37 #include <darwintest/utils.h>
38 
39 static char template[MAXPATHLEN];
40 static char *testdir = NULL;
41 static char file[PATH_MAX], sym[PATH_MAX], symloop[PATH_MAX], dirloop[PATH_MAX];
42 static char lfile1[PATH_MAX], lfile2[PATH_MAX], lfile3[PATH_MAX];
43 static char lfile4[PATH_MAX], lfile5[PATH_MAX], lfile6[PATH_MAX];
44 static char lfile7[PATH_MAX], lfile8[PATH_MAX];
45 
46 T_GLOBAL_META(
47 	T_META_NAMESPACE("xnu.vfs"),
48 	T_META_RADAR_COMPONENT_NAME("xnu"),
49 	T_META_RADAR_COMPONENT_VERSION("vfs"),
50 	T_META_ASROOT(false),
51 	T_META_CHECK_LEAKS(false));
52 
53 static void
cleanup(void)54 cleanup(void)
55 {
56 	if (lfile8[0] != '\0') {
57 		unlink(lfile8);
58 	}
59 	if (lfile7[0] != '\0') {
60 		unlink(lfile7);
61 	}
62 	if (lfile6[0] != '\0') {
63 		unlink(lfile6);
64 	}
65 	if (lfile5[0] != '\0') {
66 		unlink(lfile5);
67 	}
68 	if (lfile4[0] != '\0') {
69 		unlink(lfile4);
70 	}
71 	if (lfile3[0] != '\0') {
72 		unlink(lfile3);
73 	}
74 	if (lfile2[0] != '\0') {
75 		unlink(lfile2);
76 	}
77 	if (lfile1[0] != '\0') {
78 		unlink(lfile1);
79 	}
80 	if (dirloop[0] != '\0') {
81 		unlink(dirloop);
82 	}
83 	if (sym[0] != '\0') {
84 		unlink(sym);
85 	}
86 	if (file[0] != '\0') {
87 		unlink(file);
88 	}
89 	if (testdir) {
90 		rmdir(testdir);
91 	}
92 }
93 
94 static void
verify_stat(nlink_t file_nlink,nlink_t sym_nlink)95 verify_stat(nlink_t file_nlink, nlink_t sym_nlink)
96 {
97 	int error;
98 	struct stat buf;
99 
100 	/* Verify file's status */
101 	memset(&buf, 0, sizeof(buf));
102 	error = fstatat(AT_FDCWD, file, &buf, 0);
103 	if (error) {
104 		T_ASSERT_FAIL("Calling fstatat for the file failed with %s", strerror(errno));
105 	}
106 	T_ASSERT_EQ(file_nlink, buf.st_nlink, "Validating file's nlink count of %d", file_nlink);
107 
108 	/* Verify symlink's status */
109 	memset(&buf, 0, sizeof(buf));
110 	error = fstatat(AT_FDCWD, sym, &buf, AT_SYMLINK_NOFOLLOW);
111 	if (error) {
112 		T_ASSERT_FAIL("Calling fstatat for the symlink failed with %s", strerror(errno));
113 	}
114 	T_ASSERT_EQ(sym_nlink, buf.st_nlink, "Validating symlink's nlink count of %d", sym_nlink);
115 }
116 
117 T_DECL(linkat_flags,
118     "Test linkat's AT_SYMLINK_FOLLOW and AT_SYMLINK_NOFOLLOW_ANY flags")
119 {
120 	int fd;
121 	char testdir_path[MAXPATHLEN + 1];
122 
123 	file[0] = sym[0] = dirloop[0] = '\0';
124 	lfile1[0] = lfile2[0] = lfile3[0] = '\0';
125 	lfile4[0] = lfile5[0] = lfile6[0] = '\0';
126 	lfile7[0] = lfile8[0] = '\0';
127 
128 	T_ATEND(cleanup);
129 	T_SETUPBEGIN;
130 
131 	/* Create test root dir */
132 	snprintf(template, sizeof(template), "%s/linkat_flags-XXXXXX", dt_tmpdir());
133 	T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root dir");
134 
135 	/* Get testdir full path */
136 	T_ASSERT_POSIX_SUCCESS((fd = open(testdir, O_SEARCH, 0777)), "Opening the test root directory");
137 	T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_GETPATH, testdir_path), "Calling fcntl() to get the path");
138 	T_ASSERT_POSIX_SUCCESS(close(fd), "Closing %s", testdir);
139 
140 	/* Setup file names */
141 	snprintf(file, sizeof(file), "%s/%s", testdir_path, "file");
142 	snprintf(sym, sizeof(sym), "%s/%s", testdir_path, "sym");
143 	snprintf(dirloop, sizeof(dirloop), "%s/%s", testdir_path, "dirloop");
144 	snprintf(symloop, sizeof(symloop), "%s/%s", dirloop, "sym");
145 	snprintf(lfile1, sizeof(lfile1), "%s/%s", testdir_path, "lfile1");
146 	snprintf(lfile2, sizeof(lfile2), "%s/%s", testdir_path, "lfile2");
147 	snprintf(lfile3, sizeof(lfile3), "%s/%s", testdir_path, "lfile3");
148 	snprintf(lfile4, sizeof(lfile4), "%s/%s", testdir_path, "lfile4");
149 	snprintf(lfile5, sizeof(lfile5), "%s/%s", testdir_path, "lfile5");
150 	snprintf(lfile6, sizeof(lfile6), "%s/%s", testdir_path, "lfile6");
151 	snprintf(lfile7, sizeof(lfile7), "%s/%s", testdir_path, "lfile7");
152 	snprintf(lfile8, sizeof(lfile8), "%s/%s", testdir_path, "lfile8");
153 
154 	/* Create the test files */
155 	T_ASSERT_POSIX_SUCCESS((fd = open(file, O_CREAT | O_RDWR, 0777)), "Creating %s", file);
156 	T_ASSERT_POSIX_SUCCESS(symlink(file, sym), "Creating symbolic link %s ---> %s", sym, file);
157 	T_ASSERT_POSIX_SUCCESS(symlink(testdir_path, dirloop), "Creating symbolic link %s ---> %s", dirloop, testdir_path);
158 
159 	/* Validating nlink count */
160 	verify_stat(1, 1);
161 
162 	/* Close the open files */
163 	T_ASSERT_POSIX_SUCCESS(close(fd), "Closing %s", file);
164 
165 	T_SETUPEND;
166 
167 	T_LOG("Testing linkat() using no flags");
168 	{
169 		T_ASSERT_POSIX_SUCCESS(linkat(AT_FDCWD, file, AT_FDCWD, lfile1, 0), "Calling linkat() while name1 is a file");
170 		verify_stat(2, 1);
171 
172 		T_ASSERT_POSIX_SUCCESS(linkat(AT_FDCWD, sym, AT_FDCWD, lfile2, 0), "Calling linkat() while name1 is a symbolic link");
173 		verify_stat(2, 2);
174 
175 		T_ASSERT_POSIX_SUCCESS(linkat(AT_FDCWD, symloop, AT_FDCWD, lfile3, 0), "Calling linkat() while name1 is a symbolic link and it's path contains a symbolic");
176 		verify_stat(2, 3);
177 	}
178 
179 	T_LOG("Testing linkat() using the AT_SYMLINK_FOLLOW flag");
180 	{
181 		T_ASSERT_POSIX_SUCCESS(linkat(AT_FDCWD, file, AT_FDCWD, lfile4, AT_SYMLINK_FOLLOW), "Calling linkat() while name1 is a file");
182 		verify_stat(3, 3);
183 
184 		T_ASSERT_POSIX_SUCCESS(linkat(AT_FDCWD, sym, AT_FDCWD, lfile5, AT_SYMLINK_FOLLOW), "Calling linkat() while name1 is a symbolic link");
185 		verify_stat(4, 3);
186 
187 		T_ASSERT_POSIX_SUCCESS(linkat(AT_FDCWD, symloop, AT_FDCWD, lfile6, AT_SYMLINK_FOLLOW), "Calling linkat() while name1 is a symbolic link and it's path contains a symbolic");
188 		verify_stat(5, 3);
189 	}
190 
191 	T_LOG("Testing linkat() using the AT_SYMLINK_NOFOLLOW_ANY flag");
192 	{
193 		T_ASSERT_POSIX_SUCCESS(linkat(AT_FDCWD, file, AT_FDCWD, lfile7, AT_SYMLINK_NOFOLLOW_ANY), "Calling linkat() while name1 is a file %s", file);
194 		verify_stat(6, 3);
195 
196 		T_ASSERT_POSIX_SUCCESS(linkat(AT_FDCWD, sym, AT_FDCWD, lfile8, AT_SYMLINK_NOFOLLOW_ANY), "Calling linkat() while name1 is a symbolic link");
197 		verify_stat(6, 4);
198 
199 		T_ASSERT_POSIX_FAILURE(linkat(AT_FDCWD, symloop, AT_FDCWD, "invalid_path", AT_SYMLINK_NOFOLLOW_ANY), ELOOP, "Calling linkat() while name1 is a symbolic link and it's path contains a symbolic");
200 	}
201 
202 	/* See resolve_beneath.c for the AT_RESOLVE_BENEATH flag tests */
203 
204 	/* See open_unique.c for the AT_UNIQUE flag tests */
205 }
206