* [LTP] [PATCH v2] unlink: Add error tests for EPERM and EROFS
@ 2024-04-11 5:49 Yang Xu via ltp
2024-04-16 14:47 ` Avinesh Kumar
0 siblings, 1 reply; 2+ messages in thread
From: Yang Xu via ltp @ 2024-04-11 5:49 UTC (permalink / raw)
To: ltp
Add negative cases for unlink(), when errno is EPERM or EROFS.
Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
---
runtest/syscalls | 1 +
testcases/kernel/syscalls/unlink/.gitignore | 1 +
testcases/kernel/syscalls/unlink/unlink09.c | 102 ++++++++++++++++++++
3 files changed, 104 insertions(+)
create mode 100644 testcases/kernel/syscalls/unlink/unlink09.c
diff --git a/runtest/syscalls b/runtest/syscalls
index 4ed2b5602..b99ce7170 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1651,6 +1651,7 @@ unlink01 symlink01 -T unlink01
unlink05 unlink05
unlink07 unlink07
unlink08 unlink08
+unlink09 unlink09
#unlinkat test cases
unlinkat01 unlinkat01
diff --git a/testcases/kernel/syscalls/unlink/.gitignore b/testcases/kernel/syscalls/unlink/.gitignore
index 2e783580d..6038cc29d 100644
--- a/testcases/kernel/syscalls/unlink/.gitignore
+++ b/testcases/kernel/syscalls/unlink/.gitignore
@@ -1,3 +1,4 @@
/unlink05
/unlink07
/unlink08
+/unlink09
diff --git a/testcases/kernel/syscalls/unlink/unlink09.c b/testcases/kernel/syscalls/unlink/unlink09.c
new file mode 100644
index 000000000..3ab478e50
--- /dev/null
+++ b/testcases/kernel/syscalls/unlink/unlink09.c
@@ -0,0 +1,102 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that unlink(2) fails with
+ *
+ * - EPERM when target file is marked as immutable or append-only
+ * - EROFS when target file is on a read-only filesystem.
+ */
+
+#include <sys/ioctl.h>
+#include "tst_test.h"
+#include "lapi/fs.h"
+
+#define TEST_EPERM_IMMUTABLE "test_eperm_immutable"
+#define TEST_EPERM_APPEND_ONLY "test_eperm_append_only"
+#define DIR_EROFS "erofs"
+#define TEST_EROFS "erofs/test_erofs"
+
+static int fd_immutable;
+static int fd_append_only;
+
+static struct test_case_t {
+ char *filename;
+ int *fd;
+ int flag;
+ int expected_errno;
+ char *desc;
+} tcases[] = {
+ {TEST_EPERM_IMMUTABLE, &fd_immutable, FS_IMMUTABLE_FL, EPERM,
+ "target file is immutable"},
+ {TEST_EPERM_APPEND_ONLY, &fd_append_only, FS_APPEND_FL, EPERM,
+ "target file is append-only"},
+ {TEST_EROFS, NULL, 0, EROFS, "target file in read-only filesystem"},
+};
+
+static void setup(void)
+{
+ int attr;
+
+ fd_immutable = SAFE_OPEN(TEST_EPERM_IMMUTABLE, O_CREAT, 0600);
+ SAFE_IOCTL(fd_immutable, FS_IOC_GETFLAGS, &attr);
+ attr |= FS_IMMUTABLE_FL;
+ SAFE_IOCTL(fd_immutable, FS_IOC_SETFLAGS, &attr);
+
+ fd_append_only = SAFE_OPEN(TEST_EPERM_APPEND_ONLY, O_CREAT, 0600);
+ SAFE_IOCTL(fd_append_only, FS_IOC_GETFLAGS, &attr);
+ attr |= FS_APPEND_FL;
+ SAFE_IOCTL(fd_append_only, FS_IOC_SETFLAGS, &attr);
+}
+
+static void cleanup(void)
+{
+ int attr;
+
+ SAFE_IOCTL(fd_immutable, FS_IOC_GETFLAGS, &attr);
+ attr &= ~FS_IMMUTABLE_FL;
+ SAFE_IOCTL(fd_immutable, FS_IOC_SETFLAGS, &attr);
+ SAFE_CLOSE(fd_immutable);
+
+ SAFE_IOCTL(fd_append_only, FS_IOC_GETFLAGS, &attr);
+ attr &= ~FS_APPEND_FL;
+ SAFE_IOCTL(fd_append_only, FS_IOC_SETFLAGS, &attr);
+ SAFE_CLOSE(fd_append_only);
+}
+
+static void verify_unlink(unsigned int i)
+{
+ struct test_case_t *tc = &tcases[i];
+ int attr;
+
+ TST_EXP_FAIL(unlink(tc->filename), tc->expected_errno, "%s", tc->desc);
+
+ /* If unlink() suceeded unexpectedly, test file should be retored. */
+ if (!TST_RET) {
+ if (tc->fd) {
+ *(tc->fd) = SAFE_OPEN(tc->filename, O_CREAT, 0600);
+ if (tc->flag) {
+ SAFE_IOCTL(*(tc->fd), FS_IOC_GETFLAGS, &attr);
+ attr |= tc->flag;
+ SAFE_IOCTL(*(tc->fd), FS_IOC_SETFLAGS, &attr);
+ }
+ } else {
+ SAFE_TOUCH(tc->filename, 0600, 0);
+ }
+ }
+}
+
+static struct tst_test test = {
+ .setup = setup,
+ .tcnt = ARRAY_SIZE(tcases),
+ .cleanup = cleanup,
+ .test = verify_unlink,
+ .needs_rofs = 1,
+ .mntpoint = DIR_EROFS,
+ .needs_root = 1,
+};
--
2.39.3
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [LTP] [PATCH v2] unlink: Add error tests for EPERM and EROFS
2024-04-11 5:49 [LTP] [PATCH v2] unlink: Add error tests for EPERM and EROFS Yang Xu via ltp
@ 2024-04-16 14:47 ` Avinesh Kumar
0 siblings, 0 replies; 2+ messages in thread
From: Avinesh Kumar @ 2024-04-16 14:47 UTC (permalink / raw)
To: Yang Xu; +Cc: ltp
Hi,
Reviewed-by: Avinesh Kumar <akumar@suse.de>
with below typo fixes.
On Thursday, April 11, 2024 7:49:40 AM GMT+2 you wrote:
> Add negative cases for unlink(), when errno is EPERM or EROFS.
>
> Signed-off-by: Yang Xu <xuyang2018.jy@fujitsu.com>
> ---
> runtest/syscalls | 1 +
> testcases/kernel/syscalls/unlink/.gitignore | 1 +
> testcases/kernel/syscalls/unlink/unlink09.c | 102 ++++++++++++++++++++
> 3 files changed, 104 insertions(+)
> create mode 100644 testcases/kernel/syscalls/unlink/unlink09.c
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index 4ed2b5602..b99ce7170 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -1651,6 +1651,7 @@ unlink01 symlink01 -T unlink01
> unlink05 unlink05
> unlink07 unlink07
> unlink08 unlink08
> +unlink09 unlink09
>
> #unlinkat test cases
> unlinkat01 unlinkat01
> diff --git a/testcases/kernel/syscalls/unlink/.gitignore
> b/testcases/kernel/syscalls/unlink/.gitignore index 2e783580d..6038cc29d
> 100644
> --- a/testcases/kernel/syscalls/unlink/.gitignore
> +++ b/testcases/kernel/syscalls/unlink/.gitignore
> @@ -1,3 +1,4 @@
> /unlink05
> /unlink07
> /unlink08
> +/unlink09
> diff --git a/testcases/kernel/syscalls/unlink/unlink09.c
> b/testcases/kernel/syscalls/unlink/unlink09.c new file mode 100644
> index 000000000..3ab478e50
> --- /dev/null
> +++ b/testcases/kernel/syscalls/unlink/unlink09.c
> @@ -0,0 +1,102 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
> + * Author: Yang Xu <xuyang2018.jy@fujitsu.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Verify that unlink(2) fails with
> + *
> + * - EPERM when target file is marked as immutable or append-only
> + * - EROFS when target file is on a read-only filesystem.
> + */
> +
> +#include <sys/ioctl.h>
> +#include "tst_test.h"
> +#include "lapi/fs.h"
> +
> +#define TEST_EPERM_IMMUTABLE "test_eperm_immutable"
> +#define TEST_EPERM_APPEND_ONLY "test_eperm_append_only"
> +#define DIR_EROFS "erofs"
> +#define TEST_EROFS "erofs/test_erofs"
> +
> +static int fd_immutable;
> +static int fd_append_only;
> +
> +static struct test_case_t {
> + char *filename;
> + int *fd;
> + int flag;
> + int expected_errno;
> + char *desc;
> +} tcases[] = {
> + {TEST_EPERM_IMMUTABLE, &fd_immutable, FS_IMMUTABLE_FL, EPERM,
> + "target file is immutable"},
> + {TEST_EPERM_APPEND_ONLY, &fd_append_only, FS_APPEND_FL, EPERM,
> + "target file is append-only"},
> + {TEST_EROFS, NULL, 0, EROFS, "target file in read-only filesystem"},
> +};
> +
> +static void setup(void)
> +{
> + int attr;
> +
> + fd_immutable = SAFE_OPEN(TEST_EPERM_IMMUTABLE, O_CREAT, 0600);
> + SAFE_IOCTL(fd_immutable, FS_IOC_GETFLAGS, &attr);
> + attr |= FS_IMMUTABLE_FL;
> + SAFE_IOCTL(fd_immutable, FS_IOC_SETFLAGS, &attr);
> +
> + fd_append_only = SAFE_OPEN(TEST_EPERM_APPEND_ONLY, O_CREAT, 0600);
> + SAFE_IOCTL(fd_append_only, FS_IOC_GETFLAGS, &attr);
> + attr |= FS_APPEND_FL;
> + SAFE_IOCTL(fd_append_only, FS_IOC_SETFLAGS, &attr);
> +}
> +
> +static void cleanup(void)
> +{
> + int attr;
> +
> + SAFE_IOCTL(fd_immutable, FS_IOC_GETFLAGS, &attr);
> + attr &= ~FS_IMMUTABLE_FL;
> + SAFE_IOCTL(fd_immutable, FS_IOC_SETFLAGS, &attr);
> + SAFE_CLOSE(fd_immutable);
> +
> + SAFE_IOCTL(fd_append_only, FS_IOC_GETFLAGS, &attr);
> + attr &= ~FS_APPEND_FL;
> + SAFE_IOCTL(fd_append_only, FS_IOC_SETFLAGS, &attr);
> + SAFE_CLOSE(fd_append_only);
> +}
> +
> +static void verify_unlink(unsigned int i)
> +{
> + struct test_case_t *tc = &tcases[i];
> + int attr;
> +
> + TST_EXP_FAIL(unlink(tc->filename), tc->expected_errno, "%s", tc->desc);
> +
> + /* If unlink() suceeded unexpectedly, test file should be retored. */
s/suceeded/succeeded
s/retored/restored
> + if (!TST_RET) {
> + if (tc->fd) {
> + *(tc->fd) = SAFE_OPEN(tc->filename, O_CREAT, 0600);
> + if (tc->flag) {
> + SAFE_IOCTL(*(tc->fd), FS_IOC_GETFLAGS, &attr);
> + attr |= tc->flag;
> + SAFE_IOCTL(*(tc->fd), FS_IOC_SETFLAGS, &attr);
> + }
> + } else {
> + SAFE_TOUCH(tc->filename, 0600, 0);
> + }
> + }
> +}
> +
> +static struct tst_test test = {
> + .setup = setup,
> + .tcnt = ARRAY_SIZE(tcases),
> + .cleanup = cleanup,
> + .test = verify_unlink,
> + .needs_rofs = 1,
> + .mntpoint = DIR_EROFS,
> + .needs_root = 1,
> +};
Regards,
Avinesh
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-04-16 14:47 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-11 5:49 [LTP] [PATCH v2] unlink: Add error tests for EPERM and EROFS Yang Xu via ltp
2024-04-16 14:47 ` Avinesh Kumar
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.