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