1 // SPDX-License-Identifier: GPL-2.0
7 #include <sys/socket.h>
8 #include <netinet/in.h>
10 struct socket_testcase
{
15 /* 0 = valid file descriptor
20 /* If non-zero, accept EAFNOSUPPORT to handle the case
21 * of the protocol not being configured into the kernel.
26 static struct socket_testcase tests
[] = {
27 { AF_MAX
, 0, 0, -EAFNOSUPPORT
, 0 },
28 { AF_INET
, SOCK_STREAM
, IPPROTO_TCP
, 0, 1 },
29 { AF_INET
, SOCK_DGRAM
, IPPROTO_TCP
, -EPROTONOSUPPORT
, 1 },
30 { AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
, 0, 1 },
31 { AF_INET
, SOCK_STREAM
, IPPROTO_UDP
, -EPROTONOSUPPORT
, 1 },
34 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
35 #define ERR_STRING_SZ 64
37 static int run_tests(void)
39 char err_string1
[ERR_STRING_SZ
];
40 char err_string2
[ERR_STRING_SZ
];
44 for (i
= 0; i
< ARRAY_SIZE(tests
); i
++) {
45 struct socket_testcase
*s
= &tests
[i
];
48 fd
= socket(s
->domain
, s
->type
, s
->protocol
);
50 if (s
->nosupport_ok
&&
51 errno
== EAFNOSUPPORT
)
58 strerror_r(-s
->expect
, err_string1
, ERR_STRING_SZ
);
59 strerror_r(errno
, err_string2
, ERR_STRING_SZ
);
61 fprintf(stderr
, "socket(%d, %d, %d) expected "
62 "err (%s) got (%s)\n",
63 s
->domain
, s
->type
, s
->protocol
,
64 err_string1
, err_string2
);
72 strerror_r(errno
, err_string1
, ERR_STRING_SZ
);
74 fprintf(stderr
, "socket(%d, %d, %d) expected "
75 "success got err (%s)\n",
76 s
->domain
, s
->type
, s
->protocol
,
90 int err
= run_tests();