2013-03-19 18:53:03

by David Miller

[permalink] [raw]
Subject: [PATCH] net: Add socket() system call self test.


Signed-off-by: David S. Miller <[email protected]>
---

As mentioned during the netfilter workshop, we will be adding
all sorts of networking tests now that 3.9.x has a selftest
framework in place.

The first test I'm adding to net-next does some very simple
testing of the socket() system call.

Feel free to send patches for more tests, making the run
script more powerful (perhaps by taking a whitespace
seperated list of tests to run on the command line), and
adding more checks to the socket.c test.

Thanks.

tools/testing/selftests/Makefile | 1 +
tools/testing/selftests/net-socket/Makefile | 16 ++++
.../testing/selftests/net-socket/run_netsocktests | 12 +++
tools/testing/selftests/net-socket/socket.c | 92 ++++++++++++++++++++++
4 files changed, 121 insertions(+)
create mode 100644 tools/testing/selftests/net-socket/Makefile
create mode 100644 tools/testing/selftests/net-socket/run_netsocktests
create mode 100644 tools/testing/selftests/net-socket/socket.c

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 3cc0ad7..7c6280f 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -5,6 +5,7 @@ TARGETS += vm
TARGETS += cpu-hotplug
TARGETS += memory-hotplug
TARGETS += efivarfs
+TARGETS += net-socket

all:
for TARGET in $(TARGETS); do \
diff --git a/tools/testing/selftests/net-socket/Makefile b/tools/testing/selftests/net-socket/Makefile
new file mode 100644
index 0000000..f27ee10
--- /dev/null
+++ b/tools/testing/selftests/net-socket/Makefile
@@ -0,0 +1,16 @@
+# Makefile for net-socket selftests
+
+CC = $(CROSS_COMPILE)gcc
+CFLAGS = -Wall
+
+NET_SOCK_PROGS = socket
+
+all: $(NET_SOCK_PROGS)
+%: %.c
+ $(CC) $(CFLAGS) -o $@ $^
+
+run_tests: all
+ @/bin/sh ./run_netsocktests || echo "vmtests: [FAIL]"
+
+clean:
+ $(RM) $(NET_SOCK_PROGS)
diff --git a/tools/testing/selftests/net-socket/run_netsocktests b/tools/testing/selftests/net-socket/run_netsocktests
new file mode 100644
index 0000000..c09a682
--- /dev/null
+++ b/tools/testing/selftests/net-socket/run_netsocktests
@@ -0,0 +1,12 @@
+#!/bin/bash
+
+echo "--------------------"
+echo "running socket test"
+echo "--------------------"
+./socket
+if [ $? -ne 0 ]; then
+ echo "[FAIL]"
+else
+ echo "[PASS]"
+fi
+
diff --git a/tools/testing/selftests/net-socket/socket.c b/tools/testing/selftests/net-socket/socket.c
new file mode 100644
index 0000000..0f227f2
--- /dev/null
+++ b/tools/testing/selftests/net-socket/socket.c
@@ -0,0 +1,92 @@
+#include <stdio.h>
+#include <errno.h>
+#include <unistd.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+
+struct socket_testcase {
+ int domain;
+ int type;
+ int protocol;
+
+ /* 0 = valid file descriptor
+ * -foo = error foo
+ */
+ int expect;
+
+ /* If non-zero, accept EAFNOSUPPORT to handle the case
+ * of the protocol not being configured into the kernel.
+ */
+ int nosupport_ok;
+};
+
+static struct socket_testcase tests[] = {
+ { AF_MAX, 0, 0, -EAFNOSUPPORT, 0 },
+ { AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 1 },
+ { AF_INET, SOCK_DGRAM, IPPROTO_TCP, -EPROTONOSUPPORT, 1 },
+ { AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, 1 },
+ { AF_INET, SOCK_STREAM, IPPROTO_UDP, -EPROTONOSUPPORT, 1 },
+};
+
+#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
+#define ERR_STRING_SZ 64
+
+static int run_tests(void)
+{
+ char err_string1[ERR_STRING_SZ];
+ char err_string2[ERR_STRING_SZ];
+ int i, err;
+
+ err = 0;
+ for (i = 0; i < ARRAY_SIZE(tests); i++) {
+ struct socket_testcase *s = &tests[i];
+ int fd;
+
+ fd = socket(s->domain, s->type, s->protocol);
+ if (fd < 0) {
+ if (s->nosupport_ok &&
+ errno == EAFNOSUPPORT)
+ continue;
+
+ if (s->expect < 0 &&
+ errno == -s->expect)
+ continue;
+
+ strerror_r(-s->expect, err_string1, ERR_STRING_SZ);
+ strerror_r(errno, err_string2, ERR_STRING_SZ);
+
+ fprintf(stderr, "socket(%d, %d, %d) expected "
+ "err (%s) got (%s)\n",
+ s->domain, s->type, s->protocol,
+ err_string1, err_string2);
+
+ err = -1;
+ break;
+ } else {
+ close(fd);
+
+ if (s->expect < 0) {
+ strerror_r(errno, err_string1, ERR_STRING_SZ);
+
+ fprintf(stderr, "socket(%d, %d, %d) expected "
+ "success got err (%s)\n",
+ s->domain, s->type, s->protocol,
+ err_string1);
+
+ err = -1;
+ break;
+ }
+ }
+ }
+
+ return err;
+}
+
+int main(void)
+{
+ int err = run_tests();
+
+ return err;
+}
--
1.7.11.7



2013-03-20 19:29:56

by Daniel Baluta

[permalink] [raw]
Subject: Re: [PATCH] net: Add socket() system call self test.

On Wed, Mar 20, 2013 at 9:10 PM, David Miller <[email protected]> wrote:
> From: Daniel Baluta <[email protected]>
> Date: Wed, 20 Mar 2013 00:14:05 +0200
>
>> I think it's better to have a selftests/net directory dedicated for
>> networking tests. Over time
>> the number of net-{x} directories may become very large.
>
> Good idea, here's what I've done for now:
>
> ====================
> net: Move selftests to common net/ subdirectory.
>
> Suggested-by: Daniel Baluta <[email protected]>
> Signed-off-by: David S. Miller <[email protected]>
> ---
> tools/testing/selftests/Makefile | 3 +--
> tools/testing/selftests/net-socket/Makefile | 16 ----------------
> tools/testing/selftests/{net-afpacket => net}/Makefile | 9 +++++----
> .../selftests/{net-afpacket => net}/psock_fanout.c | 0
> .../selftests/{net-afpacket => net}/run_afpackettests | 0
> .../selftests/{net-socket => net}/run_netsocktests | 0
> tools/testing/selftests/{net-socket => net}/socket.c | 0
> 7 files changed, 6 insertions(+), 22 deletions(-)
> delete mode 100644 tools/testing/selftests/net-socket/Makefile
> rename tools/testing/selftests/{net-afpacket => net}/Makefile (55%)
> rename tools/testing/selftests/{net-afpacket => net}/psock_fanout.c (100%)
> rename tools/testing/selftests/{net-afpacket => net}/run_afpackettests (100%)
> rename tools/testing/selftests/{net-socket => net}/run_netsocktests (100%)
> rename tools/testing/selftests/{net-socket => net}/socket.c (100%)
>
> diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
> index 7f50078..a480593 100644
> --- a/tools/testing/selftests/Makefile
> +++ b/tools/testing/selftests/Makefile
> @@ -5,8 +5,7 @@ TARGETS += vm
> TARGETS += cpu-hotplug
> TARGETS += memory-hotplug
> TARGETS += efivarfs
> -TARGETS += net-socket
> -TARGETS += net-afpacket
> +TARGETS += net
>
> all:
> for TARGET in $(TARGETS); do \
> diff --git a/tools/testing/selftests/net-socket/Makefile b/tools/testing/selftests/net-socket/Makefile
> deleted file mode 100644
> index 2450fd8..0000000
> --- a/tools/testing/selftests/net-socket/Makefile
> +++ /dev/null
> @@ -1,16 +0,0 @@
> -# Makefile for net-socket selftests
> -
> -CC = $(CROSS_COMPILE)gcc
> -CFLAGS = -Wall
> -
> -NET_SOCK_PROGS = socket
> -
> -all: $(NET_SOCK_PROGS)
> -%: %.c
> - $(CC) $(CFLAGS) -o $@ $^
> -
> -run_tests: all
> - @/bin/sh ./run_netsocktests || echo "sockettests: [FAIL]"
> -
> -clean:
> - $(RM) $(NET_SOCK_PROGS)
> diff --git a/tools/testing/selftests/net-afpacket/Makefile b/tools/testing/selftests/net/Makefile
> similarity index 55%
> rename from tools/testing/selftests/net-afpacket/Makefile
> rename to tools/testing/selftests/net/Makefile
> index 45f2ffb..bd6e272 100644
> --- a/tools/testing/selftests/net-afpacket/Makefile
> +++ b/tools/testing/selftests/net/Makefile
> @@ -1,18 +1,19 @@
> -# Makefile for net-socket selftests
> +# Makefile for net selftests
>
> CC = $(CROSS_COMPILE)gcc
> CFLAGS = -Wall
>
> CFLAGS += -I../../../../usr/include/
>
> -AF_PACKET_PROGS = psock_fanout
> +NET_PROGS = socket psock_fanout
>
> -all: $(AF_PACKET_PROGS)
> +all: $(NET_PROGS)
> %: %.c
> $(CC) $(CFLAGS) -o $@ $^
>
> run_tests: all
> + @/bin/sh ./run_netsocktests || echo "sockettests: [FAIL]"
> @/bin/sh ./run_afpackettests || echo "afpackettests: [FAIL]"
>
> clean:
> - $(RM) $(AF_PACKET_PROGS)
> + $(RM) $(NET_PROGS)
> diff --git a/tools/testing/selftests/net-afpacket/psock_fanout.c b/tools/testing/selftests/net/psock_fanout.c
> similarity index 100%
> rename from tools/testing/selftests/net-afpacket/psock_fanout.c
> rename to tools/testing/selftests/net/psock_fanout.c
> diff --git a/tools/testing/selftests/net-afpacket/run_afpackettests b/tools/testing/selftests/net/run_afpackettests
> similarity index 100%
> rename from tools/testing/selftests/net-afpacket/run_afpackettests
> rename to tools/testing/selftests/net/run_afpackettests
> diff --git a/tools/testing/selftests/net-socket/run_netsocktests b/tools/testing/selftests/net/run_netsocktests
> similarity index 100%
> rename from tools/testing/selftests/net-socket/run_netsocktests
> rename to tools/testing/selftests/net/run_netsocktests
> diff --git a/tools/testing/selftests/net-socket/socket.c b/tools/testing/selftests/net/socket.c
> similarity index 100%
> rename from tools/testing/selftests/net-socket/socket.c
> rename to tools/testing/selftests/net/socket.c
> --
> 1.7.11.7
>
Looks good. Thanks!

2013-03-19 22:12:31

by Julian Calaby

[permalink] [raw]
Subject: Re: [PATCH] net: Add socket() system call self test.

Hi David,

On Wed, Mar 20, 2013 at 5:52 AM, David Miller <[email protected]> wrote:
>
> Signed-off-by: David S. Miller <[email protected]>
> ---
>
> As mentioned during the netfilter workshop, we will be adding
> all sorts of networking tests now that 3.9.x has a selftest
> framework in place.
>
> The first test I'm adding to net-next does some very simple
> testing of the socket() system call.
>
> Feel free to send patches for more tests, making the run
> script more powerful (perhaps by taking a whitespace
> seperated list of tests to run on the command line), and
> adding more checks to the socket.c test.
>
> Thanks.
>
> tools/testing/selftests/Makefile | 1 +
> tools/testing/selftests/net-socket/Makefile | 16 ++++
> .../testing/selftests/net-socket/run_netsocktests | 12 +++
> tools/testing/selftests/net-socket/socket.c | 92 ++++++++++++++++++++++
> 4 files changed, 121 insertions(+)
> create mode 100644 tools/testing/selftests/net-socket/Makefile
> create mode 100644 tools/testing/selftests/net-socket/run_netsocktests
> create mode 100644 tools/testing/selftests/net-socket/socket.c
>
> diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
> index 3cc0ad7..7c6280f 100644
> --- a/tools/testing/selftests/Makefile
> +++ b/tools/testing/selftests/Makefile
> @@ -5,6 +5,7 @@ TARGETS += vm
> TARGETS += cpu-hotplug
> TARGETS += memory-hotplug
> TARGETS += efivarfs
> +TARGETS += net-socket
>
> all:
> for TARGET in $(TARGETS); do \
> diff --git a/tools/testing/selftests/net-socket/Makefile b/tools/testing/selftests/net-socket/Makefile
> new file mode 100644
> index 0000000..f27ee10
> --- /dev/null
> +++ b/tools/testing/selftests/net-socket/Makefile
> @@ -0,0 +1,16 @@
> +# Makefile for net-socket selftests
> +
> +CC = $(CROSS_COMPILE)gcc
> +CFLAGS = -Wall
> +
> +NET_SOCK_PROGS = socket
> +
> +all: $(NET_SOCK_PROGS)
> +%: %.c
> + $(CC) $(CFLAGS) -o $@ $^
> +
> +run_tests: all
> + @/bin/sh ./run_netsocktests || echo "vmtests: [FAIL]"

Should that not be "vmtests"?

Thanks,

--
Julian Calaby

Email: [email protected]
Profile: http://www.google.com/profiles/julian.calaby/
.Plan: http://sites.google.com/site/juliancalaby/

2013-03-19 22:22:24

by Julian Calaby

[permalink] [raw]
Subject: Re: [PATCH] net: Add socket() system call self test.

Hi David,

On Wed, Mar 20, 2013 at 9:19 AM, David Miller <[email protected]> wrote:
> From: Julian Calaby <[email protected]>
> Date: Wed, 20 Mar 2013 09:12:09 +1100
>
>> Should that not be "vmtests"?
>
> Fixed already:
>
> http://patchwork.ozlabs.org/patch/229221/

I'm not subscribed to netdev so I didn't see it. =)

Thanks,

--
Julian Calaby

Email: [email protected]
Profile: http://www.google.com/profiles/julian.calaby/
.Plan: http://sites.google.com/site/juliancalaby/

2013-03-19 22:14:07

by Daniel Baluta

[permalink] [raw]
Subject: Re: [PATCH] net: Add socket() system call self test.

Hi David,

On Tue, Mar 19, 2013 at 8:52 PM, David Miller <[email protected]> wrote:
>
> Signed-off-by: David S. Miller <[email protected]>
> ---
>
> As mentioned during the netfilter workshop, we will be adding
> all sorts of networking tests now that 3.9.x has a selftest
> framework in place.
>
> The first test I'm adding to net-next does some very simple
> testing of the socket() system call.
>
> Feel free to send patches for more tests, making the run
> script more powerful (perhaps by taking a whitespace
> seperated list of tests to run on the command line), and
> adding more checks to the socket.c test.
>
> Thanks.
>
> tools/testing/selftests/Makefile | 1 +
> tools/testing/selftests/net-socket/Makefile | 16 ++++
> .../testing/selftests/net-socket/run_netsocktests | 12 +++
> tools/testing/selftests/net-socket/socket.c | 92 ++++++++++++++++++++++
> 4 files changed, 121 insertions(+)
> create mode 100644 tools/testing/selftests/net-socket/Makefile
> create mode 100644 tools/testing/selftests/net-socket/run_netsocktests
> create mode 100644 tools/testing/selftests/net-socket/socket.c
>
> diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
> index 3cc0ad7..7c6280f 100644
> --- a/tools/testing/selftests/Makefile
> +++ b/tools/testing/selftests/Makefile
> @@ -5,6 +5,7 @@ TARGETS += vm
> TARGETS += cpu-hotplug
> TARGETS += memory-hotplug
> TARGETS += efivarfs
> +TARGETS += net-socket
>
> all:
> for TARGET in $(TARGETS); do \
> diff --git a/tools/testing/selftests/net-socket/Makefile b/tools/testing/selftests/net-socket/Makefile
> new file mode 100644
> index 0000000..f27ee10
> --- /dev/null
> +++ b/tools/testing/selftests/net-socket/Makefile
> @@ -0,0 +1,16 @@
> +# Makefile for net-socket selftests
> +
> +CC = $(CROSS_COMPILE)gcc
> +CFLAGS = -Wall
> +
> +NET_SOCK_PROGS = socket
> +
> +all: $(NET_SOCK_PROGS)
> +%: %.c
> + $(CC) $(CFLAGS) -o $@ $^
> +
> +run_tests: all
> + @/bin/sh ./run_netsocktests || echo "vmtests: [FAIL]"
> +
> +clean:
> + $(RM) $(NET_SOCK_PROGS)
> diff --git a/tools/testing/selftests/net-socket/run_netsocktests b/tools/testing/selftests/net-socket/run_netsocktests

I think it's better to have a selftests/net directory dedicated for
networking tests. Over time
the number of net-{x} directories may become very large.


Daniel.

2013-03-19 22:19:04

by David Miller

[permalink] [raw]
Subject: Re: [PATCH] net: Add socket() system call self test.

From: Julian Calaby <[email protected]>
Date: Wed, 20 Mar 2013 09:12:09 +1100

> Should that not be "vmtests"?

Fixed already:

http://patchwork.ozlabs.org/patch/229221/

2013-03-20 19:10:58

by David Miller

[permalink] [raw]
Subject: Re: [PATCH] net: Add socket() system call self test.

From: Daniel Baluta <[email protected]>
Date: Wed, 20 Mar 2013 00:14:05 +0200

> I think it's better to have a selftests/net directory dedicated for
> networking tests. Over time
> the number of net-{x} directories may become very large.

Good idea, here's what I've done for now:

====================
net: Move selftests to common net/ subdirectory.

Suggested-by: Daniel Baluta <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
---
tools/testing/selftests/Makefile | 3 +--
tools/testing/selftests/net-socket/Makefile | 16 ----------------
tools/testing/selftests/{net-afpacket => net}/Makefile | 9 +++++----
.../selftests/{net-afpacket => net}/psock_fanout.c | 0
.../selftests/{net-afpacket => net}/run_afpackettests | 0
.../selftests/{net-socket => net}/run_netsocktests | 0
tools/testing/selftests/{net-socket => net}/socket.c | 0
7 files changed, 6 insertions(+), 22 deletions(-)
delete mode 100644 tools/testing/selftests/net-socket/Makefile
rename tools/testing/selftests/{net-afpacket => net}/Makefile (55%)
rename tools/testing/selftests/{net-afpacket => net}/psock_fanout.c (100%)
rename tools/testing/selftests/{net-afpacket => net}/run_afpackettests (100%)
rename tools/testing/selftests/{net-socket => net}/run_netsocktests (100%)
rename tools/testing/selftests/{net-socket => net}/socket.c (100%)

diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile
index 7f50078..a480593 100644
--- a/tools/testing/selftests/Makefile
+++ b/tools/testing/selftests/Makefile
@@ -5,8 +5,7 @@ TARGETS += vm
TARGETS += cpu-hotplug
TARGETS += memory-hotplug
TARGETS += efivarfs
-TARGETS += net-socket
-TARGETS += net-afpacket
+TARGETS += net

all:
for TARGET in $(TARGETS); do \
diff --git a/tools/testing/selftests/net-socket/Makefile b/tools/testing/selftests/net-socket/Makefile
deleted file mode 100644
index 2450fd8..0000000
--- a/tools/testing/selftests/net-socket/Makefile
+++ /dev/null
@@ -1,16 +0,0 @@
-# Makefile for net-socket selftests
-
-CC = $(CROSS_COMPILE)gcc
-CFLAGS = -Wall
-
-NET_SOCK_PROGS = socket
-
-all: $(NET_SOCK_PROGS)
-%: %.c
- $(CC) $(CFLAGS) -o $@ $^
-
-run_tests: all
- @/bin/sh ./run_netsocktests || echo "sockettests: [FAIL]"
-
-clean:
- $(RM) $(NET_SOCK_PROGS)
diff --git a/tools/testing/selftests/net-afpacket/Makefile b/tools/testing/selftests/net/Makefile
similarity index 55%
rename from tools/testing/selftests/net-afpacket/Makefile
rename to tools/testing/selftests/net/Makefile
index 45f2ffb..bd6e272 100644
--- a/tools/testing/selftests/net-afpacket/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -1,18 +1,19 @@
-# Makefile for net-socket selftests
+# Makefile for net selftests

CC = $(CROSS_COMPILE)gcc
CFLAGS = -Wall

CFLAGS += -I../../../../usr/include/

-AF_PACKET_PROGS = psock_fanout
+NET_PROGS = socket psock_fanout

-all: $(AF_PACKET_PROGS)
+all: $(NET_PROGS)
%: %.c
$(CC) $(CFLAGS) -o $@ $^

run_tests: all
+ @/bin/sh ./run_netsocktests || echo "sockettests: [FAIL]"
@/bin/sh ./run_afpackettests || echo "afpackettests: [FAIL]"

clean:
- $(RM) $(AF_PACKET_PROGS)
+ $(RM) $(NET_PROGS)
diff --git a/tools/testing/selftests/net-afpacket/psock_fanout.c b/tools/testing/selftests/net/psock_fanout.c
similarity index 100%
rename from tools/testing/selftests/net-afpacket/psock_fanout.c
rename to tools/testing/selftests/net/psock_fanout.c
diff --git a/tools/testing/selftests/net-afpacket/run_afpackettests b/tools/testing/selftests/net/run_afpackettests
similarity index 100%
rename from tools/testing/selftests/net-afpacket/run_afpackettests
rename to tools/testing/selftests/net/run_afpackettests
diff --git a/tools/testing/selftests/net-socket/run_netsocktests b/tools/testing/selftests/net/run_netsocktests
similarity index 100%
rename from tools/testing/selftests/net-socket/run_netsocktests
rename to tools/testing/selftests/net/run_netsocktests
diff --git a/tools/testing/selftests/net-socket/socket.c b/tools/testing/selftests/net/socket.c
similarity index 100%
rename from tools/testing/selftests/net-socket/socket.c
rename to tools/testing/selftests/net/socket.c
--
1.7.11.7