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