diff mbox series

[V4,2/3] syscalls: select: Verify that data is available to read

Message ID 63646c1ba9e1a3061b44be4f1f9a29d9d6d03f2e.1603254560.git.viresh.kumar@linaro.org
State Accepted
Headers show
Series None | expand

Commit Message

Viresh Kumar Oct. 21, 2020, 4:32 a.m. UTC
select() returns a positive value on success if timeout hasn't happened,
else returns 0. Check that and send some data to the write file
descriptor for the same.

Acked-by: Li Wang <liwang@redhat.com>
Signed-off-by: Viresh Kumar <viresh.kumar@linaro.org>
---
V4:
- test individual bits in the fd_sets to verify properly.

 testcases/kernel/syscalls/select/select01.c | 23 +++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)

Comments

Cyril Hrubis Oct. 21, 2020, 12:04 p.m. UTC | #1
Hi!
> +	else if (!TST_RET)
> +		tst_res(TFAIL, "select() timed out %s", tc->desc);
> +	else if (TST_RET != exp_ret)
> +		tst_res(TFAIL, "select() returned incorrect value %s, expected: %d, got: %lu", tc->desc, exp_ret, TST_RET);
> +	else if (FD_ISSET(*tc->readfd, tc->readfds) && (!tc->writefd || FD_ISSET(*tc->writefd, tc->writefds)))
>  		tst_res(TPASS, "select() passed %s", tc->desc);
> +	else
> +		tst_res(TFAIL, "select() returned expected value %s, but all file descriptors aren't ready", tc->desc);

In the end I've rewritten this part to use returns in order to avoid
this if else maze with overly long lines and pushed, thanks.

I guess that we still need a test where select would clear the bits from
fd->set though, I supposes that the easiest solution would be to add
select04.c for that...
Viresh Kumar Oct. 22, 2020, 4:54 a.m. UTC | #2
On 21-10-20, 14:04, Cyril Hrubis wrote:
> I guess that we still need a test where select would clear the bits from
> fd->set though, I supposes that the easiest solution would be to add
> select04.c for that...

I am not sure how to do that and why would that matter ? :)
Cyril Hrubis Oct. 22, 2020, 10:13 a.m. UTC | #3
Hi!
> > I guess that we still need a test where select would clear the bits from
> > fd->set though, I supposes that the easiest solution would be to add
> > select04.c for that...
> 
> I am not sure how to do that and why would that matter ? :)

So the tests we do have now are checking that the bits in the fdset are
not cleared when there are data ready to be read/written from/to the
filke descriptor, right?

What we need is a test where we ask for a data to be read from an empty
pipe, ask for data to be written to a pipe filled with data (write there
till we get EAGAIN in setup), etc. We can do this with a very short
timeout or even with a timeout set to zero (polling) and check that the
bits were cleared once we have returned from the call.
diff mbox series

Patch

diff --git a/testcases/kernel/syscalls/select/select01.c b/testcases/kernel/syscalls/select/select01.c
index 74538026adbe..68f813bb24b5 100644
--- a/testcases/kernel/syscalls/select/select01.c
+++ b/testcases/kernel/syscalls/select/select01.c
@@ -25,27 +25,42 @@  static struct tcases {
 	int *nfds;
 	fd_set *readfds;
 	fd_set *writefds;
+	int *readfd;
+	int *writefd;
 	char *desc;
 } tests[] = {
-	{&fd_reg, &readfds_reg, NULL, "with regular file"},
-	{&fds_pipe[1], &readfds_pipe, &writefds_pipe, "with system pipe"},
-	{&fd_npipe, &readfds_npipe, &writefds_npipe, "with named pipe (FIFO)"},
+	{&fd_reg, &readfds_reg, NULL, &fd_reg, NULL, "with regular file"},
+	{&fds_pipe[1], &readfds_pipe, &writefds_pipe, &fds_pipe[0], &fds_pipe[1], "with system pipe"},
+	{&fd_npipe, &readfds_npipe, &writefds_npipe, &fd_npipe, &fd_npipe, "with named pipe (FIFO)"},
 };
 
 static void run(unsigned int n)
 {
 	struct tcases *tc = &tests[n];
 	struct timeval timeout;
+	char buf;
+	int exp_ret = 1;
 
 	timeout.tv_sec = 0;
 	timeout.tv_usec = 100000;
 
+	if (tc->writefd) {
+		SAFE_WRITE(0, *tc->writefd, &buf, sizeof(buf));
+		exp_ret++;
+	}
+
 	TEST(do_select(*tc->nfds + 1, tc->readfds, tc->writefds, 0, &timeout));
 
 	if (TST_RET == -1)
 		tst_res(TFAIL | TTERRNO, "select() failed %s", tc->desc);
-	else
+	else if (!TST_RET)
+		tst_res(TFAIL, "select() timed out %s", tc->desc);
+	else if (TST_RET != exp_ret)
+		tst_res(TFAIL, "select() returned incorrect value %s, expected: %d, got: %lu", tc->desc, exp_ret, TST_RET);
+	else if (FD_ISSET(*tc->readfd, tc->readfds) && (!tc->writefd || FD_ISSET(*tc->writefd, tc->writefds)))
 		tst_res(TPASS, "select() passed %s", tc->desc);
+	else
+		tst_res(TFAIL, "select() returned expected value %s, but all file descriptors aren't ready", tc->desc);
 }
 
 static void setup(void)