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