Skip to contentSkip to conversation

selftests: introduce additional eventfd test coverage

PATCH Superseded Wen Yang 2 participants 3 messages May 5, 2024 – May 7, 2024 Kselftest

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wen Yang

author
#0 lore raw

PATCH 1/1 selftests: introduce additional eventfd test coverage

Add several new test cases which assert corner cases on the eventfd mechanism, for example, the supplied buffer is less than 8 bytes, attempting to write a value that is too large, etc.

./eventfd_test
# Starting 9 tests from 1 test cases.
# RUN global.eventfd01 ...
# OK global.eventfd01
ok 1 global.eventfd01
# RUN global.eventfd02 ...
# OK global.eventfd02
ok 2 global.eventfd02
# RUN global.eventfd03 ...
# OK global.eventfd03
ok 3 global.eventfd03
# RUN global.eventfd04 ...
# OK global.eventfd04
ok 4 global.eventfd04
# RUN global.eventfd05 ...
# OK global.eventfd05
ok 5 global.eventfd05
# RUN global.eventfd06 ...
# OK global.eventfd06
ok 6 global.eventfd06
# RUN global.eventfd07 ...
# OK global.eventfd07
ok 7 global.eventfd07
# RUN global.eventfd08 ...
# OK global.eventfd08
ok 8 global.eventfd08
# RUN global.eventfd09 ...
# OK global.eventfd09
ok 9 global.eventfd09
# PASSED: 9 / 9 tests passed.
# Totals: pass:9 fail:0 xfail:0 xpass:0 skip:0 error:0

Cc SShuah Khan <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Andrei Vagin <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Dave Young <[email protected]>
---
 .../filesystems/eventfd/eventfd_test.c        | 116 ++++++++++++++++++
 1 file changed, 116 insertions(+)
tools/testing/selftests/filesystems/eventfd/eventfd_test.c +116 -0
index f142a137526c..eeab8df5b1b5 100644
@@ -183,4 +183,120 @@ TEST(eventfd05)
 	close(fd);
 }
 
+/*
+ * A write(2) fails with the error EINVAL if the size of the supplied buffer
+ * is less than 8 bytes, or if an attempt is made to write the value
+ * 0xffffffffffffffff.
+ */
+TEST(eventfd06)
+{
+	uint64_t value = 1;
+	ssize_t size;
+	int fd;
+
+	fd = sys_eventfd2(0, 0);
+	ASSERT_GE(fd, 0);
+
+	size = write(fd, &value, sizeof(int));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	value = (uint64_t)-1;
+	size = write(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	close(fd);
+}
+
+/*
+ * A read(2) fails with the error EINVAL if the size of the supplied buffer is
+ * less than 8 bytes.
+ */
+TEST(eventfd07)
+{
+	int value = 0;
+	ssize_t size;
+	int fd;
+
+	fd = sys_eventfd2(0, 0);
+	ASSERT_GE(fd, 0);
+
+	size = write(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	close(fd);
+}
+
+/*
+ * If EFD_SEMAPHORE was not specified and the eventfd counter has a nonzero
+ * value, then a read(2) returns 8 bytes containing that value, and the
+ * counter's value is reset to zero.
+ * If the eventfd counter is zero at the time of the call to read(2), then the
+ * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
+ */
+TEST(eventfd08)
+{
+	uint64_t value;
+	ssize_t size;
+	int fd;
+	int i;
+
+	fd = sys_eventfd2(0, EFD_NONBLOCK);
+	ASSERT_GE(fd, 0);
+
+	value = 1;
+	for (i = 0; i < 10000000; i++) {
+		size = write(fd, &value, sizeof(value));
+		EXPECT_EQ(size, sizeof(value));
+	}
+
+	size = read(fd, &value, sizeof(value));
+	EXPECT_EQ(size, sizeof(uint64_t));
+	EXPECT_EQ(value, 10000000);
+
+	size = read(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EAGAIN);
+
+	close(fd);
+}
+
+/*
+ * If EFD_SEMAPHORE was specified and the eventfd counter has a nonzero value,
+ * then a read(2) returns 8 bytes containing the value 1, and the counter's
+ * value is decremented by 1.
+ * If the eventfd counter is zero at the time of the call to read(2), then the
+ * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
+ */
+TEST(eventfd09)
+{
+	uint64_t value;
+	ssize_t size;
+	int fd;
+	int i;
+
+	fd = sys_eventfd2(0, EFD_SEMAPHORE|EFD_NONBLOCK);
+	ASSERT_GE(fd, 0);
+
+	value = 1;
+	for (i = 0; i < 10000000; i++) {
+		size = write(fd, &value, sizeof(value));
+		EXPECT_EQ(size, sizeof(value));
+	}
+
+	for (i = 0; i < 10000000; i++) {
+		size = read(fd, &value, sizeof(value));
+		EXPECT_EQ(size, sizeof(value));
+		EXPECT_EQ(value, 1);
+	}
+
+	size = read(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EAGAIN);
+
+	close(fd);
+}
+
 TEST_HARNESS_MAIN
Signature · 1 line
2.25.1
#1 lore raw
Quoted 8 lines

-----Original Message-----
From: Wen Yang <[email protected]>
Add several new test cases which assert corner cases on the eventfd mechanism, for example, the supplied buffer is less than 8 bytes, attempting to write a value that is too large, etc.

Quoted 5 lines

./eventfd_test
# Starting 9 tests from 1 test cases.
# RUN global.eventfd01 ...
# OK global.eventfd01
ok 1 global.eventfd01

These are horrible test case names. Can you please use strings which indicate what is being tested, that are useful to a human?
-- Tim

Quoted 169 lines

# RUN global.eventfd02 ...
# OK global.eventfd02
ok 2 global.eventfd02
# RUN global.eventfd03 ...
# OK global.eventfd03
ok 3 global.eventfd03
# RUN global.eventfd04 ...
# OK global.eventfd04
ok 4 global.eventfd04
# RUN global.eventfd05 ...
# OK global.eventfd05
ok 5 global.eventfd05
# RUN global.eventfd06 ...
# OK global.eventfd06
ok 6 global.eventfd06
# RUN global.eventfd07 ...
# OK global.eventfd07
ok 7 global.eventfd07
# RUN global.eventfd08 ...
# OK global.eventfd08
ok 8 global.eventfd08
# RUN global.eventfd09 ...
# OK global.eventfd09
ok 9 global.eventfd09
# PASSED: 9 / 9 tests passed.
# Totals: pass:9 fail:0 xfail:0 xpass:0 skip:0 error:0

Cc SShuah Khan <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Andrei Vagin <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Dave Young <[email protected]>
---
 .../filesystems/eventfd/eventfd_test.c        | 116 ++++++++++++++++++
 1 file changed, 116 insertions(+)
tools/testing/selftests/filesystems/eventfd/eventfd_test.c +116 -1
index f142a137526c..eeab8df5b1b5 100644
@@ -183,4 +183,120 @@ TEST(eventfd05)
 	close(fd);
 }
 
+/*
+ * A write(2) fails with the error EINVAL if the size of the supplied buffer
+ * is less than 8 bytes, or if an attempt is made to write the value
+ * 0xffffffffffffffff.
+ */
+TEST(eventfd06)
+{
+	uint64_t value = 1;
+	ssize_t size;
+	int fd;
+
+	fd = sys_eventfd2(0, 0);
+	ASSERT_GE(fd, 0);
+
+	size = write(fd, &value, sizeof(int));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	value = (uint64_t)-1;
+	size = write(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	close(fd);
+}
+
+/*
+ * A read(2) fails with the error EINVAL if the size of the supplied buffer is
+ * less than 8 bytes.
+ */
+TEST(eventfd07)
+{
+	int value = 0;
+	ssize_t size;
+	int fd;
+
+	fd = sys_eventfd2(0, 0);
+	ASSERT_GE(fd, 0);
+
+	size = write(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	close(fd);
+}
+
+/*
+ * If EFD_SEMAPHORE was not specified and the eventfd counter has a nonzero
+ * value, then a read(2) returns 8 bytes containing that value, and the
+ * counter's value is reset to zero.
+ * If the eventfd counter is zero at the time of the call to read(2), then the
+ * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
+ */
+TEST(eventfd08)
+{
+	uint64_t value;
+	ssize_t size;
+	int fd;
+	int i;
+
+	fd = sys_eventfd2(0, EFD_NONBLOCK);
+	ASSERT_GE(fd, 0);
+
+	value = 1;
+	for (i = 0; i < 10000000; i++) {
+		size = write(fd, &value, sizeof(value));
+		EXPECT_EQ(size, sizeof(value));
+	}
+
+	size = read(fd, &value, sizeof(value));
+	EXPECT_EQ(size, sizeof(uint64_t));
+	EXPECT_EQ(value, 10000000);
+
+	size = read(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EAGAIN);
+
+	close(fd);
+}
+
+/*
+ * If EFD_SEMAPHORE was specified and the eventfd counter has a nonzero value,
+ * then a read(2) returns 8 bytes containing the value 1, and the counter's
+ * value is decremented by 1.
+ * If the eventfd counter is zero at the time of the call to read(2), then the
+ * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
+ */
+TEST(eventfd09)
+{
+	uint64_t value;
+	ssize_t size;
+	int fd;
+	int i;
+
+	fd = sys_eventfd2(0, EFD_SEMAPHORE|EFD_NONBLOCK);
+	ASSERT_GE(fd, 0);
+
+	value = 1;
+	for (i = 0; i < 10000000; i++) {
+		size = write(fd, &value, sizeof(value));
+		EXPECT_EQ(size, sizeof(value));
+	}
+
+	for (i = 0; i < 10000000; i++) {
+		size = read(fd, &value, sizeof(value));
+		EXPECT_EQ(size, sizeof(value));
+		EXPECT_EQ(value, 1);
+	}
+
+	size = read(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EAGAIN);
+
+	close(fd);
+}
+
 TEST_HARNESS_MAIN
--

2.25.1

author
#2 lore raw
Quoted 14 lines · Tim
Quoted 8 lines

-----Original Message-----
From: Wen Yang <[email protected]>
Add several new test cases which assert corner cases on the eventfd mechanism, for example, the supplied buffer is less than 8 bytes, attempting to write a value that is too large, etc.

Quoted 5 lines

./eventfd_test
# Starting 9 tests from 1 test cases.
# RUN global.eventfd01 ...
# OK global.eventfd01
ok 1 global.eventfd01

Quoted 4 lines

These are horrible test case names. Can you please use strings which indicate what is being tested, that are useful to a human?
-- Tim

Thank you for the review.
I will do the change and send v2 soon.

--
Best wishes,
Wen

Quoted 170 lines
Quoted 169 lines

# RUN global.eventfd02 ...
# OK global.eventfd02
ok 2 global.eventfd02
# RUN global.eventfd03 ...
# OK global.eventfd03
ok 3 global.eventfd03
# RUN global.eventfd04 ...
# OK global.eventfd04
ok 4 global.eventfd04
# RUN global.eventfd05 ...
# OK global.eventfd05
ok 5 global.eventfd05
# RUN global.eventfd06 ...
# OK global.eventfd06
ok 6 global.eventfd06
# RUN global.eventfd07 ...
# OK global.eventfd07
ok 7 global.eventfd07
# RUN global.eventfd08 ...
# OK global.eventfd08
ok 8 global.eventfd08
# RUN global.eventfd09 ...
# OK global.eventfd09
ok 9 global.eventfd09
# PASSED: 9 / 9 tests passed.
# Totals: pass:9 fail:0 xfail:0 xpass:0 skip:0 error:0

Cc SShuah Khan <[email protected]>
Cc Christian Brauner <[email protected]>
Cc Andrei Vagin <[email protected]>
Cc Mathieu Desnoyers <[email protected]>
Cc Steven Rostedt <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Dave Young <[email protected]>
---
  .../filesystems/eventfd/eventfd_test.c        | 116 ++++++++++++++++++
  1 file changed, 116 insertions(+)
tools/testing/selftests/filesystems/eventfd/eventfd_test.c +116 -1
index f142a137526c..eeab8df5b1b5 100644
@@ -183,4 +183,120 @@ TEST(eventfd05)
  	close(fd);
  }
 
+/*
+ * A write(2) fails with the error EINVAL if the size of the supplied buffer
+ * is less than 8 bytes, or if an attempt is made to write the value
+ * 0xffffffffffffffff.
+ */
+TEST(eventfd06)
+{
+	uint64_t value = 1;
+	ssize_t size;
+	int fd;
+
+	fd = sys_eventfd2(0, 0);
+	ASSERT_GE(fd, 0);
+
+	size = write(fd, &value, sizeof(int));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	value = (uint64_t)-1;
+	size = write(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	close(fd);
+}
+
+/*
+ * A read(2) fails with the error EINVAL if the size of the supplied buffer is
+ * less than 8 bytes.
+ */
+TEST(eventfd07)
+{
+	int value = 0;
+	ssize_t size;
+	int fd;
+
+	fd = sys_eventfd2(0, 0);
+	ASSERT_GE(fd, 0);
+
+	size = write(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EINVAL);
+
+	close(fd);
+}
+
+/*
+ * If EFD_SEMAPHORE was not specified and the eventfd counter has a nonzero
+ * value, then a read(2) returns 8 bytes containing that value, and the
+ * counter's value is reset to zero.
+ * If the eventfd counter is zero at the time of the call to read(2), then the
+ * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
+ */
+TEST(eventfd08)
+{
+	uint64_t value;
+	ssize_t size;
+	int fd;
+	int i;
+
+	fd = sys_eventfd2(0, EFD_NONBLOCK);
+	ASSERT_GE(fd, 0);
+
+	value = 1;
+	for (i = 0; i < 10000000; i++) {
+		size = write(fd, &value, sizeof(value));
+		EXPECT_EQ(size, sizeof(value));
+	}
+
+	size = read(fd, &value, sizeof(value));
+	EXPECT_EQ(size, sizeof(uint64_t));
+	EXPECT_EQ(value, 10000000);
+
+	size = read(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EAGAIN);
+
+	close(fd);
+}
+
+/*
+ * If EFD_SEMAPHORE was specified and the eventfd counter has a nonzero value,
+ * then a read(2) returns 8 bytes containing the value 1, and the counter's
+ * value is decremented by 1.
+ * If the eventfd counter is zero at the time of the call to read(2), then the
+ * call fails with the error EAGAIN if the file descriptor has been made nonblocking.
+ */
+TEST(eventfd09)
+{
+	uint64_t value;
+	ssize_t size;
+	int fd;
+	int i;
+
+	fd = sys_eventfd2(0, EFD_SEMAPHORE|EFD_NONBLOCK);
+	ASSERT_GE(fd, 0);
+
+	value = 1;
+	for (i = 0; i < 10000000; i++) {
+		size = write(fd, &value, sizeof(value));
+		EXPECT_EQ(size, sizeof(value));
+	}
+
+	for (i = 0; i < 10000000; i++) {
+		size = read(fd, &value, sizeof(value));
+		EXPECT_EQ(size, sizeof(value));
+		EXPECT_EQ(value, 1);
+	}
+
+	size = read(fd, &value, sizeof(value));
+	EXPECT_EQ(size, -1);
+	EXPECT_EQ(errno, EAGAIN);
+
+	close(fd);
+}
+
  TEST_HARNESS_MAIN
--

2.25.1