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