File Index
Files and directories
Sign in
FILE

socket.c

File preview with syntax highlighting.

linux-6.19.14/tools/testing/selftests/net/socket.c C
 1// SPDX-License-Identifier: GPL-2.0
 2#include <stdio.h>
 3#include <errno.h>
 4#include <unistd.h>
 5#include <string.h>
 6#include <sys/types.h>
 7#include <sys/socket.h>
 8#include <netinet/in.h>
 9
10#include "kselftest.h"
11
12struct socket_testcase {
13	int	domain;
14	int	type;
15	int	protocol;
16
17	/* 0    = valid file descriptor
18	 * -foo = error foo
19	 */
20	int	expect;
21
22	/* If non-zero, accept EAFNOSUPPORT to handle the case
23	 * of the protocol not being configured into the kernel.
24	 */
25	int	nosupport_ok;
26};
27
28static 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  },
34};
35
36#define ERR_STRING_SZ	64
37
38static int run_tests(void)
39{
40	char err_string1[ERR_STRING_SZ];
41	char err_string2[ERR_STRING_SZ];
42	const char *msg1, *msg2;
43	int i, err;
44
45	err = 0;
46	for (i = 0; i < ARRAY_SIZE(tests); i++) {
47		struct socket_testcase *s = &tests[i];
48		int fd;
49
50		fd = socket(s->domain, s->type, s->protocol);
51		if (fd < 0) {
52			if (s->nosupport_ok &&
53			    errno == EAFNOSUPPORT)
54				continue;
55
56			if (s->expect < 0 &&
57			    errno == -s->expect)
58				continue;
59
60			msg1 = strerror_r(-s->expect, err_string1, ERR_STRING_SZ);
61			msg2 = strerror_r(errno, err_string2, ERR_STRING_SZ);
62
63			fprintf(stderr, "socket(%d, %d, %d) expected "
64				"err (%s) got (%s)\n",
65				s->domain, s->type, s->protocol,
66				msg1, msg2);
67
68			err = -1;
69			break;
70		} else {
71			close(fd);
72
73			if (s->expect < 0) {
74				msg1 = strerror_r(errno, err_string1, ERR_STRING_SZ);
75
76				fprintf(stderr, "socket(%d, %d, %d) expected "
77					"success got err (%s)\n",
78					s->domain, s->type, s->protocol,
79					msg1);
80
81				err = -1;
82				break;
83			}
84		}
85	}
86
87	return err;
88}
89
90int main(void)
91{
92	int err = run_tests();
93
94	return err;
95}
~/ Read-only file preview.