PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/test_term_endpoint.cpp

http://github.com/zeromq/libzmq
C++ | 183 lines | 104 code | 31 blank | 48 comment | 5 complexity | 66c55a9af2f5abee757e875b1765bac6 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0
  1. /*
  2. Copyright (c) 2007-2016 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. SETUP_TEARDOWN_TESTCONTEXT
  27. /* Use the worst case filename size for the buffer (+1 for trailing NUL), this
  28. * is larger than MAX_SOCKET_STRING, which is not large enough for IPC */
  29. #define BUF_SIZE (FILENAME_MAX + 1)
  30. const char *ep_wc_tcp = "tcp://127.0.0.1:*";
  31. #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
  32. const char *ep_wc_ipc = "ipc://*";
  33. #endif
  34. #if defined ZMQ_HAVE_VMCI
  35. const char *ep_wc_vmci = "vmci://*:*";
  36. #endif
  37. void test_send_after_unbind_fails ()
  38. {
  39. char my_endpoint[BUF_SIZE];
  40. // Create infrastructure.
  41. void *push = test_context_socket (ZMQ_PUSH);
  42. bind_loopback_ipv4 (push, my_endpoint, BUF_SIZE);
  43. void *pull = test_context_socket (ZMQ_PULL);
  44. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (pull, my_endpoint));
  45. // Pass one message through to ensure the connection is established
  46. send_string_expect_success (push, "ABC", 0);
  47. recv_string_expect_success (pull, "ABC", 0);
  48. // Unbind the listening endpoint
  49. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (push, my_endpoint));
  50. // Allow unbind to settle
  51. msleep (SETTLE_TIME);
  52. // Check that sending would block (there's no outbound connection)
  53. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (push, "ABC", 3, ZMQ_DONTWAIT));
  54. // Clean up
  55. test_context_socket_close (pull);
  56. test_context_socket_close (push);
  57. }
  58. void test_send_after_disconnect_fails ()
  59. {
  60. // Create infrastructure
  61. void *pull = test_context_socket (ZMQ_PULL);
  62. char my_endpoint[BUF_SIZE];
  63. bind_loopback_ipv4 (pull, my_endpoint, BUF_SIZE);
  64. void *push = test_context_socket (ZMQ_PUSH);
  65. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (push, my_endpoint));
  66. // Pass one message through to ensure the connection is established.
  67. send_string_expect_success (push, "ABC", 0);
  68. recv_string_expect_success (pull, "ABC", 0);
  69. // Disconnect the bound endpoint
  70. TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (push, my_endpoint));
  71. // Allow disconnect to settle
  72. msleep (SETTLE_TIME);
  73. // Check that sending would block (there's no inbound connections).
  74. TEST_ASSERT_FAILURE_ERRNO (EAGAIN, zmq_send (push, "ABC", 3, ZMQ_DONTWAIT));
  75. // Clean up
  76. test_context_socket_close (pull);
  77. test_context_socket_close (push);
  78. }
  79. void test_unbind_via_last_endpoint ()
  80. {
  81. // Create infrastructure (wild-card binding)
  82. void *push = test_context_socket (ZMQ_PUSH);
  83. char my_endpoint[BUF_SIZE];
  84. bind_loopback_ipv4 (push, my_endpoint, BUF_SIZE);
  85. void *pull = test_context_socket (ZMQ_PULL);
  86. #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
  87. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pull, ep_wc_ipc));
  88. #endif
  89. #if defined ZMQ_HAVE_VMCI
  90. void *req = test_context_socket (ZMQ_REQ);
  91. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, ep_wc_vmci));
  92. #endif
  93. // Unbind sockets binded by wild-card address
  94. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (push, my_endpoint));
  95. size_t buf_size = 0;
  96. (void) buf_size;
  97. #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
  98. buf_size = sizeof (my_endpoint);
  99. TEST_ASSERT_SUCCESS_ERRNO (
  100. zmq_getsockopt (pull, ZMQ_LAST_ENDPOINT, my_endpoint, &buf_size));
  101. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (pull, my_endpoint));
  102. #endif
  103. #if defined ZMQ_HAVE_VMCI
  104. buf_size = sizeof (my_endpoint);
  105. TEST_ASSERT_SUCCESS_ERRNO (
  106. zmq_getsockopt (req, ZMQ_LAST_ENDPOINT, my_endpoint, &buf_size));
  107. TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (req, my_endpoint));
  108. #endif
  109. // Clean up
  110. test_context_socket_close (pull);
  111. test_context_socket_close (push);
  112. }
  113. void test_wildcard_unbind_fails ()
  114. {
  115. // Create infrastructure (wild-card binding)
  116. void *push = test_context_socket (ZMQ_PUSH);
  117. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (push, ep_wc_tcp));
  118. void *pull = test_context_socket (ZMQ_PULL);
  119. #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
  120. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (pull, ep_wc_ipc));
  121. #endif
  122. #if defined ZMQ_HAVE_VMCI
  123. void *req = test_context_socket (ZMQ_REQ);
  124. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (req, ep_wc_vmci));
  125. #endif
  126. // Sockets binded by wild-card address can't be unbinded by wild-card address
  127. TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (push, ep_wc_tcp));
  128. #if !defined ZMQ_HAVE_WINDOWS && !defined ZMQ_HAVE_OPENVMS
  129. TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (pull, ep_wc_ipc));
  130. #endif
  131. #if defined ZMQ_HAVE_VMCI
  132. TEST_ASSERT_FAILURE_ERRNO (ENOENT, zmq_unbind (req, ep_wc_vmci));
  133. #endif
  134. // Clean up
  135. test_context_socket_close (pull);
  136. test_context_socket_close (push);
  137. }
  138. int main ()
  139. {
  140. setup_test_environment ();
  141. UNITY_BEGIN ();
  142. RUN_TEST (test_send_after_unbind_fails);
  143. RUN_TEST (test_send_after_disconnect_fails);
  144. RUN_TEST (test_unbind_via_last_endpoint);
  145. RUN_TEST (test_wildcard_unbind_fails);
  146. return UNITY_END ();
  147. }