2 * Copyright (c) 2006 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/socket.h>
33 #include <netinet/in.h>
45 * Simple regression test for sendfile. Creates a file sized at four pages
46 * and then proceeds to send it over a series of sockets, exercising a number
47 * of cases and performing limited validation.
50 #define TEST_PORT 5678
51 #define TEST_MAGIC 0x4440f7bb
53 #define TEST_SECONDS 30
57 u_int32_t th_header_length;
62 pid_t child_pid, parent_pid;
67 test_th(struct test_header *th, u_int32_t *header_length, u_int32_t *offset,
71 if (th->th_magic != htonl(TEST_MAGIC))
73 *header_length = ntohl(th->th_header_length);
74 *offset = ntohl(th->th_offset);
75 *length = ntohl(th->th_length);
80 signal_alarm(int signum)
87 setup_alarm(int seconds)
90 signal(SIGALRM, signal_alarm);
99 signal(SIGALRM, SIG_DFL);
103 receive_test(int accept_socket)
105 u_int32_t header_length, offset, length, counter;
106 struct test_header th;
110 len = read(accept_socket, &th, sizeof(th));
113 if ((size_t)len < sizeof(th))
114 errx(1, "read: %zd", len);
116 if (test_th(&th, &header_length, &offset, &length) == 0)
117 errx(1, "test_th: bad");
121 len = read(accept_socket, &ch, sizeof(ch));
127 /* XXXRW: Validate byte here. */
129 if (counter != header_length + length)
130 errx(1, "receive_test: expected (%d, %d) received %d",
131 header_length, length, counter);
140 accept_socket = accept(listen_socket, NULL, NULL);
141 setup_alarm(TEST_SECONDS);
142 receive_test(accept_socket);
144 close(accept_socket);
149 new_test_socket(void)
151 struct sockaddr_in sin;
154 connect_socket = socket(PF_INET, SOCK_STREAM, 0);
155 if (connect_socket < 0)
158 bzero(&sin, sizeof(sin));
159 sin.sin_len = sizeof(sin);
160 sin.sin_family = AF_INET;
161 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
162 sin.sin_port = htons(TEST_PORT);
164 if (connect(connect_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0)
167 return (connect_socket);
171 init_th(struct test_header *th, u_int32_t header_length, u_int32_t offset,
175 bzero(th, sizeof(*th));
176 th->th_magic = htonl(TEST_MAGIC);
177 th->th_header_length = htonl(header_length);
178 th->th_offset = htonl(offset);
179 th->th_length = htonl(length);
183 send_test(int connect_socket, u_int32_t header_length, u_int32_t offset,
186 struct test_header th;
187 struct sf_hdtr hdtr, *hdtrp;
188 struct iovec headers;
193 len = lseek(file_fd, 0, SEEK_SET);
197 errx(1, "lseek: %zd", len);
199 init_th(&th, header_length, offset, length);
201 len = write(connect_socket, &th, sizeof(th));
204 if (len != sizeof(th))
205 err(1, "send: %zd", len);
207 if (header_length != 0) {
208 header = malloc(header_length);
212 bzero(&headers, sizeof(headers));
213 headers.iov_base = header;
214 headers.iov_len = header_length;
215 bzero(&hdtr, sizeof(hdtr));
216 hdtr.headers = &headers;
218 hdtr.trailers = NULL;
225 if (sendfile(file_fd, connect_socket, offset, length, hdtrp, &off,
232 if (fstat(file_fd, &sb) < 0)
234 length = sb.st_size - offset;
238 errx(1, "sendfile: off(%ju) != length(%ju)",
239 (uintmax_t)off, (uintmax_t)length);
251 connect_socket = new_test_socket();
252 send_test(connect_socket, 0, 0, 1);
253 close(connect_socket);
257 connect_socket = new_test_socket();
258 send_test(connect_socket, 0, 0, getpagesize());
259 close(connect_socket);
263 connect_socket = new_test_socket();
264 send_test(connect_socket, 0, 1, 1);
265 close(connect_socket);
269 connect_socket = new_test_socket();
270 send_test(connect_socket, 0, 1, getpagesize());
271 close(connect_socket);
275 connect_socket = new_test_socket();
276 send_test(connect_socket, 0, getpagesize(), getpagesize());
277 close(connect_socket);
281 connect_socket = new_test_socket();
282 send_test(connect_socket, 0, 0, 2 * getpagesize());
283 close(connect_socket);
287 connect_socket = new_test_socket();
288 send_test(connect_socket, 0, 0, 0);
289 close(connect_socket);
293 connect_socket = new_test_socket();
294 send_test(connect_socket, 0, getpagesize(), 0);
295 close(connect_socket);
299 connect_socket = new_test_socket();
300 send_test(connect_socket, 0, 2 * getpagesize(), 0);
301 close(connect_socket);
305 connect_socket = new_test_socket();
306 send_test(connect_socket, 0, TEST_PAGES * getpagesize(), 0);
307 close(connect_socket);
311 (void)kill(child_pid, SIGKILL);
317 char path[PATH_MAX], *page_buffer;
318 struct sockaddr_in sin;
322 pagesize = getpagesize();
323 page_buffer = malloc(TEST_PAGES * pagesize);
324 if (page_buffer == NULL)
326 bzero(page_buffer, TEST_PAGES * pagesize);
328 listen_socket = socket(PF_INET, SOCK_STREAM, 0);
329 if (listen_socket < 0)
332 bzero(&sin, sizeof(sin));
333 sin.sin_len = sizeof(sin);
334 sin.sin_family = AF_INET;
335 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
336 sin.sin_port = htons(TEST_PORT);
338 snprintf(path, PATH_MAX, "/tmp/sendfile.XXXXXXXXXXXX");
339 file_fd = mkstemp(path);
342 len = write(file_fd, page_buffer, TEST_PAGES * pagesize);
346 len = lseek(file_fd, 0, SEEK_SET);
350 errx(1, "lseek: %zd", len);
352 if (bind(listen_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0)
355 if (listen(listen_socket, -1) < 0)
358 parent_pid = getpid();
362 if (child_pid == 0) {
363 child_pid = getpid();