Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 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 | |
| 5 | #include "net/dns/host_resolver_results_test_util.h" |
| 6 | |
| 7 | #include <ostream> |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 8 | #include <utility> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include "net/base/connection_endpoint_metadata.h" |
| 12 | #include "net/base/ip_endpoint.h" |
| 13 | #include "net/dns/host_resolver_results.h" |
| 14 | #include "testing/gmock/include/gmock/gmock.h" |
| 15 | #include "testing/gtest/include/gtest/gtest.h" |
| 16 | |
| 17 | namespace net { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | class EndpointResultMatcher |
| 22 | : public testing::MatcherInterface<const HostResolverEndpointResult&> { |
| 23 | public: |
| 24 | EndpointResultMatcher( |
Eric Orth | 92c0771 | 2021-12-02 17:54:58 | [diff] [blame] | 25 | testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher, |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 26 | testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher) |
Eric Orth | 92c0771 | 2021-12-02 17:54:58 | [diff] [blame] | 27 | : ip_endpoints_matcher_(std::move(ip_endpoints_matcher)), |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 28 | metadata_matcher_(std::move(metadata_matcher)) {} |
| 29 | |
| 30 | ~EndpointResultMatcher() override = default; |
| 31 | |
| 32 | EndpointResultMatcher(const EndpointResultMatcher&) = default; |
| 33 | EndpointResultMatcher& operator=(const EndpointResultMatcher&) = default; |
| 34 | EndpointResultMatcher(EndpointResultMatcher&&) = default; |
| 35 | EndpointResultMatcher& operator=(EndpointResultMatcher&&) = default; |
| 36 | |
| 37 | bool MatchAndExplain( |
| 38 | const HostResolverEndpointResult& endpoint, |
| 39 | testing::MatchResultListener* result_listener) const override { |
| 40 | return ExplainMatchResult( |
Eric Orth | 92c0771 | 2021-12-02 17:54:58 | [diff] [blame] | 41 | testing::Field("ip_endpoints", |
| 42 | &HostResolverEndpointResult::ip_endpoints, |
| 43 | ip_endpoints_matcher_), |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 44 | endpoint, result_listener) && |
| 45 | ExplainMatchResult( |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 46 | testing::Field("metadata", &HostResolverEndpointResult::metadata, |
| 47 | metadata_matcher_), |
| 48 | endpoint, result_listener); |
| 49 | } |
| 50 | |
| 51 | void DescribeTo(std::ostream* os) const override { |
| 52 | *os << "matches "; |
| 53 | Describe(*os); |
| 54 | } |
| 55 | |
| 56 | void DescribeNegationTo(std::ostream* os) const override { |
| 57 | *os << "does not match "; |
| 58 | Describe(*os); |
| 59 | } |
| 60 | |
| 61 | private: |
| 62 | void Describe(std::ostream& os) const { |
Eric Orth | 92c0771 | 2021-12-02 17:54:58 | [diff] [blame] | 63 | os << "HostResolverEndpointResult {\nip_endpoints: " |
Eric Orth | fc87d10 | 2021-12-23 20:31:27 | [diff] [blame] | 64 | << testing::PrintToString(ip_endpoints_matcher_) |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 65 | << "\nmetadata: " << testing::PrintToString(metadata_matcher_) << "\n}"; |
| 66 | } |
| 67 | |
Eric Orth | 92c0771 | 2021-12-02 17:54:58 | [diff] [blame] | 68 | testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher_; |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 69 | testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher_; |
| 70 | }; |
| 71 | |
| 72 | } // namespace |
| 73 | |
| 74 | testing::Matcher<const HostResolverEndpointResult&> ExpectEndpointResult( |
Eric Orth | 92c0771 | 2021-12-02 17:54:58 | [diff] [blame] | 75 | testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher, |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 76 | testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher) { |
| 77 | return testing::MakeMatcher(new EndpointResultMatcher( |
Eric Orth | fc87d10 | 2021-12-23 20:31:27 | [diff] [blame] | 78 | std::move(ip_endpoints_matcher), std::move(metadata_matcher))); |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | std::ostream& operator<<(std::ostream& os, |
| 82 | const HostResolverEndpointResult& endpoint_result) { |
Eric Orth | 92c0771 | 2021-12-02 17:54:58 | [diff] [blame] | 83 | return os << "HostResolverEndpointResult {\nip_endpoints: " |
| 84 | << testing::PrintToString(endpoint_result.ip_endpoints) |
Eric Orth | d39d067 | 2021-11-16 21:17:58 | [diff] [blame] | 85 | << "\nmetadata: " |
| 86 | << testing::PrintToString(endpoint_result.metadata) << "\n}"; |
| 87 | } |
| 88 | |
| 89 | } // namespace net |