1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2015-2016 Fujitsu Ltd.
4 * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5 */
6 
7 /*
8 * Test Name: preadv02
9 *
10 * Description:
11 * 1) preadv(2) fails if iov_len is invalid.
12 * 2) preadv(2) fails if the vector count iovcnt is less than zero.
13 * 3) preadv(2) fails if offset is negative.
14 * 4) preadv(2) fails when attempts to read into a invalid address.
15 * 5) preadv(2) fails if file descriptor is invalid.
16 * 6) preadv(2) fails if file descriptor is not open for reading.
17 * 7) preadv(2) fails when fd refers to a directory.
18 * 8) preadv(2) fails if fd is associated with a pipe.
19 *
20 * Expected Result:
21 * 1) preadv(2) should return -1 and set errno to EINVAL.
22 * 2) preadv(2) should return -1 and set errno to EINVAL.
23 * 3) preadv(2) should return -1 and set errno to EINVAL.
24 * 4) preadv(2) should return -1 and set errno to EFAULT.
25 * 5) preadv(2) should return -1 and set errno to EBADF.
26 * 6) preadv(2) should return -1 and set errno to EBADF.
27 * 7) preadv(2) should return -1 and set errno to EISDIR.
28 * 8) preadv(2) should return -1 and set errno to ESPIPE.
29 */
30 
31 #define _GNU_SOURCE
32 
33 #include <sys/uio.h>
34 #include <unistd.h>
35 #include "tst_test.h"
36 #include "preadv.h"
37 
38 #define CHUNK           64
39 
40 static int fd1;
41 static int fd2;
42 static int fd3 = -1;
43 static int fd4;
44 static int fd5[2];
45 
46 static char buf[CHUNK];
47 
48 static struct iovec rd_iovec1[] = {
49 	{buf, -1},
50 };
51 
52 static struct iovec rd_iovec2[] = {
53 	{buf, CHUNK},
54 };
55 
56 static struct iovec rd_iovec3[] = {
57 	{(char *)-1, CHUNK},
58 };
59 
60 static struct tcase {
61 	int *fd;
62 	struct iovec *name;
63 	int count;
64 	off_t offset;
65 	int exp_err;
66 } tcases[] = {
67 	{&fd1, rd_iovec1, 1, 0, EINVAL},
68 	{&fd1, rd_iovec2, -1, 0, EINVAL},
69 	{&fd1, rd_iovec2, 1, -1, EINVAL},
70 	{&fd1, rd_iovec3, 1, 0, EFAULT},
71 	{&fd3, rd_iovec2, 1, 0, EBADF},
72 	{&fd2, rd_iovec2, 1, 0, EBADF},
73 	{&fd4, rd_iovec2, 1, 0, EISDIR},
74 	{&fd5[0], rd_iovec2, 1, 0, ESPIPE}
75 };
76 
verify_preadv(unsigned int n)77 static void verify_preadv(unsigned int n)
78 {
79 	struct tcase *tc = &tcases[n];
80 
81 	TEST(preadv(*tc->fd, tc->name, tc->count, tc->offset));
82 
83 	if (TST_RET == 0) {
84 		tst_res(TFAIL, "preadv() succeeded unexpectedly");
85 		return;
86 	}
87 
88 	if (TST_ERR == tc->exp_err) {
89 		tst_res(TPASS | TTERRNO, "preadv() failed as expected");
90 		return;
91 	}
92 
93 	tst_res(TFAIL | TTERRNO, "preadv() failed unexpectedly, expected %s",
94 		tst_strerrno(tc->exp_err));
95 }
96 
setup(void)97 static void setup(void)
98 {
99 	fd1 = SAFE_OPEN("file1", O_RDWR | O_CREAT, 0644);
100 	SAFE_FTRUNCATE(fd1, getpagesize());
101 	fd2 = SAFE_OPEN("file2", O_WRONLY | O_CREAT, 0644);
102 	fd4 = SAFE_OPEN(".", O_RDONLY);
103 	SAFE_PIPE(fd5);
104 }
105 
cleanup(void)106 static void cleanup(void)
107 {
108 	if (fd1 > 0)
109 		SAFE_CLOSE(fd1);
110 
111 	if (fd2 > 0)
112 		SAFE_CLOSE(fd2);
113 
114 	if (fd4 > 0)
115 		SAFE_CLOSE(fd4);
116 
117 	if (fd5[0] > 0)
118 		SAFE_CLOSE(fd5[0]);
119 
120 	if (fd5[1] > 0)
121 		SAFE_CLOSE(fd5[1]);
122 }
123 
124 static struct tst_test test = {
125 	.tcnt = ARRAY_SIZE(tcases),
126 	.setup = setup,
127 	.cleanup = cleanup,
128 	.test = verify_preadv,
129 	.min_kver = "2.6.30",
130 	.needs_tmpdir = 1,
131 };
132