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