1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2018 FUJITSU LIMITED. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Xiao Yang <yangx.jy@cn.fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*
8f08c3bdfSopenharmony_ci * Description:
9f08c3bdfSopenharmony_ci * Testcase to check the basic functionality of the preadv2(2).
10f08c3bdfSopenharmony_ci * 1) If the file offset argument is not -1, preadv2() should succeed
11f08c3bdfSopenharmony_ci *    in reading the expected content of data and the file offset is
12f08c3bdfSopenharmony_ci *    not changed after reading.
13f08c3bdfSopenharmony_ci * 2) If the file offset argument is -1, preadv2() should succeed in
14f08c3bdfSopenharmony_ci *    reading the expected content of data and the current file offset
15f08c3bdfSopenharmony_ci *    is used and changed after reading.
16f08c3bdfSopenharmony_ci */
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#define _GNU_SOURCE
19f08c3bdfSopenharmony_ci#include <string.h>
20f08c3bdfSopenharmony_ci#include <sys/uio.h>
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_ci#include "tst_test.h"
23f08c3bdfSopenharmony_ci#include "lapi/preadv2.h"
24f08c3bdfSopenharmony_ci
25f08c3bdfSopenharmony_ci#define CHUNK           64
26f08c3bdfSopenharmony_ci
27f08c3bdfSopenharmony_cistatic int fd;
28f08c3bdfSopenharmony_cistatic char buf[CHUNK];
29f08c3bdfSopenharmony_ci
30f08c3bdfSopenharmony_cistatic struct iovec rd_iovec[] = {
31f08c3bdfSopenharmony_ci	{buf, CHUNK},
32f08c3bdfSopenharmony_ci	{NULL, 0},
33f08c3bdfSopenharmony_ci};
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic struct tcase {
36f08c3bdfSopenharmony_ci	off_t seek_off;
37f08c3bdfSopenharmony_ci	int count;
38f08c3bdfSopenharmony_ci	off_t read_off;
39f08c3bdfSopenharmony_ci	ssize_t size;
40f08c3bdfSopenharmony_ci	char content;
41f08c3bdfSopenharmony_ci	off_t exp_off;
42f08c3bdfSopenharmony_ci} tcases[] = {
43f08c3bdfSopenharmony_ci	{0,     1, 0,           CHUNK,     'a', 0},
44f08c3bdfSopenharmony_ci	{CHUNK, 2, 0,           CHUNK,     'a', CHUNK},
45f08c3bdfSopenharmony_ci	{0,     1, CHUNK*3 / 2, CHUNK / 2, 'b', 0},
46f08c3bdfSopenharmony_ci	{0,     1, -1,          CHUNK,     'a', CHUNK},
47f08c3bdfSopenharmony_ci	{0,     2, -1,          CHUNK,     'a', CHUNK},
48f08c3bdfSopenharmony_ci	{CHUNK, 1, -1,          CHUNK,     'b', CHUNK * 2},
49f08c3bdfSopenharmony_ci};
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void verify_preadv2(unsigned int n)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	int i;
54f08c3bdfSopenharmony_ci	char *vec;
55f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	vec = rd_iovec[0].iov_base;
58f08c3bdfSopenharmony_ci	memset(vec, 0x00, CHUNK);
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	SAFE_LSEEK(fd, tc->seek_off, SEEK_SET);
61f08c3bdfSopenharmony_ci
62f08c3bdfSopenharmony_ci	TEST(preadv2(fd, rd_iovec, tc->count, tc->read_off, 0));
63f08c3bdfSopenharmony_ci	if (TST_RET < 0) {
64f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "preadv2() failed");
65f08c3bdfSopenharmony_ci		return;
66f08c3bdfSopenharmony_ci	}
67f08c3bdfSopenharmony_ci
68f08c3bdfSopenharmony_ci	if (TST_RET != tc->size) {
69f08c3bdfSopenharmony_ci		tst_res(TFAIL, "preadv2() read %li bytes, expected %zi",
70f08c3bdfSopenharmony_ci			 TST_RET, tc->size);
71f08c3bdfSopenharmony_ci		return;
72f08c3bdfSopenharmony_ci	}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	for (i = 0; i < tc->size; i++) {
75f08c3bdfSopenharmony_ci		if (vec[i] != tc->content)
76f08c3bdfSopenharmony_ci			break;
77f08c3bdfSopenharmony_ci	}
78f08c3bdfSopenharmony_ci
79f08c3bdfSopenharmony_ci	if (i < tc->size) {
80f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Buffer wrong at %i have %02x expected %02x",
81f08c3bdfSopenharmony_ci			 i, vec[i], tc->content);
82f08c3bdfSopenharmony_ci		return;
83f08c3bdfSopenharmony_ci	}
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	if (SAFE_LSEEK(fd, 0, SEEK_CUR) != tc->exp_off) {
86f08c3bdfSopenharmony_ci		tst_res(TFAIL, "preadv2() has changed file offset");
87f08c3bdfSopenharmony_ci		return;
88f08c3bdfSopenharmony_ci	}
89f08c3bdfSopenharmony_ci
90f08c3bdfSopenharmony_ci	tst_res(TPASS, "preadv2() read %zi bytes with content '%c' expectedly",
91f08c3bdfSopenharmony_ci		tc->size, tc->content);
92f08c3bdfSopenharmony_ci}
93f08c3bdfSopenharmony_ci
94f08c3bdfSopenharmony_cistatic void setup(void)
95f08c3bdfSopenharmony_ci{
96f08c3bdfSopenharmony_ci	char buf[CHUNK];
97f08c3bdfSopenharmony_ci
98f08c3bdfSopenharmony_ci	fd = SAFE_OPEN("file", O_RDWR | O_CREAT, 0644);
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	memset(buf, 'a', sizeof(buf));
101f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, sizeof(buf));
102f08c3bdfSopenharmony_ci
103f08c3bdfSopenharmony_ci	memset(buf, 'b', sizeof(buf));
104f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, sizeof(buf));
105f08c3bdfSopenharmony_ci}
106f08c3bdfSopenharmony_ci
107f08c3bdfSopenharmony_cistatic void cleanup(void)
108f08c3bdfSopenharmony_ci{
109f08c3bdfSopenharmony_ci	if (fd > 0)
110f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
111f08c3bdfSopenharmony_ci}
112f08c3bdfSopenharmony_ci
113f08c3bdfSopenharmony_cistatic struct tst_test test = {
114f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
115f08c3bdfSopenharmony_ci	.setup = setup,
116f08c3bdfSopenharmony_ci	.cleanup = cleanup,
117f08c3bdfSopenharmony_ci	.test = verify_preadv2,
118f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
119f08c3bdfSopenharmony_ci};
120