unittest_resolver_common.hpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #ifndef __UNITTEST_RESOLVER_COMMON_INCLUDED__
4 #define __UNITTEST_RESOLVER_COMMON_INCLUDED__
5 
6 #include <ip_resolver.hpp>
7 #include <string.h>
8 
9 // Attempt a resolution and test the results.
10 //
11 // On windows we can receive an IPv4 address even when an IPv6 is requested, if
12 // we're in this situation then we compare to 'expected_addr_v4_failover_'
13 // instead.
14 void validate_address (int family,
15  const zmq::ip_addr_t *addr_,
16  const char *expected_addr_,
17  uint16_t expected_port_ = 0,
18  uint16_t expected_zone_ = 0,
19  const char *expected_addr_v4_failover_ = NULL)
20 {
21 #if defined ZMQ_HAVE_WINDOWS
22  if (family == AF_INET6 && expected_addr_v4_failover_ != NULL
23  && addr_->family () == AF_INET) {
24  // We've requested an IPv6 but the system gave us an IPv4, use the
25  // failover address
26  family = AF_INET;
27  expected_addr_ = expected_addr_v4_failover_;
28  }
29 #else
30  (void) expected_addr_v4_failover_;
31 #endif
32 
33  TEST_ASSERT_EQUAL (family, addr_->family ());
34 
35  if (family == AF_INET6) {
36  struct in6_addr expected_addr;
37  const sockaddr_in6 *ip6_addr = &addr_->ipv6;
38 
40  1, test_inet_pton (AF_INET6, expected_addr_, &expected_addr));
41 
42  int neq = memcmp (&ip6_addr->sin6_addr, &expected_addr,
43  sizeof (expected_addr_));
44 
45  TEST_ASSERT_EQUAL (0, neq);
46  TEST_ASSERT_EQUAL (htons (expected_port_), ip6_addr->sin6_port);
47  TEST_ASSERT_EQUAL (expected_zone_, ip6_addr->sin6_scope_id);
48  } else {
49  struct in_addr expected_addr;
50  const sockaddr_in *ip4_addr = &addr_->ipv4;
51 
53  1, test_inet_pton (AF_INET, expected_addr_, &expected_addr));
54 
55  TEST_ASSERT_EQUAL (expected_addr.s_addr, ip4_addr->sin_addr.s_addr);
56  TEST_ASSERT_EQUAL (htons (expected_port_), ip4_addr->sin_port);
57  }
58 }
59 
60 #endif // __UNITTEST_RESOLVER_COMMON_INCLUDED__
zmq::ip_addr_t::ipv6
sockaddr_in6 ipv6
Definition: ip_resolver.hpp:20
NULL
NULL
Definition: test_security_zap.cpp:405
validate_address
void validate_address(int family, const zmq::ip_addr_t *addr_, const char *expected_addr_, uint16_t expected_port_=0, uint16_t expected_zone_=0, const char *expected_addr_v4_failover_=NULL)
Definition: unittest_resolver_common.hpp:14
zmq::ip_addr_t::ipv4
sockaddr_in ipv4
Definition: ip_resolver.hpp:19
zmq::ip_addr_t
Definition: ip_resolver.hpp:16
TEST_ASSERT_EQUAL
#define TEST_ASSERT_EQUAL(expected, actual)
Definition: unity.h:133
void
typedef void(APIENTRY *GLDEBUGPROCARB)(GLenum source
zmq::ip_addr_t::family
int family() const
Definition: ip_resolver.cpp:23
test_inet_pton
int test_inet_pton(int af_, const char *src_, void *dst_)
Definition: testutil.cpp:303
ip_resolver.hpp


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00