unittest_resolver_common.hpp 2.7 KB

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