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