• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: pwritev02
9 *
10 * Description:
11 * 1) pwritev(2) fails if iov_len is invalid.
12 * 2) pwritev(2) fails if the vector count iovcnt is less than zero.
13 * 3) pwritev(2) fails if offset is negative.
14 * 4) pwritev(2) fails when attempts to write from a invalid address
15 * 5) pwritev(2) fails if file descriptor is invalid.
16 * 6) pwritev(2) fails if file descriptor is not open for writing.
17 * 7) pwritev(2) fails if fd is associated with a pipe.
18 *
19 * Expected Result:
20 * 1) pwritev(2) should return -1 and set errno to EINVAL.
21 * 2) pwritev(2) should return -1 and set errno to EINVAL.
22 * 3) pwritev(2) should return -1 and set errno to EINVAL.
23 * 4) pwritev(2) should return -1 and set errno to EFAULT.
24 * 5) pwritev(2) should return -1 and set errno to EBADF.
25 * 6) pwritev(2) should return -1 and set errno to EBADF.
26 * 7) pwritev(2) should return -1 and set errno to ESPIPE.
27 */
28 
29 #define _GNU_SOURCE
30 
31 #include <sys/uio.h>
32 #include <unistd.h>
33 #include "tst_test.h"
34 #include "pwritev.h"
35 
36 #define CHUNK           64
37 
38 static int fd1;
39 static int fd2;
40 static int fd3 = -1;
41 static int fd4[2];
42 
43 static char buf[CHUNK];
44 
45 static struct iovec wr_iovec1[] = {
46 	{buf, -1},
47 };
48 
49 static struct iovec wr_iovec2[] = {
50 	{buf, CHUNK},
51 };
52 
53 static struct iovec wr_iovec3[] = {
54 	{(char *)-1, CHUNK},
55 };
56 
57 static struct tcase {
58 	int *fd;
59 	struct iovec *name;
60 	int count;
61 	off_t offset;
62 	int exp_err;
63 } tcases[] = {
64 	{&fd1, wr_iovec1, 1, 0, EINVAL},
65 	{&fd1, wr_iovec2, -1, 0, EINVAL},
66 	{&fd1, wr_iovec2, 1, -1, EINVAL},
67 	{&fd1, wr_iovec3, 1, 0, EFAULT},
68 	{&fd3, wr_iovec2, 1, 0, EBADF},
69 	{&fd2, wr_iovec2, 1, 0, EBADF},
70 	{&fd4[1], wr_iovec2, 1, 0, ESPIPE}
71 };
72 
verify_pwritev(unsigned int n)73 static void verify_pwritev(unsigned int n)
74 {
75 	struct tcase *tc = &tcases[n];
76 
77 	TEST(pwritev(*tc->fd, tc->name, tc->count, tc->offset));
78 	if (TST_RET == 0) {
79 		tst_res(TFAIL, "pwritev() succeeded unexpectedly");
80 		return;
81 	}
82 
83 	if (TST_ERR == tc->exp_err) {
84 		tst_res(TPASS | TTERRNO, "pwritev() failed as expected");
85 		return;
86 	}
87 
88 	tst_res(TFAIL | TTERRNO, "pwritev() failed unexpectedly, expected %s",
89 		tst_strerrno(tc->exp_err));
90 }
91 
setup(void)92 static void setup(void)
93 {
94 	fd1 = SAFE_OPEN("file", O_RDWR | O_CREAT, 0644);
95 	SAFE_FTRUNCATE(fd1, getpagesize());
96 	fd2 = SAFE_OPEN("file", O_RDONLY | O_CREAT, 0644);
97 	SAFE_PIPE(fd4);
98 }
99 
cleanup(void)100 static void cleanup(void)
101 {
102 	if (fd1 > 0)
103 		SAFE_CLOSE(fd1);
104 
105 	if (fd2 > 0)
106 		SAFE_CLOSE(fd2);
107 
108 	if (fd4[0] > 0)
109 		SAFE_CLOSE(fd4[0]);
110 
111 	if (fd4[1] > 0)
112 		SAFE_CLOSE(fd4[1]);
113 }
114 
115 static struct tst_test test = {
116 	.tcnt = ARRAY_SIZE(tcases),
117 	.setup = setup,
118 	.cleanup = cleanup,
119 	.test = verify_pwritev,
120 	.min_kver = "2.6.30",
121 	.needs_tmpdir = 1,
122 };
123