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