diff mbox series

[v1,3/3] syscalls/dup207: Add file offset check test

Message ID 1632289182-2191-3-git-send-email-xuyang2018.jy@fujitsu.com
State Accepted
Headers show
Series [v1,1/3] syscalls/dup202: Also check mode whethter change when calling chmod after dup2 | expand

Commit Message

Yang Xu \(Fujitsu\) Sept. 22, 2021, 5:39 a.m. UTC
Since the two file descriptors refer to the same open file description, they share file offset.
If the file offset is modified by using lseek(2) on one of the file descriptors, the offset is
also changed for the other file descriptor.

Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/dup2/.gitignore |  1 +
 testcases/kernel/syscalls/dup2/dup207.c   | 82 +++++++++++++++++++++++
 3 files changed, 84 insertions(+)
 create mode 100644 testcases/kernel/syscalls/dup2/dup207.c

Comments

Cyril Hrubis Oct. 7, 2021, 1:36 p.m. UTC | #1
Hi!
Pushed with minor changes, thanks.

- removed the pind from the filename as well
- shortened the description messages a bit
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index 068fba456..b19316805 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -154,6 +154,7 @@  dup203 dup203
 dup204 dup204
 dup205 dup205
 dup206 dup206
+dup207 dup207
 
 dup3_01 dup3_01
 dup3_02 dup3_02
diff --git a/testcases/kernel/syscalls/dup2/.gitignore b/testcases/kernel/syscalls/dup2/.gitignore
index e2e008b58..f5938a182 100644
--- a/testcases/kernel/syscalls/dup2/.gitignore
+++ b/testcases/kernel/syscalls/dup2/.gitignore
@@ -4,3 +4,4 @@ 
 /dup204
 /dup205
 /dup206
+/dup207
diff --git a/testcases/kernel/syscalls/dup2/dup207.c b/testcases/kernel/syscalls/dup2/dup207.c
new file mode 100644
index 000000000..8badf4229
--- /dev/null
+++ b/testcases/kernel/syscalls/dup2/dup207.c
@@ -0,0 +1,82 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Test whether the file offset are the same for both file descriptors.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+
+#define WRITE_STR "abcdefg"
+
+static int ofd = -1, nfd = 10;
+
+static struct tcase {
+	off_t offset;
+	size_t exp_size;
+	/* 0 - change offset before dup2, 1 - change offset after dup2 */
+	int flag;
+	char *exp_data;
+	char *desc;
+} tcases[] = {
+	{1, 6, 0, "bcdefg", "Test offset check when using lseek before dup2"},
+	{2, 5, 1, "cdefg", "Test offset check when using lseek after dup2"},
+};
+
+static void setup(void)
+{
+	char testfile[40];
+
+	sprintf(testfile, "dup207.%d", getpid());
+	ofd = SAFE_OPEN(testfile, O_RDWR | O_CREAT, 0644);
+	SAFE_WRITE(1, ofd, WRITE_STR, sizeof(WRITE_STR) - 1);
+}
+
+static void cleanup(void)
+{
+	if (ofd > 0)
+		SAFE_CLOSE(ofd);
+	close(nfd);
+}
+
+static void run(unsigned int i)
+{
+	struct tcase *tc = tcases + i;
+	char read_buf[20];
+
+	memset(read_buf, 0, sizeof(read_buf));
+	tst_res(TINFO, "%s", tc->desc);
+	if (!tc->flag)
+		SAFE_LSEEK(ofd, tc->offset, SEEK_SET);
+
+	TEST(dup2(ofd, nfd));
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "call failed unexpectedly");
+		return;
+	}
+	if (tc->flag)
+		SAFE_LSEEK(ofd, tc->offset, SEEK_SET);
+
+	SAFE_READ(1, nfd, read_buf, tc->exp_size);
+	if (strncmp(read_buf, tc->exp_data, tc->exp_size))
+		tst_res(TFAIL, "Expect %s, but get %s.", tc->exp_data, read_buf);
+	else
+		tst_res(TPASS, "Get expected buf %s", read_buf);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = run,
+	.setup = setup,
+	.cleanup = cleanup,
+};