1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 */
5
6 /*
7 * Test Name: socket01
8 *
9 * Test Description:
10 * Verify that socket() returns the proper errno for various failure cases
11 *
12 */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <netinet/in.h>
21 #include "tst_test.h"
22
23 struct test_case_t {
24 int domain;
25 int type;
26 int proto;
27 int retval;
28 int experrno;
29 char *desc;
30 } tdat[] = {
31 {0, SOCK_STREAM, 0, -1, EAFNOSUPPORT, "invalid domain"},
32 {PF_INET, 75, 0, -1, EINVAL, "invalid type"},
33 {PF_UNIX, SOCK_DGRAM, 0, 0, 0, "UNIX domain dgram"},
34 {PF_INET, SOCK_RAW, 0, -1, EPROTONOSUPPORT, "raw open as non-root"},
35 {PF_INET, SOCK_DGRAM, 17, 0, 0, "UDP socket"},
36 {PF_INET, SOCK_STREAM, 17, -1, EPROTONOSUPPORT, "UDP stream"},
37 {PF_INET, SOCK_DGRAM, 6, -1, EPROTONOSUPPORT, "TCP dgram"},
38 {PF_INET, SOCK_STREAM, 6, 0, 0, "TCP socket"},
39 {PF_INET, SOCK_STREAM, 1, -1, EPROTONOSUPPORT, "ICMP stream"}
40 };
41
verify_socket(unsigned int n)42 static void verify_socket(unsigned int n)
43 {
44 int fd;
45 struct test_case_t *tc = &tdat[n];
46
47 TEST(fd = socket(tc->domain, tc->type, tc->proto));
48 if (TST_RET >= 0)
49 TST_RET = 0;
50
51 if (fd > 0)
52 SAFE_CLOSE(fd);
53
54 if (TST_RET != tc->retval) {
55 tst_res(TFAIL, "%s returned %d (expected %d)",
56 tc->desc, fd, tc->retval);
57 return;
58 }
59
60 if (TST_ERR != tc->experrno) {
61 tst_res(TFAIL | TTERRNO, "expected %s(%d)",
62 tst_strerrno(tc->experrno), tc->experrno);
63 return;
64 }
65
66 tst_res(TPASS, "%s successful", tc->desc);
67 }
68
69 /*
70 * See:
71 * commit 86c8f9d158f68538a971a47206a46a22c7479bac
72 * ...
73 * [IPV4] Fix EPROTONOSUPPORT error in inet_create
74 */
setup(void)75 static void setup(void)
76 {
77 unsigned int i;
78
79 if (tst_kvercmp(2, 6, 16) >= 0)
80 return;
81
82 for (i = 0; i < ARRAY_SIZE(tdat); i++) {
83 if (tdat[i].experrno == EPROTONOSUPPORT)
84 tdat[i].experrno = ESOCKTNOSUPPORT;
85 }
86 }
87
88 static struct tst_test test = {
89 .tcnt = ARRAY_SIZE(tdat),
90 .setup = setup,
91 .test = verify_socket
92 };
93