1 | /* |
2 | Copyright (c) 2007-2017 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 | |
35 | void test_probe_router_router () |
36 | { |
37 | // Create server and bind to endpoint |
38 | void *server = test_context_socket (ZMQ_ROUTER); |
39 | |
40 | char my_endpoint[MAX_SOCKET_STRING]; |
41 | bind_loopback_ipv4 (server, my_endpoint, sizeof (my_endpoint)); |
42 | |
43 | // Create client and connect to server, doing a probe |
44 | void *client = test_context_socket (ZMQ_ROUTER); |
45 | TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (client, ZMQ_ROUTING_ID, "X" , 1)); |
46 | int probe = 1; |
47 | TEST_ASSERT_SUCCESS_ERRNO ( |
48 | zmq_setsockopt (client, ZMQ_PROBE_ROUTER, &probe, sizeof (probe))); |
49 | TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint)); |
50 | |
51 | // We expect a routing id=X + empty message from client |
52 | recv_string_expect_success (server, "X" , 0); |
53 | unsigned char buffer[255]; |
54 | TEST_ASSERT_EQUAL_INT ( |
55 | 0, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, buffer, 255, 0))); |
56 | |
57 | // Send a message to client now |
58 | send_string_expect_success (server, "X" , ZMQ_SNDMORE); |
59 | send_string_expect_success (server, "Hello" , 0); |
60 | |
61 | // receive the routing ID, which is auto-generated in this case, since the |
62 | // peer did not set one explicitly |
63 | TEST_ASSERT_EQUAL_INT ( |
64 | 5, TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (client, buffer, 255, 0))); |
65 | |
66 | recv_string_expect_success (client, "Hello" , 0); |
67 | |
68 | test_context_socket_close (server); |
69 | test_context_socket_close (client); |
70 | } |
71 | |
72 | void test_probe_router_dealer () |
73 | { |
74 | // Create server and bind to endpoint |
75 | void *server = test_context_socket (ZMQ_ROUTER); |
76 | |
77 | char my_endpoint[MAX_SOCKET_STRING]; |
78 | bind_loopback_ipv4 (server, my_endpoint, sizeof (my_endpoint)); |
79 | |
80 | // Create client and connect to server, doing a probe |
81 | void *client = test_context_socket (ZMQ_DEALER); |
82 | TEST_ASSERT_SUCCESS_ERRNO (zmq_setsockopt (client, ZMQ_ROUTING_ID, "X" , 1)); |
83 | int probe = 1; |
84 | TEST_ASSERT_SUCCESS_ERRNO ( |
85 | zmq_setsockopt (client, ZMQ_PROBE_ROUTER, &probe, sizeof (probe))); |
86 | TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (client, my_endpoint)); |
87 | |
88 | // We expect a routing id=X + empty message from client |
89 | recv_string_expect_success (server, "X" , 0); |
90 | unsigned char buffer[255]; |
91 | TEST_ASSERT_SUCCESS_ERRNO (zmq_recv (server, buffer, 255, 0)); |
92 | |
93 | // Send a message to client now |
94 | send_string_expect_success (server, "X" , ZMQ_SNDMORE); |
95 | send_string_expect_success (server, "Hello" , 0); |
96 | |
97 | recv_string_expect_success (client, "Hello" , 0); |
98 | |
99 | test_context_socket_close (server); |
100 | test_context_socket_close (client); |
101 | } |
102 | |
103 | int main () |
104 | { |
105 | setup_test_environment (); |
106 | |
107 | UNITY_BEGIN (); |
108 | |
109 | RUN_TEST (test_probe_router_router); |
110 | RUN_TEST (test_probe_router_dealer); |
111 | |
112 | return UNITY_END (); |
113 | } |
114 | |