blob: d042fbde05ece02844e9b8d9b3526c9bbfd873d4 [file] [log] [blame]
Eric Orthd39d0672021-11-16 21:17:581// 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>
8#include <string>
9#include <utility>
10#include <vector>
11
12#include "net/base/connection_endpoint_metadata.h"
13#include "net/base/ip_endpoint.h"
14#include "net/dns/host_resolver_results.h"
15#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18namespace net {
19
20namespace {
21
22class EndpointResultMatcher
23 : public testing::MatcherInterface<const HostResolverEndpointResult&> {
24 public:
25 EndpointResultMatcher(
26 testing::Matcher<std::vector<IPEndPoint>> ipv4_endpoints_matcher,
27 testing::Matcher<std::string> ipv4_alias_name_matcher,
28 testing::Matcher<std::vector<IPEndPoint>> ipv6_endpoints_matcher,
29 testing::Matcher<std::string> ipv6_alias_name_matcher,
30 testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher)
31 : ipv4_endpoints_matcher_(std::move(ipv4_endpoints_matcher)),
32 ipv4_alias_name_matcher_(std::move(ipv4_alias_name_matcher)),
33 ipv6_endpoints_matcher_(std::move(ipv6_endpoints_matcher)),
34 ipv6_alias_name_matcher_(std::move(ipv6_alias_name_matcher)),
35 metadata_matcher_(std::move(metadata_matcher)) {}
36
37 ~EndpointResultMatcher() override = default;
38
39 EndpointResultMatcher(const EndpointResultMatcher&) = default;
40 EndpointResultMatcher& operator=(const EndpointResultMatcher&) = default;
41 EndpointResultMatcher(EndpointResultMatcher&&) = default;
42 EndpointResultMatcher& operator=(EndpointResultMatcher&&) = default;
43
44 bool MatchAndExplain(
45 const HostResolverEndpointResult& endpoint,
46 testing::MatchResultListener* result_listener) const override {
47 return ExplainMatchResult(
48 testing::Field("ipv4_endpoints",
49 &HostResolverEndpointResult::ipv4_endpoints,
50 ipv4_endpoints_matcher_),
51 endpoint, result_listener) &&
52 ExplainMatchResult(
53 testing::Field("ipv4_alias_name",
54 &HostResolverEndpointResult::ipv4_alias_name,
55 ipv4_alias_name_matcher_),
56 endpoint, result_listener) &&
57 ExplainMatchResult(
58 testing::Field("ipv6_endpoints",
59 &HostResolverEndpointResult::ipv6_endpoints,
60 ipv6_endpoints_matcher_),
61 endpoint, result_listener) &&
62 ExplainMatchResult(
63 testing::Field("ipv6_alias_name",
64 &HostResolverEndpointResult::ipv6_alias_name,
65 ipv6_alias_name_matcher_),
66 endpoint, result_listener) &&
67 ExplainMatchResult(
68 testing::Field("metadata", &HostResolverEndpointResult::metadata,
69 metadata_matcher_),
70 endpoint, result_listener);
71 }
72
73 void DescribeTo(std::ostream* os) const override {
74 *os << "matches ";
75 Describe(*os);
76 }
77
78 void DescribeNegationTo(std::ostream* os) const override {
79 *os << "does not match ";
80 Describe(*os);
81 }
82
83 private:
84 void Describe(std::ostream& os) const {
85 os << "HostResolverEndpointResult {\nipv4_endpoints: "
86 << testing::PrintToString(ipv4_endpoints_matcher_)
87 << "\nipv4_alias_name: "
88 << testing::PrintToString(ipv4_alias_name_matcher_)
89 << "\nipv6_endpoints: "
90 << testing::PrintToString(ipv6_endpoints_matcher_)
91 << "\nipv6_alias_name: "
92 << testing::PrintToString(ipv6_alias_name_matcher_)
93 << "\nmetadata: " << testing::PrintToString(metadata_matcher_) << "\n}";
94 }
95
96 testing::Matcher<std::vector<IPEndPoint>> ipv4_endpoints_matcher_;
97 testing::Matcher<std::string> ipv4_alias_name_matcher_;
98 testing::Matcher<std::vector<IPEndPoint>> ipv6_endpoints_matcher_;
99 testing::Matcher<std::string> ipv6_alias_name_matcher_;
100 testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher_;
101};
102
103} // namespace
104
105testing::Matcher<const HostResolverEndpointResult&> ExpectEndpointResult(
106 testing::Matcher<std::vector<IPEndPoint>> ipv4_endpoints_matcher,
107 testing::Matcher<std::string> ipv4_alias_name_matcher,
108 testing::Matcher<std::vector<IPEndPoint>> ipv6_endpoints_matcher,
109 testing::Matcher<std::string> ipv6_alias_name_matcher,
110 testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher) {
111 return testing::MakeMatcher(new EndpointResultMatcher(
112 std::move(ipv4_endpoints_matcher), std::move(ipv4_alias_name_matcher),
113 std::move(ipv6_endpoints_matcher), std::move(ipv6_alias_name_matcher),
114 std::move(metadata_matcher)));
115}
116
117std::ostream& operator<<(std::ostream& os,
118 const HostResolverEndpointResult& endpoint_result) {
119 return os << "HostResolverEndpointResult {\nipv4_endpoints: "
120 << testing::PrintToString(endpoint_result.ipv4_endpoints)
121 << "\nipv4_alias_name: " << endpoint_result.ipv4_alias_name
122 << "\nipv6_endpoints: "
123 << testing::PrintToString(endpoint_result.ipv6_endpoints)
124 << "\nipv6_alias_name: " << endpoint_result.ipv6_alias_name
125 << "\nmetadata: "
126 << testing::PrintToString(endpoint_result.metadata) << "\n}";
127}
128
129} // namespace net