1 /*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 * Copyright (c) Cyril Hrubis <[email protected]> 2012
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
14 * the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /*
22 * Test Name: sendto01
23 *
24 * Test Description:
25 * Verify that sendto() returns the proper errno for various failure cases
26 *
27 * HISTORY
28 * 07/2001 Ported by Wayne Boyer
29 */
30
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <errno.h>
34 #include <fcntl.h>
35
36 #include <sys/types.h>
37 #include <sys/socket.h>
38 #include <sys/signal.h>
39 #include <sys/un.h>
40
41 #include <netinet/in.h>
42
43 #include "test.h"
44 #include "safe_macros.h"
45
46 char *TCID = "sendto01";
47 int testno;
48
49 static char buf[1024], bigbuf[128 * 1024];
50 static int s;
51 static struct sockaddr_in sin1, sin2;
52 static int sfd;
53
54 struct test_case_t { /* test case structure */
55 int domain; /* PF_INET, PF_UNIX, ... */
56 int type; /* SOCK_STREAM, SOCK_DGRAM ... */
57 int proto; /* protocol number (usually 0 = default) */
58 void *buf; /* send data buffer */
59 int buflen; /* send's 3rd argument */
60 unsigned flags; /* send's 4th argument */
61 struct sockaddr_in *to; /* destination */
62 int tolen; /* length of "to" buffer */
63 int retval;
64 int experrno;
65 void (*setup) (void);
66 void (*cleanup) (void);
67 char *desc;
68 };
69
70 static void setup(void);
71 static void setup0(void);
72 static void setup1(void);
73 static void setup2(void);
74 static void setup3(void);
75 static void cleanup(void);
76 static void cleanup0(void);
77 static void cleanup1(void);
78 static void do_child(void);
79
80 struct test_case_t tdat[] = {
81 {.domain = PF_INET,
82 .type = SOCK_STREAM,
83 .proto = 0,
84 .buf = buf,
85 .buflen = sizeof(buf),
86 .flags = 0,
87 .to = &sin1,
88 .tolen = sizeof(sin1),
89 .retval = -1,
90 .experrno = EBADF,
91 .setup = setup0,
92 .cleanup = cleanup0,
93 .desc = "bad file descriptor"}
94 ,
95 {.domain = 0,
96 .type = 0,
97 .proto = 0,
98 .buf = buf,
99 .buflen = sizeof(buf),
100 .flags = 0,
101 .to = &sin1,
102 .tolen = sizeof(sin1),
103 .retval = -1,
104 .experrno = ENOTSOCK,
105 .setup = setup0,
106 .cleanup = cleanup0,
107 .desc = "invalid socket"}
108 ,
109 {.domain = PF_INET,
110 .type = SOCK_DGRAM,
111 .proto = 0,
112 .buf = (void *)-1,
113 .buflen = sizeof(buf),
114 .flags = 0,
115 .to = &sin1,
116 .tolen = sizeof(sin1),
117 .retval = -1,
118 .experrno = EFAULT,
119 .setup = setup1,
120 .cleanup = cleanup1,
121 .desc = "invalid send buffer"}
122 ,
123 {.domain = PF_INET,
124 .type = SOCK_STREAM,
125 .proto = 0,
126 .buf = buf,
127 .buflen = sizeof(buf),
128 .flags = 0,
129 .to = &sin2,
130 .tolen = sizeof(sin2),
131 .retval = 0,
132 .experrno = EFAULT,
133 .setup = setup1,
134 .cleanup = cleanup1,
135 .desc = "connected TCP"}
136 ,
137 {.domain = PF_INET,
138 .type = SOCK_STREAM,
139 .proto = 0,
140 .buf = buf,
141 .buflen = sizeof(buf),
142 .flags = 0,
143 .to = &sin1,
144 .tolen = sizeof(sin1),
145 .retval = -1,
146 .experrno = EPIPE,
147 .setup = setup3,
148 .cleanup = cleanup1,
149 .desc = "not connected TCP"}
150 ,
151 {.domain = PF_INET,
152 .type = SOCK_DGRAM,
153 .proto = 0,
154 .buf = buf,
155 .buflen = sizeof(buf),
156 .flags = 0,
157 .to = &sin1,
158 .tolen = -1,
159 .retval = -1,
160 .experrno = EINVAL,
161 .setup = setup1,
162 .cleanup = cleanup1,
163 .desc = "invalid to buffer length"}
164 ,
165 {.domain = PF_INET,
166 .type = SOCK_DGRAM,
167 .proto = 0,
168 .buf = buf,
169 .buflen = sizeof(buf),
170 .flags = 0,
171 .to = (struct sockaddr_in *)-1,
172 .tolen = sizeof(sin1),
173 .retval = -1,
174 .experrno = EFAULT,
175 .setup = setup1,
176 .cleanup = cleanup1,
177 .desc = "invalid to buffer"}
178 ,
179 {.domain = PF_INET,
180 .type = SOCK_DGRAM,
181 .proto = 0,
182 .buf = bigbuf,
183 .buflen = sizeof(bigbuf),
184 .flags = 0,
185 .to = &sin1,
186 .tolen = sizeof(sin1),
187 .retval = -1,
188 .experrno = EMSGSIZE,
189 .setup = setup1,
190 .cleanup = cleanup1,
191 .desc = "UDP message too big"}
192 ,
193 {.domain = PF_INET,
194 .type = SOCK_STREAM,
195 .proto = 0,
196 .buf = buf,
197 .buflen = sizeof(buf),
198 .flags = 0,
199 .to = &sin1,
200 .tolen = sizeof(sin1),
201 .retval = -1,
202 .experrno = EPIPE,
203 .setup = setup2,
204 .cleanup = cleanup1,
205 .desc = "local endpoint shutdown"}
206 ,
207 {.domain = PF_INET,
208 .type = SOCK_DGRAM,
209 .proto = 0,
210 .buf = buf,
211 .buflen = sizeof(buf),
212 .flags = MSG_OOB,
213 .to = &sin1,
214 .tolen = sizeof(sin1),
215 .retval = -1,
216 .experrno = EOPNOTSUPP,
217 .setup = setup1,
218 .cleanup = cleanup1,
219 .desc = "invalid flags set"}
220 };
221
222 int TST_TOTAL = sizeof(tdat) / sizeof(tdat[0]);
223
start_server(struct sockaddr_in * sin0)224 static 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 SAFE_GETSOCKNAME(cleanup, sfd, (struct sockaddr *)sin0, &slen);
247
248 switch ((pid = tst_fork())) {
249 case 0:
250 do_child();
251 break;
252 case -1:
253 tst_brkm(TBROK | TERRNO, cleanup, "server fork failed");
254 default:
255 (void)close(sfd);
256 return pid;
257 }
258
259 exit(1);
260 }
261
do_child(void)262 static void do_child(void)
263 {
264 struct sockaddr_in fsin;
265 fd_set afds, rfds;
266 int nfds, cc, fd;
267
268 FD_ZERO(&afds);
269 FD_SET(sfd, &afds);
270
271 nfds = sfd + 1;
272
273 /* accept connections until killed */
274 while (1) {
275 socklen_t fromlen;
276
277 memcpy(&rfds, &afds, sizeof(rfds));
278
279 if (select(nfds, &rfds, NULL, NULL, NULL) < 0 && errno != EINTR)
280 exit(1);
281
282 if (FD_ISSET(sfd, &rfds)) {
283 int newfd;
284
285 fromlen = sizeof(fsin);
286 newfd = accept(sfd, (struct sockaddr *)&fsin, &fromlen);
287 if (newfd >= 0) {
288 FD_SET(newfd, &afds);
289 nfds = MAX(nfds, newfd + 1);
290 }
291 }
292 for (fd = 0; fd < nfds; ++fd) {
293 if (fd != sfd && FD_ISSET(fd, &rfds)) {
294 cc = read(fd, buf, sizeof(buf));
295 if (cc == 0 || (cc < 0 && errno != EINTR)) {
296 (void)close(fd);
297 FD_CLR(fd, &afds);
298 }
299 }
300 }
301 }
302 }
303
main(int ac,char * av[])304 int main(int ac, char *av[])
305 {
306 int lc;
307
308 tst_parse_opts(ac, av, NULL, NULL);
309
310 setup();
311
312 for (lc = 0; TEST_LOOPING(lc); ++lc) {
313
314 tst_count = 0;
315 for (testno = 0; testno < TST_TOTAL; ++testno) {
316 tdat[testno].setup();
317
318 TEST(sendto(s, tdat[testno].buf, tdat[testno].buflen,
319 tdat[testno].flags,
320 (const struct sockaddr *)tdat[testno].to,
321 tdat[testno].tolen));
322
323 if (TEST_RETURN > 0)
324 TEST_RETURN = 0;
325
326 if (TEST_RETURN != tdat[testno].retval ||
327 (TEST_RETURN < 0 &&
328 TEST_ERRNO != tdat[testno].experrno)) {
329 tst_resm(TFAIL, "%s ; returned"
330 " %ld (expected %d), errno %d (expected"
331 " %d)", tdat[testno].desc,
332 TEST_RETURN, tdat[testno].retval,
333 TEST_ERRNO, tdat[testno].experrno);
334 } else {
335 tst_resm(TPASS, "%s successful",
336 tdat[testno].desc);
337 }
338 tdat[testno].cleanup();
339 }
340 }
341 cleanup();
342
343 tst_exit();
344 }
345
346 static pid_t server_pid;
347
setup(void)348 static void setup(void)
349 {
350 TEST_PAUSE;
351
352 server_pid = start_server(&sin1);
353
354 signal(SIGPIPE, SIG_IGN);
355 }
356
cleanup(void)357 static void cleanup(void)
358 {
359 kill(server_pid, SIGKILL);
360 }
361
setup0(void)362 static void setup0(void)
363 {
364 if (tdat[testno].experrno == EBADF)
365 s = 400;
366 else if ((s = open("/dev/null", O_WRONLY)) == -1)
367 tst_brkm(TBROK | TERRNO, cleanup, "open(/dev/null) failed");
368 }
369
cleanup0(void)370 static void cleanup0(void)
371 {
372 s = -1;
373 }
374
setup1(void)375 static void setup1(void)
376 {
377 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
378 tdat[testno].proto);
379 SAFE_CONNECT(cleanup, s, (const struct sockaddr *)&sin1, sizeof(sin1));
380 }
381
cleanup1(void)382 static void cleanup1(void)
383 {
384 (void)close(s);
385 s = -1;
386 }
387
setup2(void)388 static void setup2(void)
389 {
390 setup1();
391 if (shutdown(s, 1) < 0)
392 tst_brkm(TBROK | TERRNO, cleanup, "socket setup failed connect "
393 "test %d", testno);
394 }
395
setup3(void)396 static void setup3(void)
397 {
398 s = SAFE_SOCKET(cleanup, tdat[testno].domain, tdat[testno].type,
399 tdat[testno].proto);
400 }
401