1/*
2Copyright (c) 2018 Contributors as noted in the AUTHORS file
3
4This file is part of 0MQ.
5
60MQ is free software; you can redistribute it and/or modify it under
7the terms of the GNU Lesser General Public License as published by
8the Free Software Foundation; either version 3 of the License, or
9(at your option) any later version.
10
110MQ is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU Lesser General Public License for more details.
15
16You should have received a copy of the GNU Lesser General Public License
17along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef __UNITTEST_RESOLVER_COMMON_INCLUDED__
21#define __UNITTEST_RESOLVER_COMMON_INCLUDED__
22
23#include <ip_resolver.hpp>
24#include <string.h>
25
26// Attempt a resolution and test the results.
27//
28// On windows we can receive an IPv4 address even when an IPv6 is requested, if
29// we're in this situation then we compare to 'expected_addr_v4_failover_'
30// instead.
31void validate_address (int family,
32 const zmq::ip_addr_t *addr_,
33 const char *expected_addr_,
34 uint16_t expected_port_ = 0,
35 uint16_t expected_zone_ = 0,
36 const char *expected_addr_v4_failover_ = NULL)
37{
38#if defined ZMQ_HAVE_WINDOWS
39 if (family == AF_INET6 && expected_addr_v4_failover_ != NULL
40 && addr_->family () == AF_INET) {
41 // We've requested an IPv6 but the system gave us an IPv4, use the
42 // failover address
43 family = AF_INET;
44 expected_addr_ = expected_addr_v4_failover_;
45 }
46#else
47 (void) expected_addr_v4_failover_;
48#endif
49
50 TEST_ASSERT_EQUAL (family, addr_->family ());
51
52 if (family == AF_INET6) {
53 struct in6_addr expected_addr;
54 const sockaddr_in6 *ip6_addr = &addr_->ipv6;
55
56 TEST_ASSERT_EQUAL (
57 1, test_inet_pton (AF_INET6, expected_addr_, &expected_addr));
58
59 int neq = memcmp (&ip6_addr->sin6_addr, &expected_addr,
60 sizeof (expected_addr_));
61
62 TEST_ASSERT_EQUAL (0, neq);
63 TEST_ASSERT_EQUAL (htons (expected_port_), ip6_addr->sin6_port);
64 TEST_ASSERT_EQUAL (expected_zone_, ip6_addr->sin6_scope_id);
65 } else {
66 struct in_addr expected_addr;
67 const sockaddr_in *ip4_addr = &addr_->ipv4;
68
69 TEST_ASSERT_EQUAL (
70 1, test_inet_pton (AF_INET, expected_addr_, &expected_addr));
71
72 TEST_ASSERT_EQUAL (expected_addr.s_addr, ip4_addr->sin_addr.s_addr);
73 TEST_ASSERT_EQUAL (htons (expected_port_), ip4_addr->sin_port);
74 }
75}
76
77#endif // __UNITTEST_RESOLVER_COMMON_INCLUDED__
78