[LTP] [PATCH v3] Add lstat03 test

Andrea Cervesato andrea.cervesato@suse.de
Fri Apr 19 14:31:37 CEST 2024


From: Andrea Cervesato <andrea.cervesato@suse.com>

This test has been extracted from symlink01 test and it checks that
lstat() provides the right information, according with device, access
time, block size, ownership, etc.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
Changed algorithm into multiple test cases checking for specific information
in `struct stat`

 runtest/syscalls                           |   4 +-
 testcases/kernel/syscalls/lstat/.gitignore |   2 +
 testcases/kernel/syscalls/lstat/lstat03.c  | 204 +++++++++++++++++++++
 3 files changed, 208 insertions(+), 2 deletions(-)
 create mode 100644 testcases/kernel/syscalls/lstat/lstat03.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 010a1a752..3e46bc2aa 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -721,12 +721,12 @@ lseek02 lseek02
 lseek07 lseek07
 lseek11 lseek11
 
-lstat01A symlink01 -T lstat01
-lstat01A_64 symlink01 -T lstat01_64
 lstat01 lstat01
 lstat01_64 lstat01_64
 lstat02 lstat02
 lstat02_64 lstat02_64
+lstat03 lstat03
+lstat03_64 lstat03_64
 
 mallinfo02 mallinfo02
 
diff --git a/testcases/kernel/syscalls/lstat/.gitignore b/testcases/kernel/syscalls/lstat/.gitignore
index a497a445f..72cba871f 100644
--- a/testcases/kernel/syscalls/lstat/.gitignore
+++ b/testcases/kernel/syscalls/lstat/.gitignore
@@ -2,3 +2,5 @@
 /lstat01_64
 /lstat02
 /lstat02_64
+/lstat03
+/lstat03_64
diff --git a/testcases/kernel/syscalls/lstat/lstat03.c b/testcases/kernel/syscalls/lstat/lstat03.c
new file mode 100644
index 000000000..af852169f
--- /dev/null
+++ b/testcases/kernel/syscalls/lstat/lstat03.c
@@ -0,0 +1,204 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2000 Silicon Graphics, Inc.  All Rights Reserved.
+ *    Author: David Fenner, Jon Hendrickson
+ * Copyright (C) 2024 Andrea Cervesato andrea.cervesato@suse.com
+ */
+
+/*\
+ * [Description]
+ *
+ * This test verifies that lstat() provides correct information according
+ * with device, access time, block size, ownership, etc.
+ * The implementation provides a set of tests which are specific for each one
+ * of the `struct stat` used to read file and symlink information.
+ */
+
+#include "tst_test.h"
+
+#define MNTPOINT "mntpoint"
+
+static void test_dev(void)
+{
+	char *filename = "file_dev";
+	char *symname = MNTPOINT"/sym_dev";
+
+	tst_res(TINFO, "Test symlink device");
+
+	SAFE_TOUCH(filename, 0777, NULL);
+	SAFE_SYMLINK(filename, symname);
+
+	struct stat path;
+	struct stat link;
+
+	TST_EXP_PASS(lstat(filename, &path));
+	TST_EXP_PASS(lstat(symname, &link));
+
+	TST_EXP_EXPR(path.st_dev != link.st_dev, "path.st_dev != link.st_dev");
+	TST_EXP_EXPR(path.st_ino != link.st_ino, "path.st_ino != link.st_ino");
+
+	SAFE_UNLINK(symname);
+}
+
+static void test_nlink(void)
+{
+	char *filename = "file_nlink";
+	char *symname = "sym_nlink";
+
+	tst_res(TINFO, "Test symlink hard link");
+
+	SAFE_TOUCH(filename, 0777, NULL);
+	SAFE_SYMLINK(filename, symname);
+
+	struct stat path;
+	struct stat link;
+
+	TST_EXP_PASS(lstat(filename, &path));
+	TST_EXP_PASS(lstat(symname, &link));
+
+	TST_EXP_EQ_LI(path.st_nlink, link.st_nlink);
+
+	SAFE_UNLINK(symname);
+}
+
+static void test_ownership(void)
+{
+	char *filename = "file_all";
+	char *symname = "sym_ownership";
+
+	tst_res(TINFO, "Test symlink ownership");
+
+	SAFE_TOUCH(filename, 0777, NULL);
+	SAFE_SYMLINK(filename, symname);
+
+	SAFE_CHOWN(symname, 1000, 1000);
+
+	struct stat path;
+	struct stat link;
+
+	TST_EXP_PASS(lstat(filename, &path));
+	TST_EXP_PASS(lstat(symname, &link));
+
+	TST_EXP_EXPR(path.st_uid != link.st_uid, "path.st_uid != link.st_uid");
+	TST_EXP_EXPR(path.st_gid != link.st_gid, "path.st_gid != link.st_gid");
+
+	SAFE_UNLINK(symname);
+}
+
+static void test_filesize(void)
+{
+	char *filename = "file_size";
+	char *symname = "sym_size";
+	int fd;
+
+	tst_res(TINFO, "Test symlink filesize");
+
+	SAFE_TOUCH(filename, 0777, NULL);
+
+	fd = SAFE_OPEN(filename, O_WRONLY, 0777);
+	tst_fill_fd(fd, 'a', TST_KB, 500);
+	SAFE_CLOSE(fd);
+
+	SAFE_SYMLINK(filename, symname);
+
+	struct stat path;
+	struct stat link;
+
+	TST_EXP_PASS(lstat(filename, &path));
+	TST_EXP_PASS(lstat(symname, &link));
+
+	TST_EXP_EXPR(path.st_size != link.st_size, "path.st_size != link.st_size");
+	TST_EXP_EXPR(path.st_blocks != link.st_blocks, "path.st_blocks != link.st_blocks");
+
+	SAFE_UNLINK(symname);
+}
+
+static void test_blksize(void)
+{
+	char *filename = "file_blksize";
+	char *symname = MNTPOINT"/sym_blksize";
+
+	tst_res(TINFO, "Test symlink blksize");
+
+	SAFE_TOUCH(filename, 0777, NULL);
+	SAFE_SYMLINK(filename, symname);
+
+	struct stat path;
+	struct stat link;
+
+	TST_EXP_PASS(lstat(filename, &path));
+	TST_EXP_PASS(lstat(symname, &link));
+
+	TST_EXP_EXPR(path.st_blksize != link.st_blksize, "path.st_blksize != link.st_blksize");
+
+	SAFE_UNLINK(symname);
+}
+
+static void test_timestamp(void)
+{
+	char *filename = "file_timestamp";
+	char *symname = "sym_timestamp";
+
+	tst_res(TINFO, "Test symlink timestamp");
+
+	SAFE_TOUCH(filename, 0777, NULL);
+
+	/* we wait a bit before creating symlink in order to obtain
+	 * different timestamp values
+	 */
+	sleep(1);
+	SAFE_SYMLINK(filename, symname);
+
+	struct stat path;
+	struct stat link;
+
+	TST_EXP_PASS(lstat(filename, &path));
+	TST_EXP_PASS(lstat(symname, &link));
+
+	TST_EXP_EXPR(path.st_atime != link.st_atime, "path.st_atime != link.st_atime");
+	TST_EXP_EXPR(path.st_mtime != link.st_mtime, "path.st_mtime != link.st_mtime");
+	TST_EXP_EXPR(path.st_ctime != link.st_ctime, "path.st_ctime != link.st_ctime");
+
+	SAFE_UNLINK(symname);
+}
+
+static void run(void)
+{
+	test_dev();
+	test_nlink();
+	test_ownership();
+	test_filesize();
+	test_blksize();
+	test_timestamp();
+}
+
+static void setup(void)
+{
+	char opt_bsize[32];
+	const char *const fs_opts[] = {opt_bsize, NULL};
+	struct stat sb;
+	int pagesize;
+
+	SAFE_STAT(".", &sb);
+	pagesize = sb.st_blksize == 4096 ? 1024 : 4096;
+
+	snprintf(opt_bsize, sizeof(opt_bsize), "-b %i", pagesize);
+	SAFE_MKFS(tst_device->dev, tst_device->fs_type, fs_opts, NULL);
+	SAFE_MOUNT(tst_device->dev, MNTPOINT, tst_device->fs_type, 0, 0);
+}
+
+static void cleanup(void)
+{
+	if (tst_is_mounted(MNTPOINT))
+		SAFE_UMOUNT(MNTPOINT);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test_all = run,
+	.needs_root = 1,
+	.needs_tmpdir = 1,
+	.needs_device = 1,
+	.mntpoint = MNTPOINT,
+};
-- 
2.35.3



More information about the ltp mailing list