1 // SPDX-License-Identifier: GPL-2.0
7 #include <sys/socket.h>
8 #include <netinet/in.h>
10 #include "../kselftest.h"
12 struct socket_testcase {
17 /* 0 = valid file descriptor
22 /* If non-zero, accept EAFNOSUPPORT to handle the case
23 * of the protocol not being configured into the kernel.
28 static struct socket_testcase tests[] = {
29 { AF_MAX, 0, 0, -EAFNOSUPPORT, 0 },
30 { AF_INET, SOCK_STREAM, IPPROTO_TCP, 0, 1 },
31 { AF_INET, SOCK_DGRAM, IPPROTO_TCP, -EPROTONOSUPPORT, 1 },
32 { AF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, 1 },
33 { AF_INET, SOCK_STREAM, IPPROTO_UDP, -EPROTONOSUPPORT, 1 },
36 #define ERR_STRING_SZ 64
38 static int run_tests(void)
40 char err_string1[ERR_STRING_SZ];
41 char err_string2[ERR_STRING_SZ];
45 for (i = 0; i < ARRAY_SIZE(tests); i++) {
46 struct socket_testcase *s = &tests[i];
49 fd = socket(s->domain, s->type, s->protocol);
51 if (s->nosupport_ok &&
52 errno == EAFNOSUPPORT)
59 strerror_r(-s->expect, err_string1, ERR_STRING_SZ);
60 strerror_r(errno, err_string2, ERR_STRING_SZ);
62 fprintf(stderr, "socket(%d, %d, %d) expected "
63 "err (%s) got (%s)\n",
64 s->domain, s->type, s->protocol,
65 err_string1, err_string2);
73 strerror_r(errno, err_string1, ERR_STRING_SZ);
75 fprintf(stderr, "socket(%d, %d, %d) expected "
76 "success got err (%s)\n",
77 s->domain, s->type, s->protocol,
91 int err = run_tests();