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: getsockopt01
22 *
23 * Test Description:
24 * Verify that getsockopt() returns the proper errno for various failure cases
25 *
26 * Usage: <for command-line>
27 * getsockopt01 [-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 Ported by Wayne Boyer
37 *
38 * RESTRICTIONS:
39 * None.
40 */
41
42 #include <stdio.h>
43 #include <unistd.h>
44 #include <errno.h>
45 #include <fcntl.h>
46
47 #include <sys/types.h>
48 #include <sys/socket.h>
49 #include <sys/signal.h>
50 #include <sys/ioctl.h>
51
52 #include <netinet/in.h>
53
54 #include "test.h"
55 #include "safe_macros.h"
56
57 char *TCID = "getsockopt01";
58 int testno;
59
60 int s; /* socket descriptor */
61 struct sockaddr_in sin0, fsin1;
62 int sinlen;
63 int optval;
64 socklen_t optlen;
65
66 void setup(void), setup0(void), setup1(void),
67 cleanup(void), cleanup0(void), cleanup1(void);
68
69 struct test_case_t { /* test case structure */
70 int domain; /* PF_INET, PF_UNIX, ... */
71 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
72 int proto; /* protocol number (usually 0 = default) */
73 int level; /* IPPROTO_* */
74 int optname;
75 void *optval;
76 socklen_t *optlen;
77 struct sockaddr *sin;
78 int salen;
79 int retval; /* syscall return value */
80 int experrno; /* expected errno */
81 void (*setup) (void);
82 void (*cleanup) (void);
83 char *desc;
84 } tdat[] = {
85 {
86 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval,
87 &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1),
88 -1, EBADF, setup0, cleanup0, "bad file descriptor"}
89 , {
90 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval,
91 &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1),
92 -1, ENOTSOCK, setup0, cleanup0, "bad file descriptor"}
93 ,
94 #ifndef UCLINUX
95 {
96 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, 0, &optlen,
97 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
98 EFAULT, setup1, cleanup1, "invalid option buffer"}
99 , {
100 PF_INET, SOCK_STREAM, 0, SOL_SOCKET, SO_OOBINLINE, &optval, 0,
101 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
102 EFAULT, setup1, cleanup1, "invalid optlen"}
103 ,
104 #endif /* UCLINUX */
105 {
106 PF_INET, SOCK_STREAM, 0, 500, SO_OOBINLINE, &optval, &optlen,
107 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
108 EOPNOTSUPP, setup1, cleanup1, "invalid level"}
109 , {
110 PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval,
111 &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1),
112 -1, EOPNOTSUPP, setup1, cleanup1, "invalid option name"}
113 , {
114 PF_INET, SOCK_STREAM, 0, IPPROTO_UDP, SO_OOBINLINE, &optval,
115 &optlen, (struct sockaddr *)&fsin1, sizeof(fsin1),
116 -1, EOPNOTSUPP, setup1, cleanup1,
117 "invalid option name (UDP)"}
118 , {
119 PF_INET, SOCK_STREAM, 0, IPPROTO_IP, -1, &optval, &optlen,
120 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
121 ENOPROTOOPT, setup1, cleanup1, "invalid option name (IP)"}
122 , {
123 PF_INET, SOCK_STREAM, 0, IPPROTO_TCP, -1, &optval, &optlen,
124 (struct sockaddr *)&fsin1, sizeof(fsin1), -1,
125 ENOPROTOOPT, setup1, cleanup1, "invalid option name (TCP)"}
126 ,};
127
128 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
129
main(int argc,char * argv[])130 int main(int argc, char *argv[])
131 {
132 int lc;
133
134 tst_parse_opts(argc, argv, NULL, NULL);
135
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(getsockopt(s, tdat[testno].level,
144 tdat[testno].optname,
145 tdat[testno].optval,
146 tdat[testno].optlen));
147 if (TEST_RETURN != tdat[testno].retval ||
148 (TEST_RETURN < 0 &&
149 TEST_ERRNO != tdat[testno].experrno)) {
150 tst_resm(TFAIL, "%s ; returned"
151 " %ld (expected %d), errno %d (expected"
152 " %d)", tdat[testno].desc,
153 TEST_RETURN, tdat[testno].retval,
154 TEST_ERRNO, tdat[testno].experrno);
155 } else {
156 tst_resm(TPASS, "%s successful",
157 tdat[testno].desc);
158 }
159 tdat[testno].cleanup();
160 }
161 }
162 cleanup();
163
164 tst_exit();
165 }
166
setup(void)167 void setup(void)
168 {
169 TEST_PAUSE;
170
171 /* initialize local sockaddr */
172 sin0.sin_family = AF_INET;
173 sin0.sin_port = 0;
174 sin0.sin_addr.s_addr = INADDR_ANY;
175 }
176
cleanup(void)177 void cleanup(void)
178 {
179 }
180
setup0(void)181 void setup0(void)
182 {
183 if (tdat[testno].experrno == EBADF)
184 s = 400; /* anything not an open file */
185 else if ((s = open("/dev/null", O_WRONLY)) == -1)
186 tst_brkm(TBROK, cleanup, "error opening /dev/null - "
187 "errno: %s", strerror(errno));
188 }
189
cleanup0(void)190 void cleanup0(void)
191 {
192 s = -1;
193 }
194
setup1(void)195 void setup1(void)
196 {
197 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
198 tdat[testno].proto);
199 SAFE_BIND(cleanup, s, (struct sockaddr *)&sin0, sizeof(sin0));
200 sinlen = sizeof(fsin1);
201 optlen = sizeof(optval);
202 }
203
cleanup1(void)204 void cleanup1(void)
205 {
206 (void)close(s);
207 s = -1;
208 }
209