xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getsockname/getsockname01.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  * Verify that getsockname() returns the proper errno for various failure cases:
11*49cdfc7eSAndroid Build Coastguard Worker  *
12*49cdfc7eSAndroid Build Coastguard Worker  * - EBADF on a not open file
13*49cdfc7eSAndroid Build Coastguard Worker  * - ENOTSOCK on a file descriptor not linked to a socket
14*49cdfc7eSAndroid Build Coastguard Worker  * - EFAULT on invalid socket buffer o invalid socklen
15*49cdfc7eSAndroid Build Coastguard Worker  * - EINVALI on an invalid addrlen
16*49cdfc7eSAndroid Build Coastguard Worker  */
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in sin0, fsin1;
21*49cdfc7eSAndroid Build Coastguard Worker static int sock_null, sock_bind, sock_fake;
22*49cdfc7eSAndroid Build Coastguard Worker static socklen_t sinlen;
23*49cdfc7eSAndroid Build Coastguard Worker static socklen_t sininval = -1;
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
26*49cdfc7eSAndroid Build Coastguard Worker 	int *sock;
27*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr_in *sockaddr;
28*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t *addrlen;
29*49cdfc7eSAndroid Build Coastguard Worker 	int experrno;
30*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
31*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
32*49cdfc7eSAndroid Build Coastguard Worker 	{ .sock = &sock_fake, .sockaddr = &fsin1, .addrlen = &sinlen,
33*49cdfc7eSAndroid Build Coastguard Worker 		.experrno = EBADF, "bad file descriptor"},
34*49cdfc7eSAndroid Build Coastguard Worker 	{ .sock = &sock_null, .sockaddr = &fsin1, .addrlen = &sinlen,
35*49cdfc7eSAndroid Build Coastguard Worker 		.experrno = ENOTSOCK, "bad file descriptor"},
36*49cdfc7eSAndroid Build Coastguard Worker 	{ .sock = &sock_bind, .sockaddr = NULL, .addrlen = &sinlen,
37*49cdfc7eSAndroid Build Coastguard Worker 		.experrno = EFAULT, "invalid socket buffer"},
38*49cdfc7eSAndroid Build Coastguard Worker 	{ .sock = &sock_bind, .sockaddr = &fsin1, .addrlen = NULL,
39*49cdfc7eSAndroid Build Coastguard Worker 		.experrno = EFAULT, "invalid aligned salen"},
40*49cdfc7eSAndroid Build Coastguard Worker 	{ .sock = &sock_bind, .sockaddr = &fsin1, .addrlen = (socklen_t *) 1,
41*49cdfc7eSAndroid Build Coastguard Worker 		.experrno = EFAULT, "invalid unaligned salen"},
42*49cdfc7eSAndroid Build Coastguard Worker 	{ .sock = &sock_bind, .sockaddr = &fsin1, .addrlen = &sininval,
43*49cdfc7eSAndroid Build Coastguard Worker 		.experrno = EINVAL, "invalid socklen"},
44*49cdfc7eSAndroid Build Coastguard Worker };
45*49cdfc7eSAndroid Build Coastguard Worker 
check_getsockname(unsigned int nr)46*49cdfc7eSAndroid Build Coastguard Worker static void check_getsockname(unsigned int nr)
47*49cdfc7eSAndroid Build Coastguard Worker {
48*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = &tcases[nr];
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(getsockname(*(tc->sock), (struct sockaddr *) tc->sockaddr,
51*49cdfc7eSAndroid Build Coastguard Worker 				 tc->addrlen), tc->experrno, "%s", tc->desc);
52*49cdfc7eSAndroid Build Coastguard Worker }
53*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)54*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
55*49cdfc7eSAndroid Build Coastguard Worker {
56*49cdfc7eSAndroid Build Coastguard Worker 	sin0.sin_family = AF_INET;
57*49cdfc7eSAndroid Build Coastguard Worker 	sin0.sin_port = 0;
58*49cdfc7eSAndroid Build Coastguard Worker 	sin0.sin_addr.s_addr = INADDR_ANY;
59*49cdfc7eSAndroid Build Coastguard Worker 	sock_fake = 400;
60*49cdfc7eSAndroid Build Coastguard Worker 	sock_null = SAFE_OPEN("/dev/null", O_WRONLY);
61*49cdfc7eSAndroid Build Coastguard Worker 	sock_bind = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
62*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(sock_bind, (struct sockaddr *)&sin0, sizeof(sin0));
63*49cdfc7eSAndroid Build Coastguard Worker 	sinlen = sizeof(sin0);
64*49cdfc7eSAndroid Build Coastguard Worker }
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
67*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
68*49cdfc7eSAndroid Build Coastguard Worker 	.test = check_getsockname,
69*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
70*49cdfc7eSAndroid Build Coastguard Worker };
71