xref: /xnu-12377.81.4/tests/vfs/pwrite_appendonly.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2025 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 pwrite_appendonly pwrite_appendonly.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 
40 T_GLOBAL_META(
41 	T_META_NAMESPACE("xnu.vfs"),
42 	T_META_RADAR_COMPONENT_NAME("xnu"),
43 	T_META_RADAR_COMPONENT_VERSION("vfs"),
44 	T_META_ASROOT(false),
45 	T_META_CHECK_LEAKS(false),
46 	T_META_TAG_VM_PREFERRED);
47 
48 #define TEST_DIR  "testdir"
49 #define TEST_FILE "testfile"
50 
51 static char g_testfile[MAXPATHLEN];
52 static char g_testdir[MAXPATHLEN];
53 
54 
55 static void
exit_cleanup(void)56 exit_cleanup(void)
57 {
58 	(void)remove(g_testfile);
59 	(void)rmdir(g_testdir);
60 }
61 
62 static void
create_test_file(void)63 create_test_file(void)
64 {
65 	const char *tmpdir = dt_tmpdir();
66 	int fd;
67 
68 	T_SETUPBEGIN;
69 
70 	atexit(exit_cleanup);
71 
72 	snprintf(g_testdir, MAXPATHLEN, "%s/%s", tmpdir, TEST_DIR);
73 
74 	T_ASSERT_POSIX_SUCCESS(mkdir(g_testdir, 0777),
75 	    "Setup: creating test dir: %s", g_testdir);
76 
77 	snprintf(g_testfile, MAXPATHLEN, "%s/%s", g_testdir, TEST_FILE);
78 
79 	T_WITH_ERRNO;
80 	fd = open(g_testfile, O_CREAT | O_RDWR, 0666);
81 	T_ASSERT_NE(fd, -1, "Create test fi1e: %s", g_testfile);
82 
83 	T_ASSERT_POSIX_SUCCESS(close(fd), "Close test file: %s", TEST_FILE);
84 
85 	T_SETUPEND;
86 }
87 
88 T_DECL(pwrite_append, "Validate pwrite to arbitrary offsets should fail on append-only file")
89 {
90 	char buf[32];
91 	ssize_t ret;
92 	int err, fd;
93 
94 	create_test_file();
95 
96 	T_WITH_ERRNO;
97 	err = chflags(g_testfile, UF_APPEND);
98 	T_ASSERT_NE(err, -1, "Enable append-only on file: %s", g_testfile);
99 
100 	T_WITH_ERRNO;
101 	fd = open(g_testfile, O_RDWR | O_APPEND);
102 	T_ASSERT_NE(fd, -1, "Open test fi1e in O_RDWR|O_APPEND: %s", g_testfile);
103 
104 	T_WITH_ERRNO;
105 	ret = pwrite(fd, &buf, 16, 8);
106 	T_ASSERT_TRUE((ret == -1) && (errno == EPERM), "Write on append-only file: %s", g_testfile);
107 
108 	T_WITH_ERRNO;
109 	err = ftruncate(fd, 100);
110 	T_ASSERT_TRUE((err == -1) && (errno == EPERM), "Truncate on append-only file: %s", g_testfile);
111 
112 	T_WITH_ERRNO;
113 	err = chflags(g_testfile, 0);
114 	T_ASSERT_NE(err, -1, "Disable append-only on file: %s", g_testfile);
115 
116 	T_WITH_ERRNO;
117 	ret = pwrite(fd, &buf, 16, 8);
118 	T_ASSERT_EQ(ret, 16, "Write on file: %s", g_testfile);
119 
120 	T_WITH_ERRNO;
121 	err = ftruncate(fd, 100);
122 	T_ASSERT_NE(err, -1, "Truncate on file: %s", g_testfile);
123 }
124