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