test_hello_msg.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. Copyright (c) 2007-2020 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. void test (const char *address)
  28. {
  29. // Create a router
  30. void *router = test_context_socket (ZMQ_ROUTER);
  31. char my_endpoint[MAX_SOCKET_STRING];
  32. // set router socket options
  33. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_HELLO_MSG, "H", 1));
  34. // bind router
  35. test_bind (router, address, my_endpoint, MAX_SOCKET_STRING);
  36. // Create a dealer
  37. void *dealer = test_context_socket (ZMQ_DEALER);
  38. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
  39. // Receive the hello message
  40. recv_string_expect_success (dealer, "H", 0);
  41. // Clean up.
  42. test_context_socket_close (dealer);
  43. test_context_socket_close (router);
  44. }
  45. void test_tcp ()
  46. {
  47. test ("tcp://127.0.0.1:*");
  48. }
  49. void test_inproc ()
  50. {
  51. test ("inproc://hello-msg");
  52. }
  53. void test_inproc_late_bind ()
  54. {
  55. char address[] = "inproc://late-hello-msg";
  56. // Create a server
  57. void *server = test_context_socket (ZMQ_SERVER);
  58. // set server socket options
  59. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (server, ZMQ_HELLO_MSG, "W", 1));
  60. // Create a dealer
  61. void *client = test_context_socket (ZMQ_CLIENT);
  62. TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (client, ZMQ_HELLO_MSG, "H", 1));
  63. TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, address));
  64. // bind server after the dealer
  65. TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, address));
  66. // Receive the welcome message from server
  67. recv_string_expect_success (client, "W", 0);
  68. // Receive the hello message from client
  69. recv_string_expect_success (server, "H", 0);
  70. // Clean up.
  71. test_context_socket_close (client);
  72. test_context_socket_close (server);
  73. }
  74. int main ()
  75. {
  76. setup_test_environment ();
  77. UNITY_BEGIN ();
  78. RUN_TEST (test_tcp);
  79. RUN_TEST (test_inproc);
  80. RUN_TEST (test_inproc_late_bind);
  81. return UNITY_END ();
  82. }