blob: b779ec3572f566346de35d252d9e0651caf92d55 [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>
Eric Orthd39d0672021-11-16 21:17:588#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
17namespace net {
18
19namespace {
20
21class EndpointResultMatcher
22 : public testing::MatcherInterface<const HostResolverEndpointResult&> {
23 public:
24 EndpointResultMatcher(
Eric Orth92c07712021-12-02 17:54:5825 testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,
Eric Orthd39d0672021-11-16 21:17:5826 testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher)
Eric Orth92c07712021-12-02 17:54:5827 : ip_endpoints_matcher_(std::move(ip_endpoints_matcher)),
Eric Orthd39d0672021-11-16 21:17:5828 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 Orth92c07712021-12-02 17:54:5841 testing::Field("ip_endpoints",
42 &HostResolverEndpointResult::ip_endpoints,
43 ip_endpoints_matcher_),
Eric Orthd39d0672021-11-16 21:17:5844 endpoint, result_listener) &&
45 ExplainMatchResult(
Eric Orthd39d0672021-11-16 21:17:5846 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 Orth92c07712021-12-02 17:54:5863 os << "HostResolverEndpointResult {\nip_endpoints: "
Eric Orthfc87d102021-12-23 20:31:2764 << testing::PrintToString(ip_endpoints_matcher_)
Eric Orthd39d0672021-11-16 21:17:5865 << "\nmetadata: " << testing::PrintToString(metadata_matcher_) << "\n}";
66 }
67
Eric Orth92c07712021-12-02 17:54:5868 testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher_;
Eric Orthd39d0672021-11-16 21:17:5869 testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher_;
70};
71
72} // namespace
73
74testing::Matcher<const HostResolverEndpointResult&> ExpectEndpointResult(
Eric Orth92c07712021-12-02 17:54:5875 testing::Matcher<std::vector<IPEndPoint>> ip_endpoints_matcher,
Eric Orthd39d0672021-11-16 21:17:5876 testing::Matcher<const ConnectionEndpointMetadata&> metadata_matcher) {
77 return testing::MakeMatcher(new EndpointResultMatcher(
Eric Orthfc87d102021-12-23 20:31:2778 std::move(ip_endpoints_matcher), std::move(metadata_matcher)));
Eric Orthd39d0672021-11-16 21:17:5879}
80
81std::ostream& operator<<(std::ostream& os,
82 const HostResolverEndpointResult& endpoint_result) {
Eric Orth92c07712021-12-02 17:54:5883 return os << "HostResolverEndpointResult {\nip_endpoints: "
84 << testing::PrintToString(endpoint_result.ip_endpoints)
Eric Orthd39d0672021-11-16 21:17:5885 << "\nmetadata: "
86 << testing::PrintToString(endpoint_result.metadata) << "\n}";
87}
88
89} // namespace net