1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
4f08c3bdfSopenharmony_ci * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
5f08c3bdfSopenharmony_ci */
6f08c3bdfSopenharmony_ci
7f08c3bdfSopenharmony_ci/*\
8f08c3bdfSopenharmony_ci * [Description]
9f08c3bdfSopenharmony_ci *
10f08c3bdfSopenharmony_ci * Test whether the file offset are the same for both file descriptors.
11f08c3bdfSopenharmony_ci */
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ci#include <errno.h>
14f08c3bdfSopenharmony_ci#include <stdio.h>
15f08c3bdfSopenharmony_ci#include <unistd.h>
16f08c3bdfSopenharmony_ci#include "tst_test.h"
17f08c3bdfSopenharmony_ci#include "tst_safe_macros.h"
18f08c3bdfSopenharmony_ci
19f08c3bdfSopenharmony_ci#define WRITE_STR "abcdefg"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic int ofd = -1, nfd = 10;
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic struct tcase {
24f08c3bdfSopenharmony_ci	off_t offset;
25f08c3bdfSopenharmony_ci	size_t exp_size;
26f08c3bdfSopenharmony_ci	/* 0 - change offset before dup2, 1 - change offset after dup2 */
27f08c3bdfSopenharmony_ci	int flag;
28f08c3bdfSopenharmony_ci	char *exp_data;
29f08c3bdfSopenharmony_ci	char *desc;
30f08c3bdfSopenharmony_ci} tcases[] = {
31f08c3bdfSopenharmony_ci	{1, 6, 0, "bcdefg", "Test offset with lseek before dup2"},
32f08c3bdfSopenharmony_ci	{2, 5, 1, "cdefg", "Test offset with lseek after dup2"},
33f08c3bdfSopenharmony_ci};
34f08c3bdfSopenharmony_ci
35f08c3bdfSopenharmony_cistatic void setup(void)
36f08c3bdfSopenharmony_ci{
37f08c3bdfSopenharmony_ci	ofd = SAFE_OPEN("testfile", O_RDWR | O_CREAT, 0644);
38f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, ofd, WRITE_STR, sizeof(WRITE_STR) - 1);
39f08c3bdfSopenharmony_ci}
40f08c3bdfSopenharmony_ci
41f08c3bdfSopenharmony_cistatic void cleanup(void)
42f08c3bdfSopenharmony_ci{
43f08c3bdfSopenharmony_ci	if (ofd > 0)
44f08c3bdfSopenharmony_ci		SAFE_CLOSE(ofd);
45f08c3bdfSopenharmony_ci	close(nfd);
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void run(unsigned int i)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	struct tcase *tc = tcases + i;
51f08c3bdfSopenharmony_ci	char read_buf[20];
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	memset(read_buf, 0, sizeof(read_buf));
54f08c3bdfSopenharmony_ci
55f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s", tc->desc);
56f08c3bdfSopenharmony_ci	if (!tc->flag)
57f08c3bdfSopenharmony_ci		SAFE_LSEEK(ofd, tc->offset, SEEK_SET);
58f08c3bdfSopenharmony_ci
59f08c3bdfSopenharmony_ci	TEST(dup2(ofd, nfd));
60f08c3bdfSopenharmony_ci	if (TST_RET == -1) {
61f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
62f08c3bdfSopenharmony_ci		return;
63f08c3bdfSopenharmony_ci	}
64f08c3bdfSopenharmony_ci	if (tc->flag)
65f08c3bdfSopenharmony_ci		SAFE_LSEEK(ofd, tc->offset, SEEK_SET);
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ci	SAFE_READ(1, nfd, read_buf, tc->exp_size);
68f08c3bdfSopenharmony_ci	if (strncmp(read_buf, tc->exp_data, tc->exp_size))
69f08c3bdfSopenharmony_ci		tst_res(TFAIL, "Expect %s, but get %s.", tc->exp_data, read_buf);
70f08c3bdfSopenharmony_ci	else
71f08c3bdfSopenharmony_ci		tst_res(TPASS, "Get expected buf %s", read_buf);
72f08c3bdfSopenharmony_ci}
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_cistatic struct tst_test test = {
75f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
76f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
77f08c3bdfSopenharmony_ci	.test = run,
78f08c3bdfSopenharmony_ci	.setup = setup,
79f08c3bdfSopenharmony_ci	.cleanup = cleanup,
80f08c3bdfSopenharmony_ci};
81