xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/readv/readv01.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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  *   07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2013 Cyril Hrubis <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*
9*49cdfc7eSAndroid Build Coastguard Worker  * DESCRIPTION
10*49cdfc7eSAndroid Build Coastguard Worker  *	Testcase to check the basic functionality of the readv(2) system call.
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * ALGORITHM
13*49cdfc7eSAndroid Build Coastguard Worker  *	Create a IO vector, and attempt to readv() various components of it.
14*49cdfc7eSAndroid Build Coastguard Worker  */
15*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
18*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <memory.h>
20*49cdfc7eSAndroid Build Coastguard Worker 
21*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker /* Note: multi_iovec test assumes CHUNK is divisible by 4 */
24*49cdfc7eSAndroid Build Coastguard Worker #define	CHUNK		64
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker static char buf[CHUNK];
27*49cdfc7eSAndroid Build Coastguard Worker static struct iovec *rd_iovec, *big_iovec, *multi_iovec, *lockup_iovec;
28*49cdfc7eSAndroid Build Coastguard Worker static int fd;
29*49cdfc7eSAndroid Build Coastguard Worker 
30*49cdfc7eSAndroid Build Coastguard Worker static struct testcase {
31*49cdfc7eSAndroid Build Coastguard Worker 	struct iovec **iov;
32*49cdfc7eSAndroid Build Coastguard Worker 	int iov_count, exp_ret;
33*49cdfc7eSAndroid Build Coastguard Worker 	const char *name;
34*49cdfc7eSAndroid Build Coastguard Worker } testcase_list[] = {
35*49cdfc7eSAndroid Build Coastguard Worker 	{&rd_iovec, 0, 0, "readv() with 0 I/O vectors"},
36*49cdfc7eSAndroid Build Coastguard Worker 	{&rd_iovec, 3, CHUNK, "readv() with NULL I/O vectors"},
37*49cdfc7eSAndroid Build Coastguard Worker 	{&big_iovec, 2, CHUNK, "readv() with too big I/O vectors"},
38*49cdfc7eSAndroid Build Coastguard Worker 	{&multi_iovec, 2, 3*CHUNK/4, "readv() with multiple I/O vectors"},
39*49cdfc7eSAndroid Build Coastguard Worker 	{&lockup_iovec, 2, CHUNK, "readv() with zero-len buffer"},
40*49cdfc7eSAndroid Build Coastguard Worker };
41*49cdfc7eSAndroid Build Coastguard Worker 
test_readv(unsigned int n)42*49cdfc7eSAndroid Build Coastguard Worker static void test_readv(unsigned int n)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker 	int i, fpos, fail = 0;
45*49cdfc7eSAndroid Build Coastguard Worker 	size_t j;
46*49cdfc7eSAndroid Build Coastguard Worker 	char *ptr;
47*49cdfc7eSAndroid Build Coastguard Worker 	const struct testcase *tc = testcase_list + n;
48*49cdfc7eSAndroid Build Coastguard Worker 	struct iovec *vec;
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_LSEEK(fd, 0, SEEK_SET);
51*49cdfc7eSAndroid Build Coastguard Worker 	vec = *tc->iov;
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < tc->iov_count; i++) {
54*49cdfc7eSAndroid Build Coastguard Worker 		if (vec[i].iov_base && vec[i].iov_len)
55*49cdfc7eSAndroid Build Coastguard Worker 			memset(vec[i].iov_base, 0, vec[i].iov_len);
56*49cdfc7eSAndroid Build Coastguard Worker 	}
57*49cdfc7eSAndroid Build Coastguard Worker 
58*49cdfc7eSAndroid Build Coastguard Worker 	TEST(readv(fd, vec, tc->iov_count));
59*49cdfc7eSAndroid Build Coastguard Worker 
60*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET == -1)
61*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "readv() failed unexpectedly");
62*49cdfc7eSAndroid Build Coastguard Worker 	else if (TST_RET < 0)
63*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL | TTERRNO, "readv() returned invalid value");
64*49cdfc7eSAndroid Build Coastguard Worker 	else if (TST_RET != tc->exp_ret)
65*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "readv() returned unexpected value %ld",
66*49cdfc7eSAndroid Build Coastguard Worker 			TST_RET);
67*49cdfc7eSAndroid Build Coastguard Worker 
68*49cdfc7eSAndroid Build Coastguard Worker 	if (TST_RET != tc->exp_ret)
69*49cdfc7eSAndroid Build Coastguard Worker 		return;
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	tst_res(TPASS, "%s", tc->name);
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0, fpos = 0; i < tc->iov_count; i++) {
74*49cdfc7eSAndroid Build Coastguard Worker 		ptr = vec[i].iov_base;
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 		for (j = 0; j < vec[i].iov_len; j++, fpos++) {
77*49cdfc7eSAndroid Build Coastguard Worker 			if (ptr[j] != (fpos < tc->exp_ret ? 0x42 : 0))
78*49cdfc7eSAndroid Build Coastguard Worker 				fail++;
79*49cdfc7eSAndroid Build Coastguard Worker 		}
80*49cdfc7eSAndroid Build Coastguard Worker 	}
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	if (fail)
83*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TFAIL, "Wrong buffer content");
84*49cdfc7eSAndroid Build Coastguard Worker 	else
85*49cdfc7eSAndroid Build Coastguard Worker 		tst_res(TPASS, "readv() correctly read %d bytes ", tc->exp_ret);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)88*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker 	/* replace the default NULL pointer with invalid address */
91*49cdfc7eSAndroid Build Coastguard Worker 	lockup_iovec[0].iov_base = tst_get_bad_addr(NULL);
92*49cdfc7eSAndroid Build Coastguard Worker 
93*49cdfc7eSAndroid Build Coastguard Worker 	memset(buf, 0x42, sizeof(buf));
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("data_file", O_WRONLY | O_CREAT | O_TRUNC, 0666);
96*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, sizeof(buf));
97*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd);
98*49cdfc7eSAndroid Build Coastguard Worker 	fd = SAFE_OPEN("data_file", O_RDONLY);
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)101*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
102*49cdfc7eSAndroid Build Coastguard Worker {
103*49cdfc7eSAndroid Build Coastguard Worker 	if (fd >= 0)
104*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(fd);
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
108*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
109*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
110*49cdfc7eSAndroid Build Coastguard Worker 	.test = test_readv,
111*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(testcase_list),
112*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
113*49cdfc7eSAndroid Build Coastguard Worker 	.tags = (const struct tst_tag[]) {
114*49cdfc7eSAndroid Build Coastguard Worker 		{"linux-git", "19f18459330f"},
115*49cdfc7eSAndroid Build Coastguard Worker 		{}
116*49cdfc7eSAndroid Build Coastguard Worker 	},
117*49cdfc7eSAndroid Build Coastguard Worker 	.bufs = (struct tst_buffers[]) {
118*49cdfc7eSAndroid Build Coastguard Worker 		{&rd_iovec, .iov_sizes = (int[]){CHUNK, 0, 0, -1}},
119*49cdfc7eSAndroid Build Coastguard Worker 		{&big_iovec, .iov_sizes = (int[]){2*CHUNK, CHUNK, -1}},
120*49cdfc7eSAndroid Build Coastguard Worker 		{&multi_iovec, .iov_sizes = (int[]){CHUNK/4, CHUNK/2, -1}},
121*49cdfc7eSAndroid Build Coastguard Worker 		{&lockup_iovec, .iov_sizes = (int[]){0, CHUNK, -1}},
122*49cdfc7eSAndroid Build Coastguard Worker 		{}
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker };
125