diff mbox series

[v1,2/3] syscalls/dup206: Add a test when newfd equals oldfd

Message ID 1632289182-2191-2-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 Sept. 22, 2021, 5:39 a.m. UTC
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
 runtest/syscalls                          |  1 +
 testcases/kernel/syscalls/dup2/.gitignore |  1 +
 testcases/kernel/syscalls/dup2/dup206.c   | 51 +++++++++++++++++++++++
 3 files changed, 53 insertions(+)
 create mode 100644 testcases/kernel/syscalls/dup2/dup206.c

Comments

Cyril Hrubis Oct. 7, 2021, 1:15 p.m. UTC | #1
Hi!
> +static void setup(void)
> +{
> +	char testfile[40];
> +
> +	sprintf(testfile, "dup206.%d", getpid());
> +	fd = SAFE_OPEN(testfile, O_RDWR | O_CREAT, 0666);

There is no reason to append the pid here, so I've changed this to just
SAFE_OPEN("testfile", ...) and pushed, thanks.
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index 29d7752c7..068fba456 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -153,6 +153,7 @@  dup202 dup202
 dup203 dup203
 dup204 dup204
 dup205 dup205
+dup206 dup206
 
 dup3_01 dup3_01
 dup3_02 dup3_02
diff --git a/testcases/kernel/syscalls/dup2/.gitignore b/testcases/kernel/syscalls/dup2/.gitignore
index 6c4685b80..e2e008b58 100644
--- a/testcases/kernel/syscalls/dup2/.gitignore
+++ b/testcases/kernel/syscalls/dup2/.gitignore
@@ -3,3 +3,4 @@ 
 /dup203
 /dup204
 /dup205
+/dup206
diff --git a/testcases/kernel/syscalls/dup2/dup206.c b/testcases/kernel/syscalls/dup2/dup206.c
new file mode 100644
index 000000000..e5074ea83
--- /dev/null
+++ b/testcases/kernel/syscalls/dup2/dup206.c
@@ -0,0 +1,51 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2021 FUJITSU LIMITED. All rights reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * If oldfd is a valid file descriptor, and newfd has the same value as oldfd,
+ * then dup2() does nothing, and returns newfd.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include "tst_test.h"
+
+static int fd = -1;
+
+static void verify_dup2(void)
+{
+	TST_EXP_FD_SILENT(dup2(fd, fd), "dup2(%d, %d)", fd, fd);
+
+	if (TST_RET != fd) {
+		tst_res(TFAIL, "dup2(%d, %d) returns wrong newfd(%ld)", fd, fd, TST_RET);
+		SAFE_CLOSE(TST_RET);
+		return;
+	}
+	tst_res(TPASS, "dup2(%d, %d) returns newfd(%d)", fd, fd, fd);
+}
+
+static void setup(void)
+{
+	char testfile[40];
+
+	sprintf(testfile, "dup206.%d", getpid());
+	fd = SAFE_OPEN(testfile, O_RDWR | O_CREAT, 0666);
+}
+
+static void cleanup(void)
+{
+	if (fd > -1)
+		SAFE_CLOSE(fd);
+}
+
+static struct tst_test test = {
+	.needs_tmpdir = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = verify_dup2,
+};