xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/socketpair/socketpair01.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 	{PF_UNIX, SOCK_STREAM, 0, 0, -1, EFAULT, "bad aligned pointer"},
38 	{PF_UNIX, SOCK_STREAM, 0, (int *)7, -1, EFAULT, "bad unaligned pointer"},
39 	{PF_INET, SOCK_DGRAM, 17, fds, -1, EOPNOTSUPP, "UDP socket"},
40 	{PF_INET, SOCK_DGRAM, 6, fds, -1, EPROTONOSUPPORT, "TCP dgram"},
41 	{PF_INET, SOCK_STREAM, 6, fds, -1, EOPNOTSUPP, "TCP socket"},
42 	{PF_INET, SOCK_STREAM, 1, fds, -1, EPROTONOSUPPORT, "ICMP stream"}
43 };
44 
verify_socketpair(unsigned int n)45 static void verify_socketpair(unsigned int n)
46 {
47 	struct test_case_t *tc = &tdat[n];
48 
49 	TEST(socketpair(tc->domain, tc->type, tc->proto, tc->sv));
50 
51 	if (TST_RET == 0) {
52 		SAFE_CLOSE(fds[0]);
53 		SAFE_CLOSE(fds[1]);
54 	}
55 
56 	if (TST_RET != tc->retval) {
57 		tst_res(TFAIL, "%s returned %ld (expected %d)",
58 			tc->desc, TST_RET, tc->retval);
59 		return;
60 	}
61 
62 	if (TST_ERR != tc->experrno) {
63 		tst_res(TFAIL | TTERRNO, "expected %s(%d)",
64 		        tst_strerrno(tc->experrno), tc->experrno);
65 		return;
66 	}
67 
68 	tst_res(TPASS, "%s successful", tc->desc);
69 }
70 
71 static struct tst_test test = {
72 	.tcnt = ARRAY_SIZE(tdat),
73 	.test = verify_socketpair
74 };
75