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