diff mbox series

[mptcp-next,2/2] selftests: mptcp: check IP_TOS in/out are the same

Message ID 20211122112205.16752-2-fw@strlen.de (mailing list archive)
State Superseded, archived
Delegated to: Matthieu Baerts
Headers show
Series [mptcp-next,1/2] mptcp: getsockopt: add support for IP_TOS | expand

Commit Message

Florian Westphal Nov. 22, 2021, 11:22 a.m. UTC
Check that getsockopt(IP_TOS) returns what setsockopt(IP_TOS) did set
right before.

Signed-off-by: Florian Westphal <fw@strlen.de>
---
 .../selftests/net/mptcp/mptcp_sockopt.c       | 45 +++++++++++++++++++
 1 file changed, 45 insertions(+)

Comments

Matthieu Baerts Nov. 22, 2021, 3:05 p.m. UTC | #1
On 22/11/2021 12:22, Florian Westphal wrote:
> Check that getsockopt(IP_TOS) returns what setsockopt(IP_TOS) did set
> right before.

Thank you for adding the new selftest!

> +static void init_rng(void)

That's really a detail but this function in the other MPTCP selftests
always intrigued me. Do you mind me asking a few questions about it? :)

> +{
> +	int fd = open("/dev/urandom", O_RDONLY);
> +	unsigned int foo;
> +
> +	if (fd > 0) {

- Here, we do more if 'open()' succeeds or fails (0 is still a valid
fd). I guess it is unlikely to have 0 here because we don't close fd 0
elsewhere if I'm not mistaken.

- if open() "fails", 'foo' will be used later while undefined. Which is
fine in this context but static code analysers might complain.

> +		int ret = read(fd, &foo, sizeof(foo));
> +
> +		if (ret < 0)
> +			srand(fd + foo);

- Here we do this if 'read()' fails with an undefined 'foo'. Again it is
fine in this context but strange :)

> +		close(fd);

- should not happen: the 'fd' might not be closed if fd == 0.

> +	}
> +
> +	srand(foo);

The function looks strange but I don't see issue in this context.
Anyway, why did we not use the typical:

    srand(time(NULL))

Cheers,
Matt
diff mbox series

Patch

diff --git a/tools/testing/selftests/net/mptcp/mptcp_sockopt.c b/tools/testing/selftests/net/mptcp/mptcp_sockopt.c
index 417b11cafafe..87ed6d050e67 100644
--- a/tools/testing/selftests/net/mptcp/mptcp_sockopt.c
+++ b/tools/testing/selftests/net/mptcp/mptcp_sockopt.c
@@ -4,6 +4,7 @@ 
 
 #include <assert.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <string.h>
 #include <stdarg.h>
@@ -594,6 +595,30 @@  static int server(int pipefd)
 	return 0;
 }
 
+static void test_ip_tos_sockopt(int fd)
+{
+	uint8_t tos_in, tos_out;
+	socklen_t s;
+	int r;
+
+	tos_in = rand() & 0xfc;
+	r = setsockopt(fd, SOL_IP, IP_TOS, &tos_in, sizeof(tos_out));
+	if (r != 0)
+		die_perror("setsockopt IP_TOS");
+
+	tos_out = 0;
+	s = sizeof(tos_out);
+	r = getsockopt(fd, SOL_IP, IP_TOS, &tos_out, &s);
+	if (r != 0)
+		die_perror("getsockopt IP_TOS");
+
+	if (tos_in != tos_out)
+		xerror("tos %x != %x\n", tos_in, tos_out);
+
+	if (s != 1)
+		xerror("tos should be 1 byte");
+}
+
 static int client(int pipefd)
 {
 	int fd = -1;
@@ -611,6 +636,8 @@  static int client(int pipefd)
 		xerror("Unknown pf %d\n", pf);
 	}
 
+	test_ip_tos_sockopt(fd);
+
 	connect_one_server(fd, pipefd);
 
 	return 0;
@@ -642,6 +669,22 @@  static int rcheck(int wstatus, const char *what)
 	return 111;
 }
 
+static void init_rng(void)
+{
+	int fd = open("/dev/urandom", O_RDONLY);
+	unsigned int foo;
+
+	if (fd > 0) {
+		int ret = read(fd, &foo, sizeof(foo));
+
+		if (ret < 0)
+			srand(fd + foo);
+		close(fd);
+	}
+
+	srand(foo);
+}
+
 int main(int argc, char *argv[])
 {
 	int e1, e2, wstatus;
@@ -650,6 +693,8 @@  int main(int argc, char *argv[])
 
 	parse_opts(argc, argv);
 
+	init_rng();
+
 	e1 = pipe(pipefds);
 	if (e1 < 0)
 		die_perror("pipe");