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>
32 #include <netinet/in.h>
43 * Simple regression test for sendfile. Creates a file sized at three pages
44 * and then proceeds to send it over a series of sockets, exercising a number
45 * of cases and performing limited validation.
48 #define TEST_PORT 5678
49 #define TEST_MAGIC 0x4440f7bb
51 #define TEST_SECONDS 30
55 u_int32_t th_header_length;
60 pid_t child_pid, parent_pid;
65 test_th(struct test_header *th, u_int32_t *header_length, u_int32_t *offset,
69 if (th->th_magic != htonl(TEST_MAGIC))
71 *header_length = ntohl(th->th_header_length);
72 *offset = ntohl(th->th_offset);
73 *length = ntohl(th->th_length);
78 signal_alarm(int signum)
84 setup_alarm(int seconds)
87 signal(SIGALRM, signal_alarm);
96 signal(SIGALRM, SIG_DFL);
100 receive_test(int accept_socket)
102 u_int32_t header_length, offset, length, counter;
103 struct test_header th;
107 len = read(accept_socket, &th, sizeof(th));
110 if (len < sizeof(th))
111 errx(-1, "read: %d", len);
113 if (test_th(&th, &header_length, &offset, &length) == 0)
114 errx(-1, "test_th: bad");
118 len = read(accept_socket, &ch, sizeof(ch));
124 /* XXXRW: Validate byte here. */
126 if (counter != header_length + length)
127 errx(-1, "receive_test: expected (%d, %d) received %d",
128 header_length, length, counter);
137 accept_socket = accept(listen_socket, NULL, NULL);
138 setup_alarm(TEST_SECONDS);
139 receive_test(accept_socket);
141 close(accept_socket);
146 new_test_socket(void)
148 struct sockaddr_in sin;
151 connect_socket = socket(PF_INET, SOCK_STREAM, 0);
152 if (connect_socket < 0)
155 bzero(&sin, sizeof(sin));
156 sin.sin_len = sizeof(sin);
157 sin.sin_family = AF_INET;
158 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
159 sin.sin_port = htons(TEST_PORT);
161 if (connect(connect_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0)
164 return (connect_socket);
168 init_th(struct test_header *th, u_int32_t header_length, u_int32_t offset,
172 bzero(th, sizeof(*th));
173 th->th_magic = htonl(TEST_MAGIC);
174 th->th_header_length = htonl(header_length);
175 th->th_offset = htonl(offset);
176 th->th_length = htonl(length);
180 send_test(int connect_socket, u_int32_t header_length, u_int32_t offset,
183 struct test_header th;
184 struct sf_hdtr hdtr, *hdtrp;
185 struct iovec headers;
190 len = lseek(file_fd, 0, SEEK_SET);
194 errx(-1, "lseek: %d", len);
196 init_th(&th, header_length, offset, length);
198 len = write(connect_socket, &th, sizeof(th));
201 if (len != sizeof(th))
202 err(-1, "send: %d", len);
204 if (header_length != 0) {
205 header = malloc(header_length);
209 bzero(&headers, sizeof(headers));
210 headers.iov_base = header;
211 headers.iov_len = header_length;
212 bzero(&hdtr, sizeof(hdtr));
213 hdtr.headers = &headers;
215 hdtr.trailers = NULL;
222 if (sendfile(file_fd, connect_socket, offset, length, hdtrp, &off,
227 errx(-1, "sendfile: off %llu", off);
238 connect_socket = new_test_socket();
239 send_test(connect_socket, 0, 0, 1);
240 close(connect_socket);
244 connect_socket = new_test_socket();
245 send_test(connect_socket, 0, 0, getpagesize());
246 close(connect_socket);
250 connect_socket = new_test_socket();
251 send_test(connect_socket, 0, 1, 1);
252 close(connect_socket);
256 connect_socket = new_test_socket();
257 send_test(connect_socket, 0, 1, getpagesize());
258 close(connect_socket);
262 connect_socket = new_test_socket();
263 send_test(connect_socket, 0, getpagesize(), getpagesize());
264 close(connect_socket);
268 (void)kill(child_pid, SIGKILL);
272 main(int argc, char *argv[])
274 char path[PATH_MAX], *page_buffer;
275 struct sockaddr_in sin;
279 pagesize = getpagesize();
280 page_buffer = malloc(TEST_PAGES * pagesize);
281 if (page_buffer == NULL)
283 bzero(page_buffer, TEST_PAGES * pagesize);
285 listen_socket = socket(PF_INET, SOCK_STREAM, 0);
286 if (listen_socket < 0)
289 bzero(&sin, sizeof(sin));
290 sin.sin_len = sizeof(sin);
291 sin.sin_family = AF_INET;
292 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
293 sin.sin_port = htons(TEST_PORT);
295 snprintf(path, PATH_MAX, "/tmp/sendfile.XXXXXXXXXXXX");
296 file_fd = mkstemp(path);
299 len = write(file_fd, page_buffer, TEST_PAGES * pagesize);
303 len = lseek(file_fd, 0, SEEK_SET);
307 errx(-1, "lseek: %d", len);
309 if (bind(listen_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0)
312 if (listen(listen_socket, -1) < 0)
315 parent_pid = getpid();
319 if (child_pid == 0) {
320 child_pid = getpid();