1 // SPDX-License-Identifier: GPL-2.0-or-later
2
3 /*
4 * Copyright (c) International Business Machines Corp., 2001
5 * 07/2001 Ported by Wayne Boyer
6 */
7
8 /*\
9 * [Description]
10 * Verify that accept() returns the proper errno for various failure cases.
11 */
12
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <fcntl.h>
17
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <sys/signal.h>
21
22 #include <netinet/in.h>
23
24 #include "tst_test.h"
25
26 struct sockaddr_in sin0, sin1, fsin1;
27
28 int invalid_socketfd = 400; /* anything that is not an open file */
29 int devnull_fd;
30 int socket_fd;
31 int udp_fd;
32
33 static struct test_case {
34 int domain; /* PF_INET, PF_UNIX, ... */
35 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
36 int proto; /* protocol number (usually 0 = default) */
37 int *fd; /* File descriptor for the test case */
38 struct sockaddr *sockaddr; /* socket address buffer */
39 socklen_t salen; /* accept's 3rd argument */
40 int experrno; /* expected errno */
41 char *desc;
42 } tcases[] = {
43 {
44 PF_INET, SOCK_STREAM, 0, &invalid_socketfd,
45 (struct sockaddr *)&fsin1, sizeof(fsin1), EBADF,
46 "bad file descriptor"
47 },
48 {
49 PF_INET, SOCK_STREAM, 0, &devnull_fd, (struct sockaddr *)&fsin1,
50 sizeof(fsin1), ENOTSOCK, "fd is not socket"
51 },
52 {
53 PF_INET, SOCK_STREAM, 0, &socket_fd, (struct sockaddr *)3,
54 sizeof(fsin1), EINVAL, "invalid socket buffer"
55 },
56 {
57 PF_INET, SOCK_STREAM, 0, &socket_fd, (struct sockaddr *)&fsin1,
58 1, EINVAL, "invalid salen"
59 },
60 {
61 PF_INET, SOCK_STREAM, 0, &socket_fd, (struct sockaddr *)&fsin1,
62 sizeof(fsin1), EINVAL, "no queued connections"
63 },
64 {
65 PF_INET, SOCK_STREAM, 0, &udp_fd, (struct sockaddr *)&fsin1,
66 sizeof(fsin1), EOPNOTSUPP, "UDP accept"
67 },
68 };
69
test_setup(void)70 static void test_setup(void)
71 {
72 sin0.sin_family = AF_INET;
73 sin0.sin_port = 0;
74 sin0.sin_addr.s_addr = INADDR_ANY;
75
76 devnull_fd = SAFE_OPEN("/dev/null", O_WRONLY);
77
78 socket_fd = SAFE_SOCKET(PF_INET, SOCK_STREAM, 0);
79 SAFE_BIND(socket_fd, (struct sockaddr *)&sin0, sizeof(sin0));
80
81 sin1.sin_family = AF_INET;
82 sin1.sin_port = 0;
83 sin1.sin_addr.s_addr = INADDR_ANY;
84
85 udp_fd = SAFE_SOCKET(PF_INET, SOCK_DGRAM, 0);
86 SAFE_BIND(udp_fd, (struct sockaddr *)&sin1, sizeof(sin1));
87 }
88
test_cleanup(void)89 static void test_cleanup(void)
90 {
91 SAFE_CLOSE(devnull_fd);
92 SAFE_CLOSE(socket_fd);
93 SAFE_CLOSE(udp_fd);
94 }
95
verify_accept(unsigned int nr)96 void verify_accept(unsigned int nr)
97 {
98 struct test_case *tcase = &tcases[nr];
99
100 TST_EXP_FAIL2(accept(*tcase->fd, tcase->sockaddr, &tcase->salen),
101 tcase->experrno, "%s", tcase->desc);
102 }
103
104 static struct tst_test test = {
105 .tcnt = ARRAY_SIZE(tcases),
106 .setup = test_setup,
107 .cleanup = test_cleanup,
108 .test = verify_accept,
109 };
110