1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2014 Fujitsu Ltd.
4 * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
5 */
6 /*
7 * Description:
8 *
9 * For each of SEEK_SET, SEEK_CUR and SEEK_END verify that,
10 *
11 * 1. llseek() succeeds to set file position in the middle of the data. The
12 * file offset is checked by reading from a file and comparing the data.
13 *
14 * 2. llseek() succeeds to set file postion to the end of the data, reading
15 * this postion returns 0.
16 *
17 * 3. llseek() succeeds to set file position after the end of the data,
18 * reading from this postion returns 0 as well.
19 */
20 #define _GNU_SOURCE
21
22 #include "tst_test.h"
23
24 #define TEST_FILE "testfile"
25 #define STR "abcdefgh"
26
setup(void)27 static void setup(void)
28 {
29 int fd;
30
31 fd = SAFE_CREAT(TEST_FILE, 0644);
32
33 SAFE_WRITE(1, fd, STR, sizeof(STR) - 1);
34
35 SAFE_CLOSE(fd);
36 }
37
38 static struct tcase {
39 int whence;
40 int off;
41 int ret;
42 int read;
43 const char *str;
44 } tcases[] = {
45 /* Seek somewhere in the middle of data */
46 {SEEK_SET, 1, 1, 3, "bcd"},
47 {SEEK_CUR, 1, 5, 3, "fgh"},
48 {SEEK_END, -1, 7, 1, "h"},
49
50 /* Seek to the end of data */
51 {SEEK_SET, 8, 8, 0, NULL},
52 {SEEK_CUR, 4, 8, 0, NULL},
53 {SEEK_END, 0, 8, 0, NULL},
54
55 /* Seek after the end of data */
56 {SEEK_SET, 10, 10, 0, NULL},
57 {SEEK_CUR, 8, 12, 0, NULL},
58 {SEEK_END, 4, 12, 0, NULL},
59 };
60
str_whence(int whence)61 static const char *str_whence(int whence)
62 {
63 switch (whence) {
64 case SEEK_SET:
65 return "SEEK_SET";
66 case SEEK_CUR:
67 return "SEEK_CUR";
68 case SEEK_END:
69 return "SEEK_END";
70 default:
71 return "INVALID";
72 }
73 }
74
verify_lseek64(unsigned int n)75 static void verify_lseek64(unsigned int n)
76 {
77 struct tcase *tc = &tcases[n];
78 char read_buf[128];
79 int fd, ret;
80
81 fd = SAFE_OPEN(TEST_FILE, O_RDONLY);
82
83 SAFE_READ(1, fd, read_buf, 4);
84
85 TEST(lseek64(fd, tc->off, tc->whence));
86
87 if (TST_RET == -1) {
88 tst_res(TFAIL | TTERRNO, "llseek failed on %s ", TEST_FILE);
89 goto exit;
90 }
91
92 if (TST_RET != tc->ret) {
93 tst_res(TFAIL, "llseek returned %li expected %i", TST_RET, tc->ret);
94 goto exit;
95 } else {
96 tst_res(TPASS, "llseek returned %i", tc->ret);
97 }
98
99 memset(read_buf, 0, sizeof(read_buf));
100
101 ret = SAFE_READ(0, fd, read_buf, tc->read);
102
103 if (!tc->read) {
104 if (ret != 0)
105 tst_res(TFAIL, "Read bytes after llseek to end of file");
106 else
107 tst_res(TPASS, "%s read returned 0", str_whence(tc->whence));
108
109 goto exit;
110 }
111
112 if (strcmp(read_buf, tc->str))
113 tst_res(TFAIL, "Read wrong bytes after llseek");
114 else
115 tst_res(TPASS, "%s for llseek", str_whence(tc->whence));
116
117 exit:
118 SAFE_CLOSE(fd);
119 }
120
121 static struct tst_test test = {
122 .needs_tmpdir = 1,
123 .setup = setup,
124 .test = verify_lseek64,
125 .tcnt = ARRAY_SIZE(tcases),
126 };
127