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 } // namespace
73
ExpectEndpointResult(testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,testing::Matcher<const ConnectionEndpointMetadata &> metadata_matcher)74 testing::Matcher<const HostResolverEndpointResult&> ExpectEndpointResult(
75 testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,
76 testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher) {
77 return testing::MakeMatcher(new EndpointResultMatcher(
78 std::move(ip_endpoints_matcher), std::move(metadata_matcher)));
79 }
80
operator <<(std::ostream & os,const HostResolverEndpointResult & endpoint_result)81 std::ostream& operator<<(std::ostream& os,
82 const HostResolverEndpointResult& endpoint_result) {
83 return os << "HostResolverEndpointResult {\nip_endpoints: "
84 << testing::PrintToString(endpoint_result.ip_endpoints)
85 << "\nmetadata: "
86 << testing::PrintToString(endpoint_result.metadata) << "\n}";
87 }
88
89 } // namespace net
90