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) 2015 Fujitsu Ltd.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Xiao Yang <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) Linux Test Project, 2016-2023
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 *
11*49cdfc7eSAndroid Build Coastguard Worker * Testcase to check the basic functionality of the preadv(2).
12*49cdfc7eSAndroid Build Coastguard Worker *
13*49cdfc7eSAndroid Build Coastguard Worker * Preadv(2) should succeed to read the expected content of data
14*49cdfc7eSAndroid Build Coastguard Worker * and after reading the file, the file offset is not changed.
15*49cdfc7eSAndroid Build Coastguard Worker */
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker #define _GNU_SOURCE
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/uio.h>
21*49cdfc7eSAndroid Build Coastguard Worker
22*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
23*49cdfc7eSAndroid Build Coastguard Worker #include "preadv.h"
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker #define CHUNK 64
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker static int fd;
28*49cdfc7eSAndroid Build Coastguard Worker
29*49cdfc7eSAndroid Build Coastguard Worker static struct iovec *rd_iovec;
30*49cdfc7eSAndroid Build Coastguard Worker
31*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
32*49cdfc7eSAndroid Build Coastguard Worker int count;
33*49cdfc7eSAndroid Build Coastguard Worker off_t offset;
34*49cdfc7eSAndroid Build Coastguard Worker ssize_t size;
35*49cdfc7eSAndroid Build Coastguard Worker char content;
36*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
37*49cdfc7eSAndroid Build Coastguard Worker {1, 0, CHUNK, 'a'},
38*49cdfc7eSAndroid Build Coastguard Worker {2, 0, CHUNK, 'a'},
39*49cdfc7eSAndroid Build Coastguard Worker {1, CHUNK*3/2, CHUNK/2, 'b'}
40*49cdfc7eSAndroid Build Coastguard Worker };
41*49cdfc7eSAndroid Build Coastguard Worker
verify_preadv(unsigned int n)42*49cdfc7eSAndroid Build Coastguard Worker static void verify_preadv(unsigned int n)
43*49cdfc7eSAndroid Build Coastguard Worker {
44*49cdfc7eSAndroid Build Coastguard Worker int i;
45*49cdfc7eSAndroid Build Coastguard Worker char *vec;
46*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker vec = rd_iovec[0].iov_base;
49*49cdfc7eSAndroid Build Coastguard Worker memset(vec, 0x00, CHUNK);
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker SAFE_LSEEK(fd, 0, SEEK_SET);
52*49cdfc7eSAndroid Build Coastguard Worker
53*49cdfc7eSAndroid Build Coastguard Worker TEST(preadv(fd, rd_iovec, tc->count, tc->offset));
54*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET < 0) {
55*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "Preadv(2) failed");
56*49cdfc7eSAndroid Build Coastguard Worker return;
57*49cdfc7eSAndroid Build Coastguard Worker }
58*49cdfc7eSAndroid Build Coastguard Worker
59*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != tc->size) {
60*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Preadv(2) read %li bytes, expected %zi",
61*49cdfc7eSAndroid Build Coastguard Worker TST_RET, tc->size);
62*49cdfc7eSAndroid Build Coastguard Worker return;
63*49cdfc7eSAndroid Build Coastguard Worker }
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < tc->size; i++) {
66*49cdfc7eSAndroid Build Coastguard Worker if (vec[i] != tc->content)
67*49cdfc7eSAndroid Build Coastguard Worker break;
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker
70*49cdfc7eSAndroid Build Coastguard Worker if (i < tc->size) {
71*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Buffer wrong at %i have %02x expected %02x",
72*49cdfc7eSAndroid Build Coastguard Worker i, vec[i], tc->content);
73*49cdfc7eSAndroid Build Coastguard Worker return;
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker
76*49cdfc7eSAndroid Build Coastguard Worker if (SAFE_LSEEK(fd, 0, SEEK_CUR) != 0) {
77*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "Preadv(2) has changed file offset");
78*49cdfc7eSAndroid Build Coastguard Worker return;
79*49cdfc7eSAndroid Build Coastguard Worker }
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "Preadv(2) read %zi bytes successfully "
82*49cdfc7eSAndroid Build Coastguard Worker "with content '%c' expectedly", tc->size, tc->content);
83*49cdfc7eSAndroid Build Coastguard Worker }
84*49cdfc7eSAndroid Build Coastguard Worker
setup(void)85*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
86*49cdfc7eSAndroid Build Coastguard Worker {
87*49cdfc7eSAndroid Build Coastguard Worker char buf[CHUNK];
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN("file", O_RDWR | O_CREAT, 0644);
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker memset(buf, 'a', sizeof(buf));
92*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, sizeof(buf));
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker memset(buf, 'b', sizeof(buf));
95*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd, buf, sizeof(buf));
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)98*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
99*49cdfc7eSAndroid Build Coastguard Worker {
100*49cdfc7eSAndroid Build Coastguard Worker if (fd > 0)
101*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
105*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
106*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
107*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
108*49cdfc7eSAndroid Build Coastguard Worker .test = verify_preadv,
109*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
110*49cdfc7eSAndroid Build Coastguard Worker .bufs = (struct tst_buffers []) {
111*49cdfc7eSAndroid Build Coastguard Worker {&rd_iovec, .iov_sizes = (int[]){CHUNK, 0, -1}},
112*49cdfc7eSAndroid Build Coastguard Worker {},
113*49cdfc7eSAndroid Build Coastguard Worker }
114*49cdfc7eSAndroid Build Coastguard Worker };
115