1 /*
2     Copyright (c) 2007-2020 Contributors as noted in the AUTHORS file
3 
4     This file is part of libzmq, the ZeroMQ core engine in C++.
5 
6     libzmq is free software; you can redistribute it and/or modify it under
7     the terms of the GNU Lesser General Public License (LGPL) as published
8     by the Free Software Foundation; either version 3 of the License, or
9     (at your option) any later version.
10 
11     As a special exception, the Contributors give you permission to link
12     this library with independent modules to produce an executable,
13     regardless of the license terms of these independent modules, and to
14     copy and distribute the resulting executable under terms of your choice,
15     provided that you also meet, for each linked independent module, the
16     terms and conditions of the license of that module. An independent
17     module is a module which is not derived from or based on this library.
18     If you modify this library, you must extend this exception to your
19     version of the library.
20 
21     libzmq is distributed in the hope that it will be useful, but WITHOUT
22     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23     FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
24     License for more details.
25 
26     You should have received a copy of the GNU Lesser General Public License
27     along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 */
29 
30 #include "testutil.hpp"
31 #include "testutil_unity.hpp"
32 
33 SETUP_TEARDOWN_TESTCONTEXT
34 
test(const char * address)35 void test (const char *address)
36 {
37     //  Create a router
38     void *router = test_context_socket (ZMQ_ROUTER);
39     char my_endpoint[MAX_SOCKET_STRING];
40 
41     //  set router socket options
42     TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (router, ZMQ_HELLO_MSG, "H", 1));
43 
44     //  bind router
45     test_bind (router, address, my_endpoint, MAX_SOCKET_STRING);
46 
47     //  Create a dealer
48     void *dealer = test_context_socket (ZMQ_DEALER);
49     TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (dealer, my_endpoint));
50 
51     // Receive the hello message
52     recv_string_expect_success (dealer, "H", 0);
53 
54     //  Clean up.
55     test_context_socket_close (dealer);
56     test_context_socket_close (router);
57 }
58 
test_tcp()59 void test_tcp ()
60 {
61     test ("tcp://127.0.0.1:*");
62 }
63 
test_inproc()64 void test_inproc ()
65 {
66     test ("inproc://hello-msg");
67 }
68 
test_inproc_late_bind()69 void test_inproc_late_bind ()
70 {
71     char address[] = "inproc://late-hello-msg";
72 
73     //  Create a server
74     void *server = test_context_socket (ZMQ_SERVER);
75 
76     //  set server socket options
77     TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (server, ZMQ_HELLO_MSG, "W", 1));
78 
79     //  Create a dealer
80     void *client = test_context_socket (ZMQ_CLIENT);
81     TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (client, ZMQ_HELLO_MSG, "H", 1));
82     TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, address));
83 
84     //  bind server after the dealer
85     TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (server, address));
86 
87     // Receive the welcome message from server
88     recv_string_expect_success (client, "W", 0);
89 
90     // Receive the hello message from client
91     recv_string_expect_success (server, "H", 0);
92 
93     //  Clean up.
94     test_context_socket_close (client);
95     test_context_socket_close (server);
96 }
97 
main()98 int main ()
99 {
100     setup_test_environment ();
101 
102     UNITY_BEGIN ();
103     RUN_TEST (test_tcp);
104     RUN_TEST (test_inproc);
105     RUN_TEST (test_inproc_late_bind);
106     return UNITY_END ();
107 }
108