xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getpeername/getpeername01.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  * 07/2001 Ported by Wayne Boyer
5*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2024 SUSE LLC Andrea Manzini <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker  */
7*49cdfc7eSAndroid Build Coastguard Worker 
8*49cdfc7eSAndroid Build Coastguard Worker /*\
9*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
10*49cdfc7eSAndroid Build Coastguard Worker  *
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that getpeername() returns the proper errno for various failure cases:
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * - EBADF on invalid address.
14*49cdfc7eSAndroid Build Coastguard Worker  * - ENOTSOCK on socket opened on /dev/null.
15*49cdfc7eSAndroid Build Coastguard Worker  * - ENOTCONN on socket not connected.
16*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL on negative addrlen.
17*49cdfc7eSAndroid Build Coastguard Worker  * - EFAULT on invalid addr/addrlen pointers.
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in server_addr;
23*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in fsin1;
24*49cdfc7eSAndroid Build Coastguard Worker static socklen_t sinlen;
25*49cdfc7eSAndroid Build Coastguard Worker static socklen_t invalid_sinlen = -1;
26*49cdfc7eSAndroid Build Coastguard Worker static int sv[2];
27*49cdfc7eSAndroid Build Coastguard Worker static int sockfd = -1;
28*49cdfc7eSAndroid Build Coastguard Worker 
setup_fd_file(void)29*49cdfc7eSAndroid Build Coastguard Worker static void setup_fd_file(void)
30*49cdfc7eSAndroid Build Coastguard Worker {
31*49cdfc7eSAndroid Build Coastguard Worker 	sockfd = SAFE_OPEN("/dev/null", O_WRONLY, 0666);
32*49cdfc7eSAndroid Build Coastguard Worker }
33*49cdfc7eSAndroid Build Coastguard Worker 
setup_fd_stream(void)34*49cdfc7eSAndroid Build Coastguard Worker static void setup_fd_stream(void)
35*49cdfc7eSAndroid Build Coastguard Worker {
36*49cdfc7eSAndroid Build Coastguard Worker 	sockfd = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
37*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(sockfd, (struct sockaddr *)&server_addr, sizeof(server_addr));
38*49cdfc7eSAndroid Build Coastguard Worker }
39*49cdfc7eSAndroid Build Coastguard Worker 
cleanup_fd(void)40*49cdfc7eSAndroid Build Coastguard Worker static void cleanup_fd(void)
41*49cdfc7eSAndroid Build Coastguard Worker {
42*49cdfc7eSAndroid Build Coastguard Worker 	if (sockfd)
43*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sockfd);
44*49cdfc7eSAndroid Build Coastguard Worker }
45*49cdfc7eSAndroid Build Coastguard Worker 
setup_pair(void)46*49cdfc7eSAndroid Build Coastguard Worker static void setup_pair(void)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_SOCKETPAIR(PF_UNIX, SOCK_STREAM, 0, sv);
49*49cdfc7eSAndroid Build Coastguard Worker 	sockfd = sv[0];
50*49cdfc7eSAndroid Build Coastguard Worker }
51*49cdfc7eSAndroid Build Coastguard Worker 
cleanup_pair(void)52*49cdfc7eSAndroid Build Coastguard Worker static void cleanup_pair(void)
53*49cdfc7eSAndroid Build Coastguard Worker {
54*49cdfc7eSAndroid Build Coastguard Worker 	if (sv[0])
55*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sv[0]);
56*49cdfc7eSAndroid Build Coastguard Worker 
57*49cdfc7eSAndroid Build Coastguard Worker 	if (sv[1])
58*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(sv[1]);
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker 
61*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
62*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr *sockaddr;
63*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t *addrlen;
64*49cdfc7eSAndroid Build Coastguard Worker 	int experrno;
65*49cdfc7eSAndroid Build Coastguard Worker 	void (*setup)(void);
66*49cdfc7eSAndroid Build Coastguard Worker 	void (*cleanup)(void);
67*49cdfc7eSAndroid Build Coastguard Worker } test_cases[] = {
68*49cdfc7eSAndroid Build Coastguard Worker 	{.addrlen = &sinlen, .experrno = EBADF},
69*49cdfc7eSAndroid Build Coastguard Worker 	{.addrlen = &sinlen, .experrno = ENOTSOCK, .setup = setup_fd_file,
70*49cdfc7eSAndroid Build Coastguard Worker 		.cleanup = cleanup_fd },
71*49cdfc7eSAndroid Build Coastguard Worker 	{.addrlen = &sinlen, .experrno = ENOTCONN, .setup = setup_fd_stream,
72*49cdfc7eSAndroid Build Coastguard Worker 		.cleanup = cleanup_fd },
73*49cdfc7eSAndroid Build Coastguard Worker 	{.addrlen = &invalid_sinlen, .experrno = EINVAL, .setup = setup_pair,
74*49cdfc7eSAndroid Build Coastguard Worker 		.cleanup = cleanup_pair},
75*49cdfc7eSAndroid Build Coastguard Worker 	{.sockaddr = (struct sockaddr *) -1, .addrlen = &sinlen, .experrno = EFAULT,
76*49cdfc7eSAndroid Build Coastguard Worker 		.setup = setup_pair, .cleanup = cleanup_pair},
77*49cdfc7eSAndroid Build Coastguard Worker 	{.experrno = EFAULT, .setup = setup_pair, .cleanup = cleanup_pair},
78*49cdfc7eSAndroid Build Coastguard Worker 	{.addrlen = (socklen_t *) 1, .experrno = EFAULT, .setup = setup_pair,
79*49cdfc7eSAndroid Build Coastguard Worker 		.cleanup = cleanup_pair },
80*49cdfc7eSAndroid Build Coastguard Worker };
81*49cdfc7eSAndroid Build Coastguard Worker 
verify_getpeername(unsigned int nr)82*49cdfc7eSAndroid Build Coastguard Worker static void verify_getpeername(unsigned int nr)
83*49cdfc7eSAndroid Build Coastguard Worker {
84*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = &test_cases[nr];
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->setup != NULL)
87*49cdfc7eSAndroid Build Coastguard Worker 		tc->setup();
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	if (!tc->sockaddr)
90*49cdfc7eSAndroid Build Coastguard Worker 		tc->sockaddr = (struct sockaddr *)&fsin1;
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(getpeername(sockfd, tc->sockaddr, tc->addrlen), tc->experrno);
93*49cdfc7eSAndroid Build Coastguard Worker 
94*49cdfc7eSAndroid Build Coastguard Worker 	if (tc->cleanup != NULL)
95*49cdfc7eSAndroid Build Coastguard Worker 		tc->cleanup();
96*49cdfc7eSAndroid Build Coastguard Worker }
97*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)98*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
99*49cdfc7eSAndroid Build Coastguard Worker {
100*49cdfc7eSAndroid Build Coastguard Worker 	server_addr.sin_family = AF_INET;
101*49cdfc7eSAndroid Build Coastguard Worker 	server_addr.sin_port = 0;
102*49cdfc7eSAndroid Build Coastguard Worker 	server_addr.sin_addr.s_addr = INADDR_ANY;
103*49cdfc7eSAndroid Build Coastguard Worker 	sinlen = sizeof(fsin1);
104*49cdfc7eSAndroid Build Coastguard Worker }
105*49cdfc7eSAndroid Build Coastguard Worker 
106*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
107*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
108*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_getpeername,
109*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(test_cases),
110*49cdfc7eSAndroid Build Coastguard Worker };
111