1/*
2 Copyright (c) 2007-2018 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#include <unity.h>
33
34#include <string.h>
35
36void setUp ()
37{
38}
39void tearDown ()
40{
41}
42
43void test_app_meta_reqrep ()
44{
45 void *ctx;
46 zmq_msg_t msg;
47 void *rep_sock, *req_sock;
48 char connect_address[MAX_SOCKET_STRING];
49 const char *req_hello = "X-hello:hello";
50 const char *req_connection = "X-connection:primary";
51 const char *req_z85 = "X-bin:009c6";
52 const char *rep_hello = "X-hello:world";
53 const char *rep_connection = "X-connection:backup";
54 const char *bad_strings[] = {
55 ":",
56 "key:",
57 ":value",
58 "keyvalue",
59 "",
60 "X-"
61 "KeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKe"
62 "yTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyT"
63 "ooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyTooLongKeyToo"
64 "LongKeyTooLongKeyTooLongKeyTooLongKeyTooLong:value"};
65
66 ctx = zmq_ctx_new ();
67 rep_sock = zmq_socket (ctx, ZMQ_REP);
68 TEST_ASSERT_NOT_NULL (rep_sock);
69 req_sock = zmq_socket (ctx, ZMQ_REQ);
70 TEST_ASSERT_NOT_NULL (req_sock);
71
72 int rc =
73 zmq_setsockopt (rep_sock, ZMQ_METADATA, rep_hello, strlen (rep_hello));
74 TEST_ASSERT_EQUAL_INT (0, rc);
75
76 int l = 0;
77 rc = zmq_setsockopt (rep_sock, ZMQ_LINGER, &l, sizeof (l));
78 TEST_ASSERT_EQUAL_INT (0, rc);
79
80 rc = zmq_setsockopt (rep_sock, ZMQ_METADATA, rep_connection,
81 strlen (rep_connection));
82 TEST_ASSERT_EQUAL_INT (0, rc);
83
84 for (int i = 0; i < 6; i++) {
85 rc = zmq_setsockopt (rep_sock, ZMQ_METADATA, bad_strings[i],
86 strlen (bad_strings[i]));
87 TEST_ASSERT_EQUAL_INT (-1, rc);
88 }
89
90 bind_loopback_ipv4 (rep_sock, connect_address, sizeof connect_address);
91
92 l = 0;
93 rc = zmq_setsockopt (req_sock, ZMQ_LINGER, &l, sizeof (l));
94 TEST_ASSERT_EQUAL_INT (0, rc);
95
96 rc = zmq_setsockopt (req_sock, ZMQ_METADATA, req_hello, strlen (req_hello));
97 TEST_ASSERT_EQUAL_INT (0, rc);
98
99 rc = zmq_setsockopt (req_sock, ZMQ_METADATA, req_connection,
100 strlen (req_connection));
101 TEST_ASSERT_EQUAL_INT (0, rc);
102
103 rc = zmq_setsockopt (req_sock, ZMQ_METADATA, req_z85, strlen (req_z85));
104 TEST_ASSERT_EQUAL_INT (0, rc);
105
106 rc = zmq_connect (req_sock, connect_address);
107 TEST_ASSERT_EQUAL_INT (0, rc);
108
109 rc = zmq_msg_init_size (&msg, 1);
110 TEST_ASSERT_EQUAL_INT (0, rc);
111
112 char *data = static_cast<char *> (zmq_msg_data (&msg));
113 data[0] = 1;
114
115 rc = zmq_msg_send (&msg, req_sock, 0);
116 TEST_ASSERT_EQUAL_INT (1, rc);
117
118 rc = zmq_msg_init (&msg);
119 TEST_ASSERT_EQUAL_INT (0, rc);
120
121 rc = zmq_msg_recv (&msg, rep_sock, 0);
122 TEST_ASSERT_EQUAL_INT (1, rc);
123
124 TEST_ASSERT_EQUAL_STRING ("hello", zmq_msg_gets (&msg, "X-hello"));
125 TEST_ASSERT_EQUAL_STRING ("primary", zmq_msg_gets (&msg, "X-connection"));
126 char *bindata = const_cast<char *> (zmq_msg_gets (&msg, "X-bin"));
127 TEST_ASSERT_NOT_NULL (bindata);
128 uint8_t rawdata[4];
129 void *ret = zmq_z85_decode (rawdata, bindata);
130 TEST_ASSERT_NOT_NULL (ret);
131 TEST_ASSERT_EQUAL_UINT8 (0, rawdata[0]);
132 TEST_ASSERT_EQUAL_UINT8 (1, rawdata[1]);
133 TEST_ASSERT_EQUAL_UINT8 (2, rawdata[2]);
134 TEST_ASSERT_EQUAL_UINT8 (3, rawdata[3]);
135
136 TEST_ASSERT_NULL (zmq_msg_gets (&msg, "X-foobar"));
137 TEST_ASSERT_NULL (zmq_msg_gets (&msg, "foobar"));
138
139 rc = zmq_msg_send (&msg, rep_sock, 0);
140 TEST_ASSERT_EQUAL_INT (1, rc);
141
142 rc = zmq_msg_recv (&msg, req_sock, 0);
143 TEST_ASSERT_EQUAL_INT (1, rc);
144
145 TEST_ASSERT_EQUAL_STRING ("world", zmq_msg_gets (&msg, "X-hello"));
146 TEST_ASSERT_EQUAL_STRING ("backup", zmq_msg_gets (&msg, "X-connection"));
147
148 rc = zmq_msg_close (&msg);
149 TEST_ASSERT_EQUAL_INT (0, rc);
150
151 rc = zmq_close (req_sock);
152 TEST_ASSERT_EQUAL_INT (0, rc);
153
154 rc = zmq_close (rep_sock);
155 TEST_ASSERT_EQUAL_INT (0, rc);
156
157 zmq_ctx_term (ctx);
158}
159
160
161int main ()
162{
163 setup_test_environment ();
164
165 UNITY_BEGIN ();
166 RUN_TEST (test_app_meta_reqrep);
167
168 return UNITY_END ();
169}
170