1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
/* SPDX-License-Identifier: MPL-2.0 */
#include <unity.h>
#include "../tests/testutil.hpp"
#include "../unittests/unittest_resolver_common.hpp"
#include <ip.hpp>
#include <udp_address.hpp>
void setUp ()
{
}
void tearDown ()
{
}
// Test an UDP address resolution. If 'dest_addr_' is NULL assume the
// resolution is supposed to fail.
static void test_resolve (bool bind_,
int family_,
const char *name_,
const char *target_addr_,
uint16_t expected_port_,
const char *bind_addr_,
bool multicast_)
{
if (family_ == AF_INET6 && !is_ipv6_available ()) {
TEST_IGNORE_MESSAGE ("ipv6 is not available");
}
zmq::udp_address_t addr;
int rc = addr.resolve (name_, bind_, family_ == AF_INET6);
if (target_addr_ == NULL) {
TEST_ASSERT_EQUAL (-1, rc);
TEST_ASSERT_EQUAL (EINVAL, errno);
return;
}
TEST_ASSERT_EQUAL (0, rc);
TEST_ASSERT_EQUAL (multicast_, addr.is_mcast ());
if (bind_addr_ == NULL) {
// Bind ANY
if (family_ == AF_INET) {
bind_addr_ = "0.0.0.0";
} else {
bind_addr_ = "::";
}
}
validate_address (family_, addr.target_addr (), target_addr_,
expected_port_);
validate_address (family_, addr.bind_addr (), bind_addr_, expected_port_);
}
static void test_resolve_bind (int family_,
const char *name_,
const char *dest_addr_,
uint16_t expected_port_ = 0,
const char *bind_addr_ = NULL,
bool multicast_ = false)
{
test_resolve (true, family_, name_, dest_addr_, expected_port_, bind_addr_,
multicast_);
}
static void test_resolve_connect (int family_,
const char *name_,
const char *dest_addr_,
uint16_t expected_port_ = 0,
const char *bind_addr_ = NULL,
bool multicast_ = false)
{
test_resolve (false, family_, name_, dest_addr_, expected_port_, bind_addr_,
multicast_);
}
static void test_resolve_ipv4_simple ()
{
test_resolve_connect (AF_INET, "127.0.0.1:5555", "127.0.0.1", 5555);
}
static void test_resolve_ipv6_simple ()
{
test_resolve_connect (AF_INET6, "[::1]:123", "::1", 123);
}
static void test_resolve_ipv4_bind ()
{
test_resolve_bind (AF_INET, "127.0.0.1:5555", "127.0.0.1", 5555,
"127.0.0.1");
}
static void test_resolve_ipv6_bind ()
{
test_resolve_bind (AF_INET6, "[abcd::1234:1]:5555", "abcd::1234:1", 5555,
"abcd::1234:1");
}
static void test_resolve_ipv4_bind_any ()
{
test_resolve_bind (AF_INET, "*:*", "0.0.0.0", 0, "0.0.0.0");
}
static void test_resolve_ipv6_bind_any ()
{
test_resolve_bind (AF_INET6, "*:*", "::", 0, "::");
}
static void test_resolve_ipv4_bind_anyport ()
{
test_resolve_bind (AF_INET, "127.0.0.1:*", "127.0.0.1", 0, "127.0.0.1");
}
static void test_resolve_ipv6_bind_anyport ()
{
test_resolve_bind (AF_INET6, "[1:2:3:4::5]:*", "1:2:3:4::5", 0,
"1:2:3:4::5");
}
static void test_resolve_ipv4_bind_any_port ()
{
test_resolve_bind (AF_INET, "*:5555", "0.0.0.0", 5555, "0.0.0.0");
}
static void test_resolve_ipv6_bind_any_port ()
{
test_resolve_bind (AF_INET6, "*:5555", "::", 5555, "::");
}
static void test_resolve_ipv4_connect_any ()
{
// Cannot use wildcard for connection
test_resolve_connect (AF_INET, "*:5555", NULL);
}
static void test_resolve_ipv6_connect_any ()
{
// Cannot use wildcard for connection
test_resolve_connect (AF_INET6, "*:5555", NULL);
}
static void test_resolve_ipv4_connect_anyport ()
{
test_resolve_connect (AF_INET, "127.0.0.1:*", NULL);
}
static void test_resolve_ipv6_connect_anyport ()
{
test_resolve_connect (AF_INET6, "[::1]:*", NULL);
}
static void test_resolve_ipv4_connect_port0 ()
{
test_resolve_connect (AF_INET, "127.0.0.1:0", "127.0.0.1", 0);
}
static void test_resolve_ipv6_connect_port0 ()
{
test_resolve_connect (AF_INET6, "[2000:abcd::1]:0", "2000:abcd::1", 0);
}
static void test_resolve_ipv4_bind_mcast ()
{
test_resolve_bind (AF_INET, "239.0.0.1:1234", "239.0.0.1", 1234, "0.0.0.0",
true);
}
static void test_resolve_ipv6_bind_mcast ()
{
test_resolve_bind (AF_INET6, "[ff00::1]:1234", "ff00::1", 1234, "::", true);
}
static void test_resolve_ipv4_connect_mcast ()
{
test_resolve_connect (AF_INET, "239.0.0.1:2222", "239.0.0.1", 2222, NULL,
true);
}
static void test_resolve_ipv6_connect_mcast ()
{
test_resolve_connect (AF_INET6, "[ff00::1]:2222", "ff00::1", 2222, NULL,
true);
}
static void test_resolve_ipv4_mcast_src_bind ()
{
test_resolve_bind (AF_INET, "127.0.0.1;230.2.8.12:5555", "230.2.8.12", 5555,
"127.0.0.1", true);
}
static void test_resolve_ipv6_mcast_src_bind ()
{
if (!is_ipv6_available ()) {
TEST_IGNORE_MESSAGE ("ipv6 is not available");
}
zmq::udp_address_t addr;
int rc = addr.resolve ("[::1];[ffab::4]:5555", true, true);
// For the time being this fails because we only support binding multicast
// by interface name, not interface IP
TEST_ASSERT_EQUAL (-1, rc);
TEST_ASSERT_EQUAL (ENODEV, errno);
}
static void test_resolve_ipv4_mcast_src_bind_any ()
{
test_resolve_bind (AF_INET, "*;230.2.8.12:5555", "230.2.8.12", 5555,
"0.0.0.0", true);
}
static void test_resolve_ipv6_mcast_src_bind_any ()
{
test_resolve_bind (AF_INET6, "*;[ffff::]:5555", "ffff::", 5555, "::", true);
}
static void test_resolve_ipv4_mcast_src_connect ()
{
test_resolve_connect (AF_INET, "8.9.10.11;230.2.8.12:5555", "230.2.8.12",
5555, "8.9.10.11", true);
}
static void test_resolve_ipv6_mcast_src_connect ()
{
if (!is_ipv6_available ()) {
TEST_IGNORE_MESSAGE ("ipv6 is not available");
}
zmq::udp_address_t addr;
int rc = addr.resolve ("[1:2:3::4];[ff01::1]:5555", false, true);
// For the time being this fails because we only support binding multicast
// by interface name, not interface IP
TEST_ASSERT_EQUAL (-1, rc);
TEST_ASSERT_EQUAL (ENODEV, errno);
}
static void test_resolve_ipv4_mcast_src_connect_any ()
{
test_resolve_connect (AF_INET, "*;230.2.8.12:5555", "230.2.8.12", 5555,
"0.0.0.0", true);
}
static void test_resolve_ipv6_mcast_src_connect_any ()
{
test_resolve_connect (AF_INET6, "*;[ff10::1]:5555", "ff10::1", 5555,
"::", true);
}
static void test_resolve_ipv4_mcast_src_bind_bad ()
{
test_resolve_bind (AF_INET, "127.0.0.1;1.2.3.4:5555", NULL);
}
static void test_resolve_ipv6_mcast_src_bind_bad ()
{
test_resolve_bind (AF_INET6, "[::1];[fe00::1]:5555", NULL);
}
static void test_resolve_ipv4_mcast_src_connect_bad ()
{
test_resolve_connect (AF_INET, "127.0.0.1;1.2.3.4:5555", NULL);
}
static void test_resolve_ipv6_mcast_src_connect_bad ()
{
test_resolve_connect (AF_INET6, "[::1];[fe00:1]:5555", NULL);
}
int main (void)
{
zmq::initialize_network ();
setup_test_environment ();
UNITY_BEGIN ();
RUN_TEST (test_resolve_ipv4_simple);
RUN_TEST (test_resolve_ipv6_simple);
RUN_TEST (test_resolve_ipv4_bind);
RUN_TEST (test_resolve_ipv6_bind);
RUN_TEST (test_resolve_ipv4_bind_any);
RUN_TEST (test_resolve_ipv6_bind_any);
RUN_TEST (test_resolve_ipv4_bind_anyport);
RUN_TEST (test_resolve_ipv6_bind_anyport);
RUN_TEST (test_resolve_ipv4_bind_any_port);
RUN_TEST (test_resolve_ipv6_bind_any_port);
RUN_TEST (test_resolve_ipv4_connect_any);
RUN_TEST (test_resolve_ipv6_connect_any);
RUN_TEST (test_resolve_ipv4_connect_anyport);
RUN_TEST (test_resolve_ipv6_connect_anyport);
RUN_TEST (test_resolve_ipv4_connect_port0);
RUN_TEST (test_resolve_ipv6_connect_port0);
RUN_TEST (test_resolve_ipv4_bind_mcast);
RUN_TEST (test_resolve_ipv6_bind_mcast);
RUN_TEST (test_resolve_ipv4_connect_mcast);
RUN_TEST (test_resolve_ipv6_connect_mcast);
RUN_TEST (test_resolve_ipv4_mcast_src_bind);
RUN_TEST (test_resolve_ipv6_mcast_src_bind);
RUN_TEST (test_resolve_ipv4_mcast_src_bind_any);
RUN_TEST (test_resolve_ipv6_mcast_src_bind_any);
RUN_TEST (test_resolve_ipv4_mcast_src_connect);
RUN_TEST (test_resolve_ipv6_mcast_src_connect);
RUN_TEST (test_resolve_ipv4_mcast_src_connect_any);
RUN_TEST (test_resolve_ipv6_mcast_src_connect_any);
RUN_TEST (test_resolve_ipv4_mcast_src_bind_bad);
RUN_TEST (test_resolve_ipv6_mcast_src_bind_bad);
RUN_TEST (test_resolve_ipv4_mcast_src_connect_bad);
RUN_TEST (test_resolve_ipv6_mcast_src_connect_bad);
zmq::shutdown_network ();
return UNITY_END ();
}
|