• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Tests sendfilev with bogus inputs. */
2 
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <strings.h>
8 #include <unistd.h>
9 #include <sys/sendfile.h>
10 
11 #define CHUNK (8 * 1024)
12 #define TEST_FILE "sendfile.test"
13 
main(int argc,const char * argv[])14 int main(int argc, const char *argv[])
15 {
16    int test_fd = open(TEST_FILE, O_WRONLY | O_CREAT, 0666);
17    if (test_fd < 0) {
18       int error = errno;
19       fprintf(stderr, "open failed: %s (%d).\n", strerror(error), error);
20       return 1;
21    }
22 
23    char chunk1[CHUNK];
24    bzero(&chunk1, sizeof(chunk1));
25    ssize_t nbytes = write(test_fd, &chunk1, sizeof(chunk1));
26    if (nbytes != CHUNK) {
27       int error = errno;
28       fprintf(stderr, "write failed (nbytes=%zd): %s (%d).\n",
29               nbytes, strerror(error), error);
30       return 1;
31    }
32 
33    close(test_fd);
34    printf("Test file created.\n");
35 
36    test_fd = open(TEST_FILE, O_RDWR, 0666);
37    if (test_fd < 0) {
38       int error = errno;
39       fprintf(stderr, "open failed: %s (%d).\n", strerror(error), error);
40       return 1;
41    }
42 
43    sendfilevec_t vec[2];
44    vec[0].sfv_fd = SFV_FD_SELF;
45    vec[0].sfv_off = -1;
46    vec[0].sfv_len = 1;
47    vec[0].sfv_flag = 0;
48    vec[1].sfv_fd = test_fd;
49    vec[1].sfv_off = 0;
50    vec[1].sfv_len = CHUNK;
51    vec[1].sfv_flag = 0;
52    size_t xferred;
53 
54    nbytes = sendfilev(test_fd, vec, 2, &xferred);
55    if (nbytes < 0) {
56       if (errno == EFAULT) {
57          printf("Received EFAULT as expected.\n");
58       } else {
59          fprintf(stderr, "Expected EFAULT, got %d.\n", errno);
60       }
61    } else {
62       fprintf(stderr, "Error: sendfilev returned a positive value.\n");
63    }
64 
65    nbytes = sendfilev(test_fd, vec, -1, &xferred);
66    if (nbytes < 0) {
67       if (errno == EINVAL) {
68          printf("Received EINVAL as expected.\n");
69       } else {
70          fprintf(stderr, "Expected EINVAL, got %d.\n", errno);
71       }
72    } else {
73       fprintf(stderr, "Error: sendfilev returned a positive value.\n");
74    }
75 
76    vec[0].sfv_off = (off_t) "HEADER";
77    vec[0].sfv_len = 6;
78    nbytes = sendfilev(test_fd, vec, 1, &xferred);
79    if (nbytes < 0) {
80       int error = errno;
81       fprintf(stderr, "sendfilev failed: %s (%d).\n", strerror(error), error);
82    } else {
83       printf("sendfilev for the first buffer succeeded.\n");
84    }
85 
86    unlink(TEST_FILE);
87    return 0;
88 }
89