xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/accept/accept01.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 /*
4*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) International Business Machines  Corp., 2001
5*49cdfc7eSAndroid Build Coastguard Worker  *   07/2001 Ported by Wayne Boyer
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 accept() 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 <fcntl.h>
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
19*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
20*49cdfc7eSAndroid Build Coastguard Worker #include <sys/signal.h>
21*49cdfc7eSAndroid Build Coastguard Worker 
22*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
25*49cdfc7eSAndroid Build Coastguard Worker 
26*49cdfc7eSAndroid Build Coastguard Worker struct sockaddr_in sin0, sin1, fsin1;
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker int invalid_socketfd = 400; /* anything that is not an open file */
29*49cdfc7eSAndroid Build Coastguard Worker int socket_fd;
30*49cdfc7eSAndroid Build Coastguard Worker int udp_fd;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
33*49cdfc7eSAndroid Build Coastguard Worker 	int domain;		/* PF_INET, PF_UNIX, ... */
34*49cdfc7eSAndroid Build Coastguard Worker 	int type;		/* SOCK_STREAM, SOCK_DGRAM ... */
35*49cdfc7eSAndroid Build Coastguard Worker 	int proto;		/* protocol number (usually 0 = default) */
36*49cdfc7eSAndroid Build Coastguard Worker 	int *fd;		/* File descriptor for the test case */
37*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr *sockaddr;	/* socket address buffer */
38*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t salen;	/* accept's 3rd argument */
39*49cdfc7eSAndroid Build Coastguard Worker 	int experrno;		/* expected errno */
40*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
41*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
42*49cdfc7eSAndroid Build Coastguard Worker 	{
43*49cdfc7eSAndroid Build Coastguard Worker 		PF_INET, SOCK_STREAM, 0, &invalid_socketfd,
44*49cdfc7eSAndroid Build Coastguard Worker 		(struct sockaddr *)&fsin1, sizeof(fsin1), EBADF,
45*49cdfc7eSAndroid Build Coastguard Worker 		"bad file descriptor"
46*49cdfc7eSAndroid Build Coastguard Worker 	},
47*49cdfc7eSAndroid Build Coastguard Worker 	{
48*49cdfc7eSAndroid Build Coastguard Worker 		PF_INET, SOCK_STREAM, 0, &socket_fd, (struct sockaddr *)3,
49*49cdfc7eSAndroid Build Coastguard Worker 		sizeof(fsin1), EINVAL, "invalid socket buffer"
50*49cdfc7eSAndroid Build Coastguard Worker 	},
51*49cdfc7eSAndroid Build Coastguard Worker 	{
52*49cdfc7eSAndroid Build Coastguard Worker 		PF_INET, SOCK_STREAM, 0, &socket_fd, (struct sockaddr *)&fsin1,
53*49cdfc7eSAndroid Build Coastguard Worker 		1, EINVAL, "invalid salen"
54*49cdfc7eSAndroid Build Coastguard Worker 	},
55*49cdfc7eSAndroid Build Coastguard Worker 	{
56*49cdfc7eSAndroid Build Coastguard Worker 		PF_INET, SOCK_STREAM, 0, &socket_fd, (struct sockaddr *)&fsin1,
57*49cdfc7eSAndroid Build Coastguard Worker 		sizeof(fsin1), EINVAL, "no queued connections"
58*49cdfc7eSAndroid Build Coastguard Worker 	},
59*49cdfc7eSAndroid Build Coastguard Worker 	{
60*49cdfc7eSAndroid Build Coastguard Worker 		PF_INET, SOCK_STREAM, 0, &udp_fd, (struct sockaddr *)&fsin1,
61*49cdfc7eSAndroid Build Coastguard Worker 		sizeof(fsin1), EOPNOTSUPP, "UDP accept"
62*49cdfc7eSAndroid Build Coastguard Worker 	},
63*49cdfc7eSAndroid Build Coastguard Worker };
64*49cdfc7eSAndroid Build Coastguard Worker 
test_setup(void)65*49cdfc7eSAndroid Build Coastguard Worker static void test_setup(void)
66*49cdfc7eSAndroid Build Coastguard Worker {
67*49cdfc7eSAndroid Build Coastguard Worker 	sin0.sin_family = AF_INET;
68*49cdfc7eSAndroid Build Coastguard Worker 	sin0.sin_port = 0;
69*49cdfc7eSAndroid Build Coastguard Worker 	sin0.sin_addr.s_addr = INADDR_ANY;
70*49cdfc7eSAndroid Build Coastguard Worker 
71*49cdfc7eSAndroid Build Coastguard Worker 	socket_fd = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
72*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(socket_fd, (struct sockaddr *)&sin0, sizeof(sin0));
73*49cdfc7eSAndroid Build Coastguard Worker 
74*49cdfc7eSAndroid Build Coastguard Worker 	sin1.sin_family = AF_INET;
75*49cdfc7eSAndroid Build Coastguard Worker 	sin1.sin_port = 0;
76*49cdfc7eSAndroid Build Coastguard Worker 	sin1.sin_addr.s_addr = INADDR_ANY;
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	udp_fd = SAFE_SOCKET(PF_INET, SOCK_DGRAM, 0);
79*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_BIND(udp_fd, (struct sockaddr *)&sin1, sizeof(sin1));
80*49cdfc7eSAndroid Build Coastguard Worker }
81*49cdfc7eSAndroid Build Coastguard Worker 
test_cleanup(void)82*49cdfc7eSAndroid Build Coastguard Worker static void test_cleanup(void)
83*49cdfc7eSAndroid Build Coastguard Worker {
84*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(socket_fd);
85*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(udp_fd);
86*49cdfc7eSAndroid Build Coastguard Worker }
87*49cdfc7eSAndroid Build Coastguard Worker 
verify_accept(unsigned int nr)88*49cdfc7eSAndroid Build Coastguard Worker void verify_accept(unsigned int nr)
89*49cdfc7eSAndroid Build Coastguard Worker {
90*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tcase = &tcases[nr];
91*49cdfc7eSAndroid Build Coastguard Worker 
92*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL2(accept(*tcase->fd, tcase->sockaddr, &tcase->salen),
93*49cdfc7eSAndroid Build Coastguard Worker 	             tcase->experrno, "%s", tcase->desc);
94*49cdfc7eSAndroid Build Coastguard Worker }
95*49cdfc7eSAndroid Build Coastguard Worker 
96*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
97*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
98*49cdfc7eSAndroid Build Coastguard Worker 	.setup = test_setup,
99*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = test_cleanup,
100*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_accept,
101*49cdfc7eSAndroid Build Coastguard Worker };
102