tst-accept4.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* Test the accept4 function with differing flags arguments.
  2. Copyright (C) 2017-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <arpa/inet.h>
  16. #include <errno.h>
  17. #include <fcntl.h>
  18. #include <stdbool.h>
  19. #include <support/check.h>
  20. #include <support/xsocket.h>
  21. #include <support/xunistd.h>
  22. #include <sys/socket.h>
  23. static bool
  24. is_nonblocking (int fd)
  25. {
  26. int status = fcntl (fd, F_GETFL);
  27. if (status < 0)
  28. FAIL_EXIT1 ("fcntl (F_GETFL): %m");
  29. return status & O_NONBLOCK;
  30. }
  31. static bool
  32. is_cloexec (int fd)
  33. {
  34. int status = fcntl (fd, F_GETFD);
  35. if (status < 0)
  36. FAIL_EXIT1 ("fcntl (F_GETFD): %m");
  37. return status & FD_CLOEXEC;
  38. }
  39. struct client
  40. {
  41. int socket;
  42. struct sockaddr_in address;
  43. };
  44. /* Perform a non-blocking connect to *SERVER_ADDRESS. */
  45. static struct client
  46. client_connect (const struct sockaddr_in *server_address)
  47. {
  48. struct client result;
  49. result.socket = xsocket (AF_INET,
  50. SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  51. TEST_VERIFY (is_nonblocking (result.socket));
  52. TEST_VERIFY (is_cloexec (result.socket));
  53. int ret = connect (result.socket, (const struct sockaddr *) server_address,
  54. sizeof (*server_address));
  55. if (ret < 0 && errno != EINPROGRESS)
  56. FAIL_EXIT1 ("client connect: %m");
  57. socklen_t sa_len = sizeof (result.address);
  58. xgetsockname (result.socket, (struct sockaddr *) &result.address,
  59. &sa_len);
  60. TEST_VERIFY (sa_len == sizeof (result.address));
  61. return result;
  62. }
  63. static void
  64. check_same_address (const struct sockaddr_in *left,
  65. const struct sockaddr_in *right)
  66. {
  67. TEST_VERIFY (left->sin_family == AF_INET);
  68. TEST_VERIFY (right->sin_family == AF_INET);
  69. TEST_VERIFY (left->sin_addr.s_addr == right->sin_addr.s_addr);
  70. TEST_VERIFY (left->sin_port == right->sin_port);
  71. }
  72. static int
  73. do_test (void)
  74. {
  75. /* Create server socket. */
  76. int server_socket = xsocket (AF_INET, SOCK_STREAM, 0);
  77. TEST_VERIFY (!is_nonblocking (server_socket));
  78. TEST_VERIFY (!is_cloexec (server_socket));
  79. struct sockaddr_in server_address =
  80. {
  81. .sin_family = AF_INET,
  82. .sin_addr = {.s_addr = htonl (INADDR_LOOPBACK) },
  83. };
  84. xbind (server_socket,
  85. (struct sockaddr *) &server_address, sizeof (server_address));
  86. {
  87. socklen_t sa_len = sizeof (server_address);
  88. xgetsockname (server_socket, (struct sockaddr *) &server_address,
  89. &sa_len);
  90. TEST_VERIFY (sa_len == sizeof (server_address));
  91. }
  92. xlisten (server_socket, 5);
  93. for (int do_nonblock = 0; do_nonblock < 2; ++do_nonblock)
  94. for (int do_cloexec = 0; do_cloexec < 2; ++do_cloexec)
  95. {
  96. int sockflags = 0;
  97. if (do_nonblock)
  98. sockflags |= SOCK_NONBLOCK;
  99. if (do_cloexec)
  100. sockflags |= SOCK_CLOEXEC;
  101. struct client client = client_connect (&server_address);
  102. struct sockaddr_in client_address;
  103. socklen_t sa_len = sizeof (client_address);
  104. int client_socket = xaccept4 (server_socket,
  105. (struct sockaddr *) &client_address,
  106. &sa_len, sockflags);
  107. TEST_VERIFY (sa_len == sizeof (client_address));
  108. TEST_VERIFY (is_nonblocking (client_socket) == do_nonblock);
  109. TEST_VERIFY (is_cloexec (client_socket) == do_cloexec);
  110. check_same_address (&client.address, &client_address);
  111. xclose (client_socket);
  112. xclose (client.socket);
  113. }
  114. xclose (server_socket);
  115. return 0;
  116. }
  117. #include <support/test-driver.c>