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