xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/bind/bind01.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  */
6*49cdfc7eSAndroid Build Coastguard Worker 
7*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
8*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
9*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
10*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
11*49cdfc7eSAndroid Build Coastguard Worker 
12*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
13*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/un.h>
15*49cdfc7eSAndroid Build Coastguard Worker 
16*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
17*49cdfc7eSAndroid Build Coastguard Worker 
18*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker #define DIR_ENOTDIR "dir_enotdir"
21*49cdfc7eSAndroid Build Coastguard Worker #define TEST_ENOTDIR "test_enotdir"
22*49cdfc7eSAndroid Build Coastguard Worker 
23*49cdfc7eSAndroid Build Coastguard Worker static int inet_socket;
24*49cdfc7eSAndroid Build Coastguard Worker static int dev_null;
25*49cdfc7eSAndroid Build Coastguard Worker static int fd_ebadf = -1;
26*49cdfc7eSAndroid Build Coastguard Worker static int fd_enotdir;
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in sin1, sin2, sin3;
29*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_un sun, sock_enotdir;
30*49cdfc7eSAndroid Build Coastguard Worker 
31*49cdfc7eSAndroid Build Coastguard Worker static struct test_case {
32*49cdfc7eSAndroid Build Coastguard Worker 	int *socket_fd;
33*49cdfc7eSAndroid Build Coastguard Worker 	struct sockaddr *sockaddr;
34*49cdfc7eSAndroid Build Coastguard Worker 	socklen_t salen;
35*49cdfc7eSAndroid Build Coastguard Worker 	int retval;
36*49cdfc7eSAndroid Build Coastguard Worker 	int experrno;
37*49cdfc7eSAndroid Build Coastguard Worker 	char *desc;
38*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
39*49cdfc7eSAndroid Build Coastguard Worker 	{ &inet_socket, (struct sockaddr *)&sin1, 3, -1,
40*49cdfc7eSAndroid Build Coastguard Worker 	  EINVAL, "invalid salen" },
41*49cdfc7eSAndroid Build Coastguard Worker 	{ &dev_null, (struct sockaddr *)&sin1, sizeof(sin1), -1,
42*49cdfc7eSAndroid Build Coastguard Worker 	  ENOTSOCK, "invalid socket" },
43*49cdfc7eSAndroid Build Coastguard Worker 	{ &inet_socket, (struct sockaddr *)&sin2, sizeof(sin2), 0,
44*49cdfc7eSAndroid Build Coastguard Worker 	  0, "INADDR_ANYPORT"},
45*49cdfc7eSAndroid Build Coastguard Worker 	{ &inet_socket, (struct sockaddr *)&sun, sizeof(sun), -1,
46*49cdfc7eSAndroid Build Coastguard Worker 	  EAFNOSUPPORT, "UNIX-domain of current directory" },
47*49cdfc7eSAndroid Build Coastguard Worker 	{ &inet_socket, (struct sockaddr *)&sin3, sizeof(sin3), -1,
48*49cdfc7eSAndroid Build Coastguard Worker 	  EADDRNOTAVAIL, "non-local address" },
49*49cdfc7eSAndroid Build Coastguard Worker 	{ &fd_ebadf, (struct sockaddr *)&sin1, sizeof(sin1), -1,
50*49cdfc7eSAndroid Build Coastguard Worker 	  EBADF, "sockfd is not a valid file descriptor" },
51*49cdfc7eSAndroid Build Coastguard Worker 	{ &fd_enotdir, (struct sockaddr *)&sock_enotdir, sizeof(sock_enotdir), -1,
52*49cdfc7eSAndroid Build Coastguard Worker 	  ENOTDIR, "a component of addr prefix is not a directory"},
53*49cdfc7eSAndroid Build Coastguard Worker };
54*49cdfc7eSAndroid Build Coastguard Worker 
verify_bind(unsigned int nr)55*49cdfc7eSAndroid Build Coastguard Worker static void verify_bind(unsigned int nr)
56*49cdfc7eSAndroid Build Coastguard Worker {
57*49cdfc7eSAndroid Build Coastguard Worker 	struct test_case *tcase = &tcases[nr];
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 	if (tcase->experrno) {
60*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_FAIL(bind(*tcase->socket_fd, tcase->sockaddr, tcase->salen),
61*49cdfc7eSAndroid Build Coastguard Worker 				tcase->experrno, "%s", tcase->desc);
62*49cdfc7eSAndroid Build Coastguard Worker 	} else {
63*49cdfc7eSAndroid Build Coastguard Worker 		TST_EXP_PASS(bind(*tcase->socket_fd, tcase->sockaddr, tcase->salen),
64*49cdfc7eSAndroid Build Coastguard Worker 				"%s", tcase->desc);
65*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_CLOSE(inet_socket);
66*49cdfc7eSAndroid Build Coastguard Worker 		inet_socket = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
67*49cdfc7eSAndroid Build Coastguard Worker 	}
68*49cdfc7eSAndroid Build Coastguard Worker }
69*49cdfc7eSAndroid Build Coastguard Worker 
test_setup(void)70*49cdfc7eSAndroid Build Coastguard Worker static void test_setup(void)
71*49cdfc7eSAndroid Build Coastguard Worker {
72*49cdfc7eSAndroid Build Coastguard Worker 	/* initialize sockaddr's */
73*49cdfc7eSAndroid Build Coastguard Worker 	sin1.sin_family = AF_INET;
74*49cdfc7eSAndroid Build Coastguard Worker 	/* this port must be unused! */
75*49cdfc7eSAndroid Build Coastguard Worker 	sin1.sin_port = TST_GET_UNUSED_PORT(AF_INET, SOCK_STREAM);
76*49cdfc7eSAndroid Build Coastguard Worker 	sin1.sin_addr.s_addr = INADDR_ANY;
77*49cdfc7eSAndroid Build Coastguard Worker 
78*49cdfc7eSAndroid Build Coastguard Worker 	sin2.sin_family = AF_INET;
79*49cdfc7eSAndroid Build Coastguard Worker 	sin2.sin_port = 0;
80*49cdfc7eSAndroid Build Coastguard Worker 	sin2.sin_addr.s_addr = INADDR_ANY;
81*49cdfc7eSAndroid Build Coastguard Worker 
82*49cdfc7eSAndroid Build Coastguard Worker 	sin3.sin_family = AF_INET;
83*49cdfc7eSAndroid Build Coastguard Worker 	sin3.sin_port = 0;
84*49cdfc7eSAndroid Build Coastguard Worker 	/* assumes 10.255.254.253 is not a local interface address! */
85*49cdfc7eSAndroid Build Coastguard Worker 	sin3.sin_addr.s_addr = htonl(0x0AFFFEFD);
86*49cdfc7eSAndroid Build Coastguard Worker 
87*49cdfc7eSAndroid Build Coastguard Worker 	sun.sun_family = AF_UNIX;
88*49cdfc7eSAndroid Build Coastguard Worker 	strncpy(sun.sun_path, ".", sizeof(sun.sun_path));
89*49cdfc7eSAndroid Build Coastguard Worker 
90*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_TOUCH(DIR_ENOTDIR, 0777, NULL);
91*49cdfc7eSAndroid Build Coastguard Worker 	sock_enotdir.sun_family = AF_UNIX;
92*49cdfc7eSAndroid Build Coastguard Worker 	strncpy(sock_enotdir.sun_path, DIR_ENOTDIR "/" TEST_ENOTDIR,
93*49cdfc7eSAndroid Build Coastguard Worker 		sizeof(sock_enotdir.sun_path));
94*49cdfc7eSAndroid Build Coastguard Worker 
95*49cdfc7eSAndroid Build Coastguard Worker 	inet_socket = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
96*49cdfc7eSAndroid Build Coastguard Worker 	dev_null = SAFE_OPEN("/dev/null", O_WRONLY);
97*49cdfc7eSAndroid Build Coastguard Worker 	fd_enotdir = SAFE_SOCKET(AF_UNIX, SOCK_STREAM, 0);
98*49cdfc7eSAndroid Build Coastguard Worker }
99*49cdfc7eSAndroid Build Coastguard Worker 
test_cleanup(void)100*49cdfc7eSAndroid Build Coastguard Worker static void test_cleanup(void)
101*49cdfc7eSAndroid Build Coastguard Worker {
102*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(inet_socket);
103*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(dev_null);
104*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(fd_enotdir);
105*49cdfc7eSAndroid Build Coastguard Worker }
106*49cdfc7eSAndroid Build Coastguard Worker 
107*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
108*49cdfc7eSAndroid Build Coastguard Worker 	.tcnt = ARRAY_SIZE(tcases),
109*49cdfc7eSAndroid Build Coastguard Worker 	.setup = test_setup,
110*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = test_cleanup,
111*49cdfc7eSAndroid Build Coastguard Worker 	.test = verify_bind,
112*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
113*49cdfc7eSAndroid Build Coastguard Worker };
114