1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci* Copyright (c) International Business Machines Corp., 2001
4f08c3bdfSopenharmony_ci*/
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci* Test Name: socketpair01
8f08c3bdfSopenharmony_ci*
9f08c3bdfSopenharmony_ci* Test Description:
10f08c3bdfSopenharmony_ci* Verify that socketpair() 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 <sys/types.h>
17f08c3bdfSopenharmony_ci#include <sys/socket.h>
18f08c3bdfSopenharmony_ci#include <sys/un.h>
19f08c3bdfSopenharmony_ci#include <netinet/in.h>
20f08c3bdfSopenharmony_ci#include "tst_test.h"
21f08c3bdfSopenharmony_ci
22f08c3bdfSopenharmony_cistatic int fds[2];
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistruct test_case_t {
25f08c3bdfSopenharmony_ci	int domain;
26f08c3bdfSopenharmony_ci	int type;
27f08c3bdfSopenharmony_ci	int proto;
28f08c3bdfSopenharmony_ci	int *sv;
29f08c3bdfSopenharmony_ci	int retval;
30f08c3bdfSopenharmony_ci	int experrno;
31f08c3bdfSopenharmony_ci	char *desc;
32f08c3bdfSopenharmony_ci} tdat[] = {
33f08c3bdfSopenharmony_ci	{0, SOCK_STREAM, 0, fds, -1, EAFNOSUPPORT, "invalid domain"},
34f08c3bdfSopenharmony_ci	{PF_INET, 75, 0, fds, -1, EINVAL, "invalid type"},
35f08c3bdfSopenharmony_ci	{PF_UNIX, SOCK_DGRAM, 0, fds, 0, 0, "UNIX domain dgram"},
36f08c3bdfSopenharmony_ci	{PF_INET, SOCK_RAW, 0, fds, -1, EPROTONOSUPPORT, "raw open as non-root"},
37f08c3bdfSopenharmony_ci#ifndef UCLINUX
38f08c3bdfSopenharmony_ci	{PF_UNIX, SOCK_STREAM, 0, 0, -1, EFAULT, "bad aligned pointer"},
39f08c3bdfSopenharmony_ci	{PF_UNIX, SOCK_STREAM, 0, (int *)7, -1, EFAULT, "bad unaligned pointer"},
40f08c3bdfSopenharmony_ci#endif
41f08c3bdfSopenharmony_ci	{PF_INET, SOCK_DGRAM, 17, fds, -1, EOPNOTSUPP, "UDP socket"},
42f08c3bdfSopenharmony_ci	{PF_INET, SOCK_DGRAM, 6, fds, -1, EPROTONOSUPPORT, "TCP dgram"},
43f08c3bdfSopenharmony_ci	{PF_INET, SOCK_STREAM, 6, fds, -1, EOPNOTSUPP, "TCP socket"},
44f08c3bdfSopenharmony_ci	{PF_INET, SOCK_STREAM, 1, fds, -1, EPROTONOSUPPORT, "ICMP stream"}
45f08c3bdfSopenharmony_ci};
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic void verify_socketpair(unsigned int n)
48f08c3bdfSopenharmony_ci{
49f08c3bdfSopenharmony_ci	struct test_case_t *tc = &tdat[n];
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	TEST(socketpair(tc->domain, tc->type, tc->proto, tc->sv));
52f08c3bdfSopenharmony_ci
53f08c3bdfSopenharmony_ci	if (TST_RET == 0) {
54f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[0]);
55f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[1]);
56f08c3bdfSopenharmony_ci	}
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	if (TST_RET != tc->retval) {
59f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s returned %ld (expected %d)",
60f08c3bdfSopenharmony_ci			tc->desc, TST_RET, tc->retval);
61f08c3bdfSopenharmony_ci		return;
62f08c3bdfSopenharmony_ci	}
63f08c3bdfSopenharmony_ci
64f08c3bdfSopenharmony_ci	if (TST_ERR != tc->experrno) {
65f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "expected %s(%d)",
66f08c3bdfSopenharmony_ci		        tst_strerrno(tc->experrno), tc->experrno);
67f08c3bdfSopenharmony_ci		return;
68f08c3bdfSopenharmony_ci	}
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	tst_res(TPASS, "%s successful", tc->desc);
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cistatic struct tst_test test = {
74f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tdat),
75f08c3bdfSopenharmony_ci	.test = verify_socketpair
76f08c3bdfSopenharmony_ci};
77