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>
34 #include <netinet/in.h>
49 * Simple regression test for sendfile. Creates a file sized at four pages
50 * and then proceeds to send it over a series of sockets, exercising a number
51 * of cases and performing limited validation.
54 #define FAIL(msg) {printf("# %s\n", msg); \
57 #define FAIL_ERR(msg) {printf("# %s: %s\n", msg, strerror(errno)); \
60 #define TEST_PORT 5678
61 #define TEST_MAGIC 0x4440f7bb
63 #define TEST_SECONDS 30
67 uint32_t th_header_length;
73 struct sendfile_test {
81 static char path[PATH_MAX];
82 static int listen_socket;
83 static int accept_socket;
85 static int test_th(struct test_header *th, uint32_t *header_length,
86 uint32_t *offset, uint32_t *length);
87 static void signal_alarm(int signum);
88 static void setup_alarm(int seconds);
89 static void cancel_alarm(void);
90 static int receive_test(void);
91 static void run_child(void);
92 static int new_test_socket(int *connect_socket);
93 static void init_th(struct test_header *th, uint32_t header_length,
94 uint32_t offset, uint32_t length);
95 static int send_test(int connect_socket, struct sendfile_test);
96 static int write_test_file(size_t file_size);
97 static void run_parent(void);
98 static void cleanup(void);
102 test_th(struct test_header *th, uint32_t *header_length, uint32_t *offset,
106 if (th->th_magic != htonl(TEST_MAGIC))
107 FAIL("magic number not found in header")
108 *header_length = ntohl(th->th_header_length);
109 *offset = ntohl(th->th_offset);
110 *length = ntohl(th->th_length);
115 signal_alarm(int signum)
119 printf("# test timeout\n");
121 if (accept_socket > 0)
122 close(accept_socket);
123 if (listen_socket > 0)
124 close(listen_socket);
130 setup_alarm(int seconds)
132 struct itimerval itv;
133 bzero(&itv, sizeof(itv));
135 itv.it_value.tv_sec = seconds;
137 signal(SIGALRM, signal_alarm);
138 setitimer(ITIMER_REAL, &itv, NULL);
144 struct itimerval itv;
145 bzero(&itv, sizeof(itv));
146 setitimer(ITIMER_REAL, &itv, NULL);
152 uint32_t header_length, offset, length, counter;
153 struct test_header th;
159 len = read(accept_socket, &th, sizeof(th));
160 if (len < 0 || (size_t)len < sizeof(th))
163 if (test_th(&th, &header_length, &offset, &length) != 0)
170 len = read(accept_socket, buf, sizeof(buf));
171 if (len < 0 || len == 0)
174 MD5Update(&md5ctx, buf, len);
177 rxmd5 = MD5End(&md5ctx, NULL);
179 if ((counter != header_length+length) ||
180 memcmp(th.th_md5, rxmd5, 33) != 0)
181 FAIL("receive length mismatch")
190 struct sockaddr_in sin;
193 listen_socket = socket(PF_INET, SOCK_STREAM, 0);
194 if (listen_socket < 0) {
195 printf("# socket: %s\n", strerror(errno));
200 bzero(&sin, sizeof(sin));
201 sin.sin_len = sizeof(sin);
202 sin.sin_family = AF_INET;
203 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
204 sin.sin_port = htons(TEST_PORT);
206 if (bind(listen_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
207 printf("# bind: %s\n", strerror(errno));
212 if (!rc && listen(listen_socket, -1) < 0) {
213 printf("# listen: %s\n", strerror(errno));
218 accept_socket = accept(listen_socket, NULL, NULL);
219 setup_alarm(TEST_SECONDS);
220 if (receive_test() != 0)
225 if (accept_socket > 0)
226 close(accept_socket);
227 if (listen_socket > 0)
228 close(listen_socket);
234 new_test_socket(int *connect_socket)
236 struct sockaddr_in sin;
239 *connect_socket = socket(PF_INET, SOCK_STREAM, 0);
240 if (*connect_socket < 0)
243 bzero(&sin, sizeof(sin));
244 sin.sin_len = sizeof(sin);
245 sin.sin_family = AF_INET;
246 sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
247 sin.sin_port = htons(TEST_PORT);
249 if (connect(*connect_socket, (struct sockaddr *)&sin, sizeof(sin)) < 0)
256 init_th(struct test_header *th, uint32_t header_length, uint32_t offset,
259 bzero(th, sizeof(*th));
260 th->th_magic = htonl(TEST_MAGIC);
261 th->th_header_length = htonl(header_length);
262 th->th_offset = htonl(offset);
263 th->th_length = htonl(length);
265 MD5FileChunk(path, th->th_md5, offset, length);
269 send_test(int connect_socket, struct sendfile_test test)
271 struct test_header th;
272 struct sf_hdtr hdtr, *hdtrp;
273 struct iovec headers;
279 len = lseek(file_fd, 0, SEEK_SET);
284 if (fstat(file_fd, &st) < 0)
286 length = st.st_size - test.offset;
287 if (test.length > 0 && test.length < (uint32_t)length)
288 length = test.length;
290 init_th(&th, test.hdr_length, test.offset, length);
292 len = write(connect_socket, &th, sizeof(th));
293 if (len != sizeof(th))
296 if (test.hdr_length != 0) {
297 header = malloc(test.hdr_length);
302 bzero(&headers, sizeof(headers));
303 headers.iov_base = header;
304 headers.iov_len = test.hdr_length;
305 bzero(&hdtr, sizeof(hdtr));
306 hdtr.headers = &headers;
308 hdtr.trailers = NULL;
315 if (sendfile(file_fd, connect_socket, test.offset, test.length,
316 hdtrp, &off, 0) < 0) {
325 if (fstat(file_fd, &sb) == 0)
326 length = sb.st_size - test.offset;
333 FAIL("offset != length")
339 write_test_file(size_t file_size)
343 static size_t current_file_size = 0;
345 if (file_size == current_file_size)
347 else if (file_size < current_file_size) {
348 if (ftruncate(file_fd, file_size) != 0)
349 FAIL_ERR("ftruncate");
350 current_file_size = file_size;
354 page_buffer = malloc(file_size);
355 if (page_buffer == NULL)
357 bzero(page_buffer, file_size);
359 len = write(file_fd, page_buffer, file_size);
363 len = lseek(file_fd, 0, SEEK_SET);
370 current_file_size = file_size;
382 size_t desired_file_size = 0;
384 const int pagesize = getpagesize();
386 struct sendfile_test tests[] = {
387 { .hdr_length = 0, .offset = 0, .length = 1 },
388 { .hdr_length = 0, .offset = 0, .length = pagesize },
389 { .hdr_length = 0, .offset = 1, .length = 1 },
390 { .hdr_length = 0, .offset = 1, .length = pagesize },
391 { .hdr_length = 0, .offset = pagesize, .length = pagesize },
392 { .hdr_length = 0, .offset = 0, .length = 2*pagesize },
393 { .hdr_length = 0, .offset = 0, .length = 0 },
394 { .hdr_length = 0, .offset = pagesize, .length = 0 },
395 { .hdr_length = 0, .offset = 2*pagesize, .length = 0 },
396 { .hdr_length = 0, .offset = TEST_PAGES*pagesize, .length = 0 },
397 { .hdr_length = 0, .offset = 0, .length = pagesize,
401 test_count = sizeof(tests) / sizeof(tests[0]);
402 printf("1..%d\n", test_count);
404 for (test_num = 1; test_num <= test_count; test_num++) {
406 desired_file_size = tests[test_num - 1].file_size;
407 if (desired_file_size == 0)
408 desired_file_size = TEST_PAGES * pagesize;
409 if (write_test_file(desired_file_size) != 0) {
410 printf("not ok %d\n", test_num);
416 printf("not ok %d\n", test_num);
425 if (new_test_socket(&connect_socket) != 0) {
426 printf("not ok %d\n", test_num);
428 close(connect_socket);
432 if (send_test(connect_socket, tests[test_num-1]) != 0) {
433 printf("not ok %d\n", test_num);
435 close(connect_socket);
439 close(connect_socket);
440 if (waitpid(pid, &status, 0) == pid) {
441 if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
442 printf("%s %d\n", "ok", test_num);
444 printf("%s %d\n", "not ok", test_num);
447 printf("not ok %d\n", test_num);
460 main(int argc, char *argv[])
466 snprintf(path, sizeof(path), "sendfile.XXXXXXXXXXXX");
467 file_fd = mkstemp(path);
470 } else if (argc == 2) {
471 (void)strlcpy(path, argv[1], sizeof(path));
472 file_fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0600);
476 FAIL("usage: sendfile [path]");