[LTP] [PATCH v1] Add open15 test
Andrea Cervesato
andrea.cervesato@suse.de
Fri Jan 19 10:25:54 CET 2024
From: Andrea Cervesato <andrea.cervesato@suse.com>
This test has been extracted from symlink01 and it verifies that
open() is working correctly on symlink() generated files.
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
runtest/syscalls | 2 +-
testcases/kernel/syscalls/open/.gitignore | 1 +
testcases/kernel/syscalls/open/open15.c | 147 ++++++++++++++++++++++
3 files changed, 149 insertions(+), 1 deletion(-)
create mode 100644 testcases/kernel/syscalls/open/open15.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 6e2407879..dc14b952f 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -922,7 +922,6 @@ nice04 nice04
nice05 nice05
open01 open01
-open01A symlink01 -T open01
open02 open02
open03 open03
open04 open04
@@ -935,6 +934,7 @@ open11 open11
open12 open12
open13 open13
open14 open14
+open15 open15
openat01 openat01
openat02 openat02
diff --git a/testcases/kernel/syscalls/open/.gitignore b/testcases/kernel/syscalls/open/.gitignore
index 001d874d6..af5997572 100644
--- a/testcases/kernel/syscalls/open/.gitignore
+++ b/testcases/kernel/syscalls/open/.gitignore
@@ -12,3 +12,4 @@
/open12_child
/open13
/open14
+/open15
diff --git a/testcases/kernel/syscalls/open/open15.c b/testcases/kernel/syscalls/open/open15.c
new file mode 100644
index 000000000..0a94f613a
--- /dev/null
+++ b/testcases/kernel/syscalls/open/open15.c
@@ -0,0 +1,147 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
+ * Author: David Fenner
+ * Copilot: Jon Hendrickson
+ * Copyright (C) 2024 Andrea Cervesato andrea.cervesato@suse.com
+ */
+
+/*\
+ * [Description]
+ *
+ * This test verifies that open() is working correctly on symlink()
+ * generated files.
+ */
+
+#include "tst_test.h"
+
+#define BIG_STRING "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"
+
+static char myfile_path[PATH_MAX];
+
+static void test_open_symlink(void)
+{
+ int fd;
+ int str_size;
+ char buff[128];
+ char *symname = "my_symlink0";
+
+ str_size = strlen(BIG_STRING);
+
+ SAFE_SYMLINK(myfile_path, symname);
+
+ fd = SAFE_OPEN(symname, O_CREAT | O_RDWR, 0777);
+ SAFE_WRITE(SAFE_WRITE_ALL, fd, BIG_STRING, str_size);
+ SAFE_LSEEK(fd, 0, 0);
+ SAFE_READ(1, fd, buff, str_size);
+ SAFE_CLOSE(fd);
+
+ TST_EXP_EXPR(!strncmp(buff, BIG_STRING, str_size),
+ "symlink generated file can be opened to write data");
+
+ SAFE_UNLINK(symname);
+ remove(myfile_path);
+}
+
+static void test_open_compare(void)
+{
+ int fd_file, fd_symlink;
+ int str_size;
+ char buff_file[128];
+ char buff_symlink[128];
+ char *symname = "my_symlink1";
+
+ str_size = strlen(BIG_STRING);
+
+ fd_file = SAFE_OPEN(myfile_path, O_CREAT | O_RDWR, 0777);
+ SAFE_WRITE(SAFE_WRITE_ALL, fd_file, BIG_STRING, str_size);
+
+ SAFE_SYMLINK(myfile_path, symname);
+
+ SAFE_LSEEK(fd_file, 0, 0);
+ SAFE_READ(1, fd_file, buff_file, str_size);
+
+ fd_symlink = SAFE_OPEN(symname, O_RDWR, 0777);
+ SAFE_LSEEK(fd_symlink, 0, 0);
+ SAFE_READ(1, fd_symlink, buff_symlink, str_size);
+
+ TST_EXP_EXPR(!strncmp(buff_file, buff_symlink, str_size),
+ "file data is the equivalent to symlink generated file data");
+
+ SAFE_CLOSE(fd_file);
+ SAFE_CLOSE(fd_symlink);
+
+ SAFE_UNLINK(symname);
+ remove(myfile_path);
+}
+
+static void test_open_already_exist(void)
+{
+ char *symname = "my_symlink2";
+
+ SAFE_TOUCH(myfile_path, 0644, NULL);
+ SAFE_SYMLINK(myfile_path, symname);
+
+ TST_EXP_FAIL(open(myfile_path, O_EXCL | O_CREAT, 0644), EEXIST);
+ if (TST_RET != -1)
+ SAFE_CLOSE(TST_RET);
+
+ SAFE_UNLINK(symname);
+ remove(myfile_path);
+}
+
+static void test_open_no_path(void)
+{
+ char *symname = "my_symlink3";
+
+ SAFE_SYMLINK(myfile_path, symname);
+
+ TST_EXP_FAIL(open(myfile_path, O_RDWR, 0644), ENOENT);
+ if (TST_RET != -1)
+ SAFE_CLOSE(TST_RET);
+
+ SAFE_UNLINK(symname);
+ remove(myfile_path);
+}
+
+static void test_open_loop(void)
+{
+ char *symname = "my_symlink4";
+
+ SAFE_SYMLINK(symname, symname);
+
+ TST_EXP_FAIL(open(symname, O_CREAT, 0644), ELOOP);
+ if (TST_RET != -1)
+ SAFE_CLOSE(TST_RET);
+
+ SAFE_UNLINK(symname);
+ remove(myfile_path);
+}
+
+static void setup(void)
+{
+ int size;
+ char *tmpdir;
+
+ tmpdir = tst_get_tmpdir();
+ size = strlen(tmpdir);
+
+ myfile_path[size] = '/';
+ memcpy(myfile_path, tmpdir, size);
+ memcpy(myfile_path + size + 1, "myfile.txt", 6);
+}
+
+static void run(void)
+{
+ test_open_symlink();
+ test_open_compare();
+ test_open_already_exist();
+ test_open_no_path();
+ test_open_loop();
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .test_all = run,
+ .needs_tmpdir = 1,
+};
--
2.35.3
More information about the ltp
mailing list