1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * Test Name: setsockopt01
22 *
23 * Test Description:
24 * Verify that setsockopt() returns the proper errno for various failure cases
25 *
26 * Usage: <for command-line>
27 * setsockopt01 [-c n] [-e] [-i n] [-I x] [-p x] [-t]
28 * where, -c n : Run n copies concurrently.
29 * -e : Turn on errno logging.
30 * -i n : Execute test n times.
31 * -I x : Execute test for x seconds.
32 * -P x : Pause for x seconds between iterations.
33 * -t : Turn on syscall timing.
34 *
35 * History
36 * 07/2001 John George
37 * -Ported
38 *
39 * Restrictions:
40 * None.
41 *
42 */
43
44 #include <stdio.h>
45 #include <unistd.h>
46 #include <errno.h>
47 #include <fcntl.h>
48
49 #include <sys/types.h>
50 #include <sys/socket.h>
51 #include <sys/signal.h>
52 #include <sys/ioctl.h>
53
54 #include <netinet/in.h>
55
56 #include "test.h"
57
58 char *TCID = "setsockopt01";
59 int testno;
60
61 int s; /* socket descriptor */
62 struct sockaddr_in sin0, fsin1;
63 int optval;
64
65 void setup(void), setup0(void), setup1(void),
66 cleanup(void), cleanup0(void), cleanup1(void);
67
68 struct test_case_t { /* test case structure */
69 int domain; /* PF_INET, PF_UNIX, ... */
70 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
71 int proto; /* protocol number (usually 0 = default) */
72 int level; /* IPPROTO_* */
73 int optname;
74 void *optval;
75 int optlen;
76 struct sockaddr *sin;
77 int salen;
78 int retval; /* syscall return value */
79 int experrno; /* expected errno */
80 void (*setup) (void);
81 void (*cleanup) (void);
82 char *desc;
83 } tdat[] = {
84 {
85 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval,
86 sizeof(optval), (struct sockaddr *)&fsin1,
87 sizeof(fsin1), -1, EBADF, setup0, cleanup0,
88 "bad file descriptor"}
89 , {
90 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval,
91 sizeof(optval), (struct sockaddr *)&fsin1,
92 sizeof(fsin1), -1, ENOTSOCK, setup0, cleanup0,
93 "bad file descriptor"}
94 ,
95 #if !defined(UCLINUX)
96 {
97 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, 0,
98 sizeof(optval), (struct sockaddr *)&fsin1,
99 sizeof(fsin1), -1, EFAULT, setup1, cleanup1,
100 "invalid option buffer"}
101 ,
102 #endif
103 {
104 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 0,
105 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
106 EINVAL, setup1, cleanup1, "invalid optlen"}
107 , {
108 PF_INET, SOCK_STREAM, 0, 500, SO_OOBINLINE, &optval,
109 sizeof(optval), (struct sockaddr *)&fsin1,
110 sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1,
111 "invalid level"}
112 , {
113 PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval,
114 sizeof(optval), (struct sockaddr *)&fsin1,
115 sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1,
116 "invalid option name (UDP)"}
117 , {
118 PF_INET, SOCK_STREAM, 0, IPPROTO_IP, -1, &optval,
119 sizeof(optval), (struct sockaddr *)&fsin1,
120 sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1,
121 "invalid option name (IP)"}
122 , {
123 PF_INET, SOCK_STREAM, 0, IPPROTO_TCP, -1, &optval,
124 sizeof(optval), (struct sockaddr *)&fsin1,
125 sizeof(fsin1), -1, ENOPROTOOPT, setup1, cleanup1,
126 "invalid option name (TCP)"}
127 ,};
128
129 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
130
main(int argc,char * argv[])131 int main(int argc, char *argv[])
132 {
133 int lc;
134
135 tst_parse_opts(argc, argv, NULL, NULL);
136 setup();
137
138 for (lc = 0; TEST_LOOPING(lc); ++lc) {
139 tst_count = 0;
140 for (testno = 0; testno < TST_TOTAL; ++testno) {
141 tdat[testno].setup();
142
143 TEST(setsockopt(s, tdat[testno].level,
144 tdat[testno].optname,
145 tdat[testno].optval,
146 tdat[testno].optlen));
147
148 if (TEST_RETURN == -1) {
149 }
150
151 if (TEST_RETURN != tdat[testno].retval ||
152 (TEST_RETURN < 0 &&
153 TEST_ERRNO != tdat[testno].experrno)) {
154 tst_resm(TFAIL, "%s ; returned"
155 " %ld (expected %d), errno %d (expected"
156 " %d)", tdat[testno].desc,
157 TEST_RETURN, tdat[testno].retval,
158 TEST_ERRNO, tdat[testno].experrno);
159 } else {
160 tst_resm(TPASS, "%s successful",
161 tdat[testno].desc);
162 }
163 tdat[testno].cleanup();
164 }
165 }
166 cleanup();
167 tst_exit();
168 }
169
setup(void)170 void setup(void)
171 {
172
173 tst_sig(NOFORK, DEF_HANDLER, cleanup);
174
175 TEST_PAUSE;
176
177 /* initialize local sockaddr */
178 sin0.sin_family = AF_INET;
179 sin0.sin_port = 0;
180 sin0.sin_addr.s_addr = INADDR_ANY;
181 }
182
cleanup(void)183 void cleanup(void)
184 {
185 }
186
setup0(void)187 void setup0(void)
188 {
189 if (tdat[testno].experrno == EBADF)
190 s = 400; /* anything not an open file */
191 else if ((s = open("/dev/null", O_WRONLY)) == -1)
192 tst_brkm(TBROK, cleanup, "error opening /dev/null - "
193 "errno: %s", strerror(errno));
194 }
195
cleanup0(void)196 void cleanup0(void)
197 {
198 s = -1;
199 }
200
setup1(void)201 void setup1(void)
202 {
203 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
204 if (s < 0) {
205 tst_brkm(TBROK, cleanup, "socket setup failed for setsockopt:"
206 " %s", strerror(errno));
207 }
208 if (bind(s, (struct sockaddr *)&sin0, sizeof(sin0)) < 0) {
209 tst_brkm(TBROK, cleanup, "socket bind failed for setsockopt:"
210 " %s", strerror(errno));
211 }
212 }
213
cleanup1(void)214 void cleanup1(void)
215 {
216 (void)close(s);
217 s = -1;
218 }
219