host_resolver_results_test_util.cc 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "net/dns/host_resolver_results_test_util.h"
  5. #include <ostream>
  6. #include <utility>
  7. #include <vector>
  8. #include "net/base/connection_endpoint_metadata.h"
  9. #include "net/base/ip_endpoint.h"
  10. #include "net/dns/host_resolver_results.h"
  11. #include "testing/gmock/include/gmock/gmock.h"
  12. #include "testing/gtest/include/gtest/gtest.h"
  13. namespace net {
  14. namespace {
  15. class EndpointResultMatcher
  16. : public testing::MatcherInterface<const HostResolverEndpointResult&> {
  17. public:
  18. EndpointResultMatcher(
  19. testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,
  20. testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher)
  21. : ip_endpoints_matcher_(std::move(ip_endpoints_matcher)),
  22. metadata_matcher_(std::move(metadata_matcher)) {}
  23. ~EndpointResultMatcher() override = default;
  24. EndpointResultMatcher(const EndpointResultMatcher&) = default;
  25. EndpointResultMatcher& operator=(const EndpointResultMatcher&) = default;
  26. EndpointResultMatcher(EndpointResultMatcher&&) = default;
  27. EndpointResultMatcher& operator=(EndpointResultMatcher&&) = default;
  28. bool MatchAndExplain(
  29. const HostResolverEndpointResult& endpoint,
  30. testing::MatchResultListener* result_listener) const override {
  31. return ExplainMatchResult(
  32. testing::Field("ip_endpoints",
  33. &HostResolverEndpointResult::ip_endpoints,
  34. ip_endpoints_matcher_),
  35. endpoint, result_listener) &&
  36. ExplainMatchResult(
  37. testing::Field("metadata", &HostResolverEndpointResult::metadata,
  38. metadata_matcher_),
  39. endpoint, result_listener);
  40. }
  41. void DescribeTo(std::ostream* os) const override {
  42. *os << "matches ";
  43. Describe(*os);
  44. }
  45. void DescribeNegationTo(std::ostream* os) const override {
  46. *os << "does not match ";
  47. Describe(*os);
  48. }
  49. private:
  50. void Describe(std::ostream& os) const {
  51. os << "HostResolverEndpointResult {\nip_endpoints: "
  52. << testing::PrintToString(ip_endpoints_matcher_)
  53. << "\nmetadata: " << testing::PrintToString(metadata_matcher_) << "\n}";
  54. }
  55. testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher_;
  56. testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher_;
  57. };
  58. } // namespace
  59. testing::Matcher<const HostResolverEndpointResult&> ExpectEndpointResult(
  60. testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,
  61. testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher) {
  62. return testing::MakeMatcher(new EndpointResultMatcher(
  63. std::move(ip_endpoints_matcher), std::move(metadata_matcher)));
  64. }
  65. std::ostream& operator<<(std::ostream& os,
  66. const HostResolverEndpointResult& endpoint_result) {
  67. return os << "HostResolverEndpointResult {\nip_endpoints: "
  68. << testing::PrintToString(endpoint_result.ip_endpoints)
  69. << "\nmetadata: "
  70. << testing::PrintToString(endpoint_result.metadata) << "\n}";
  71. }
  72. } // namespace net