test_srcfd.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
  3. This file is part of libzmq, the ZeroMQ core engine in C++.
  4. libzmq is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License (LGPL) as published
  6. by the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. As a special exception, the Contributors give you permission to link
  9. this library with independent modules to produce an executable,
  10. regardless of the license terms of these independent modules, and to
  11. copy and distribute the resulting executable under terms of your choice,
  12. provided that you also meet, for each linked independent module, the
  13. terms and conditions of the license of that module. An independent
  14. module is a module which is not derived from or based on this library.
  15. If you modify this library, you must extend this exception to your
  16. version of the library.
  17. libzmq is distributed in the hope that it will be useful, but WITHOUT
  18. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  19. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  20. License for more details.
  21. You should have received a copy of the GNU Lesser General Public License
  22. along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. */
  24. #include "testutil.hpp"
  25. #include "testutil_unity.hpp"
  26. #include <string.h>
  27. SETUP_TEARDOWN_TESTCONTEXT
  28. #define MSG_SIZE 20
  29. #ifdef _WIN32
  30. #include <winsock2.h>
  31. #include <ws2tcpip.h>
  32. #else
  33. #include <sys/types.h>
  34. #include <sys/socket.h>
  35. #include <netdb.h>
  36. #endif
  37. void test_srcfd ()
  38. {
  39. char my_endpoint[MAX_SOCKET_STRING];
  40. // Create the infrastructure
  41. void *rep = test_context_socket (ZMQ_REP);
  42. void *req = test_context_socket (ZMQ_REQ);
  43. bind_loopback_ipv4 (rep, my_endpoint, sizeof (my_endpoint));
  44. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (req, my_endpoint));
  45. char tmp[MSG_SIZE];
  46. memset (tmp, 0, MSG_SIZE);
  47. zmq_send (req, tmp, MSG_SIZE, 0);
  48. zmq_msg_t msg;
  49. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_init (&msg));
  50. zmq_recvmsg (rep, &msg, 0);
  51. TEST_ASSERT_EQUAL_UINT (MSG_SIZE, zmq_msg_size (&msg));
  52. // get the messages source file descriptor
  53. int src_fd = zmq_msg_get (&msg, ZMQ_SRCFD);
  54. TEST_ASSERT_GREATER_OR_EQUAL (0, src_fd);
  55. TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg));
  56. // get the remote endpoint
  57. struct sockaddr_storage ss;
  58. #ifdef ZMQ_HAVE_HPUX
  59. int addrlen = sizeof ss;
  60. #else
  61. socklen_t addrlen = sizeof ss;
  62. #endif
  63. TEST_ASSERT_SUCCESS_RAW_ERRNO (
  64. getpeername (src_fd, (struct sockaddr *) &ss, &addrlen));
  65. char host[NI_MAXHOST];
  66. TEST_ASSERT_SUCCESS_RAW_ERRNO (getnameinfo ((struct sockaddr *) &ss,
  67. addrlen, host, sizeof host,
  68. NULL, 0, NI_NUMERICHOST));
  69. // assert it is localhost which connected
  70. TEST_ASSERT_EQUAL_STRING ("127.0.0.1", host);
  71. test_context_socket_close (rep);
  72. test_context_socket_close (req);
  73. // sleep a bit for the socket to be freed
  74. msleep (SETTLE_TIME);
  75. // getting name from closed socket will fail
  76. #ifdef ZMQ_HAVE_WINDOWS
  77. const int expected_errno = WSAENOTSOCK;
  78. #else
  79. const int expected_errno = EBADF;
  80. #endif
  81. TEST_ASSERT_FAILURE_RAW_ERRNO (
  82. expected_errno, getpeername (src_fd, (struct sockaddr *) &ss, &addrlen));
  83. }
  84. int main ()
  85. {
  86. setup_test_environment ();
  87. UNITY_BEGIN ();
  88. RUN_TEST (test_srcfd);
  89. return UNITY_END ();
  90. }