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