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) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * 06/2017 modified by 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 * lseek() succeeds to set the specified offset according to whence
10*49cdfc7eSAndroid Build Coastguard Worker * and write valid data from this location.
11*49cdfc7eSAndroid Build Coastguard Worker */
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #define TFILE1 "tfile1"
20*49cdfc7eSAndroid Build Coastguard Worker #define TFILE2 "tfile2"
21*49cdfc7eSAndroid Build Coastguard Worker #define WR_STR1 "abcdefg"
22*49cdfc7eSAndroid Build Coastguard Worker #define WR_STR2 "ijk"
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker static int fd1, fd2;
25*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
26*49cdfc7eSAndroid Build Coastguard Worker int *fd;
27*49cdfc7eSAndroid Build Coastguard Worker char *fname;
28*49cdfc7eSAndroid Build Coastguard Worker off_t off;
29*49cdfc7eSAndroid Build Coastguard Worker off_t exp_off;
30*49cdfc7eSAndroid Build Coastguard Worker int exp_size;
31*49cdfc7eSAndroid Build Coastguard Worker char *exp_data;
32*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
33*49cdfc7eSAndroid Build Coastguard Worker {&fd1, TFILE1, 7, 7, 10, "abcdefgijk"},
34*49cdfc7eSAndroid Build Coastguard Worker {&fd2, TFILE2, 2, 2, 7, "abijkfg"},
35*49cdfc7eSAndroid Build Coastguard Worker };
36*49cdfc7eSAndroid Build Coastguard Worker
verify_lseek(unsigned int n)37*49cdfc7eSAndroid Build Coastguard Worker static void verify_lseek(unsigned int n)
38*49cdfc7eSAndroid Build Coastguard Worker {
39*49cdfc7eSAndroid Build Coastguard Worker char read_buf[64];
40*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
41*49cdfc7eSAndroid Build Coastguard Worker
42*49cdfc7eSAndroid Build Coastguard Worker memset(read_buf, 0, sizeof(read_buf));
43*49cdfc7eSAndroid Build Coastguard Worker
44*49cdfc7eSAndroid Build Coastguard Worker TEST(lseek(*tc->fd, tc->off, SEEK_SET));
45*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == (off_t) -1) {
46*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO, "lseek(%s, %lld, SEEK_SET) failed",
47*49cdfc7eSAndroid Build Coastguard Worker tc->fname, (long long int)tc->off);
48*49cdfc7eSAndroid Build Coastguard Worker return;
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != tc->exp_off) {
52*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "lseek(%s, %lld, SEEK_SET) returned %ld, expected %lld",
53*49cdfc7eSAndroid Build Coastguard Worker tc->fname, (long long int)tc->off, TST_RET,
54*49cdfc7eSAndroid Build Coastguard Worker (long long int)tc->exp_off);
55*49cdfc7eSAndroid Build Coastguard Worker return;
56*49cdfc7eSAndroid Build Coastguard Worker }
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, *tc->fd, WR_STR2, sizeof(WR_STR2) - 1);
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(*tc->fd);
61*49cdfc7eSAndroid Build Coastguard Worker
62*49cdfc7eSAndroid Build Coastguard Worker *tc->fd = SAFE_OPEN(tc->fname, O_RDWR);
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker SAFE_READ(1, *tc->fd, read_buf, tc->exp_size);
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker if (strcmp(read_buf, tc->exp_data)) {
67*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "lseek(%s, %lld, SEEK_SET) wrote incorrect data %s",
68*49cdfc7eSAndroid Build Coastguard Worker tc->fname, (long long int)tc->off, read_buf);
69*49cdfc7eSAndroid Build Coastguard Worker } else {
70*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "lseek(%s, %lld, SEEK_SET) wrote correct data %s",
71*49cdfc7eSAndroid Build Coastguard Worker tc->fname, (long long int)tc->off, read_buf);
72*49cdfc7eSAndroid Build Coastguard Worker }
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker
setup(void)75*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker fd1 = SAFE_OPEN(TFILE1, O_RDWR | O_CREAT, 0644);
78*49cdfc7eSAndroid Build Coastguard Worker fd2 = SAFE_OPEN(TFILE2, O_RDWR | O_CREAT, 0644);
79*49cdfc7eSAndroid Build Coastguard Worker
80*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd1, WR_STR1, sizeof(WR_STR1) - 1);
81*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd2, WR_STR1, sizeof(WR_STR1) - 1);
82*49cdfc7eSAndroid Build Coastguard Worker }
83*49cdfc7eSAndroid Build Coastguard Worker
cleanup(void)84*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
85*49cdfc7eSAndroid Build Coastguard Worker {
86*49cdfc7eSAndroid Build Coastguard Worker if (fd1 > 0)
87*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd1);
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker if (fd2 > 0)
90*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd2);
91*49cdfc7eSAndroid Build Coastguard Worker }
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
94*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
95*49cdfc7eSAndroid Build Coastguard Worker .cleanup = cleanup,
96*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
97*49cdfc7eSAndroid Build Coastguard Worker .test = verify_lseek,
98*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
99*49cdfc7eSAndroid Build Coastguard Worker };
100