xref: /xnu-12377.41.6/tests/pwrite.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1*bbb1b6f9SApple OSS Distributions /*
2*bbb1b6f9SApple OSS Distributions  * Copyright (c) 2020 Apple Inc. All rights reserved.
3*bbb1b6f9SApple OSS Distributions  *
4*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*bbb1b6f9SApple OSS Distributions  *
6*bbb1b6f9SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*bbb1b6f9SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*bbb1b6f9SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*bbb1b6f9SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*bbb1b6f9SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*bbb1b6f9SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*bbb1b6f9SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*bbb1b6f9SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*bbb1b6f9SApple OSS Distributions  *
15*bbb1b6f9SApple OSS Distributions  * Please obtain a copy of the License at
16*bbb1b6f9SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*bbb1b6f9SApple OSS Distributions  *
18*bbb1b6f9SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*bbb1b6f9SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*bbb1b6f9SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*bbb1b6f9SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*bbb1b6f9SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*bbb1b6f9SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*bbb1b6f9SApple OSS Distributions  * limitations under the License.
25*bbb1b6f9SApple OSS Distributions  *
26*bbb1b6f9SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*bbb1b6f9SApple OSS Distributions  */
28*bbb1b6f9SApple OSS Distributions 
29*bbb1b6f9SApple OSS Distributions #include <darwintest.h>
30*bbb1b6f9SApple OSS Distributions #include <fcntl.h>
31*bbb1b6f9SApple OSS Distributions #include <unistd.h>
32*bbb1b6f9SApple OSS Distributions #include <stdlib.h>
33*bbb1b6f9SApple OSS Distributions #include <sys/event.h>
34*bbb1b6f9SApple OSS Distributions #include <sys/mman.h>
35*bbb1b6f9SApple OSS Distributions #include <sys/socket.h>
36*bbb1b6f9SApple OSS Distributions 
37*bbb1b6f9SApple OSS Distributions T_GLOBAL_META(
38*bbb1b6f9SApple OSS Distributions 	T_META_NAMESPACE("xnu.syscall.pwrite"),
39*bbb1b6f9SApple OSS Distributions 	T_META_RUN_CONCURRENTLY(true)
40*bbb1b6f9SApple OSS Distributions 	);
41*bbb1b6f9SApple OSS Distributions 
42*bbb1b6f9SApple OSS Distributions static void
test_file_equals(int fd,const char * expected_buffer,size_t size)43*bbb1b6f9SApple OSS Distributions test_file_equals(int fd, const char* expected_buffer, size_t size)
44*bbb1b6f9SApple OSS Distributions {
45*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(lseek(fd, 0, SEEK_SET), "Reset file offset");
46*bbb1b6f9SApple OSS Distributions 	char* read_buffer = malloc(size);
47*bbb1b6f9SApple OSS Distributions 	T_ASSERT_TRUE(read_buffer != 0, "Allocated read_buffer");
48*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(read(fd, read_buffer, size), (ssize_t)size, "Read expected buffer");
49*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(strncmp(read_buffer, expected_buffer, size), 0, "Buffer as expected");
50*bbb1b6f9SApple OSS Distributions 	free(read_buffer);
51*bbb1b6f9SApple OSS Distributions }
52*bbb1b6f9SApple OSS Distributions 
53*bbb1b6f9SApple OSS Distributions T_DECL(pwrite_regular_file,
54*bbb1b6f9SApple OSS Distributions     "test pwrite() on a regular file.") {
55*bbb1b6f9SApple OSS Distributions 	char scratchfile_path[] = "/tmp/scratch.XXXXXX";
56*bbb1b6f9SApple OSS Distributions 	int fd = mkstemp(scratchfile_path);
57*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "created temporary file");
58*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(unlink(scratchfile_path), "unlinked temporary file");
59*bbb1b6f9SApple OSS Distributions 
60*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pwrite(fd, "a", 1, 1), "pwrite 'a' at offset 1");
61*bbb1b6f9SApple OSS Distributions 	test_file_equals(fd, "\0a", 2);
62*bbb1b6f9SApple OSS Distributions 
63*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pwrite(fd, "bcd", 3, 0), "pwrite 'bcd' at offset 0");
64*bbb1b6f9SApple OSS Distributions 	test_file_equals(fd, "bcd", 3);
65*bbb1b6f9SApple OSS Distributions }
66*bbb1b6f9SApple OSS Distributions 
67*bbb1b6f9SApple OSS Distributions static void
test_pwrite_should_fail(int fd,int expected_errno)68*bbb1b6f9SApple OSS Distributions test_pwrite_should_fail(int fd, int expected_errno)
69*bbb1b6f9SApple OSS Distributions {
70*bbb1b6f9SApple OSS Distributions 	char input_buffer = 'A';
71*bbb1b6f9SApple OSS Distributions 	ssize_t pwrite_result = pwrite(fd, &input_buffer, 1, 0);
72*bbb1b6f9SApple OSS Distributions 	int err = errno;
73*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(pwrite_result, (ssize_t)-1, "pwrite offset 0 size 1 returns -1");
74*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(err, expected_errno, "errno is as expected");
75*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(input_buffer, 'A', "input buffer is unchanged");
76*bbb1b6f9SApple OSS Distributions 
77*bbb1b6f9SApple OSS Distributions 	pwrite_result = pwrite(fd, &input_buffer, 1, 1);
78*bbb1b6f9SApple OSS Distributions 	err = errno;
79*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(pwrite_result, (ssize_t)-1, "pwrite offset 1 size 1 returns -1");
80*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(err, expected_errno, "errno is as expected");
81*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(input_buffer, 'A', "input buffer is unchanged");
82*bbb1b6f9SApple OSS Distributions 
83*bbb1b6f9SApple OSS Distributions 	pwrite_result = pwrite(fd, &input_buffer, 0, 0);
84*bbb1b6f9SApple OSS Distributions 	err = errno;
85*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(pwrite_result, (ssize_t)-1, "pwrite offset 0 size 0 returns -1");
86*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(err, expected_errno, "errno is as expected");
87*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(input_buffer, 'A', "input buffer is unchanged");
88*bbb1b6f9SApple OSS Distributions 
89*bbb1b6f9SApple OSS Distributions 	pwrite_result = pwrite(fd, &input_buffer, 0, 1);
90*bbb1b6f9SApple OSS Distributions 	err = errno;
91*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(pwrite_result, (ssize_t)-1, "pwrite offset 1 size 0 returns -1");
92*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(err, expected_errno, "errno is as expected");
93*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(input_buffer, 'A', "input buffer is unchanged");
94*bbb1b6f9SApple OSS Distributions }
95*bbb1b6f9SApple OSS Distributions 
96*bbb1b6f9SApple OSS Distributions T_DECL(pwrite_socket,
97*bbb1b6f9SApple OSS Distributions     "test pwrite() on a socket.") {
98*bbb1b6f9SApple OSS Distributions 	int sockets[2];
99*bbb1b6f9SApple OSS Distributions 	int result = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
100*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(result, "Created socket pair");
101*bbb1b6f9SApple OSS Distributions 
102*bbb1b6f9SApple OSS Distributions 	test_pwrite_should_fail(sockets[0], ESPIPE);
103*bbb1b6f9SApple OSS Distributions 	test_pwrite_should_fail(sockets[1], ESPIPE);
104*bbb1b6f9SApple OSS Distributions 
105*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(sockets[0]), "Closed socket 0");
106*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(sockets[1]), "Closed socket 1");
107*bbb1b6f9SApple OSS Distributions }
108*bbb1b6f9SApple OSS Distributions 
109*bbb1b6f9SApple OSS Distributions T_DECL(pwrite_unix_shared_memory,
110*bbb1b6f9SApple OSS Distributions     "test pwrite() on unix shared memory.") {
111*bbb1b6f9SApple OSS Distributions 	const char* memory_path = "test_pwrite_unix_shared_memory";
112*bbb1b6f9SApple OSS Distributions 	int shm_fd = shm_open(memory_path, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
113*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(shm_fd, "Created shared memory");
114*bbb1b6f9SApple OSS Distributions 
115*bbb1b6f9SApple OSS Distributions 	test_pwrite_should_fail(shm_fd, ESPIPE);
116*bbb1b6f9SApple OSS Distributions 
117*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(shm_fd), "Closed shm fd");
118*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(shm_unlink(memory_path), "Unlinked");
119*bbb1b6f9SApple OSS Distributions }
120*bbb1b6f9SApple OSS Distributions 
121*bbb1b6f9SApple OSS Distributions T_DECL(pwrite_kqueue,
122*bbb1b6f9SApple OSS Distributions     "test pwrite() on kqueue.") {
123*bbb1b6f9SApple OSS Distributions 	int queue = kqueue();
124*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(queue, "Got kqueue");
125*bbb1b6f9SApple OSS Distributions 
126*bbb1b6f9SApple OSS Distributions 	test_pwrite_should_fail(queue, ESPIPE);
127*bbb1b6f9SApple OSS Distributions 
128*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(queue), "Close queue");
129*bbb1b6f9SApple OSS Distributions }
130*bbb1b6f9SApple OSS Distributions 
131*bbb1b6f9SApple OSS Distributions T_DECL(pwrite_pipe,
132*bbb1b6f9SApple OSS Distributions     "test pwrite() on pipe.") {
133*bbb1b6f9SApple OSS Distributions 	int pipe_fds[2];
134*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pipe(pipe_fds), "Created pipe");
135*bbb1b6f9SApple OSS Distributions 
136*bbb1b6f9SApple OSS Distributions 	test_pwrite_should_fail(pipe_fds[0], EBADF);
137*bbb1b6f9SApple OSS Distributions 	test_pwrite_should_fail(pipe_fds[1], ESPIPE);
138*bbb1b6f9SApple OSS Distributions 
139*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(pipe_fds[1]), "Close write pipe");
140*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(pipe_fds[0]), "Close read pipe");
141*bbb1b6f9SApple OSS Distributions }
142*bbb1b6f9SApple OSS Distributions 
143*bbb1b6f9SApple OSS Distributions T_DECL(pwrite_write_to_dev_null,
144*bbb1b6f9SApple OSS Distributions     "test pwrite() from null.") {
145*bbb1b6f9SApple OSS Distributions 	int fd = open("/dev/null", O_RDONLY);
146*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(fd, "Opened /dev/null");
147*bbb1b6f9SApple OSS Distributions 
148*bbb1b6f9SApple OSS Distributions 	char buffer = 'A';
149*bbb1b6f9SApple OSS Distributions 	errno = 0;
150*bbb1b6f9SApple OSS Distributions 	ssize_t pwrite_result = pwrite(fd, &buffer, 1, 0);
151*bbb1b6f9SApple OSS Distributions 	int err = errno;
152*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(pwrite_result, (ssize_t)-1, "pwrite offset 0 size 1 returns -1");
153*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(err, EBADF, "pwrite on /dev/null results in EBADF");
154*bbb1b6f9SApple OSS Distributions 
155*bbb1b6f9SApple OSS Distributions 	errno = 0;
156*bbb1b6f9SApple OSS Distributions 	pwrite_result = pwrite(fd, &buffer, 1, 1);
157*bbb1b6f9SApple OSS Distributions 	err = errno;
158*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(pwrite_result, (ssize_t)-1, "pwrite offset 1 size 1 returns -1");
159*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(buffer, 'A', "input buffer is unchanged");
160*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(err, EBADF, "pwrite on /dev/null results in EBADF");
161*bbb1b6f9SApple OSS Distributions 
162*bbb1b6f9SApple OSS Distributions 	errno = 0;
163*bbb1b6f9SApple OSS Distributions 	pwrite_result = pwrite(fd, &buffer, 0, 0);
164*bbb1b6f9SApple OSS Distributions 	err = errno;
165*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(pwrite_result, (ssize_t)-1, "pwrite offset 0 size 0 returns -1");
166*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(err, EBADF, "pwrite on /dev/null results in EBADF");
167*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(buffer, 'A', "input buffer is unchanged");
168*bbb1b6f9SApple OSS Distributions 
169*bbb1b6f9SApple OSS Distributions 	errno = 0;
170*bbb1b6f9SApple OSS Distributions 	pwrite_result = pwrite(fd, &buffer, 0, 1);
171*bbb1b6f9SApple OSS Distributions 	err = errno;
172*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(pwrite_result, (ssize_t)-1, "pwrite offset 1 size 0 returns -1");
173*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(err, EBADF, "pwrite on /dev/null results in EBADF");
174*bbb1b6f9SApple OSS Distributions 	T_ASSERT_EQ(buffer, 'A', "input buffer is unchanged");
175*bbb1b6f9SApple OSS Distributions 
176*bbb1b6f9SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(fd), "Closed /dev/null");
177*bbb1b6f9SApple OSS Distributions }
178