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: connect01
22 *
23 * Test Description:
24 * Verify that connect() returns the proper errno for various failure cases
25 *
26 * Usage: <for command-line>
27 * connect01 [-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
43 #include <stdio.h>
44 #include <unistd.h>
45 #include <errno.h>
46 #include <fcntl.h>
47
48 #include <sys/types.h>
49 #include <sys/socket.h>
50 #include <sys/signal.h>
51 #include <sys/un.h>
52
53 #include <netinet/in.h>
54
55 #include "test.h"
56
57 char *TCID = "connect01";
58 int testno;
59
60 int s, s2; /* socket descriptor */
61 struct sockaddr_in sin1, sin2, sin3, sin4;
62 static int sfd; /* shared between start_server and do_child */
63
64 void setup(void), setup0(void), setup1(void), setup2(void),
65 cleanup(void), cleanup0(void), cleanup1(void), do_child(void);
66
67 static pid_t start_server(struct sockaddr_in *);
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 struct sockaddr *sockaddr; /* socket address buffer */
74 int salen; /* connect's 3rd argument */
75 int retval; /* syscall return value */
76 int experrno; /* expected errno */
77 void (*setup) (void);
78 void (*cleanup) (void);
79 char *desc;
80 } tdat[] = {
81 {
82 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1,
83 sizeof(struct sockaddr_in), -1, EBADF, setup0,
84 cleanup0, "bad file descriptor"},
85 #ifndef UCLINUX
86 /* Skip since uClinux does not implement memory protection */
87 {
88 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)-1,
89 sizeof(struct sockaddr_in), -1, EFAULT, setup1,
90 cleanup1, "invalid socket buffer"},
91 #endif
92 {
93 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1,
94 3, -1, EINVAL, setup1, cleanup1, "invalid salen"}, {
95 0, 0, 0, (struct sockaddr *)&sin1,
96 sizeof(sin1), -1, ENOTSOCK, setup0, cleanup0,
97 "invalid socket"}
98 , {
99 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin1,
100 sizeof(sin1), -1, EISCONN, setup2, cleanup1,
101 "already connected"}
102 , {
103 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin2,
104 sizeof(sin2), -1, ECONNREFUSED, setup1, cleanup1,
105 "connection refused"}
106 , {
107 PF_INET, SOCK_STREAM, 0, (struct sockaddr *)&sin4,
108 sizeof(sin4), -1, EAFNOSUPPORT, setup1, cleanup1,
109 "invalid address family"}
110 ,};
111
112 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
113
114 #ifdef UCLINUX
115 static char *argv0;
116 #endif
117
main(int argc,char * argv[])118 int main(int argc, char *argv[])
119 {
120 int lc;
121
122 tst_parse_opts(argc, argv, NULL, NULL);
123 #ifdef UCLINUX
124 argv0 = argv[0];
125 maybe_run_child(&do_child, "d", &sfd);
126 #endif
127
128 setup();
129
130 for (lc = 0; TEST_LOOPING(lc); ++lc) {
131 tst_count = 0;
132 for (testno = 0; testno < TST_TOTAL; ++testno) {
133 tdat[testno].setup();
134
135 TEST(connect
136 (s, tdat[testno].sockaddr, tdat[testno].salen));
137
138 if (TEST_RETURN != tdat[testno].retval ||
139 (TEST_RETURN < 0 &&
140 TEST_ERRNO != tdat[testno].experrno)) {
141 tst_resm(TFAIL, "%s ; returned"
142 " %ld (expected %d), errno %d (expected"
143 " %d)", tdat[testno].desc,
144 TEST_RETURN, tdat[testno].retval,
145 TEST_ERRNO, tdat[testno].experrno);
146 } else {
147 tst_resm(TPASS, "%s successful",
148 tdat[testno].desc);
149 }
150 tdat[testno].cleanup();
151 }
152 }
153 cleanup();
154
155 tst_exit();
156 }
157
158 pid_t pid;
159
setup(void)160 void setup(void)
161 {
162 TEST_PAUSE; /* if -p option specified */
163
164 pid = start_server(&sin1);
165
166 sin2.sin_family = AF_INET;
167 /* this port must be unused! */
168 sin2.sin_port = tst_get_unused_port(NULL, AF_INET, SOCK_STREAM);
169 sin2.sin_addr.s_addr = INADDR_ANY;
170
171 sin3.sin_family = AF_INET;
172 sin3.sin_port = 0;
173 /* assumes no route to this network! */
174 sin3.sin_addr.s_addr = htonl(0x0AFFFEFD);
175
176 sin4.sin_family = 47; /* bogus address family */
177 sin4.sin_port = 0;
178 sin4.sin_addr.s_addr = htonl(0x0AFFFEFD);
179
180 }
181
cleanup(void)182 void cleanup(void)
183 {
184 (void)kill(pid, SIGKILL);
185
186 }
187
setup0(void)188 void setup0(void)
189 {
190 if (tdat[testno].experrno == EBADF)
191 s = 400; /* anything not an open file */
192 else if ((s = open("/dev/null", O_WRONLY)) == -1)
193 tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
194
195 }
196
cleanup0(void)197 void cleanup0(void)
198 {
199 close(s);
200 s = -1;
201 }
202
setup1(void)203 void setup1(void)
204 {
205 s = socket(tdat[testno].domain, tdat[testno].type, tdat[testno].proto);
206 if (s < 0)
207 tst_brkm(TBROK | TERRNO, cleanup, "socket() failed");
208 }
209
cleanup1(void)210 void cleanup1(void)
211 {
212 (void)close(s);
213 s = -1;
214 }
215
setup2(void)216 void setup2(void)
217 {
218 setup1(); /* get a socket in s */
219 if (connect(s, (const struct sockaddr *)&sin1, sizeof(sin1)) < 0)
220 tst_brkm(TBROK | TERRNO, cleanup,
221 "socket setup failed connect test %d", testno);
222 }
223
start_server(struct sockaddr_in * sin0)224 pid_t start_server(struct sockaddr_in *sin0)
225 {
226 pid_t pid;
227 socklen_t slen = sizeof(*sin0);
228
229 sin0->sin_family = AF_INET;
230 sin0->sin_port = 0; /* pick random free port */
231 sin0->sin_addr.s_addr = INADDR_ANY;
232
233 sfd = socket(PF_INET, SOCK_STREAM, 0);
234 if (sfd < 0) {
235 tst_brkm(TBROK | TERRNO, cleanup, "server socket failed");
236 return -1;
237 }
238 if (bind(sfd, (struct sockaddr *)sin0, sizeof(*sin0)) < 0) {
239 tst_brkm(TBROK | TERRNO, cleanup, "server bind failed");
240 return -1;
241 }
242 if (listen(sfd, 10) < 0) {
243 tst_brkm(TBROK | TERRNO, cleanup, "server listen failed");
244 return -1;
245 }
246 if (getsockname(sfd, (struct sockaddr *)sin0, &slen) == -1)
247 tst_brkm(TBROK | TERRNO, cleanup, "getsockname failed");
248
249 switch ((pid = FORK_OR_VFORK())) {
250 case 0: /* child */
251 #ifdef UCLINUX
252 self_exec(argv0, "d", sfd);
253 #else
254 do_child();
255 #endif
256 break;
257 case -1:
258 tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
259 /* fall through */
260 default: /* parent */
261 (void)close(sfd);
262 return pid;
263 }
264
265 return -1;
266 }
267
do_child(void)268 void do_child(void)
269 {
270 struct sockaddr_in fsin;
271 fd_set afds, rfds;
272 int nfds, cc, fd;
273 char c;
274
275 FD_ZERO(&afds);
276 FD_SET(sfd, &afds);
277
278 nfds = sfd + 1;
279
280 /* accept connections until killed */
281 while (1) {
282 socklen_t fromlen;
283
284 memcpy(&rfds, &afds, sizeof(rfds));
285
286 if (select(nfds, &rfds, NULL, NULL,
287 NULL) < 0)
288 if (errno != EINTR)
289 exit(1);
290 if (FD_ISSET(sfd, &rfds)) {
291 int newfd;
292
293 fromlen = sizeof(fsin);
294 newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
295 if (newfd >= 0) {
296 FD_SET(newfd, &afds);
297 nfds = MAX(nfds, newfd + 1);
298 }
299 }
300 for (fd = 0; fd < nfds; ++fd)
301 if (fd != sfd && FD_ISSET(fd, &rfds)) {
302 if ((cc = read(fd, &c, 1)) == 0) {
303 (void)close(fd);
304 FD_CLR(fd, &afds);
305 }
306 }
307 }
308 }
309