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