test_pair_tcp.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. #if defined _WIN32
  28. #include "../src/windows.hpp"
  29. #endif
  30. SETUP_TEARDOWN_TESTCONTEXT
  31. typedef void (*extra_func_t) (void *socket_);
  32. #ifdef ZMQ_BUILD_DRAFT
  33. void set_sockopt_fastpath (void *socket)
  34. {
  35. int value = 1;
  36. int rc =
  37. zmq_setsockopt (socket, ZMQ_LOOPBACK_FASTPATH, &value, sizeof value);
  38. assert (rc == 0);
  39. }
  40. #endif
  41. void test_pair_tcp (extra_func_t extra_func_ = NULL)
  42. {
  43. void *sb = test_context_socket (ZMQ_PAIR);
  44. if (extra_func_)
  45. extra_func_ (sb);
  46. char my_endpoint[MAX_SOCKET_STRING];
  47. bind_loopback_ipv4 (sb, my_endpoint, sizeof my_endpoint);
  48. void *sc = test_context_socket (ZMQ_PAIR);
  49. if (extra_func_)
  50. extra_func_ (sc);
  51. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, my_endpoint));
  52. bounce (sb, sc);
  53. test_context_socket_close (sc);
  54. test_context_socket_close (sb);
  55. }
  56. void test_pair_tcp_regular ()
  57. {
  58. test_pair_tcp ();
  59. }
  60. void test_pair_tcp_connect_by_name ()
  61. {
  62. // all other tcp test cases bind to a loopback wildcard address, then
  63. // retrieve the bound endpoint, which is numerical, and use that to
  64. // connect. this test cases specifically uses "localhost" to connect
  65. // to ensure that names are correctly resolved
  66. void *sb = test_context_socket (ZMQ_PAIR);
  67. char bound_endpoint[MAX_SOCKET_STRING];
  68. bind_loopback_ipv4 (sb, bound_endpoint, sizeof bound_endpoint);
  69. // extract the bound port number
  70. const char *pos = strrchr (bound_endpoint, ':');
  71. TEST_ASSERT_NOT_NULL (pos);
  72. const char connect_endpoint_prefix[] = "tcp://localhost";
  73. char connect_endpoint[MAX_SOCKET_STRING];
  74. strcpy (connect_endpoint, connect_endpoint_prefix);
  75. strcat (connect_endpoint, pos);
  76. void *sc = test_context_socket (ZMQ_PAIR);
  77. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_endpoint));
  78. bounce (sb, sc);
  79. test_context_socket_close (sc);
  80. test_context_socket_close (sb);
  81. }
  82. #ifdef ZMQ_BUILD_DRAFT
  83. void test_pair_tcp_fastpath ()
  84. {
  85. test_pair_tcp (set_sockopt_fastpath);
  86. }
  87. #endif
  88. #ifdef _WIN32
  89. void test_io_completion_port ()
  90. {
  91. void *const s = test_context_socket (ZMQ_PAIR);
  92. SOCKET fd;
  93. size_t fd_size = sizeof fd;
  94. TEST_ASSERT_SUCCESS_ERRNO (zmq_getsockopt (s, ZMQ_FD, &fd, &fd_size));
  95. ::WSAPROTOCOL_INFO pi;
  96. TEST_ASSERT_SUCCESS_RAW_ERRNO (
  97. ::WSADuplicateSocket (fd, ::GetCurrentProcessId (), &pi));
  98. const SOCKET socket = ::WSASocket (pi.iAddressFamily /*AF_INET*/,
  99. pi.iSocketType /*SOCK_STREAM*/,
  100. pi.iProtocol /*IPPROTO_TCP*/, &pi, 0, 0);
  101. const HANDLE iocp =
  102. ::CreateIoCompletionPort (INVALID_HANDLE_VALUE, NULL, 0, 0);
  103. TEST_ASSERT_NOT_EQUAL (NULL, iocp);
  104. const HANDLE res =
  105. ::CreateIoCompletionPort (reinterpret_cast<HANDLE> (socket), iocp, 0, 0);
  106. TEST_ASSERT_NOT_EQUAL (NULL, res);
  107. TEST_ASSERT_SUCCESS_RAW_ERRNO (closesocket (socket));
  108. TEST_ASSERT_TRUE (CloseHandle (iocp));
  109. test_context_socket_close (s);
  110. }
  111. #endif
  112. int main ()
  113. {
  114. setup_test_environment ();
  115. UNITY_BEGIN ();
  116. RUN_TEST (test_pair_tcp_regular);
  117. RUN_TEST (test_pair_tcp_connect_by_name);
  118. #ifdef ZMQ_BUILD_DRAFT
  119. RUN_TEST (test_pair_tcp_fastpath);
  120. #endif
  121. #ifdef _WIN32
  122. RUN_TEST (test_io_completion_port);
  123. #endif
  124. return UNITY_END ();
  125. }