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