diff options
-rw-r--r-- | tools/testing/selftests/Makefile | 1 | ||||
-rw-r--r-- | tools/testing/selftests/net-socket/Makefile | 16 | ||||
-rw-r--r-- | tools/testing/selftests/net-socket/run_netsocktests | 12 | ||||
-rw-r--r-- | tools/testing/selftests/net-socket/socket.c | 92 |
4 files changed, 121 insertions, 0 deletions
diff --git a/tools/testing/selftests/Makefile b/tools/testing/selftests/Makefile index 3cc0ad7ae863..7c6280f1cf4d 100644 --- a/tools/testing/selftests/Makefile +++ b/tools/testing/selftests/Makefile | |||
@@ -5,6 +5,7 @@ TARGETS += vm | |||
5 | TARGETS += cpu-hotplug | 5 | TARGETS += cpu-hotplug |
6 | TARGETS += memory-hotplug | 6 | TARGETS += memory-hotplug |
7 | TARGETS += efivarfs | 7 | TARGETS += efivarfs |
8 | TARGETS += net-socket | ||
8 | 9 | ||
9 | all: | 10 | all: |
10 | for TARGET in $(TARGETS); do \ | 11 | 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 000000000000..f27ee10da530 --- /dev/null +++ b/tools/testing/selftests/net-socket/Makefile | |||
@@ -0,0 +1,16 @@ | |||
1 | # Makefile for net-socket selftests | ||
2 | |||
3 | CC = $(CROSS_COMPILE)gcc | ||
4 | CFLAGS = -Wall | ||
5 | |||
6 | NET_SOCK_PROGS = socket | ||
7 | |||
8 | all: $(NET_SOCK_PROGS) | ||
9 | %: %.c | ||
10 | $(CC) $(CFLAGS) -o $@ $^ | ||
11 | |||
12 | run_tests: all | ||
13 | @/bin/sh ./run_netsocktests || echo "vmtests: [FAIL]" | ||
14 | |||
15 | clean: | ||
16 | $(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 000000000000..c09a682df56a --- /dev/null +++ b/tools/testing/selftests/net-socket/run_netsocktests | |||
@@ -0,0 +1,12 @@ | |||
1 | #!/bin/bash | ||
2 | |||
3 | echo "--------------------" | ||
4 | echo "running socket test" | ||
5 | echo "--------------------" | ||
6 | ./socket | ||
7 | if [ $? -ne 0 ]; then | ||
8 | echo "[FAIL]" | ||
9 | else | ||
10 | echo "[PASS]" | ||
11 | fi | ||
12 | |||
diff --git a/tools/testing/selftests/net-socket/socket.c b/tools/testing/selftests/net-socket/socket.c new file mode 100644 index 000000000000..0f227f2f9be9 --- /dev/null +++ b/tools/testing/selftests/net-socket/socket.c | |||
@@ -0,0 +1,92 @@ | |||
1 | #include <stdio.h> | ||
2 | #include <errno.h> | ||
3 | #include <unistd.h> | ||
4 | #include <string.h> | ||
5 | #include <sys/types.h> | ||
6 | #include <sys/socket.h> | ||
7 | #include <netinet/in.h> | ||
8 | |||
9 | struct socket_testcase { | ||
10 | int domain; | ||
11 | int type; | ||
12 | int protocol; | ||
13 | |||
14 | /* 0 = valid file descriptor | ||
15 | * -foo = error foo | ||
16 | */ | ||
17 | int expect; | ||
18 | |||
19 | /* If non-zero, accept EAFNOSUPPORT to handle the case | ||
20 | * of the protocol not being configured into the kernel. | ||
21 | */ | ||
22 | int nosupport_ok; | ||
23 | }; | ||
24 | |||
25 | static struct socket_testcase tests[] = { | ||
26 | { AF_MAX, 0, 0, -EAFNOSUPPORT, 0 }, | ||
27 | { AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 1 }, | ||
28 | { AF_INET, SOCK_DGRAM, IPPROTO_TCP, -EPROTONOSUPPORT, 1 }, | ||
29 | { AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, 1 }, | ||
30 | { AF_INET, SOCK_STREAM, IPPROTO_UDP, -EPROTONOSUPPORT, 1 }, | ||
31 | }; | ||
32 | |||
33 | #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) | ||
34 | #define ERR_STRING_SZ 64 | ||
35 | |||
36 | static int run_tests(void) | ||
37 | { | ||
38 | char err_string1[ERR_STRING_SZ]; | ||
39 | char err_string2[ERR_STRING_SZ]; | ||
40 | int i, err; | ||
41 | |||
42 | err = 0; | ||
43 | for (i = 0; i < ARRAY_SIZE(tests); i++) { | ||
44 | struct socket_testcase *s = &tests[i]; | ||
45 | int fd; | ||
46 | |||
47 | fd = socket(s->domain, s->type, s->protocol); | ||
48 | if (fd < 0) { | ||
49 | if (s->nosupport_ok && | ||
50 | errno == EAFNOSUPPORT) | ||
51 | continue; | ||
52 | |||
53 | if (s->expect < 0 && | ||
54 | errno == -s->expect) | ||
55 | continue; | ||
56 | |||
57 | strerror_r(-s->expect, err_string1, ERR_STRING_SZ); | ||
58 | strerror_r(errno, err_string2, ERR_STRING_SZ); | ||
59 | |||
60 | fprintf(stderr, "socket(%d, %d, %d) expected " | ||
61 | "err (%s) got (%s)\n", | ||
62 | s->domain, s->type, s->protocol, | ||
63 | err_string1, err_string2); | ||
64 | |||
65 | err = -1; | ||
66 | break; | ||
67 | } else { | ||
68 | close(fd); | ||
69 | |||
70 | if (s->expect < 0) { | ||
71 | strerror_r(errno, err_string1, ERR_STRING_SZ); | ||
72 | |||
73 | fprintf(stderr, "socket(%d, %d, %d) expected " | ||
74 | "success got err (%s)\n", | ||
75 | s->domain, s->type, s->protocol, | ||
76 | err_string1); | ||
77 | |||
78 | err = -1; | ||
79 | break; | ||
80 | } | ||
81 | } | ||
82 | } | ||
83 | |||
84 | return err; | ||
85 | } | ||
86 | |||
87 | int main(void) | ||
88 | { | ||
89 | int err = run_tests(); | ||
90 | |||
91 | return err; | ||
92 | } | ||