xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/readv/readv02.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) Bull S.A. 2001
4*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) International Business Machines  Corp., 2001
5*49cdfc7eSAndroid Build Coastguard Worker  * 07/2001 Ported by Wayne Boyer
6*49cdfc7eSAndroid Build Coastguard Worker  * 05/2002 Ported by Jacky Malcles
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * Tests readv() failures:
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL the sum of the iov_len values overflows an ssize_t value
15*49cdfc7eSAndroid Build Coastguard Worker  * - EFAULT buffer is outside the accessible address space
16*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL the vector count iovcnt is less than zero
17*49cdfc7eSAndroid Build Coastguard Worker  * - EISDIR the file descriptor is a directory
18*49cdfc7eSAndroid Build Coastguard Worker  * - EBADF  the file descriptor is not valid
19*49cdfc7eSAndroid Build Coastguard Worker  */
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #define K_1     1024
25*49cdfc7eSAndroid Build Coastguard Worker #define MODES   0700
26*49cdfc7eSAndroid Build Coastguard Worker 
27*49cdfc7eSAndroid Build Coastguard Worker #define CHUNK           64
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static int badfd = -1;
30*49cdfc7eSAndroid Build Coastguard Worker static int fd_dir, fd_file;
31*49cdfc7eSAndroid Build Coastguard Worker static char buf1[K_1];
32*49cdfc7eSAndroid Build Coastguard Worker const char *TEST_DIR = "test_dir";
33*49cdfc7eSAndroid Build Coastguard Worker const char *TEST_FILE = "test_file";
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker static struct iovec invalid_iovec[] = {
36*49cdfc7eSAndroid Build Coastguard Worker 	{buf1, -1},
37*49cdfc7eSAndroid Build Coastguard Worker 	{buf1 + CHUNK, CHUNK},
38*49cdfc7eSAndroid Build Coastguard Worker 	{buf1 + 2*CHUNK, CHUNK},
39*49cdfc7eSAndroid Build Coastguard Worker };
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker static struct iovec large_iovec[] = {
42*49cdfc7eSAndroid Build Coastguard Worker 	{buf1, K_1},
43*49cdfc7eSAndroid Build Coastguard Worker 	{buf1 + CHUNK, K_1},
44*49cdfc7eSAndroid Build Coastguard Worker 	{buf1 + CHUNK*2, K_1},
45*49cdfc7eSAndroid Build Coastguard Worker };
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker static struct iovec efault_iovec[] = {
48*49cdfc7eSAndroid Build Coastguard Worker 	{NULL, CHUNK},
49*49cdfc7eSAndroid Build Coastguard Worker 	{buf1 + CHUNK, CHUNK},
50*49cdfc7eSAndroid Build Coastguard Worker 	{buf1 + 2*CHUNK, CHUNK},
51*49cdfc7eSAndroid Build Coastguard Worker };
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker static struct iovec valid_iovec[] = {
54*49cdfc7eSAndroid Build Coastguard Worker 	{buf1, CHUNK},
55*49cdfc7eSAndroid Build Coastguard Worker };
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
58*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;
59*49cdfc7eSAndroid Build Coastguard Worker 	void *buf;
60*49cdfc7eSAndroid Build Coastguard Worker 	int count;
61*49cdfc7eSAndroid Build Coastguard Worker 	int exp_error;
62*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
63*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_file, invalid_iovec, 1, EINVAL},
64*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_file, efault_iovec, 3, EFAULT},
65*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_file, large_iovec, -1, EINVAL},
66*49cdfc7eSAndroid Build Coastguard Worker 	{&fd_dir, valid_iovec, 1, EISDIR},
67*49cdfc7eSAndroid Build Coastguard Worker 	{&badfd, valid_iovec, 3, EBADF},
68*49cdfc7eSAndroid Build Coastguard Worker };
69*49cdfc7eSAndroid Build Coastguard Worker 
verify_readv(unsigned int n)70*49cdfc7eSAndroid Build Coastguard Worker static void verify_readv(unsigned int n)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	struct tcase *tc = &tcases[n];
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(readv(*tc->fd, tc->buf, tc->count), tc->exp_error,
75*49cdfc7eSAndroid Build Coastguard Worker 		"readv(%d, %p, %d)", *tc->fd, tc->buf, tc->count);
76*49cdfc7eSAndroid Build Coastguard Worker }
77*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)78*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
79*49cdfc7eSAndroid Build Coastguard Worker {
80*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_FILE_PRINTF(TEST_FILE, "test");
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	fd_file = SAFE_OPEN(TEST_FILE, O_RDONLY);
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	efault_iovec[0].iov_base = tst_get_bad_addr(NULL);
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKDIR(TEST_DIR, MODES);
87*49cdfc7eSAndroid Build Coastguard Worker 	fd_dir = SAFE_OPEN(TEST_DIR, O_RDONLY);
88*49cdfc7eSAndroid Build Coastguard Worker }
89*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)90*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
91*49cdfc7eSAndroid Build Coastguard Worker {
92*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_file > 0)
93*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_file);
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	if (fd_dir > 0)
96*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd_dir);
97*49cdfc7eSAndroid Build Coastguard Worker }
98*49cdfc7eSAndroid Build Coastguard Worker 
99*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
100*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
101*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
102*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
103*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
104*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_readv,
105*49cdfc7eSAndroid Build Coastguard Worker };
106