diff mbox series

[2/2] signalfd: Add negative tests

Message ID 20240528103258.2390-2-maxj.fnst@fujitsu.com
State Changes Requested
Headers show
Series signalfd: Improve testsuite for signalfd | expand

Commit Message

Ma Xinjian May 28, 2024, 10:33 a.m. UTC
Add negative cases for signalfd(), when errno is EBADF or EINVAL

Signed-off-by: Ma Xinjian <maxj.fnst@fujitsu.com>
---
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/signalfd/.gitignore |   1 +
 .../kernel/syscalls/signalfd/signalfd02.c     | 115 ++++++++++++++++++
 3 files changed, 117 insertions(+)
 create mode 100644 testcases/kernel/syscalls/signalfd/signalfd02.c

Comments

Andrea Cervesato May 28, 2024, 11:31 a.m. UTC | #1
Hi!

Please take into account the same considerations made in signalfd01 test 
refactoring.

Regards,
Andrea Cervesato

On 5/28/24 12:33, Ma Xinjian via ltp wrote:
> Add negative cases for signalfd(), when errno is EBADF or EINVAL
>
> Signed-off-by: Ma Xinjian <maxj.fnst@fujitsu.com>
> ---
>   runtest/syscalls                              |   1 +
>   testcases/kernel/syscalls/signalfd/.gitignore |   1 +
>   .../kernel/syscalls/signalfd/signalfd02.c     | 115 ++++++++++++++++++
>   3 files changed, 117 insertions(+)
>   create mode 100644 testcases/kernel/syscalls/signalfd/signalfd02.c
>
> diff --git a/runtest/syscalls b/runtest/syscalls
> index a06b046ac..2dac79798 100644
> --- a/runtest/syscalls
> +++ b/runtest/syscalls
> @@ -1487,6 +1487,7 @@ signal05 signal05
>   signal06 signal06
>   
>   signalfd01 signalfd01
> +signalfd02 signalfd02
>   
>   signalfd4_01 signalfd4_01
>   signalfd4_02 signalfd4_02
> diff --git a/testcases/kernel/syscalls/signalfd/.gitignore b/testcases/kernel/syscalls/signalfd/.gitignore
> index 3c9ed737c..959022f41 100644
> --- a/testcases/kernel/syscalls/signalfd/.gitignore
> +++ b/testcases/kernel/syscalls/signalfd/.gitignore
> @@ -1 +1,2 @@
>   /signalfd01
> +/signalfd02
> diff --git a/testcases/kernel/syscalls/signalfd/signalfd02.c b/testcases/kernel/syscalls/signalfd/signalfd02.c
> new file mode 100644
> index 000000000..9ca943942
> --- /dev/null
> +++ b/testcases/kernel/syscalls/signalfd/signalfd02.c
> @@ -0,0 +1,115 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
> + * Author: Ma Xinjian <maxj.fnst@fujitsu.com>
> + */
> +
> +/*\
> + * [Description]
> + *
> + * Verify that signalfd(2) fails with
> + *
> + * - EBADF when fd is invalid
> + * - EINVAL when fd is not a valid signalfd file descriptor
> + * - EINVAL when flags are invalid
> + */
> +
> +#define _GNU_SOURCE
> +
> +#include <signal.h>
> +#include "tst_test.h"
> +#include "config.h"
> +
> +#ifndef HAVE_SIGNALFD
> +#define USE_STUB
> +#endif
> +
> +#ifdef HAVE_SYS_SIGNALFD_H
> +#include <sys/signalfd.h>
> +#elif defined HAVE_LINUX_SIGNALFD_H
> +#include <linux/signalfd.h>
> +#define USE_OWNIMPL
> +#else
> +#define USE_STUB
> +#endif /* HAVE_SYS_SIGNALFD_H */
> +
> +#ifndef HAVE_STRUCT_SIGNALFD_SIGINFO_SSI_SIGNO
> +#define USE_STUB
> +#endif /* HAVE_STRUCT_SIGNALFD_SIGINFO_SSI_SIGNO */
> +
> +#ifdef USE_STUB
> +TST_TEST_TCONF("This system does not support signalfd()");
> +#else /* USE_STUB */
> +#if defined USE_OWNIMPL
> +#include "lapi/syscalls.h"
> +int signalfd(int fd, const sigset_t *mask, int flags)
> +{
> +	return tst_syscall(__NR_signalfd, fd, mask, flags);
> +}
> +#endif /* USE_OWNIMPL */
> +
> +#define SIGNAL_FILE "signal_file"
> +
> +static int fd_ebadf = -2;
> +static int fd_einval1;
> +static int fd_einval2 = -1;
> +
> +static sigset_t mask1;
> +static sigset_t mask2;
> +static sigset_t mask3;
> +
> +static struct test_case_t {
> +	int *fd;
> +	sigset_t *mask;
> +	int flags;
> +	int expected_errno;
> +	char *desc;
> +} tcases[] = {
> +	{&fd_ebadf, &mask1, 0, EBADF, "fd is invalid"},
> +	{&fd_einval1, &mask2, 0, EINVAL,
> +		"fd is not a valid signalfd file descriptor"},
> +	{&fd_einval2, &mask3, -1, EINVAL, "flags are invalid"},
> +};
> +
> +static void setup(void)
> +{
> +	SAFE_SIGEMPTYSET(&mask1);
> +	SAFE_SIGADDSET(&mask1, SIGUSR1);
> +	SAFE_SIGPROCMASK(SIG_BLOCK, &mask1, NULL);
> +	SAFE_SIGEMPTYSET(&mask2);
> +	SAFE_SIGADDSET(&mask2, SIGUSR2);
> +	SAFE_SIGPROCMASK(SIG_BLOCK, &mask2, NULL);
> +	SAFE_SIGEMPTYSET(&mask2);
> +	SAFE_SIGADDSET(&mask3, SIGUSR2);
> +	SAFE_SIGPROCMASK(SIG_BLOCK, &mask3, NULL);
> +
> +	fd_einval1 = SAFE_OPEN(SIGNAL_FILE, O_CREAT, 0777);
> +}
> +
> +static void cleanup(void)
> +{
> +	if (fd_ebadf > 0)
> +		SAFE_CLOSE(fd_ebadf);
> +	if (fd_einval1 > 0)
> +		SAFE_CLOSE(fd_einval1);
> +	if (fd_einval2 > 0)
> +		SAFE_CLOSE(fd_einval2);
> +}
> +
> +static void verify_signalfd(unsigned int i)
> +{
> +	struct test_case_t *tc = &tcases[i];
> +
> +	TST_EXP_FAIL2(signalfd(*(tc->fd), tc->mask, tc->flags),
> +		tc->expected_errno, "%s", tc->desc);
> +}
> +
> +static struct tst_test test = {
> +	.tcnt = ARRAY_SIZE(tcases),
> +	.test = verify_signalfd,
> +	.setup = setup,
> +	.cleanup = cleanup,
> +	.needs_tmpdir = 1,
> +};
> +
> +#endif /* USE_STUB */
diff mbox series

Patch

diff --git a/runtest/syscalls b/runtest/syscalls
index a06b046ac..2dac79798 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -1487,6 +1487,7 @@  signal05 signal05
 signal06 signal06
 
 signalfd01 signalfd01
+signalfd02 signalfd02
 
 signalfd4_01 signalfd4_01
 signalfd4_02 signalfd4_02
diff --git a/testcases/kernel/syscalls/signalfd/.gitignore b/testcases/kernel/syscalls/signalfd/.gitignore
index 3c9ed737c..959022f41 100644
--- a/testcases/kernel/syscalls/signalfd/.gitignore
+++ b/testcases/kernel/syscalls/signalfd/.gitignore
@@ -1 +1,2 @@ 
 /signalfd01
+/signalfd02
diff --git a/testcases/kernel/syscalls/signalfd/signalfd02.c b/testcases/kernel/syscalls/signalfd/signalfd02.c
new file mode 100644
index 000000000..9ca943942
--- /dev/null
+++ b/testcases/kernel/syscalls/signalfd/signalfd02.c
@@ -0,0 +1,115 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2024 FUJITSU LIMITED. All Rights Reserved.
+ * Author: Ma Xinjian <maxj.fnst@fujitsu.com>
+ */
+
+/*\
+ * [Description]
+ *
+ * Verify that signalfd(2) fails with
+ *
+ * - EBADF when fd is invalid
+ * - EINVAL when fd is not a valid signalfd file descriptor
+ * - EINVAL when flags are invalid
+ */
+
+#define _GNU_SOURCE
+
+#include <signal.h>
+#include "tst_test.h"
+#include "config.h"
+
+#ifndef HAVE_SIGNALFD
+#define USE_STUB
+#endif
+
+#ifdef HAVE_SYS_SIGNALFD_H
+#include <sys/signalfd.h>
+#elif defined HAVE_LINUX_SIGNALFD_H
+#include <linux/signalfd.h>
+#define USE_OWNIMPL
+#else
+#define USE_STUB
+#endif /* HAVE_SYS_SIGNALFD_H */
+
+#ifndef HAVE_STRUCT_SIGNALFD_SIGINFO_SSI_SIGNO
+#define USE_STUB
+#endif /* HAVE_STRUCT_SIGNALFD_SIGINFO_SSI_SIGNO */
+
+#ifdef USE_STUB
+TST_TEST_TCONF("This system does not support signalfd()");
+#else /* USE_STUB */
+#if defined USE_OWNIMPL
+#include "lapi/syscalls.h"
+int signalfd(int fd, const sigset_t *mask, int flags)
+{
+	return tst_syscall(__NR_signalfd, fd, mask, flags);
+}
+#endif /* USE_OWNIMPL */
+
+#define SIGNAL_FILE "signal_file"
+
+static int fd_ebadf = -2;
+static int fd_einval1;
+static int fd_einval2 = -1;
+
+static sigset_t mask1;
+static sigset_t mask2;
+static sigset_t mask3;
+
+static struct test_case_t {
+	int *fd;
+	sigset_t *mask;
+	int flags;
+	int expected_errno;
+	char *desc;
+} tcases[] = {
+	{&fd_ebadf, &mask1, 0, EBADF, "fd is invalid"},
+	{&fd_einval1, &mask2, 0, EINVAL,
+		"fd is not a valid signalfd file descriptor"},
+	{&fd_einval2, &mask3, -1, EINVAL, "flags are invalid"},
+};
+
+static void setup(void)
+{
+	SAFE_SIGEMPTYSET(&mask1);
+	SAFE_SIGADDSET(&mask1, SIGUSR1);
+	SAFE_SIGPROCMASK(SIG_BLOCK, &mask1, NULL);
+	SAFE_SIGEMPTYSET(&mask2);
+	SAFE_SIGADDSET(&mask2, SIGUSR2);
+	SAFE_SIGPROCMASK(SIG_BLOCK, &mask2, NULL);
+	SAFE_SIGEMPTYSET(&mask2);
+	SAFE_SIGADDSET(&mask3, SIGUSR2);
+	SAFE_SIGPROCMASK(SIG_BLOCK, &mask3, NULL);
+
+	fd_einval1 = SAFE_OPEN(SIGNAL_FILE, O_CREAT, 0777);
+}
+
+static void cleanup(void)
+{
+	if (fd_ebadf > 0)
+		SAFE_CLOSE(fd_ebadf);
+	if (fd_einval1 > 0)
+		SAFE_CLOSE(fd_einval1);
+	if (fd_einval2 > 0)
+		SAFE_CLOSE(fd_einval2);
+}
+
+static void verify_signalfd(unsigned int i)
+{
+	struct test_case_t *tc = &tcases[i];
+
+	TST_EXP_FAIL2(signalfd(*(tc->fd), tc->mask, tc->flags),
+		tc->expected_errno, "%s", tc->desc);
+}
+
+static struct tst_test test = {
+	.tcnt = ARRAY_SIZE(tcases),
+	.test = verify_signalfd,
+	.setup = setup,
+	.cleanup = cleanup,
+	.needs_tmpdir = 1,
+};
+
+#endif /* USE_STUB */