xref: /aosp_15_r20/external/cronet/net/dns/host_resolver_results_test_util.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2021 The Chromium Authors
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>
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/public/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:
EndpointResultMatcher(testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,testing::Matcher<const ConnectionEndpointMetadata &> metadata_matcher)24   EndpointResultMatcher(
25       testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,
26       testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher)
27       : ip_endpoints_matcher_(std::move(ip_endpoints_matcher)),
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 
MatchAndExplain(const HostResolverEndpointResult & endpoint,testing::MatchResultListener * result_listener) const37   bool MatchAndExplain(
38       const HostResolverEndpointResult& endpoint,
39       testing::MatchResultListener* result_listener) const override {
40     return ExplainMatchResult(
41                testing::Field("ip_endpoints",
42                               &HostResolverEndpointResult::ip_endpoints,
43                               ip_endpoints_matcher_),
44                endpoint, result_listener) &&
45            ExplainMatchResult(
46                testing::Field("metadata", &HostResolverEndpointResult::metadata,
47                               metadata_matcher_),
48                endpoint, result_listener);
49   }
50 
DescribeTo(std::ostream * os) const51   void DescribeTo(std::ostream* os) const override {
52     *os << "matches ";
53     Describe(*os);
54   }
55 
DescribeNegationTo(std::ostream * os) const56   void DescribeNegationTo(std::ostream* os) const override {
57     *os << "does not match ";
58     Describe(*os);
59   }
60 
61  private:
Describe(std::ostream & os) const62   void Describe(std::ostream& os) const {
63     os << "HostResolverEndpointResult {\nip_endpoints: "
64        << testing::PrintToString(ip_endpoints_matcher_)
65        << "\nmetadata: " << testing::PrintToString(metadata_matcher_) << "\n}";
66   }
67 
68   testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher_;
69   testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher_;
70 };
71 
72 class ServiceEndpointMatcher
73     : public testing::MatcherInterface<const ServiceEndpoint&> {
74  public:
ServiceEndpointMatcher(testing::Matcher<std::vector<IPEndPoint>> ipv4_endpoints_matcher,testing::Matcher<std::vector<IPEndPoint>> ipv6_endpoints_matcher,testing::Matcher<const ConnectionEndpointMetadata &> metadata_matcher)75   ServiceEndpointMatcher(
76       testing::Matcher<std::vector<IPEndPoint>> ipv4_endpoints_matcher,
77       testing::Matcher<std::vector<IPEndPoint>> ipv6_endpoints_matcher,
78       testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher)
79       : ipv4_endpoints_matcher_(std::move(ipv4_endpoints_matcher)),
80         ipv6_endpoints_matcher_(std::move(ipv6_endpoints_matcher)),
81         metadata_matcher_(std::move(metadata_matcher)) {}
82 
83   ~ServiceEndpointMatcher() override = default;
84 
85   ServiceEndpointMatcher(const ServiceEndpointMatcher&) = default;
86   ServiceEndpointMatcher& operator=(const ServiceEndpointMatcher&) = default;
87   ServiceEndpointMatcher(ServiceEndpointMatcher&&) = default;
88   ServiceEndpointMatcher& operator=(ServiceEndpointMatcher&&) = default;
89 
MatchAndExplain(const ServiceEndpoint & endpoint,testing::MatchResultListener * result_listener) const90   bool MatchAndExplain(
91       const ServiceEndpoint& endpoint,
92       testing::MatchResultListener* result_listener) const override {
93     return ExplainMatchResult(testing::Field("ipv4_endpoints",
94                                              &ServiceEndpoint::ipv4_endpoints,
95                                              ipv4_endpoints_matcher_),
96                               endpoint, result_listener) &&
97            ExplainMatchResult(testing::Field("ipv6_endpoints",
98                                              &ServiceEndpoint::ipv6_endpoints,
99                                              ipv6_endpoints_matcher_),
100                               endpoint, result_listener) &&
101            ExplainMatchResult(
102                testing::Field("metadata", &ServiceEndpoint::metadata,
103                               metadata_matcher_),
104                endpoint, result_listener);
105   }
106 
DescribeTo(std::ostream * os) const107   void DescribeTo(std::ostream* os) const override {
108     *os << "matches ";
109     Describe(*os);
110   }
111 
DescribeNegationTo(std::ostream * os) const112   void DescribeNegationTo(std::ostream* os) const override {
113     *os << "does not match ";
114     Describe(*os);
115   }
116 
117  private:
Describe(std::ostream & os) const118   void Describe(std::ostream& os) const {
119     os << "ServiceEndpoint {\nipv4_endpoints: "
120        << testing::PrintToString(ipv4_endpoints_matcher_)
121        << "\npv6_endpoints: " << testing::PrintToString(ipv6_endpoints_matcher_)
122        << "\nmetadata: " << testing::PrintToString(metadata_matcher_) << "\n}";
123   }
124 
125   testing::Matcher<std::vector<IPEndPoint>> ipv4_endpoints_matcher_;
126   testing::Matcher<std::vector<IPEndPoint>> ipv6_endpoints_matcher_;
127   testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher_;
128 };
129 
130 }  // namespace
131 
ExpectEndpointResult(testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,testing::Matcher<const ConnectionEndpointMetadata &> metadata_matcher)132 testing::Matcher<const HostResolverEndpointResult&> ExpectEndpointResult(
133     testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,
134     testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher) {
135   return testing::MakeMatcher(new EndpointResultMatcher(
136       std::move(ip_endpoints_matcher), std::move(metadata_matcher)));
137 }
138 
ExpectServiceEndpoint(testing::Matcher<std::vector<IPEndPoint>> ipv4_endpoints_matcher,testing::Matcher<std::vector<IPEndPoint>> ipv6_endpoints_matcher,testing::Matcher<const ConnectionEndpointMetadata &> metadata_matcher)139 testing::Matcher<const ServiceEndpoint&> ExpectServiceEndpoint(
140     testing::Matcher<std::vector<IPEndPoint>> ipv4_endpoints_matcher,
141     testing::Matcher<std::vector<IPEndPoint>> ipv6_endpoints_matcher,
142     testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher) {
143   return testing::MakeMatcher(new ServiceEndpointMatcher(
144       std::move(ipv4_endpoints_matcher), std::move(ipv6_endpoints_matcher),
145       std::move(metadata_matcher)));
146 }
147 
operator <<(std::ostream & os,const HostResolverEndpointResult & endpoint_result)148 std::ostream& operator<<(std::ostream& os,
149                          const HostResolverEndpointResult& endpoint_result) {
150   return os << "HostResolverEndpointResult {\nip_endpoints: "
151             << testing::PrintToString(endpoint_result.ip_endpoints)
152             << "\nmetadata: "
153             << testing::PrintToString(endpoint_result.metadata) << "\n}";
154 }
155 
operator <<(std::ostream & os,const ServiceEndpoint & endpoint)156 std::ostream& operator<<(std::ostream& os, const ServiceEndpoint& endpoint) {
157   return os << "ServiceEndpoint {\nipv4_endpoints: "
158             << testing::PrintToString(endpoint.ipv4_endpoints)
159             << "\nipv6_endpoints: "
160             << testing::PrintToString(endpoint.ipv6_endpoints)
161             << "\nmetadata: " << testing::PrintToString(endpoint.metadata)
162             << "\n}";
163 }
164 
165 }  // namespace net
166