1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 John George
5*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2020 Martin Doucha <[email protected]>
6*49cdfc7eSAndroid Build Coastguard Worker */
7*49cdfc7eSAndroid Build Coastguard Worker
8*49cdfc7eSAndroid Build Coastguard Worker /*
9*49cdfc7eSAndroid Build Coastguard Worker * Test Description:
10*49cdfc7eSAndroid Build Coastguard Worker * Verify that setsockopt() returns the proper errno for various failure cases
11*49cdfc7eSAndroid Build Coastguard Worker */
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include <sys/socket.h>
16*49cdfc7eSAndroid Build Coastguard Worker #include <sys/ioctl.h>
17*49cdfc7eSAndroid Build Coastguard Worker #include <netinet/in.h>
18*49cdfc7eSAndroid Build Coastguard Worker
19*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
20*49cdfc7eSAndroid Build Coastguard Worker
21*49cdfc7eSAndroid Build Coastguard Worker static struct sockaddr_in addr;
22*49cdfc7eSAndroid Build Coastguard Worker static int optval;
23*49cdfc7eSAndroid Build Coastguard Worker
24*49cdfc7eSAndroid Build Coastguard Worker static struct test_case { /* test case structure */
25*49cdfc7eSAndroid Build Coastguard Worker int domain; /* PF_INET, PF_UNIX, ... */
26*49cdfc7eSAndroid Build Coastguard Worker int type; /* SOCK_STREAM, SOCK_DGRAM ... */
27*49cdfc7eSAndroid Build Coastguard Worker int proto; /* protocol number (usually 0 = default) */
28*49cdfc7eSAndroid Build Coastguard Worker int level; /* IPPROTO_* */
29*49cdfc7eSAndroid Build Coastguard Worker int optname;
30*49cdfc7eSAndroid Build Coastguard Worker void *optval;
31*49cdfc7eSAndroid Build Coastguard Worker int optlen;
32*49cdfc7eSAndroid Build Coastguard Worker int experrno; /* expected errno */
33*49cdfc7eSAndroid Build Coastguard Worker char *desc;
34*49cdfc7eSAndroid Build Coastguard Worker } testcase_list[] = {
35*49cdfc7eSAndroid Build Coastguard Worker {-1, -1, -1, SOL_SOCKET, SO_OOBINLINE, &optval, sizeof(optval),
36*49cdfc7eSAndroid Build Coastguard Worker EBADF, "invalid file descriptor"},
37*49cdfc7eSAndroid Build Coastguard Worker {-1, -1, -1, SOL_SOCKET, SO_OOBINLINE, &optval, sizeof(optval),
38*49cdfc7eSAndroid Build Coastguard Worker ENOTSOCK, "non-socket file descriptor"},
39*49cdfc7eSAndroid Build Coastguard Worker {PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, NULL,
40*49cdfc7eSAndroid Build Coastguard Worker sizeof(optval), EFAULT, "invalid option buffer"},
41*49cdfc7eSAndroid Build Coastguard Worker {PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 0,
42*49cdfc7eSAndroid Build Coastguard Worker EINVAL, "invalid optlen"},
43*49cdfc7eSAndroid Build Coastguard Worker {PF_INET, SOCK_STREAM, 0, 500, SO_OOBINLINE, &optval, sizeof(optval),
44*49cdfc7eSAndroid Build Coastguard Worker ENOPROTOOPT, "invalid level"},
45*49cdfc7eSAndroid Build Coastguard Worker {PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval,
46*49cdfc7eSAndroid Build Coastguard Worker sizeof(optval), ENOPROTOOPT, "invalid option name (UDP)"},
47*49cdfc7eSAndroid Build Coastguard Worker {PF_INET, SOCK_STREAM, 0, IPPROTO_IP, -1, &optval, sizeof(optval),
48*49cdfc7eSAndroid Build Coastguard Worker ENOPROTOOPT, "invalid option name (IP)"},
49*49cdfc7eSAndroid Build Coastguard Worker {PF_INET, SOCK_STREAM, 0, IPPROTO_TCP, -1, &optval, sizeof(optval),
50*49cdfc7eSAndroid Build Coastguard Worker ENOPROTOOPT, "invalid option name (TCP)"}
51*49cdfc7eSAndroid Build Coastguard Worker };
52*49cdfc7eSAndroid Build Coastguard Worker
setup(void)53*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
54*49cdfc7eSAndroid Build Coastguard Worker {
55*49cdfc7eSAndroid Build Coastguard Worker /* initialize local sockaddr */
56*49cdfc7eSAndroid Build Coastguard Worker addr.sin_family = AF_INET;
57*49cdfc7eSAndroid Build Coastguard Worker addr.sin_port = 0;
58*49cdfc7eSAndroid Build Coastguard Worker addr.sin_addr.s_addr = INADDR_ANY;
59*49cdfc7eSAndroid Build Coastguard Worker }
60*49cdfc7eSAndroid Build Coastguard Worker
run(unsigned int n)61*49cdfc7eSAndroid Build Coastguard Worker static void run(unsigned int n)
62*49cdfc7eSAndroid Build Coastguard Worker {
63*49cdfc7eSAndroid Build Coastguard Worker struct test_case *tc = testcase_list + n;
64*49cdfc7eSAndroid Build Coastguard Worker int tmpfd, fd;
65*49cdfc7eSAndroid Build Coastguard Worker
66*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "Testing %s", tc->desc);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker if (tc->domain == -1) {
69*49cdfc7eSAndroid Build Coastguard Worker tmpfd = fd = SAFE_OPEN("/dev/null", O_WRONLY);
70*49cdfc7eSAndroid Build Coastguard Worker } else {
71*49cdfc7eSAndroid Build Coastguard Worker tmpfd = fd = SAFE_SOCKET(tc->domain, tc->type, tc->proto);
72*49cdfc7eSAndroid Build Coastguard Worker SAFE_BIND(fd, (struct sockaddr *)&addr, sizeof(addr));
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker
75*49cdfc7eSAndroid Build Coastguard Worker /* Use closed file descriptor rather than -1 */
76*49cdfc7eSAndroid Build Coastguard Worker if (tc->experrno == EBADF)
77*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(tmpfd);
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker TEST(setsockopt(fd, tc->level, tc->optname, tc->optval, tc->optlen));
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker if (tc->experrno != EBADF)
82*49cdfc7eSAndroid Build Coastguard Worker SAFE_CLOSE(fd);
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == 0) {
85*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "setsockopt() succeeded unexpectedly");
86*49cdfc7eSAndroid Build Coastguard Worker return;
87*49cdfc7eSAndroid Build Coastguard Worker }
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET != -1) {
90*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
91*49cdfc7eSAndroid Build Coastguard Worker "Invalid setsockopt() return value %ld", TST_RET);
92*49cdfc7eSAndroid Build Coastguard Worker return;
93*49cdfc7eSAndroid Build Coastguard Worker }
94*49cdfc7eSAndroid Build Coastguard Worker
95*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR != tc->experrno) {
96*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
97*49cdfc7eSAndroid Build Coastguard Worker "setsockopt() returned unexpected error");
98*49cdfc7eSAndroid Build Coastguard Worker return;
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS | TTERRNO, "setsockopt() returned the expected error");
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker
104*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
105*49cdfc7eSAndroid Build Coastguard Worker .test = run,
106*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(testcase_list),
107*49cdfc7eSAndroid Build Coastguard Worker .setup = setup
108*49cdfc7eSAndroid Build Coastguard Worker };
109