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: socket01
8f08c3bdfSopenharmony_ci*
9f08c3bdfSopenharmony_ci* Test Description:
10f08c3bdfSopenharmony_ci* Verify that socket() returns the proper errno for various failure cases
11f08c3bdfSopenharmony_ci*
12f08c3bdfSopenharmony_ci*/
13f08c3bdfSopenharmony_ci
14f08c3bdfSopenharmony_ci#include <stdio.h>
15f08c3bdfSopenharmony_ci#include <unistd.h>
16f08c3bdfSopenharmony_ci#include <errno.h>
17f08c3bdfSopenharmony_ci#include <sys/types.h>
18f08c3bdfSopenharmony_ci#include <sys/socket.h>
19f08c3bdfSopenharmony_ci#include <sys/un.h>
20f08c3bdfSopenharmony_ci#include <netinet/in.h>
21f08c3bdfSopenharmony_ci#include "tst_test.h"
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistruct test_case_t {
24f08c3bdfSopenharmony_ci	int domain;
25f08c3bdfSopenharmony_ci	int type;
26f08c3bdfSopenharmony_ci	int proto;
27f08c3bdfSopenharmony_ci	int retval;
28f08c3bdfSopenharmony_ci	int experrno;
29f08c3bdfSopenharmony_ci	char *desc;
30f08c3bdfSopenharmony_ci} tdat[] = {
31f08c3bdfSopenharmony_ci	{0, SOCK_STREAM, 0, -1, EAFNOSUPPORT, "invalid domain"},
32f08c3bdfSopenharmony_ci	{PF_INET, 75, 0, -1, EINVAL, "invalid type"},
33f08c3bdfSopenharmony_ci	{PF_UNIX, SOCK_DGRAM, 0, 0, 0, "UNIX domain dgram"},
34f08c3bdfSopenharmony_ci	{PF_INET, SOCK_RAW, 0, -1, EPROTONOSUPPORT, "raw open as non-root"},
35f08c3bdfSopenharmony_ci	{PF_INET, SOCK_DGRAM, 17, 0, 0, "UDP socket"},
36f08c3bdfSopenharmony_ci	{PF_INET, SOCK_STREAM, 17, -1, EPROTONOSUPPORT, "UDP stream"},
37f08c3bdfSopenharmony_ci	{PF_INET, SOCK_DGRAM, 6, -1, EPROTONOSUPPORT, "TCP dgram"},
38f08c3bdfSopenharmony_ci	{PF_INET, SOCK_STREAM, 6, 0, 0, "TCP socket"},
39f08c3bdfSopenharmony_ci	{PF_INET, SOCK_STREAM, 1, -1, EPROTONOSUPPORT, "ICMP stream"}
40f08c3bdfSopenharmony_ci};
41f08c3bdfSopenharmony_ci
42f08c3bdfSopenharmony_cistatic void verify_socket(unsigned int n)
43f08c3bdfSopenharmony_ci{
44f08c3bdfSopenharmony_ci	int fd;
45f08c3bdfSopenharmony_ci	struct test_case_t *tc = &tdat[n];
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ci	TEST(fd = socket(tc->domain, tc->type, tc->proto));
48f08c3bdfSopenharmony_ci	if (TST_RET >= 0)
49f08c3bdfSopenharmony_ci		TST_RET = 0;
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_ci	if (fd > 0)
52f08c3bdfSopenharmony_ci		SAFE_CLOSE(fd);
53f08c3bdfSopenharmony_ci
54f08c3bdfSopenharmony_ci	if (TST_RET != tc->retval) {
55f08c3bdfSopenharmony_ci		tst_res(TFAIL, "%s returned %d (expected %d)",
56f08c3bdfSopenharmony_ci			tc->desc, fd, tc->retval);
57f08c3bdfSopenharmony_ci		return;
58f08c3bdfSopenharmony_ci	}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_ci	if (TST_ERR != tc->experrno) {
61f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "expected %s(%d)",
62f08c3bdfSopenharmony_ci		        tst_strerrno(tc->experrno), tc->experrno);
63f08c3bdfSopenharmony_ci		return;
64f08c3bdfSopenharmony_ci	}
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	tst_res(TPASS, "%s successful", tc->desc);
67f08c3bdfSopenharmony_ci}
68f08c3bdfSopenharmony_ci
69f08c3bdfSopenharmony_cistatic struct tst_test test = {
70f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tdat),
71f08c3bdfSopenharmony_ci	.test = verify_socket
72f08c3bdfSopenharmony_ci};
73