1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  * 06/2017 modified by Xiao Yang <yangx.jy@cn.fujitsu.com>
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program;  if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 /*
20  * Description:
21  * lseek() succeeds to set the specified offset according to whence
22  * and write valid data from this location.
23  */
24 
25 #include <errno.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29 #include "tst_test.h"
30 
31 #define TFILE1 "tfile1"
32 #define TFILE2 "tfile2"
33 #define WR_STR1 "abcdefg"
34 #define WR_STR2 "ijk"
35 
36 static int fd1, fd2;
37 static struct tcase {
38 	int *fd;
39 	char *fname;
40 	off_t off;
41 	off_t exp_off;
42 	int exp_size;
43 	char *exp_data;
44 } tcases[] = {
45 	{&fd1, TFILE1, 7, 7, 10, "abcdefgijk"},
46 	{&fd2, TFILE2, 2, 2, 7, "abijkfg"},
47 };
48 
49 static void verify_lseek(unsigned int n)
50 {
51 	char read_buf[64];
52 	struct tcase *tc = &tcases[n];
53 
54 	memset(read_buf, 0, sizeof(read_buf));
55 
56 	TEST(lseek(*tc->fd, tc->off, SEEK_SET));
57 	if (TEST_RETURN == (off_t) -1) {
58 		tst_res(TFAIL | TTERRNO, "lseek(%s, %ld, SEEK_SET) failed",
59 			tc->fname, tc->off);
60 		return;
61 	}
62 
63 	if (TEST_RETURN != tc->exp_off) {
64 		tst_res(TFAIL, "lseek(%s, %ld, SEEK_SET) returned %ld, expected %ld",
65 			tc->fname, tc->off, TEST_RETURN, tc->exp_off);
66 		return;
67 	}
68 
69 	SAFE_WRITE(1, *tc->fd, WR_STR2, sizeof(WR_STR2) - 1);
70 
71 	SAFE_CLOSE(*tc->fd);
72 
73 	*tc->fd = SAFE_OPEN(tc->fname, O_RDWR);
74 
75 	SAFE_READ(1, *tc->fd, read_buf, tc->exp_size);
76 
77 	if (strcmp(read_buf, tc->exp_data)) {
78 		tst_res(TFAIL, "lseek(%s, %ld, SEEK_SET) wrote incorrect data %s",
79 			tc->fname, tc->off, read_buf);
80 	} else {
81 		tst_res(TPASS, "lseek(%s, %ld, SEEK_SET) wrote correct data %s",
82 			tc->fname, tc->off, read_buf);
83 	}
84 }
85 
86 static void setup(void)
87 {
88 	fd1 = SAFE_OPEN(TFILE1, O_RDWR | O_CREAT, 0644);
89 	fd2 = SAFE_OPEN(TFILE2, O_RDWR | O_CREAT, 0644);
90 
91 	SAFE_WRITE(1, fd1, WR_STR1, sizeof(WR_STR1) - 1);
92 	SAFE_WRITE(1, fd2, WR_STR1, sizeof(WR_STR1) - 1);
93 }
94 
95 static void cleanup(void)
96 {
97 	if (fd1 > 0)
98 		SAFE_CLOSE(fd1);
99 
100 	if (fd2 > 0)
101 		SAFE_CLOSE(fd2);
102 }
103 
104 static struct tst_test test = {
105 	.setup = setup,
106 	.cleanup = cleanup,
107 	.tcnt = ARRAY_SIZE(tcases),
108 	.test = verify_lseek,
109 	.needs_tmpdir = 1,
110 };
111