xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/getsockopt/getsockopt01.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) Linux Test Project, 2003-2017
6*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (C) 2024 SUSE LLC Andrea Manzini <[email protected]>
7*49cdfc7eSAndroid Build Coastguard Worker  */
8*49cdfc7eSAndroid Build Coastguard Worker 
9*49cdfc7eSAndroid Build Coastguard Worker /*\
10*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
11*49cdfc7eSAndroid Build Coastguard Worker  * Verify that getsockopt() returns the proper errno for various failure cases:
12*49cdfc7eSAndroid Build Coastguard Worker  *
13*49cdfc7eSAndroid Build Coastguard Worker  * - EBADF on a not open file
14*49cdfc7eSAndroid Build Coastguard Worker  * - ENOTSOCK on a file descriptor not linked to a socket
15*49cdfc7eSAndroid Build Coastguard Worker  * - EFAULT on invalid address of value or length
16*49cdfc7eSAndroid Build Coastguard Worker  * - EOPNOTSUPP on invalid option name or protocol
17*49cdfc7eSAndroid Build Coastguard Worker  * - EINVAL on an invalid optlen
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 int sock_fake, sock_null, sock_bind;
23*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in sin0;
24*49cdfc7eSAndroid Build Coastguard Worker static int sinlen;
25*49cdfc7eSAndroid Build Coastguard Worker static int optval;
26*49cdfc7eSAndroid Build Coastguard Worker static socklen_t optlen;
27*49cdfc7eSAndroid Build Coastguard Worker static socklen_t optleninval = -1;
28*49cdfc7eSAndroid Build Coastguard Worker 
29*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
30*49cdfc7eSAndroid Build Coastguard Worker 	int *sockfd;
31*49cdfc7eSAndroid Build Coastguard Worker 	int level;
32*49cdfc7eSAndroid Build Coastguard Worker 	int optname;
33*49cdfc7eSAndroid Build Coastguard Worker 	void *optval;
34*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t *optlen;
35*49cdfc7eSAndroid Build Coastguard Worker 	int experrno;
36*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
37*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
38*49cdfc7eSAndroid Build Coastguard Worker 	{.sockfd = &sock_fake, .level = SOL_SOCKET, .optname = SO_OOBINLINE, .optval = &optval,
39*49cdfc7eSAndroid Build Coastguard Worker 	.optlen = &optlen, .experrno = EBADF, .desc = "bad file descriptor"},
40*49cdfc7eSAndroid Build Coastguard Worker 
41*49cdfc7eSAndroid Build Coastguard Worker 	{.sockfd = &sock_null, .level = SOL_SOCKET, .optname = SO_OOBINLINE, .optval = &optval,
42*49cdfc7eSAndroid Build Coastguard Worker 	.optlen = &optlen, .experrno = ENOTSOCK, .desc = "bad file descriptor"},
43*49cdfc7eSAndroid Build Coastguard Worker 
44*49cdfc7eSAndroid Build Coastguard Worker 	{.sockfd = &sock_bind, .level = SOL_SOCKET, .optname = SO_OOBINLINE, .optval = 0,
45*49cdfc7eSAndroid Build Coastguard Worker 	.optlen = &optlen, .experrno = EFAULT, .desc = "invalid option buffer"},
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker 	{.sockfd = &sock_bind, .level = SOL_SOCKET, .optname = SO_OOBINLINE, .optval = &optval,
48*49cdfc7eSAndroid Build Coastguard Worker 	.optlen = 0, .experrno = EFAULT, .desc = "invalid optlen"},
49*49cdfc7eSAndroid Build Coastguard Worker 
50*49cdfc7eSAndroid Build Coastguard Worker 	{.sockfd = &sock_bind, .level = 500, .optname = SO_OOBINLINE, .optval = &optval,
51*49cdfc7eSAndroid Build Coastguard Worker 	.optlen = &optlen, .experrno = EOPNOTSUPP, .desc = "invalid level"},
52*49cdfc7eSAndroid Build Coastguard Worker 
53*49cdfc7eSAndroid Build Coastguard Worker 	{.sockfd = &sock_bind, .level = IPPROTO_UDP, .optname = SO_OOBINLINE, .optval = &optval,
54*49cdfc7eSAndroid Build Coastguard Worker 	.optlen = &optlen, .experrno = EOPNOTSUPP, .desc = "not supported option name (UDP)"},
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	{.sockfd = &sock_bind, .level = IPPROTO_IP, .optname = -1, .optval = &optval,
57*49cdfc7eSAndroid Build Coastguard Worker 	.optlen = &optlen, .experrno = ENOPROTOOPT, .desc =  "invalid option name (IP)"},
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	{.sockfd = &sock_bind, .level = IPPROTO_TCP, .optname = -1, .optval = &optval,
60*49cdfc7eSAndroid Build Coastguard Worker 	.optlen = &optlen, .experrno = ENOPROTOOPT, .desc = "invalid option name (TCP)"},
61*49cdfc7eSAndroid Build Coastguard Worker 
62*49cdfc7eSAndroid Build Coastguard Worker 	{.sockfd = &sock_bind, .level = SOL_SOCKET, .optname = SO_OOBINLINE, .optval = &optval,
63*49cdfc7eSAndroid Build Coastguard Worker 	.optlen = &optleninval, .experrno = EINVAL, .desc = "invalid optlen"},
64*49cdfc7eSAndroid Build Coastguard Worker };
65*49cdfc7eSAndroid Build Coastguard Worker 
66*49cdfc7eSAndroid Build Coastguard Worker 
check_getsockopt(unsigned int nr)67*49cdfc7eSAndroid Build Coastguard Worker static void check_getsockopt(unsigned int nr)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tc = &tcases[nr];
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(getsockopt(*(tc->sockfd), tc->level, tc->optname, tc->optval, tc->optlen),
72*49cdfc7eSAndroid Build Coastguard Worker 		     tc->experrno, "%s", tc->desc);
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)75*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker 	sin0.sin_family = AF_INET;
78*49cdfc7eSAndroid Build Coastguard Worker 	sin0.sin_port = 0;
79*49cdfc7eSAndroid Build Coastguard Worker 	sin0.sin_addr.s_addr = INADDR_ANY;
80*49cdfc7eSAndroid Build Coastguard Worker 	sock_fake = 400;
81*49cdfc7eSAndroid Build Coastguard Worker 	sock_null = SAFE_OPEN("/dev/null", O_WRONLY);
82*49cdfc7eSAndroid Build Coastguard Worker 	sock_bind = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
83*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(sock_bind, (struct sockaddr *)&sin0, sizeof(sin0));
84*49cdfc7eSAndroid Build Coastguard Worker 	sinlen = sizeof(sin0);
85*49cdfc7eSAndroid Build Coastguard Worker 	optlen = sizeof(optval);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker 
88*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
89*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
90*49cdfc7eSAndroid Build Coastguard Worker 	.test = check_getsockopt,
91*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
92*49cdfc7eSAndroid Build Coastguard Worker };
93