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