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