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