xref: /minix/minix/tests/common-socket.c (revision 0a6a1f1d)
1 #include <assert.h>
2 #include <ctype.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <signal.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/socket.h>
10 #include <sys/stat.h>
11 #include <sys/wait.h>
12 #include <time.h>
13 #include <unistd.h>
14 
15 #include "common.h"
16 #include "common-socket.h"
17 
18 #define ISO8601_FORMAT "%Y-%m-%dT%H:%M:%S"
19 
20 /* timestamps for debug and error logs */
21 static char *get_timestamp(void)
22 {
23 	struct tm *tm;
24 	time_t t;
25 	size_t len;
26 	char *s;
27 
28 	len = sizeof(char) * 32;
29 
30 	t = time(NULL);
31 	if (t == -1) {
32 		return NULL;
33 	}
34 	tm = gmtime(&t);
35 	if (tm == NULL) {
36 		return NULL;
37 	}
38 
39 	s = (char *) malloc(len);
40 	if (!s) {
41 		perror("malloc");
42 		return NULL;
43 	}
44 	memset(s, '\0', len);
45 
46 	strftime(s, len - 1, ISO8601_FORMAT, tm);
47 	return s;
48 }
49 
50 void test_fail_fl(char *msg, char *file, int line)
51 {
52 	char *timestamp;
53 	timestamp = get_timestamp();
54 	if (errct == 0) fprintf(stderr, "\n");
55 	fprintf(stderr, "[ERROR][%s] (%s Line %d) %s [pid=%d:errno=%d:%s]\n",
56 			timestamp, file, line, msg, getpid(),
57 					errno, strerror(errno));
58 	fflush(stderr);
59 	if (timestamp != NULL) {
60 		free(timestamp);
61 		timestamp = NULL;
62 	}
63 	e(7);
64 }
65 
66 #if DEBUG == 1
67 void debug_fl(char *msg, char *file, int line)
68 {
69 	char *timestamp;
70 	timestamp = get_timestamp();
71 	fprintf(stdout,"[DEBUG][%s] (%s:%d) %s [pid=%d]\n",
72 		timestamp, __FILE__, __LINE__, msg, getpid());
73 	fflush(stdout);
74 	if (timestamp != NULL) {
75 		free(timestamp);
76 		timestamp = NULL;
77 	}
78 }
79 #endif
80 
81 void test_socket(const struct socket_test_info *info)
82 {
83 	struct stat statbuf, statbuf2;
84 	int sd, sd2;
85 	int rc;
86 	int i;
87 
88 	debug("entering test_socket()");
89 
90 	debug("Test socket() with an unsupported address family");
91 
92 	errno = 0;
93 	sd = socket(-1, info->type, 0);
94 	if (!(sd == -1 && errno == EAFNOSUPPORT)) {
95 		test_fail("socket");
96 		if (sd != -1) {
97 			CLOSE(sd);
98 		}
99 	}
100 
101 	debug("Test socket() with all available FDs open by this process");
102 
103 	for (i = 3; i < getdtablesize(); i++) {
104 		rc = open("/dev/null", O_RDONLY);
105 		if (rc == -1) {
106 			test_fail("we couldn't open /dev/null for read");
107 		}
108 	}
109 
110 	errno = 0;
111 	sd = socket(info->domain, info->type, 0);
112 	if (!(sd == -1 && errno == EMFILE)) {
113 		test_fail("socket() call with all fds open should fail");
114 		if (sd != -1) {
115 			CLOSE(sd);
116 		}
117 	}
118 
119 	for (i = 3; i < getdtablesize(); i++) {
120 		CLOSE(i);
121 	}
122 
123 	debug("Test socket() with an mismatched protocol");
124 
125 	errno = 0;
126 	sd = socket(info->domain, info->type, 4);
127 	if (!(sd == -1 && errno == EPROTONOSUPPORT)) {
128 		test_fail("socket() should fail with errno = EPROTONOSUPPORT");
129 		if (sd != -1) {
130 			CLOSE(sd);
131 		}
132 	}
133 
134 	debug("Test socket() success");
135 
136 	/*
137 	 * open 2 sockets at once and *then* close them.
138 	 * This will test that /dev/uds is cloning properly.
139 	 */
140 
141 	SOCKET(sd, info->domain, info->type, 0);
142 	SOCKET(sd2, info->domain, info->type, 0);
143 
144 	rc = fstat(sd, &statbuf);
145 	if (rc == -1) {
146 		test_fail("fstat failed on sd");
147 	}
148 
149 	rc = fstat(sd2, &statbuf2);
150 	if (rc == -1) {
151 		test_fail("fstat failed on sd2");
152 	}
153 
154 
155 	if (statbuf.st_dev == statbuf2.st_dev) {
156 		test_fail("/dev/uds isn't being cloned");
157 	}
158 
159 	CLOSE(sd2);
160 	CLOSE(sd);
161 
162 	debug("leaving test_socket()");
163 }
164 
165 void test_getsockname(const struct socket_test_info *info)
166 {
167 	int sd;
168 	int rc;
169 	struct sockaddr_storage sock_addr;
170 	socklen_t sock_addr_len;
171 
172 	SOCKET(sd, info->domain, info->type, 0);
173 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
174 	if (rc == -1) {
175 		test_fail("bind() should have worked");
176 	}
177 
178 	debug("Test getsockname() success");
179 
180 	memset(&sock_addr, '\0', sizeof(sock_addr));
181 	sock_addr_len = sizeof(sock_addr);
182 
183 	rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
184 	if (rc == -1) {
185 		test_fail("getsockname() should have worked");
186 	}
187 
188 	info->callback_check_sockaddr((struct sockaddr *) &sock_addr,
189 		sock_addr_len, "getsockname", 1);
190 
191 	CLOSE(sd);
192 }
193 
194 void test_bind(const struct socket_test_info *info)
195 {
196 	struct sockaddr_storage sock_addr;
197 	socklen_t sock_addr_len;
198 	int sd;
199 	int sd2;
200 	int rc;
201 
202 	debug("entering test_bind()");
203 	info->callback_cleanup();
204 
205 	debug("Test bind() success");
206 
207 	SOCKET(sd, info->domain, info->type, 0);
208 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
209 	if (rc == -1) {
210 		test_fail("bind() should have worked");
211 	}
212 
213 	debug("Test getsockname() success");
214 
215 	memset(&sock_addr, '\0', sizeof(sock_addr));
216 	sock_addr_len = sizeof(sock_addr);
217 
218 	rc = getsockname(sd, (struct sockaddr *) &sock_addr, &sock_addr_len);
219 	if (rc == -1) {
220 		test_fail("getsockname() should have worked");
221 	}
222 
223 	info->callback_check_sockaddr((struct sockaddr *) &sock_addr,
224 		sock_addr_len, "getsockname", 1);
225 
226 	debug("Test bind() with a address that has already been bind()'d");
227 
228 	SOCKET(sd2, info->domain, info->type, 0);
229 	errno = 0;
230 	rc = bind(sd2, info->serveraddr, info->serveraddrlen);
231 	if (!((rc == -1) && (errno == EADDRINUSE)) &&
232 		!info->bug_bind_in_use) {
233 		test_fail("bind() should have failed with EADDRINUSE");
234 	}
235 	CLOSE(sd2);
236 	CLOSE(sd);
237 	info->callback_cleanup();
238 
239 	if (!info->bug_bind_null) {
240 		debug("Test bind() with a NULL address");
241 
242 		SOCKET(sd, info->domain, info->type, 0);
243 		errno = 0;
244 		rc = bind(sd, (struct sockaddr *) NULL,
245 			sizeof(struct sockaddr_storage));
246 		if (!((rc == -1) && (errno == EFAULT))) {
247 			test_fail("bind() should have failed with EFAULT");
248 		}
249 		CLOSE(sd);
250 	}
251 
252 	debug("leaving test_bind()");
253 }
254 
255 void test_listen(const struct socket_test_info *info)
256 {
257 	int rc;
258 
259 	debug("entering test_listen()");
260 
261 	debug("Test listen() with a bad file descriptor");
262 
263 	errno = 0;
264 	rc = listen(-1, 0);
265 	if (!(rc == -1 && errno == EBADF)) {
266 		test_fail("listen(-1, 0) should have failed");
267 	}
268 
269 	debug("Test listen() with a non-socket file descriptor");
270 
271 	errno = 0;
272 	rc = listen(0, 0);
273 	/* Test on errno disabled here: there's currently no telling what this
274 	 * will return. POSIX says it should be ENOTSOCK, MINIX3 libc returns
275 	 * ENOSYS, and we used to test for ENOTTY here..
276 	 */
277 	if (!(rc == -1)) {
278 		test_fail("listen(0, 0) should have failed");
279 	}
280 
281 	debug("leaving test_listen()");
282 }
283 
284 void test_shutdown(const struct socket_test_info *info)
285 {
286 	int how[3] = { SHUT_RD, SHUT_WR, SHUT_RDWR };
287 	int sd;
288 	int rc;
289 	int i;
290 
291 	debug("entering test_shutdown()");
292 
293 	/* test for each direction (read, write, read-write) */
294 	for (i = 0; i < 3; i++) {
295 
296 		if (info->bug_shutdown_read && how[i] == SHUT_RD) continue;
297 
298 		debug("test shutdown() with an invalid descriptor");
299 
300 		errno = 0;
301 		rc = shutdown(-1, how[i]);
302 		if (!(rc == -1 && errno == EBADF) && !info->bug_shutdown) {
303 			test_fail("shutdown(-1, how[i]) should have failed");
304 		}
305 
306 		debug("test shutdown() with a non-socket descriptor");
307 
308 		errno = 0;
309 		rc = shutdown(0, how[i]);
310 		if (!(rc == -1 && errno == ENOSYS) && !info->bug_shutdown) {
311 			test_fail("shutdown() should have failed with ENOSYS");
312 		}
313 
314 		debug("test shutdown() with a socket that is not connected");
315 
316 		SOCKET(sd, info->domain, info->type, 0);
317 		errno = 0;
318 		rc = shutdown(sd, how[i]);
319 		if (!(rc == -1 && errno == ENOTCONN) &&
320 			!info->bug_shutdown_not_conn &&
321 			!info->bug_shutdown) {
322 			test_fail("shutdown() should have failed");
323 		}
324 		CLOSE(sd);
325 	}
326 
327 	SOCKET(sd, info->domain, info->type, 0);
328 	errno = 0;
329 	rc = shutdown(sd, -1);
330 	if (!(rc == -1 && errno == ENOTCONN) &&
331 		!info->bug_shutdown_not_conn &&
332 		!info->bug_shutdown) {
333 		test_fail("shutdown(sd, -1) should have failed with ENOTCONN");
334 	}
335 	CLOSE(sd);
336 
337 	debug("leaving test_shutdown()");
338 }
339 
340 void test_close(const struct socket_test_info *info)
341 {
342 	int sd, sd2;
343 	int rc, i;
344 
345 	debug("entering test_close()");
346 
347 	info->callback_cleanup();
348 
349 	debug("Test close() success");
350 
351 	SOCKET(sd, info->domain, info->type, 0);
352 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
353 	if (rc != 0) {
354 		test_fail("bind() should have worked");
355 	}
356 
357 	CLOSE(sd);
358 
359 	debug("Close an already closed file descriptor");
360 
361 	errno = 0;
362 	rc = close(sd);
363 	if (!(rc == -1 && errno == EBADF)) {
364 		test_fail("close(sd) should have failed with EBADF");
365 	}
366 
367 	info->callback_cleanup();
368 
369 	debug("dup()'ing a file descriptor and closing both should work");
370 
371 	SOCKET(sd, info->domain, info->type, 0);
372 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
373 	if (rc != 0) {
374 		test_fail("bind() should have worked");
375 	}
376 
377 	errno = 0;
378 	sd2 = dup(sd);
379 	if (sd2 == -1) {
380 		test_fail("dup(sd) should have worked");
381 	} else {
382 		CLOSE(sd2);
383 		CLOSE(sd);
384 	}
385 
386 	info->callback_cleanup();
387 
388 	/* Create and close a socket a bunch of times.
389 	 * If the implementation doesn't properly free the
390 	 * socket during close(), eventually socket() will
391 	 * fail when the internal descriptor table is full.
392 	 */
393 	for (i = 0; i < 1024; i++) {
394 		SOCKET(sd, info->domain, info->type, 0);
395 		CLOSE(sd);
396 	}
397 
398 	debug("leaving test_close()");
399 }
400 
401 void test_sockopts(const struct socket_test_info *info)
402 {
403 	int i;
404 	int rc;
405 	int sd;
406 	int option_value;
407 	socklen_t option_len;
408 
409 	debug("entering test_sockopts()");
410 
411 	for (i = 0; i < info->typecount; i++) {
412 
413 		SOCKET(sd, info->domain, info->types[i], 0);
414 
415 		debug("Test setsockopt() works");
416 
417 		option_value = 0;
418 		option_len = sizeof(option_value);
419 		errno = 0;
420 		rc = getsockopt(sd, SOL_SOCKET, SO_TYPE, &option_value,
421 							&option_len);
422 		if (rc != 0) {
423 			test_fail("setsockopt() should have worked");
424 		}
425 
426 		if (option_value != info->types[i]) {
427 			test_fail("SO_TYPE didn't seem to work.");
428 		}
429 
430 		CLOSE(sd);
431 	}
432 
433 
434 
435 	SOCKET(sd, info->domain, info->type, 0);
436 
437 	debug("Test setsockopt() works");
438 
439 	option_value = 0;
440 	option_len = sizeof(option_value);
441 	errno = 0;
442 	rc = getsockopt(sd, SOL_SOCKET, SO_SNDBUF, &option_value, &option_len);
443 	if (rc != 0 && !info->bug_sockopt_sndbuf) {
444 		test_fail("getsockopt() should have worked");
445 	}
446 
447 	if (info->expected_sndbuf >= 0 &&
448 		option_value != info->expected_sndbuf &&
449 		!info->bug_sockopt_sndbuf) {
450 		test_fail("SO_SNDBUF didn't seem to work.");
451 	}
452 
453 	CLOSE(sd);
454 
455 
456 	SOCKET(sd, info->domain, info->type, 0);
457 
458 	debug("Test setsockopt() works");
459 
460 	option_value = 0;
461 	option_len = sizeof(option_value);
462 	errno = 0;
463 	rc = getsockopt(sd, SOL_SOCKET, SO_RCVBUF, &option_value, &option_len);
464 	if (rc != 0 && !info->bug_sockopt_rcvbuf) {
465 		test_fail("getsockopt() should have worked");
466 	}
467 
468 	if (info->expected_rcvbuf >= 0 &&
469 		option_value != info->expected_rcvbuf &&
470 		!info->bug_sockopt_rcvbuf) {
471 		test_fail("SO_RCVBUF didn't seem to work.");
472 	}
473 
474 	CLOSE(sd);
475 
476 
477 	debug("leaving test_sockopts()");
478 }
479 
480 void test_read(const struct socket_test_info *info)
481 {
482 	int rc;
483 	int fd;
484 	char buf[BUFSIZE];
485 
486 	debug("entering test_read()");
487 
488 	errno = 0;
489 	rc = read(-1, buf, sizeof(buf));
490 	if (!(rc == -1 && errno == EBADF)) {
491 		test_fail("read() should have failed with EBADF");
492 	}
493 
494 	fd = open("/tmp", O_RDONLY);
495 	if (fd == -1) {
496 		test_fail("open(\"/tmp\", O_RDONLY) should have worked");
497 	}
498 
499 	CLOSE(fd);
500 
501 	debug("leaving test_read()");
502 }
503 
504 void test_write(const struct socket_test_info *info)
505 {
506 	int rc;
507 	char buf[BUFSIZE];
508 
509 	debug("entering test_write()");
510 
511 	errno = 0;
512 	rc = write(-1, buf, sizeof(buf));
513 	if (!(rc == -1 && errno == EBADF)) {
514 		test_fail("write() should have failed with EBADF");
515 	}
516 
517 	debug("leaving test_write()");
518 }
519 
520 void test_dup(const struct socket_test_info *info)
521 {
522 	struct stat info1;
523 	struct stat info2;
524 	int sd, sd2;
525 	int rc;
526 	int i;
527 
528 	debug("entering test_dup()");
529 
530 	info->callback_cleanup();
531 
532 	debug("Test dup()");
533 
534 	SOCKET(sd, info->domain, info->type, 0);
535 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
536 	if (rc != 0) {
537 		test_fail("bind() should have worked");
538 	}
539 
540 	errno = 0;
541 	sd2 = dup(sd);
542 	if (sd2 == -1) {
543 		test_fail("dup(sd) should have worked");
544 	}
545 
546 	rc = fstat(sd, &info1);
547 	if (rc == -1) {
548 		test_fail("fstat(fd, &info1) failed");
549 	}
550 
551 	rc = fstat(sd2, &info2);
552 	if (rc == -1) {
553 		test_fail("fstat(sd, &info2) failed");
554 	}
555 
556 	if (info1.st_ino != info2.st_ino) {
557 		test_fail("dup() failed info1.st_ino != info2.st_ino");
558 	}
559 
560 	CLOSE(sd);
561 	CLOSE(sd2);
562 
563 	debug("Test dup() with a closed socket");
564 
565 	errno = 0;
566 	rc = dup(sd);
567 	if (!(rc == -1 && errno == EBADF)) {
568 		test_fail("dup(sd) on a closed socket shouldn't have worked");
569 	}
570 
571 	debug("Test dup() with socket descriptor of -1");
572 
573 	errno = 0;
574 	rc = dup(-1);
575 	if (!(rc == -1 && errno == EBADF)) {
576 		test_fail("dup(-1) shouldn't have worked");
577 	}
578 
579 	debug("Test dup() when all of the file descriptors are taken");
580 
581 	SOCKET(sd, info->domain, info->type, 0);
582 
583 	for (i = 4; i < getdtablesize(); i++) {
584 		rc = open("/dev/null", O_RDONLY);
585 		if (rc == -1) {
586 			test_fail("we couldn't open /dev/null for read");
587 		}
588 	}
589 
590 	errno = 0;
591 	sd2 = dup(sd);
592 	if (!(sd2 == -1 && errno == EMFILE)) {
593 		test_fail("dup(sd) should have failed with errno = EMFILE");
594 	}
595 
596 	for (i = 3; i < getdtablesize(); i++) {
597 		CLOSE(i);
598 	}
599 
600 	info->callback_cleanup();
601 
602 	debug("leaving test_dup()");
603 }
604 
605 void test_dup2(const struct socket_test_info *info)
606 {
607 	struct stat info1;
608 	struct stat info2;
609 	int sd;
610 	int fd;
611 	int rc;
612 
613 	debug("entering test_dup2()");
614 	info->callback_cleanup();
615 
616 	SOCKET(sd, info->domain, info->type, 0);
617 
618 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
619 	if (rc != 0) {
620 		test_fail("bind() should have worked");
621 	}
622 
623 	fd = open("/dev/null", O_RDONLY);
624 	if (fd == -1) {
625 		test_fail("open(\"/dev/null\", O_RDONLY) failed");
626 	}
627 
628 	fd = dup2(sd, fd);
629 	if (fd == -1) {
630 		test_fail("dup2(sd, fd) failed.");
631 	}
632 
633 	memset(&info1, '\0', sizeof(struct stat));
634 	memset(&info2, '\0', sizeof(struct stat));
635 
636 	rc = fstat(fd, &info1);
637 	if (rc == -1) {
638 		test_fail("fstat(fd, &info1) failed");
639 	}
640 
641 	rc = fstat(sd, &info2);
642 	if (rc == -1) {
643 		test_fail("fstat(sd, &info2) failed");
644 	}
645 
646 	if (!(info1.st_ino == info2.st_ino &&
647 		major(info1.st_dev) == major(info2.st_dev) &&
648 		minor(info1.st_dev) == minor(info2.st_dev))) {
649 
650 		test_fail("dup2() failed");
651 	}
652 
653 	CLOSE(fd);
654 	CLOSE(sd);
655 
656 	info->callback_cleanup();
657 	debug("leaving test_dup2()");
658 
659 }
660 
661 /*
662  * A toupper() server. This toy server converts a string to upper case.
663  */
664 static void test_xfer_server(const struct socket_test_info *info, pid_t pid)
665 {
666 	int i;
667 	struct timeval tv;
668 	fd_set readfds;
669 	int status;
670 	int rc;
671 	int sd;
672 	unsigned char buf[BUFSIZE];
673 	socklen_t client_addr_size;
674 	int client_sd;
675 	struct sockaddr_storage client_addr;
676 
677 	status = 0;
678 	rc = 0;
679 	sd = 0;
680 	client_sd = 0;
681 	client_addr_size = sizeof(struct sockaddr_storage);
682 
683 	memset(&buf, '\0', sizeof(buf));
684 	memset(&client_addr, '\0', sizeof(client_addr));
685 
686 	SOCKET(sd, info->domain, info->type, 0);
687 
688 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
689 	if (rc == -1) {
690 		test_fail("bind() should have worked");
691 	}
692 
693 	rc = listen(sd, 8);
694 	if (rc == -1) {
695 		test_fail("listen(sd, 8) should have worked");
696 	}
697 
698 	/* we're ready for connections, time to tell the client to start
699 	 * the test
700 	 */
701 	kill(pid, SIGUSR1);
702 
703 	tv.tv_sec = 10;
704 	tv.tv_usec = 0;
705 
706 	FD_ZERO(&readfds);
707 	FD_SET(sd, &readfds);
708 
709 	/* use select() in case the client is really broken and never
710 	 * attempts to connect (we don't want to block on accept()
711 	 * forever).
712 	 */
713 	rc = select(sd + 1, &readfds, NULL, NULL, &tv);
714 	if (rc == -1) {
715 		test_fail("[server] select() should not have failed");
716 	}
717 
718 	if (rc != 1) {
719 		test_fail("[server] select() should have returned 1");
720 		printf("[server] select returned %d\n", rc);
721 	}
722 
723 	if (!(FD_ISSET(sd, &readfds))) {
724 		test_fail("[server] client didn't connect within 10 seconds");
725 		kill(pid, SIGKILL);
726 		return;
727 	}
728 
729 	client_sd = accept(sd, (struct sockaddr *) &client_addr,
730 						&client_addr_size);
731 
732 	if (client_sd == -1) {
733 		test_fail("accept() should have worked");
734 		kill(pid, SIGKILL);
735 		return;
736 	} else {
737 		debug("[server] client accept()'d");
738 	}
739 
740 	debug("[server] Reading message");
741 	rc = read(client_sd, buf, sizeof(buf));
742 	if (rc == -1) {
743 		test_fail("read() failed unexpectedly");
744 		kill(pid, SIGKILL);
745 		return;
746 	}
747 	debug("[server] we got the following message:");
748 	debug(buf);
749 
750 	for (i = 0; i < rc && i < 127; i++) {
751 		buf[i] = toupper(buf[i]);
752 	}
753 
754 	debug("[server] Writing message...");
755 	rc = write(client_sd, buf, sizeof(buf));
756 	if (rc == -1) {
757 		test_fail("write(client_sd, buf, sizeof(buf)) failed");
758 		kill(pid, SIGKILL);
759 		return;
760 	}
761 
762 	if (rc < strlen((char *)buf)) {
763 		test_fail("[server] write didn't write all the bytes");
764 	}
765 
766 	memset(&buf, '\0', sizeof(buf));
767 
768 	debug("[server] Recv message");
769 	rc = recv(client_sd, buf, sizeof(buf), 0);
770 	if (rc == -1) {
771 		test_fail("recv() failed unexpectedly");
772 		kill(pid, SIGKILL);
773 		return;
774 	}
775 	debug("[server] we got the following message:");
776 	debug(buf);
777 
778 	for (i = 0; i < rc && i < 127; i++) {
779 		buf[i] = toupper(buf[i]);
780 	}
781 
782 	debug("[server] Sending message...");
783 	rc = send(client_sd, buf, sizeof(buf), 0);
784 	if (rc == -1) {
785 		test_fail("send(client_sd, buf, sizeof(buf), 0) failed");
786 		kill(pid, SIGKILL);
787 		return;
788 	}
789 
790 	if (rc < strlen((char *)buf)) {
791 		test_fail("[server] write didn't write all the bytes");
792 	}
793 
794 	memset(&buf, '\0', sizeof(buf));
795 
796 	debug("[server] Recvfrom message");
797 	rc = recvfrom(client_sd, buf, sizeof(buf), 0, NULL, 0);
798 	if (rc == -1) {
799 		test_fail("recvfrom() failed unexpectedly");
800 		kill(pid, SIGKILL);
801 		return;
802 	}
803 	debug("[server] we got the following message:");
804 	debug(buf);
805 
806 	for (i = 0; i < rc && i < 127; i++) {
807 		buf[i] = toupper(buf[i]);
808 	}
809 
810 	debug("[server] Sendto message...");
811 	rc = sendto(client_sd, buf, sizeof(buf), 0, NULL, 0);
812 	if (rc == -1) {
813 		test_fail("sendto() failed");
814 		kill(pid, SIGKILL);
815 		return;
816 	}
817 
818 	if (rc < strlen((char *)buf)) {
819 		test_fail("[server] write didn't write all the bytes");
820 	}
821 
822 	shutdown(client_sd, SHUT_RDWR);
823 	CLOSE(client_sd);
824 
825 	shutdown(sd, SHUT_RDWR);
826 	CLOSE(sd);
827 
828 	/* wait for client to exit */
829 	do {
830 		errno = 0;
831 		rc = waitpid(pid, &status, 0);
832 	} while (rc == -1 && errno == EINTR);
833 
834 	/* we use the exit status to get its error count */
835 	errct += WEXITSTATUS(status);
836 }
837 
838 int server_ready = 0;
839 
840 /* signal handler for the client */
841 void test_xfer_sighdlr(int sig)
842 {
843 	debug("entering signal handler");
844 	switch (sig) {
845 		/* the server will send SIGUSR1 when it is time for us
846 		 * to start the tests
847 		 */
848 	case SIGUSR1:
849 		server_ready = 1;
850 		debug("got SIGUSR1, the server is ready for the client");
851 		break;
852 	default:
853 		debug("didn't get SIGUSR1");
854 	}
855 	debug("leaving signal handler");
856 }
857 
858 /*
859  * A toupper() client.
860  */
861 static void test_xfer_client(const struct socket_test_info *info)
862 {
863 	struct timeval tv;
864 	fd_set readfds;
865 	struct sockaddr_storage peer_addr;
866 	socklen_t peer_addr_len;
867 	int sd;
868 	int rc;
869 	char buf[BUFSIZE];
870 
871 	debug("[client] entering test_xfer_client()");
872 	errct = 0;	/* reset error count */
873 	memset(&buf, '\0', sizeof(buf));
874 
875 	while (server_ready == 0) {
876 		debug("[client] waiting for the server to signal");
877 		sleep(1);
878 	}
879 
880 	peer_addr_len = sizeof(peer_addr);
881 
882 
883 	if (info->callback_xfer_prepclient) info->callback_xfer_prepclient();
884 
885 	debug("[client] creating client socket");
886 	SOCKET(sd, info->domain, info->type, 0);
887 
888 	debug("[client] connecting to server through the symlink");
889 	rc = connect(sd, info->clientaddrsym, info->clientaddrsymlen);
890 	if (rc == -1) {
891 		test_fail("[client] connect() should have worked");
892 	} else {
893 		debug("[client] connected");
894 	}
895 
896 	debug("[client] testing getpeername()");
897 	memset(&peer_addr, '\0', sizeof(peer_addr));
898 	rc = getpeername(sd, (struct sockaddr *) &peer_addr, &peer_addr_len);
899 	if (rc == -1) {
900 		test_fail("[client] getpeername() should have worked");
901 	}
902 
903 	/* we need to use the full path "/usr/src/test/DIR_56/test.sock"
904 	 * because that is what is returned by getpeername().
905 	 */
906 
907 	info->callback_check_sockaddr((struct sockaddr *) &peer_addr,
908 		peer_addr_len, "getpeername", 1);
909 
910 	strncpy(buf, "Hello, World!", sizeof(buf) - 1);
911 	debug("[client] send to server");
912 	rc = write(sd, buf, sizeof(buf));
913 	if (rc == -1) {
914 		test_fail("[client] write() failed unexpectedly");
915 	}
916 
917 	memset(buf, '\0', sizeof(buf));
918 	debug("[client] read from server");
919 	rc = read(sd, buf, sizeof(buf));
920 	if (rc == -1) {
921 		test_fail("[client] read() failed unexpectedly");
922 	} else {
923 		debug("[client] we got the following message:");
924 		debug(buf);
925 	}
926 
927 	if (strncmp(buf, "HELLO, WORLD!", sizeof(buf)) != 0) {
928 		test_fail("[client] We didn't get the correct response");
929 	}
930 
931 	memset(&buf, '\0', sizeof(buf));
932 	strncpy(buf, "Bonjour!", sizeof(buf) - 1);
933 
934 	debug("[client] send to server");
935 	rc = send(sd, buf, sizeof(buf), 0);
936 	if (rc == -1) {
937 		test_fail("[client] send() failed unexpectedly");
938 	}
939 
940 	if (info->callback_xfer_peercred) info->callback_xfer_peercred(sd);
941 
942 	debug("Testing select()");
943 
944 	tv.tv_sec = 2;
945 	tv.tv_usec = 500000;
946 
947 	FD_ZERO(&readfds);
948 	FD_SET(sd, &readfds);
949 
950 	rc = select(sd + 1, &readfds, NULL, NULL, &tv);
951 	if (rc == -1) {
952 		test_fail("[client] select() should not have failed");
953 	}
954 
955 	if (rc != 1) {
956 		test_fail("[client] select() should have returned 1");
957 	}
958 
959 	if (!(FD_ISSET(sd, &readfds))) {
960 		test_fail("The server didn't respond within 2.5 seconds");
961 	}
962 
963 	memset(buf, '\0', sizeof(buf));
964 	debug("[client] recv from server");
965 	rc = recv(sd, buf, sizeof(buf), 0);
966 	if (rc == -1) {
967 		test_fail("[client] recv() failed unexpectedly");
968 	} else {
969 		debug("[client] we got the following message:");
970 		debug(buf);
971 	}
972 
973 	if (strncmp(buf, "BONJOUR!", sizeof(buf)) != 0) {
974 		test_fail("[client] We didn't get the right response.");
975 	}
976 
977 	memset(&buf, '\0', sizeof(buf));
978 	strncpy(buf, "Hola!", sizeof(buf) - 1);
979 
980 	debug("[client] sendto to server");
981 	rc = sendto(sd, buf, sizeof(buf), 0, NULL, 0);
982 	if (rc == -1) {
983 		test_fail("[client] sendto() failed");
984 	}
985 
986 	debug("Testing select()");
987 
988 	tv.tv_sec = 2;
989 	tv.tv_usec = 500000;
990 
991 	FD_ZERO(&readfds);
992 	FD_SET(sd, &readfds);
993 
994 	rc = select(sd + 1, &readfds, NULL, NULL, &tv);
995 	if (rc == -1) {
996 		test_fail("[client] select() should not have failed");
997 	}
998 
999 	if (rc != 1) {
1000 		test_fail("[client] select() should have returned 1");
1001 	}
1002 
1003 	if (!(FD_ISSET(sd, &readfds))) {
1004 		test_fail("[client] The server didn't respond in 2.5 seconds");
1005 	}
1006 
1007 	memset(buf, '\0', sizeof(buf));
1008 	debug("[client] recvfrom from server");
1009 	rc = recvfrom(sd, buf, sizeof(buf), 0, NULL, 0);
1010 	if (rc == -1) {
1011 		test_fail("[cleint] recvfrom() failed unexpectedly");
1012 	} else {
1013 		debug("[client] we got the following message:");
1014 		debug(buf);
1015 	}
1016 
1017 	if (strncmp(buf, "HOLA!", sizeof(buf)) != 0) {
1018 		test_fail("[client] We didn't get the right response.");
1019 	}
1020 
1021 	debug("[client] closing socket");
1022 	CLOSE(sd);
1023 
1024 	debug("[client] leaving test_xfer_client()");
1025 	exit(errct);
1026 }
1027 
1028 void test_xfer(const struct socket_test_info *info)
1029 {
1030 	pid_t pid;
1031 
1032 	debug("entering test_xfer()");
1033 	info->callback_cleanup();
1034 
1035 	/* the signal handler is only used by the client, but we have to
1036 	 * install it now. if we don't the server may signal the client
1037 	 * before the handler is installed.
1038 	 */
1039 	debug("installing signal handler");
1040 	if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
1041 		test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
1042 	}
1043 
1044 	debug("signal handler installed");
1045 
1046 	server_ready = 0;
1047 
1048 	pid = fork();
1049 	if (pid == -1) {
1050 		test_fail("fork() failed");
1051 		return;
1052 	} else if (pid == 0) {
1053 		debug("child");
1054 		errct = 0;
1055 		test_xfer_client(info);
1056 		test_fail("we should never get here");
1057 		exit(1);
1058 	} else {
1059 		debug("parent");
1060 		test_xfer_server(info, pid);
1061 		debug("parent done");
1062 	}
1063 
1064 	info->callback_cleanup();
1065 	debug("leaving test_xfer()");
1066 }
1067 
1068 static void test_simple_client(const struct socket_test_info *info, int type)
1069 {
1070 	char buf[BUFSIZE];
1071 	int sd, rc;
1072 
1073 	sd = socket(info->domain, type, 0);
1074 	if (sd == -1) {
1075 		test_fail("socket");
1076 		exit(errct);
1077 	}
1078 
1079 	while (server_ready == 0) {
1080 		debug("[client] waiting for the server");
1081 		sleep(1);
1082 	}
1083 
1084 	bzero(buf, BUFSIZE);
1085 	snprintf(buf, BUFSIZE-1, "Hello, My Name is Client.");
1086 
1087 	if (type == SOCK_DGRAM) {
1088 
1089 		rc = sendto(sd, buf, strlen(buf) + 1, 0,
1090 			info->clientaddr, info->clientaddrlen);
1091 		if (rc == -1) {
1092 			test_fail("sendto");
1093 			exit(errct);
1094 		}
1095 
1096 	} else {
1097 
1098 		rc = connect(sd, info->clientaddr, info->clientaddrlen);
1099 		if (rc == -1) {
1100 			test_fail("connect");
1101 			exit(errct);
1102 		}
1103 
1104 		rc = write(sd, buf, strlen(buf) + 1);
1105 
1106 		if (rc == -1) {
1107 			test_fail("write");
1108 		}
1109 
1110 		memset(buf, '\0', BUFSIZE);
1111 		rc = read(sd, buf, BUFSIZE);
1112 		if (rc == -1) {
1113 			test_fail("read");
1114 		}
1115 
1116 		if (strcmp("Hello, My Name is Server.", buf) != 0) {
1117 			test_fail("didn't read the correct string");
1118 		}
1119 	}
1120 
1121 	rc = close(sd);
1122 	if (rc == -1) {
1123 		test_fail("close");
1124 	}
1125 
1126 	exit(errct);
1127 }
1128 
1129 static void test_simple_server(const struct socket_test_info *info, int type,
1130 	pid_t pid)
1131 {
1132 	char buf[BUFSIZE];
1133 	int sd, rc, client_sd, status;
1134 	struct sockaddr_storage addr;
1135 	socklen_t addr_len;
1136 
1137 	addr_len = info->clientaddrlen;
1138 
1139 	sd = socket(info->domain, type, 0);
1140 	if (sd == -1) {
1141 		test_fail("socket");
1142 	}
1143 
1144 	assert(info->clientaddrlen <= sizeof(addr));
1145 	memcpy(&addr, info->clientaddr, info->clientaddrlen);
1146 
1147 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
1148 	if (rc == -1) {
1149 		test_fail("bind");
1150 	}
1151 
1152 	if (type == SOCK_DGRAM) {
1153 
1154 		/* ready for client */
1155 		kill(pid, SIGUSR1);
1156 
1157 		rc = recvfrom(sd, buf, BUFSIZE, 0,
1158 				(struct sockaddr *) &addr, &addr_len);
1159 		if (rc == -1) {
1160 			test_fail("recvfrom");
1161 		}
1162 
1163 	} else {
1164 
1165 		rc = listen(sd, 5);
1166 		if (rc == -1) {
1167 			test_fail("listen");
1168 		}
1169 
1170 		/* we're ready for connections, time to tell the client
1171 		 * to start the test
1172 		 */
1173 		kill(pid, SIGUSR1);
1174 
1175 		client_sd = accept(sd, (struct sockaddr *) &addr, &addr_len);
1176 		if (client_sd == -1) {
1177 			test_fail("accept");
1178 		}
1179 
1180 		memset(buf, '\0', BUFSIZE);
1181 		rc = read(client_sd, buf, BUFSIZE);
1182 		if (rc == -1) {
1183 			test_fail("read");
1184 		}
1185 
1186 		if (strcmp("Hello, My Name is Client.", buf) != 0) {
1187 			test_fail("didn't read the correct string");
1188 		}
1189 
1190 		/* added for extra fun to make the client block on read() */
1191 		sleep(1);
1192 
1193 		bzero(buf, BUFSIZE);
1194 		snprintf(buf, BUFSIZE-1, "Hello, My Name is Server.");
1195 
1196 		rc = write(client_sd, buf, strlen(buf) + 1);
1197 		if (rc == -1) {
1198 			test_fail("write");
1199 		}
1200 		rc = close(client_sd);
1201 		if (rc == -1) {
1202 			test_fail("close");
1203 		}
1204 	}
1205 
1206 	rc = close(sd);
1207 	if (rc == -1) {
1208 		test_fail("close");
1209 	}
1210 
1211 	/* wait for client to exit */
1212 	do {
1213 		errno = 0;
1214 		rc = waitpid(pid, &status, 0);
1215 	} while (rc == -1 && errno == EINTR);
1216 
1217 	/* we use the exit status to get its error count */
1218 	errct += WEXITSTATUS(status);
1219 }
1220 
1221 static void test_abort_client(const struct socket_test_info *info,
1222 	int abort_type);
1223 static void test_abort_server(const struct socket_test_info *info,
1224 	pid_t pid, int abort_type);
1225 
1226 void test_abort_client_server(const struct socket_test_info *info,
1227 	int abort_type)
1228 {
1229 	pid_t pid;
1230 
1231 	debug("test_simple_client_server()");
1232 
1233 	info->callback_cleanup();
1234 
1235 	/* the signal handler is only used by the client, but we have to
1236 	 * install it now. if we don't the server may signal the client
1237 	 * before the handler is installed.
1238 	 */
1239 	debug("installing signal handler");
1240 	if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
1241 		test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
1242 	}
1243 
1244 	debug("signal handler installed");
1245 
1246 	server_ready = 0;
1247 
1248 	pid = fork();
1249 	if (pid == -1) {
1250 		test_fail("fork() failed");
1251 		return;
1252 	} else if (pid == 0) {
1253 		debug("child");
1254 		errct = 0;
1255 		test_abort_client(info, abort_type);
1256 		test_fail("we should never get here");
1257 		exit(1);
1258 	} else {
1259 		debug("parent");
1260 		test_abort_server(info, pid, abort_type);
1261 		debug("parent done");
1262 	}
1263 
1264 	info->callback_cleanup();
1265 }
1266 
1267 static void test_abort_client(const struct socket_test_info *info,
1268 	int abort_type)
1269 {
1270 	char buf[BUFSIZE];
1271 	int sd, rc;
1272 
1273 	sd = socket(info->domain, info->type, 0);
1274 	if (sd == -1) {
1275 		test_fail("socket");
1276 		exit(errct);
1277 	}
1278 
1279 	while (server_ready == 0) {
1280 		debug("[client] waiting for the server");
1281 		sleep(1);
1282 	}
1283 
1284 	bzero(buf, BUFSIZE);
1285 	snprintf(buf, BUFSIZE-1, "Hello, My Name is Client.");
1286 
1287 	rc = connect(sd, info->clientaddr, info->clientaddrlen);
1288 	if (rc == -1) {
1289 		test_fail("connect");
1290 		exit(errct);
1291 	}
1292 
1293 	if (abort_type == 2) {
1294 		/* Give server a chance to close connection */
1295 		sleep(2);
1296 		rc = write(sd, buf, strlen(buf) + 1);
1297 		if (rc != -1) {
1298 			if (!info->ignore_write_conn_reset) {
1299 				test_fail("write should have failed\n");
1300 			}
1301 		} else if (errno != ECONNRESET) {
1302 			test_fail("errno should've been ECONNRESET\n");
1303 		}
1304 	}
1305 
1306 	rc = close(sd);
1307 	if (rc == -1) {
1308 		test_fail("close");
1309 	}
1310 
1311 	exit(errct);
1312 }
1313 
1314 static void test_abort_server(const struct socket_test_info *info,
1315 	pid_t pid, int abort_type)
1316 {
1317 	char buf[BUFSIZE];
1318 	int sd, rc, client_sd, status;
1319 	struct sockaddr_storage addr;
1320 	socklen_t addr_len;
1321 
1322 	addr_len = info->clientaddrlen;
1323 
1324 	sd = socket(info->domain, info->type, 0);
1325 	if (sd == -1) {
1326 		test_fail("socket");
1327 	}
1328 
1329 	assert(sizeof(addr) >= info->clientaddrlen);
1330 	memcpy(&addr, info->clientaddr, info->clientaddrlen);
1331 
1332 	rc = bind(sd, info->serveraddr, info->serveraddrlen);
1333 	if (rc == -1) {
1334 		test_fail("bind");
1335 	}
1336 
1337 	rc = listen(sd, 5);
1338 	if (rc == -1) {
1339 		test_fail("listen");
1340 	}
1341 
1342 	/* we're ready for connections, time to tell the client
1343 	 * to start the test
1344 	 */
1345 	kill(pid, SIGUSR1);
1346 
1347 	client_sd = accept(sd, (struct sockaddr *) &addr, &addr_len);
1348 	if (client_sd == -1) {
1349 		test_fail("accept");
1350 	}
1351 
1352 	if (abort_type == 1) {
1353 		memset(buf, '\0', BUFSIZE);
1354 		rc = read(client_sd, buf, BUFSIZE);
1355 		if (rc != -1 && (rc != 0 || !info->ignore_read_conn_reset)) {
1356 			test_fail("read should've failed or returned zero\n");
1357 		}
1358 		if (rc != 0 && errno != ECONNRESET) {
1359 			test_fail("errno should've been ECONNRESET\n");
1360 		}
1361 	} /* else if (abort_type == 2) { */
1362 		rc = close(client_sd);
1363 		if (rc == -1) {
1364 			test_fail("close");
1365 		}
1366 	/* } */
1367 
1368 	rc = close(sd);
1369 	if (rc == -1) {
1370 		test_fail("close");
1371 	}
1372 
1373 	/* wait for client to exit */
1374 	do {
1375 		errno = 0;
1376 		rc = waitpid(pid, &status, 0);
1377 	} while (rc == -1 && errno == EINTR);
1378 
1379 	/* we use the exit status to get its error count */
1380 	errct += WEXITSTATUS(status);
1381 }
1382 
1383 void test_simple_client_server(const struct socket_test_info *info, int type)
1384 {
1385 	pid_t pid;
1386 
1387 	debug("entering test_simple_client_server()");
1388 
1389 	info->callback_cleanup();
1390 
1391 	/* the signal handler is only used by the client, but we have to
1392 	 * install it now. if we don't the server may signal the client
1393 	 * before the handler is installed.
1394 	 */
1395 	debug("installing signal handler");
1396 	if (signal(SIGUSR1, test_xfer_sighdlr) == SIG_ERR) {
1397 		test_fail("signal(SIGUSR1, test_xfer_sighdlr) failed");
1398 	}
1399 
1400 	debug("signal handler installed");
1401 
1402 	server_ready = 0;
1403 
1404 	pid = fork();
1405 	if (pid == -1) {
1406 		test_fail("fork() failed");
1407 		return;
1408 	} else if (pid == 0) {
1409 		debug("child");
1410 		errct = 0;
1411 		test_simple_client(info, type);
1412 		test_fail("we should never get here");
1413 		exit(1);
1414 	} else {
1415 		debug("parent");
1416 		test_simple_server(info, type, pid);
1417 		debug("parent done");
1418 	}
1419 
1420 	info->callback_cleanup();
1421 	debug("leaving test_simple_client_server()");
1422 }
1423 
1424 void test_msg_dgram(const struct socket_test_info *info)
1425 {
1426 	int rc;
1427 	int src;
1428 	int dst;
1429 	struct sockaddr_storage addr;
1430 	struct iovec iov[3];
1431 	struct msghdr msg1;
1432 	struct msghdr msg2;
1433 	char buf1[BUFSIZE];
1434 	char buf2[BUFSIZE];
1435 	char buf3[BUFSIZE];
1436 
1437 	debug("entering test_msg_dgram");
1438 
1439 	info->callback_cleanup();
1440 
1441 	src = socket(info->domain, SOCK_DGRAM, 0);
1442 	if (src == -1) {
1443 		test_fail("socket");
1444 	}
1445 
1446 	dst = socket(info->domain, SOCK_DGRAM, 0);
1447 	if (dst == -1) {
1448 		test_fail("socket");
1449 	}
1450 
1451 	rc = bind(src, info->serveraddr2, info->serveraddr2len);
1452 	if (rc == -1) {
1453 		test_fail("bind");
1454 	}
1455 
1456 	assert(info->clientaddrlen <= sizeof(addr));
1457 	memcpy(&addr, info->clientaddr, info->clientaddrlen);
1458 
1459 	rc = bind(dst, info->serveraddr, info->serveraddrlen);
1460 	if (rc == -1) {
1461 		test_fail("bind");
1462 	}
1463 
1464 	memset(&buf1, '\0', BUFSIZE);
1465 	memset(&buf2, '\0', BUFSIZE);
1466 	memset(&buf3, '\0', BUFSIZE);
1467 
1468 	strncpy(buf1, "Minix ", BUFSIZE-1);
1469 	strncpy(buf2, "is ", BUFSIZE-1);
1470 	strncpy(buf3, "great!", BUFSIZE-1);
1471 
1472 	iov[0].iov_base = buf1;
1473 	iov[0].iov_len  = 6;
1474 	iov[1].iov_base = buf2;
1475 	iov[1].iov_len  = 3;
1476 	iov[2].iov_base = buf3;
1477 	iov[2].iov_len  = 32;
1478 
1479 	memset(&msg1, '\0', sizeof(struct msghdr));
1480 	msg1.msg_name = &addr;
1481 	msg1.msg_namelen = info->clientaddrlen;
1482 	msg1.msg_iov = iov;
1483 	msg1.msg_iovlen = 3;
1484 	msg1.msg_control = NULL;
1485 	msg1.msg_controllen = 0;
1486 	msg1.msg_flags = 0;
1487 
1488 	rc = sendmsg(src, &msg1, 0);
1489 	if (rc == -1) {
1490 		test_fail("sendmsg");
1491 	}
1492 
1493 	memset(&buf1, '\0', BUFSIZE);
1494 	memset(&buf2, '\0', BUFSIZE);
1495 
1496 	iov[0].iov_base = buf1;
1497 	iov[0].iov_len  = 9;
1498 	iov[1].iov_base = buf2;
1499 	iov[1].iov_len  = 32;
1500 
1501 	memset(&addr, '\0', sizeof(addr));
1502 	memset(&msg2, '\0', sizeof(struct msghdr));
1503 	msg2.msg_name = &addr;
1504 	msg2.msg_namelen = sizeof(addr);
1505 	msg2.msg_iov = iov;
1506 	msg2.msg_iovlen = 2;
1507 	msg2.msg_control = NULL;
1508 	msg2.msg_controllen = 0;
1509 	msg2.msg_flags = 0;
1510 
1511 	rc = recvmsg(dst, &msg2, 0);
1512 	if (rc == -1) {
1513 		test_fail("recvmsg");
1514 	}
1515 
1516 	if (strncmp(buf1, "Minix is ", 9) || strncmp(buf2, "great!", 6)) {
1517 		test_fail("recvmsg");
1518 	}
1519 
1520 	/* we need to use the full path "/usr/src/test/DIR_56/testb.sock"
1521 	 * because that is what is returned by recvmsg().
1522 	 */
1523 	info->callback_check_sockaddr((struct sockaddr *) &addr,
1524 		msg2.msg_namelen, "recvmsg", 2);
1525 
1526 	rc = close(dst);
1527 	if (rc == -1) {
1528 		test_fail("close");
1529 	}
1530 
1531 	rc = close(src);
1532 	if (rc == -1) {
1533 		test_fail("close");
1534 	}
1535 
1536 	info->callback_cleanup();
1537 	debug("leaving test_msg_dgram");
1538 }
1539 
1540 #define check_select(sd, rd, wr, block) \
1541 	check_select_internal(sd, rd, wr, block, 1, __LINE__)
1542 #define check_select_cond(sd, rd, wr, block, allchecks) \
1543 	check_select_internal(sd, rd, wr, block, allchecks, __LINE__)
1544 
1545 static void
1546 check_select_internal(int sd, int rd, int wr, int block, int allchecks, int line)
1547 {
1548 	fd_set read_set, write_set;
1549 	struct timeval tv;
1550 
1551 	FD_ZERO(&read_set);
1552 	if (rd != -1)
1553 		FD_SET(sd, &read_set);
1554 
1555 	FD_ZERO(&write_set);
1556 	if (wr != -1)
1557 		FD_SET(sd, &write_set);
1558 
1559 	tv.tv_sec = block ? 2 : 0;
1560 	tv.tv_usec = 0;
1561 
1562 	errno = 0;
1563 	if (select(sd + 1, &read_set, &write_set, NULL, &tv) < 0)
1564 		test_fail_fl("select() failed unexpectedly", __FILE__, line);
1565 
1566 	if (rd != -1 && !!FD_ISSET(sd, &read_set) != rd && allchecks)
1567 		test_fail_fl("select() mismatch on read operation",
1568 			__FILE__, line);
1569 
1570 	if (wr != -1 && !!FD_ISSET(sd, &write_set) != wr && allchecks)
1571 		test_fail_fl("select() mismatch on write operation",
1572 			__FILE__, line);
1573 }
1574 
1575 /*
1576  * Verify that:
1577  * - a nonblocking connecting socket for which there is no accepter, will
1578  *   return EINPROGRESS and complete in the background later;
1579  * - a nonblocking listening socket will return EAGAIN on accept;
1580  * - connecting a connecting socket yields EALREADY;
1581  * - connecting a connected socket yields EISCONN;
1582  * - selecting for read and write on a connecting socket will only satisfy the
1583  *   write only once it is connected;
1584  * - doing a nonblocking write on a connecting socket yields EAGAIN;
1585  * - doing a nonblocking read on a connected socket with no pending data yields
1586  *   EAGAIN.
1587  */
1588 void
1589 test_nonblock(const struct socket_test_info *info)
1590 {
1591 	char buf[BUFSIZE];
1592 	socklen_t len;
1593 	int server_sd, client_sd;
1594 	struct sockaddr_storage addr;
1595 	int status;
1596 
1597 	debug("entering test_nonblock()");
1598 	memset(buf, 0, sizeof(buf));
1599 
1600 	SOCKET(server_sd, info->domain, info->type, 0);
1601 
1602 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
1603 		test_fail("bind() should have worked");
1604 
1605 	if (listen(server_sd, 8) == -1)
1606 		test_fail("listen() should have worked");
1607 
1608 	fcntl(server_sd, F_SETFL, fcntl(server_sd, F_GETFL) | O_NONBLOCK);
1609 
1610 	check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1611 
1612 	len = sizeof(addr);
1613 	if (accept(server_sd, (struct sockaddr *) &addr, &len) != -1 ||
1614 	    errno != EAGAIN)
1615 		test_fail("accept() should have yielded EAGAIN");
1616 
1617 	SOCKET(client_sd, info->domain, info->type, 0);
1618 
1619 	fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
1620 
1621 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
1622 		test_fail("connect() should have failed");
1623 	} else if (errno != EINPROGRESS) {
1624 		test_fail("connect() should have yielded EINPROGRESS");
1625 	}
1626 
1627 	check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
1628 		!info->ignore_select_delay);
1629 
1630 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
1631 		test_fail("connect() should have failed");
1632 	} else if (errno != EALREADY && errno != EISCONN) {
1633 		test_fail("connect() should have yielded EALREADY");
1634 	}
1635 
1636 	if (recv(client_sd, buf, sizeof(buf), 0) != -1 || errno != EAGAIN)
1637 		test_fail("recv() should have yielded EAGAIN");
1638 
1639 	/* This may be an implementation aspect, or even plain wrong (?). */
1640 	if (send(client_sd, buf, sizeof(buf), 0) != -1) {
1641 		if (!info->ignore_send_waiting) {
1642 			test_fail("send() should have failed");
1643 		}
1644 	} else if (errno != EAGAIN) {
1645 		test_fail("send() should have yielded EAGAIN");
1646 	}
1647 
1648 	switch (fork()) {
1649 	case 0:
1650 		errct = 0;
1651 		close(client_sd);
1652 
1653 		check_select(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1654 
1655 		len = sizeof(addr);
1656 		client_sd = accept(server_sd, (struct sockaddr *) &addr, &len);
1657 		if (client_sd == -1)
1658 			test_fail("accept() should have succeeded");
1659 
1660 		check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1661 
1662 		close(server_sd);
1663 
1664 		/* Let the socket become writable in the parent process. */
1665 		sleep(1);
1666 
1667 		if (write(client_sd, buf, 1) != 1)
1668 			test_fail("write() should have succeeded");
1669 
1670 		/* Wait for the client side to close. */
1671 		check_select_cond(client_sd, 0 /*read*/, 1 /*write*/,
1672 			0 /*block*/, !info->ignore_select_delay /*allchecks*/);
1673 		check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
1674 		check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1675 
1676 		exit(errct);
1677 	case -1:
1678 		test_fail("can't fork");
1679 	default:
1680 		break;
1681 	}
1682 
1683 	close(server_sd);
1684 
1685 	check_select(client_sd, 0 /*read*/, 1 /*write*/, 1 /*block*/);
1686 	check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1687 
1688 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
1689 		errno != EISCONN)
1690 		test_fail("connect() should have yielded EISCONN");
1691 
1692 	check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
1693 	check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1694 
1695 	if (read(client_sd, buf, 1) != 1)
1696 		test_fail("read() should have succeeded");
1697 
1698 	check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1699 
1700 	if (read(client_sd, buf, 1) != -1 || errno != EAGAIN)
1701 		test_fail("read() should have yielded EAGAIN");
1702 
1703 	/* Let the child process block on the select waiting for the close. */
1704 	sleep(1);
1705 
1706 	close(client_sd);
1707 
1708 	errno = 0;
1709 	if (wait(&status) <= 0)
1710 		test_fail("wait() should have succeeded");
1711 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1712 		test_fail("child process failed the test");
1713 
1714 	info->callback_cleanup();
1715 	debug("leaving test_nonblock()");
1716 }
1717 
1718 /*
1719  * Verify that a nonblocking connect for which there is an accepter, succeeds
1720  * immediately.  A pretty lame test, only here for completeness.
1721  */
1722 void
1723 test_connect_nb(const struct socket_test_info *info)
1724 {
1725 	socklen_t len;
1726 	int server_sd, client_sd;
1727 	struct sockaddr_storage addr;
1728 	int status;
1729 
1730 	debug("entering test_connect_nb()");
1731 	SOCKET(server_sd, info->domain, info->type, 0);
1732 
1733 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
1734 		test_fail("bind() should have worked");
1735 
1736 	if (listen(server_sd, 8) == -1)
1737 		test_fail("listen() should have worked");
1738 
1739 	switch (fork()) {
1740 	case 0:
1741 		errct = 0;
1742 		len = sizeof(addr);
1743 		if (accept(server_sd, (struct sockaddr *) &addr, &len) == -1)
1744 			test_fail("accept() should have succeeded");
1745 
1746 		exit(errct);
1747 	case -1:
1748 		test_fail("can't fork");
1749 	default:
1750 		break;
1751 	}
1752 
1753 	close(server_sd);
1754 
1755 	sleep(1);
1756 
1757 	SOCKET(client_sd, info->domain, info->type, 0);
1758 
1759 	fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
1760 
1761 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != 0) {
1762 		if (!info->ignore_connect_delay) {
1763 			test_fail("connect() should have succeeded");
1764 		} else if (errno != EINPROGRESS) {
1765 			test_fail("connect() should have succeeded or "
1766 				"failed with EINPROGRESS");
1767 		}
1768 	}
1769 
1770 	close(client_sd);
1771 
1772 	if (wait(&status) <= 0)
1773 		test_fail("wait() should have succeeded");
1774 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1775 		test_fail("child process failed the test");
1776 
1777 	info->callback_cleanup();
1778 	debug("leaving test_connect_nb()");
1779 }
1780 
1781 static void
1782 dummy_handler(int sig)
1783 {
1784 	/* Nothing. */
1785 }
1786 
1787 /*
1788  * Verify that:
1789  * - interrupting a blocking connect will return EINTR but complete in the
1790  *   background later;
1791  * - doing a blocking write on an asynchronously connecting socket succeeds
1792  *   once the socket is connected.
1793  * - doing a nonblocking write on a connected socket with lots of pending data
1794  *   yields EAGAIN.
1795  */
1796 void
1797 test_intr(const struct socket_test_info *info)
1798 {
1799 	struct sigaction act, oact;
1800 	char buf[BUFSIZE];
1801 	int isconn;
1802 	socklen_t len;
1803 	int server_sd, client_sd;
1804 	struct sockaddr_storage addr;
1805 	int r, status;
1806 
1807 	debug("entering test_intr()");
1808 	memset(buf, 0, sizeof(buf));
1809 
1810 	SOCKET(server_sd, info->domain, info->type, 0);
1811 
1812 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
1813 		test_fail("bind() should have worked");
1814 
1815 	if (listen(server_sd, 8) == -1)
1816 		test_fail("listen() should have worked");
1817 
1818 	SOCKET(client_sd, info->domain, info->type, 0);
1819 
1820 	memset(&act, 0, sizeof(act));
1821 	act.sa_handler = dummy_handler;
1822 	if (sigaction(SIGALRM, &act, &oact) == -1)
1823 		test_fail("sigaction() should have succeeded");
1824 
1825 	if (info->domain != PF_INET) alarm(1);
1826 
1827 	isconn = 0;
1828 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
1829 		if (!info->ignore_connect_unaccepted) {
1830 			test_fail("connect() should have failed");
1831 		}
1832 		isconn = 1;
1833 	} else if (errno != EINTR) {
1834 		test_fail("connect() should have yielded EINTR");
1835 	}
1836 
1837 	alarm(0);
1838 
1839 	check_select(client_sd, 0 /*read*/, isconn /*write*/, 0 /*block*/);
1840 
1841 	switch (fork()) {
1842 	case 0:
1843 		errct = 0;
1844 		close(client_sd);
1845 
1846 		check_select(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1847 
1848 		len = sizeof(addr);
1849 		client_sd = accept(server_sd, (struct sockaddr *) &addr, &len);
1850 		if (client_sd == -1)
1851 			test_fail("accept() should have succeeded");
1852 
1853 		check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1854 
1855 		close(server_sd);
1856 
1857 		check_select(client_sd, 1 /*read*/, -1 /*write*/, 1 /*block*/);
1858 		check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1859 
1860 		if (recv(client_sd, buf, sizeof(buf), 0) != sizeof(buf))
1861 			test_fail("recv() should have yielded bytes");
1862 
1863 		/* No partial transfers should be happening. */
1864 		check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1865 
1866 		sleep(1);
1867 
1868 		fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) |
1869 		    O_NONBLOCK);
1870 
1871 		/* We can only test nonblocking writes by filling the pipe. */
1872 		while ((r = write(client_sd, buf, sizeof(buf))) > 0);
1873 
1874 		if (r != -1) {
1875 			test_fail("write() should have failed");
1876 		} else if (errno != EAGAIN) {
1877 			test_fail("write() should have yielded EAGAIN");
1878 		}
1879 
1880 		check_select(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/);
1881 
1882 		if (write(client_sd, buf, 1) != -1) {
1883 			test_fail("write() should have failed");
1884 		} else if (errno != EAGAIN) {
1885 			test_fail("write() should have yielded EAGAIN");
1886 		}
1887 
1888 		exit(errct);
1889 	case -1:
1890 		test_fail("can't fork");
1891 	default:
1892 		break;
1893 	}
1894 
1895 	close(server_sd);
1896 
1897 	if (send(client_sd, buf, sizeof(buf), 0) != sizeof(buf))
1898 		test_fail("send() should have succeded");
1899 
1900 	check_select(client_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1901 
1902 	if (wait(&status) <= 0)
1903 		test_fail("wait() should have succeeded");
1904 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
1905 		test_fail("child process failed the test");
1906 
1907 	check_select(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/);
1908 
1909 	close(client_sd);
1910 
1911 	sigaction(SIGALRM, &oact, NULL);
1912 
1913 	info->callback_cleanup();
1914 	debug("leaving test_intr()");
1915 }
1916 
1917 /*
1918  * Verify that closing a connecting socket before it is accepted will result in
1919  * no activity on the accepting side later.
1920  */
1921 void
1922 test_connect_close(const struct socket_test_info *info)
1923 {
1924 	int server_sd, client_sd;
1925 	struct sockaddr_storage addr;
1926 	socklen_t len;
1927 
1928 	debug("entering test_connect_close()");
1929 	SOCKET(server_sd, info->domain, info->type, 0);
1930 
1931 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
1932 		test_fail("bind() should have worked");
1933 
1934 	if (listen(server_sd, 8) == -1)
1935 		test_fail("listen() should have worked");
1936 
1937 	fcntl(server_sd, F_SETFL, fcntl(server_sd, F_GETFL) | O_NONBLOCK);
1938 
1939 	check_select(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/);
1940 
1941 	SOCKET(client_sd, info->domain, info->type, 0);
1942 
1943 	fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
1944 
1945 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1	||
1946 		errno != EINPROGRESS)
1947 		test_fail("connect() should have yielded EINPROGRESS");
1948 
1949 	check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
1950 		!info->ignore_select_delay);
1951 	check_select_cond(server_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/,
1952 		!info->ignore_select_delay);
1953 
1954 	close(client_sd);
1955 
1956 	check_select_cond(server_sd, 0 /*read*/, 1 /*write*/, 0 /*block*/,
1957 		!info->ignore_select_delay);
1958 
1959 	len = sizeof(addr);
1960 	errno = 0;
1961 	if (accept(server_sd, (struct sockaddr *) &addr, &len) != -1) {
1962 		if (!info->ignore_accept_delay) {
1963 			test_fail("accept() should have failed");
1964 		}
1965 	} else if (errno != EAGAIN) {
1966 		test_fail("accept() should have yielded EAGAIN");
1967 	}
1968 	close(server_sd);
1969 
1970 	info->callback_cleanup();
1971 	debug("leaving test_connect_close()");
1972 }
1973 
1974 /*
1975  * Verify that closing a listening socket will cause a blocking connect to fail
1976  * with ECONNRESET, and that a subsequent write will yield EPIPE.
1977  */
1978 void
1979 test_listen_close(const struct socket_test_info *info)
1980 {
1981 	int server_sd, client_sd;
1982 	int status;
1983 	char byte;
1984 
1985 	debug("entering test_listen_close()");
1986 	SOCKET(server_sd, info->domain, info->type, 0);
1987 
1988 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
1989 		test_fail("bind() should have worked");
1990 
1991 	if (listen(server_sd, 8) == -1)
1992 		test_fail("listen() should have worked");
1993 
1994 	switch (fork()) {
1995 	case 0:
1996 		sleep(1);
1997 
1998 		exit(0);
1999 	case -1:
2000 		test_fail("can't fork");
2001 	default:
2002 		break;
2003 	}
2004 
2005 	close(server_sd);
2006 
2007 	SOCKET(client_sd, info->domain, info->type, 0);
2008 
2009 	byte = 0;
2010 	if (write(client_sd, &byte, 1) != -1 || errno != ENOTCONN)
2011 		/* Yes, you fucked up the fix for the FIXME below. */
2012 		test_fail("write() should have yielded ENOTCONN");
2013 
2014 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1) {
2015 		if (!info->bug_connect_after_close) {
2016 			test_fail("connect() should have failed");
2017 		}
2018 	} else if (errno != ECONNRESET) {
2019 		test_fail("connect() should have yielded ECONNRESET");
2020 	}
2021 
2022 	/*
2023 	 * FIXME: currently UDS cannot distinguish between sockets that have
2024 	 * not yet been connected, and sockets that have been disconnected.
2025 	 * Thus, we get the same error for both: ENOTCONN instead of EPIPE.
2026 	 */
2027 #if 0
2028 	if (write(client_sd, &byte, 1) != -1 || errno != EPIPE)
2029 		test_fail("write() should have yielded EPIPE");
2030 #endif
2031 
2032 	close(client_sd);
2033 
2034 	if (wait(&status) <= 0)
2035 		test_fail("wait() should have succeeded");
2036 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
2037 		test_fail("child process failed the test");
2038 
2039 	info->callback_cleanup();
2040 	debug("leaving test_listen_close()");
2041 }
2042 
2043 /*
2044  * Verify that closing a listening socket will cause a nonblocking connect to
2045  * result in the socket becoming readable and writable, and yielding ECONNRESET
2046  * and EPIPE on the next two writes, respectively.
2047  */
2048 void
2049 test_listen_close_nb(const struct socket_test_info *info)
2050 {
2051 	int server_sd, client_sd;
2052 	int status;
2053 	char byte;
2054 
2055 	debug("entering test_listen_close_nb()");
2056 	SOCKET(server_sd, info->domain, info->type, 0);
2057 
2058 	if (bind(server_sd, info->serveraddr, info->serveraddrlen) == -1)
2059 		test_fail("bind() should have worked");
2060 
2061 	if (listen(server_sd, 8) == -1)
2062 		test_fail("listen() should have worked");
2063 
2064 	switch (fork()) {
2065 	case 0:
2066 		sleep(1);
2067 
2068 		exit(0);
2069 	case -1:
2070 		test_fail("can't fork");
2071 	default:
2072 		break;
2073 	}
2074 
2075 	close(server_sd);
2076 
2077 	SOCKET(client_sd, info->domain, info->type, 0);
2078 
2079 	fcntl(client_sd, F_SETFL, fcntl(client_sd, F_GETFL) | O_NONBLOCK);
2080 
2081 	if (connect(client_sd, info->clientaddr, info->clientaddrlen) != -1 ||
2082 		errno != EINPROGRESS)
2083 		test_fail("connect() should have yielded EINPROGRESS");
2084 
2085 	check_select_cond(client_sd, 0 /*read*/, 0 /*write*/, 0 /*block*/,
2086 		!info->ignore_select_delay);
2087 	check_select_cond(client_sd, 1 /*read*/, 1 /*write*/, 1 /*block*/,
2088 		!info->ignore_select_delay);
2089 
2090 	byte = 0;
2091 	if (write(client_sd, &byte, 1) != -1) {
2092 		if (!info->ignore_write_conn_reset) {
2093 			test_fail("write() should have failed");
2094 		}
2095 	} else if (errno != ECONNRESET) {
2096 		test_fail("write() should have yielded ECONNRESET");
2097 	}
2098 
2099 	/*
2100 	 * FIXME: currently UDS cannot distinguish between sockets that have
2101 	 * not yet been connected, and sockets that have been disconnected.
2102 	 * Thus, we get the same error for both: ENOTCONN instead of EPIPE.
2103 	 */
2104 #if 0
2105 	if (write(client_sd, &byte, 1) != -1 || errno != EPIPE)
2106 		test_fail("write() should have yielded EPIPE");
2107 #endif
2108 
2109 	check_select_cond(client_sd, 1 /*read*/, 1 /*write*/, 0 /*block*/,
2110 		!info->ignore_select_delay);
2111 
2112 	close(client_sd);
2113 
2114 	if (wait(&status) <= 0)
2115 		test_fail("wait() should have succeeded");
2116 	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
2117 		test_fail("child process failed the test");
2118 
2119 	info->callback_cleanup();
2120 	debug("leaving test_listen_close_nb()");
2121 }
2122