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 open_symlink open_symlink.c -g -Weverything */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/stat.h>
37 #include <sys/param.h>
38 #include <sys/fsgetpath_private.h>
39
40 #include <darwintest.h>
41 #include <darwintest/utils.h>
42
43 static char template[MAXPATHLEN];
44 static char *testdir = NULL;
45 static char file[PATH_MAX], sym[PATH_MAX];
46
47 T_GLOBAL_META(
48 T_META_NAMESPACE("xnu.vfs"),
49 T_META_RADAR_COMPONENT_NAME("xnu"),
50 T_META_RADAR_COMPONENT_VERSION("vfs"),
51 T_META_ASROOT(false),
52 T_META_CHECK_LEAKS(false));
53
54 static void
cleanup(void)55 cleanup(void)
56 {
57 if (sym[0] != '\0') {
58 unlink(sym);
59 }
60 if (file[0] != '\0') {
61 unlink(file);
62 }
63 if (testdir) {
64 rmdir(testdir);
65 }
66 }
67
68 T_DECL(open_symlink,
69 "Verify that O_SYMLINK is not being ignored while used by open() in addition to O_CREAT")
70 {
71 int fd;
72 char namebuf[MAXPATHLEN + 1];
73 char namebuf2[MAXPATHLEN + 1];
74
75 file[0] = sym[0] = '\0';
76
77 T_ATEND(cleanup);
78 T_SETUPBEGIN;
79
80 /* Create test root dir */
81 snprintf(template, sizeof(template), "%s/open_symlink-XXXXXX", dt_tmpdir());
82 T_ASSERT_POSIX_NOTNULL((testdir = mkdtemp(template)), "Creating test root dir");
83
84 /* Setup file names */
85 snprintf(file, sizeof(file), "%s/%s", testdir, "file");
86 snprintf(sym, sizeof(sym), "%s/%s", testdir, "symlink");
87
88 /* Create the test file */
89 T_ASSERT_POSIX_SUCCESS((fd = open(file, O_CREAT | O_RDWR, 0777)), "Creating file %s", file);
90
91 /* Create the symlink */
92 T_ASSERT_POSIX_SUCCESS(symlink(file, sym), "Creating symlink %s -> %s", sym, file);
93
94 /* Close the test file */
95 T_ASSERT_POSIX_SUCCESS(close(fd), "Closing %s", file);
96
97 T_SETUPEND;
98
99 /* Step 1 - Verify O_SYMLINK behaviour */
100 T_ASSERT_POSIX_SUCCESS((fd = open(sym, O_SYMLINK, 0777)), "Opening %s using the O_SYMLINK flag", sym);
101 T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_GETPATH, namebuf), "Calling fcntl() to get the path");
102 T_ASSERT_POSIX_SUCCESS(close(fd), "Closing %s", sym);
103
104 /* Step 2 - Verify O_SYMLINK | O_CREAT behaviour */
105 T_ASSERT_POSIX_SUCCESS((fd = open(sym, O_SYMLINK | O_CREAT, 0777)), "Opening %s using the O_SYMLINK | O_CREAT flags", sym);
106 T_ASSERT_POSIX_SUCCESS(fcntl(fd, F_GETPATH, namebuf2), "Calling fcntl() to get the path");
107 T_ASSERT_POSIX_SUCCESS(close(fd), "Closing %s", sym);
108
109 /* Compare names */
110 T_ASSERT_EQ(strncmp(namebuf, namebuf2, strlen(namebuf)), 0, "Verifying %s was opened, got %s", namebuf, namebuf2);
111 }
112