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