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_req_correlate () |
36 | { |
37 | void *req = test_context_socket (ZMQ_REQ); |
38 | void *router = test_context_socket (ZMQ_ROUTER); |
39 | |
40 | int enabled = 1; |
41 | TEST_ASSERT_SUCCESS_ERRNO ( |
42 | zmq_setsockopt (req, ZMQ_REQ_CORRELATE, &enabled, sizeof (int))); |
43 | |
44 | int rcvtimeo = 100; |
45 | TEST_ASSERT_SUCCESS_ERRNO ( |
46 | zmq_setsockopt (req, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int))); |
47 | |
48 | char my_endpoint[MAX_SOCKET_STRING]; |
49 | bind_loopback_ipv4 (router, my_endpoint, sizeof my_endpoint); |
50 | |
51 | TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (req, my_endpoint)); |
52 | |
53 | // Send a multi-part request. |
54 | s_send_seq (req, "ABC" , "DEF" , SEQ_END); |
55 | |
56 | zmq_msg_t msg; |
57 | zmq_msg_init (&msg); |
58 | |
59 | // Receive peer routing id |
60 | TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, router, 0)); |
61 | TEST_ASSERT_GREATER_THAN_INT (0, zmq_msg_size (&msg)); |
62 | zmq_msg_t peer_id_msg; |
63 | zmq_msg_init (&peer_id_msg); |
64 | zmq_msg_copy (&peer_id_msg, &msg); |
65 | |
66 | int more = 0; |
67 | size_t more_size = sizeof (more); |
68 | TEST_ASSERT_SUCCESS_ERRNO ( |
69 | zmq_getsockopt (router, ZMQ_RCVMORE, &more, &more_size)); |
70 | TEST_ASSERT_TRUE (more); |
71 | |
72 | // Receive request id 1 |
73 | TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_recv (&msg, router, 0)); |
74 | TEST_ASSERT_EQUAL_UINT (sizeof (uint32_t), zmq_msg_size (&msg)); |
75 | const uint32_t req_id = *static_cast<uint32_t *> (zmq_msg_data (&msg)); |
76 | zmq_msg_t req_id_msg; |
77 | zmq_msg_init (&req_id_msg); |
78 | zmq_msg_copy (&req_id_msg, &msg); |
79 | |
80 | more = 0; |
81 | more_size = sizeof (more); |
82 | TEST_ASSERT_SUCCESS_ERRNO ( |
83 | zmq_getsockopt (router, ZMQ_RCVMORE, &more, &more_size)); |
84 | TEST_ASSERT_TRUE (more); |
85 | |
86 | // Receive the rest. |
87 | s_recv_seq (router, 0, "ABC" , "DEF" , SEQ_END); |
88 | |
89 | uint32_t bad_req_id = req_id + 1; |
90 | |
91 | // Send back a bad reply: wrong req id, 0, data |
92 | zmq_msg_copy (&msg, &peer_id_msg); |
93 | TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&msg, router, ZMQ_SNDMORE)); |
94 | zmq_msg_init_data (&msg, &bad_req_id, sizeof (uint32_t), NULL, NULL); |
95 | TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&msg, router, ZMQ_SNDMORE)); |
96 | s_send_seq (router, 0, "DATA" , SEQ_END); |
97 | |
98 | // Send back a good reply: good req id, 0, data |
99 | zmq_msg_copy (&msg, &peer_id_msg); |
100 | TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&msg, router, ZMQ_SNDMORE)); |
101 | zmq_msg_copy (&msg, &req_id_msg); |
102 | TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_send (&msg, router, ZMQ_SNDMORE)); |
103 | s_send_seq (router, 0, "GHI" , SEQ_END); |
104 | |
105 | // Receive reply. If bad reply got through, we wouldn't see |
106 | // this particular data. |
107 | s_recv_seq (req, "GHI" , SEQ_END); |
108 | |
109 | TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&msg)); |
110 | TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&peer_id_msg)); |
111 | TEST_ASSERT_SUCCESS_ERRNO (zmq_msg_close (&req_id_msg)); |
112 | |
113 | test_context_socket_close_zero_linger (req); |
114 | test_context_socket_close_zero_linger (router); |
115 | } |
116 | |
117 | int main () |
118 | { |
119 | setup_test_environment (); |
120 | |
121 | UNITY_BEGIN (); |
122 | RUN_TEST (test_req_correlate); |
123 | return UNITY_END (); |
124 | } |
125 | |