1/*
2 Copyright (c) 2007-2017 Contributors as noted in the AUTHORS file
3
4 This file is part of 0MQ.
5
6 0MQ is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 0MQ is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include "testutil.hpp"
21#include "testutil_unity.hpp"
22
23#include <string.h>
24
25SETUP_TEARDOWN_TESTCONTEXT
26
27void test_address_wildcard_ipv4 ()
28{
29 /* Address wildcard, IPv6 disabled */
30 void *sb = test_context_socket (ZMQ_REP);
31 void *sc = test_context_socket (ZMQ_REQ);
32
33 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "tcp://*:*"));
34
35 char bind_endpoint[256];
36 char connect_endpoint[256];
37 size_t endpoint_len = sizeof (bind_endpoint);
38 TEST_ASSERT_SUCCESS_ERRNO (
39 zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, bind_endpoint, &endpoint_len));
40
41 // Apparently Windows can't connect to 0.0.0.0. A better fix would be welcome.
42#ifdef ZMQ_HAVE_WINDOWS
43 sprintf (connect_endpoint, "tcp://127.0.0.1:%s",
44 strrchr (bind_endpoint, ':') + 1);
45#else
46 strcpy (connect_endpoint, bind_endpoint);
47#endif
48
49 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_endpoint));
50
51 bounce (sb, sc);
52
53 TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, connect_endpoint));
54 TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, bind_endpoint));
55
56 test_context_socket_close (sc);
57 test_context_socket_close (sb);
58}
59
60void test_address_wildcard_ipv6 ()
61{
62 int ipv6 = is_ipv6_available ();
63
64 /* Address wildcard, IPv6 enabled */
65 void *sb = test_context_socket (ZMQ_REP);
66 void *sc = test_context_socket (ZMQ_REQ);
67
68 TEST_ASSERT_SUCCESS_ERRNO (
69 zmq_setsockopt (sb, ZMQ_IPV6, &ipv6, sizeof (int)));
70 TEST_ASSERT_SUCCESS_ERRNO (
71 zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int)));
72
73 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "tcp://*:*"));
74
75 char bind_endpoint[256];
76 char connect_endpoint[256];
77 size_t endpoint_len = sizeof (bind_endpoint);
78 memset (bind_endpoint, 0, endpoint_len);
79 TEST_ASSERT_SUCCESS_ERRNO (
80 zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, bind_endpoint, &endpoint_len));
81
82#ifdef ZMQ_HAVE_WINDOWS
83 if (ipv6)
84 sprintf (connect_endpoint, "tcp://[::1]:%s",
85 strrchr (bind_endpoint, ':') + 1);
86 else
87 sprintf (connect_endpoint, "tcp://127.0.0.1:%s",
88 strrchr (bind_endpoint, ':') + 1);
89#else
90 strcpy (connect_endpoint, bind_endpoint);
91#endif
92
93 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, connect_endpoint));
94
95 bounce (sb, sc);
96
97 TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, connect_endpoint));
98 TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, bind_endpoint));
99
100 test_context_socket_close (sc);
101 test_context_socket_close (sb);
102}
103
104void test_port_wildcard_ipv4_address ()
105{
106 /* Port wildcard, IPv4 address, IPv6 disabled */
107 void *sb = test_context_socket (ZMQ_REP);
108 void *sc = test_context_socket (ZMQ_REQ);
109
110 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "tcp://127.0.0.1:*"));
111
112 char endpoint[256];
113 size_t endpoint_len = sizeof (endpoint);
114 memset (endpoint, 0, endpoint_len);
115 TEST_ASSERT_SUCCESS_ERRNO (
116 zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &endpoint_len));
117
118 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, endpoint));
119
120 bounce (sb, sc);
121
122 TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, endpoint));
123 TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, endpoint));
124
125 test_context_socket_close (sc);
126 test_context_socket_close (sb);
127}
128
129void test_port_wildcard_ipv4_address_ipv6 ()
130{
131 /* Port wildcard, IPv4 address, IPv6 enabled */
132 void *sb = test_context_socket (ZMQ_REP);
133 void *sc = test_context_socket (ZMQ_REQ);
134
135 const int ipv6 = is_ipv6_available ();
136 TEST_ASSERT_SUCCESS_ERRNO (
137 zmq_setsockopt (sb, ZMQ_IPV6, &ipv6, sizeof (int)));
138 TEST_ASSERT_SUCCESS_ERRNO (
139 zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int)));
140
141 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "tcp://127.0.0.1:*"));
142
143 char endpoint[256];
144 size_t endpoint_len = sizeof (endpoint);
145 memset (endpoint, 0, endpoint_len);
146 TEST_ASSERT_SUCCESS_ERRNO (
147 zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &endpoint_len));
148
149 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, endpoint));
150
151 bounce (sb, sc);
152
153 TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, endpoint));
154 TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, endpoint));
155
156 test_context_socket_close (sc);
157 test_context_socket_close (sb);
158}
159
160void test_port_wildcard_ipv6_address ()
161{
162 const int ipv6 = is_ipv6_available ();
163 if (!ipv6)
164 TEST_IGNORE_MESSAGE ("ipv6 is not available");
165
166 /* Port wildcard, IPv6 address, IPv6 enabled */
167 void *sb = test_context_socket (ZMQ_REP);
168 void *sc = test_context_socket (ZMQ_REQ);
169
170 TEST_ASSERT_SUCCESS_ERRNO (
171 zmq_setsockopt (sb, ZMQ_IPV6, &ipv6, sizeof (int)));
172 TEST_ASSERT_SUCCESS_ERRNO (
173 zmq_setsockopt (sc, ZMQ_IPV6, &ipv6, sizeof (int)));
174
175 TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "tcp://[::1]:*"));
176
177 char endpoint[256];
178 size_t endpoint_len = sizeof (endpoint);
179 memset (endpoint, 0, endpoint_len);
180 TEST_ASSERT_SUCCESS_ERRNO (
181 zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, endpoint, &endpoint_len));
182
183 TEST_ASSERT_SUCCESS_ERRNO (zmq_connect (sc, endpoint));
184
185 bounce (sb, sc);
186
187 TEST_ASSERT_SUCCESS_ERRNO (zmq_disconnect (sc, endpoint));
188 TEST_ASSERT_SUCCESS_ERRNO (zmq_unbind (sb, endpoint));
189
190 test_context_socket_close (sc);
191 test_context_socket_close (sb);
192}
193
194int main ()
195{
196 setup_test_environment ();
197
198 UNITY_BEGIN ();
199 RUN_TEST (test_address_wildcard_ipv4);
200 RUN_TEST (test_address_wildcard_ipv6);
201 RUN_TEST (test_port_wildcard_ipv4_address);
202 RUN_TEST (test_port_wildcard_ipv4_address_ipv6);
203 RUN_TEST (test_port_wildcard_ipv6_address);
204 return UNITY_END ();
205}
206