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/base/connection_endpoint_metadata_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 "testing/gmock/include/gmock/gmock.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace net {
17
18 using EchConfigList = ConnectionEndpointMetadata::EchConfigList;
19
20 namespace {
21
22 class EndpointMetadataMatcher
23 : public testing::MatcherInterface<const ConnectionEndpointMetadata&> {
24 public:
EndpointMetadataMatcher(testing::Matcher<std::vector<std::string>> supported_protocol_alpns_matcher,testing::Matcher<EchConfigList> ech_config_list_matcher,testing::Matcher<std::string> target_name_matcher)25 EndpointMetadataMatcher(
26 testing::Matcher<std::vector<std::string>>
27 supported_protocol_alpns_matcher,
28 testing::Matcher<EchConfigList> ech_config_list_matcher,
29 testing::Matcher<std::string> target_name_matcher)
30 : supported_protocol_alpns_matcher_(
31 std::move(supported_protocol_alpns_matcher)),
32 ech_config_list_matcher_(std::move(ech_config_list_matcher)),
33 target_name_matcher_(std::move(target_name_matcher)) {}
34
35 ~EndpointMetadataMatcher() override = default;
36
37 EndpointMetadataMatcher(const EndpointMetadataMatcher&) = default;
38 EndpointMetadataMatcher& operator=(const EndpointMetadataMatcher&) = default;
39 EndpointMetadataMatcher(EndpointMetadataMatcher&&) = default;
40 EndpointMetadataMatcher& operator=(EndpointMetadataMatcher&&) = default;
41
MatchAndExplain(const ConnectionEndpointMetadata & metadata,testing::MatchResultListener * result_listener) const42 bool MatchAndExplain(
43 const ConnectionEndpointMetadata& metadata,
44 testing::MatchResultListener* result_listener) const override {
45 return ExplainMatchResult(
46 testing::Field(
47 "supported_protocol_alpns",
48 &ConnectionEndpointMetadata::supported_protocol_alpns,
49 supported_protocol_alpns_matcher_),
50 metadata, result_listener) &&
51 ExplainMatchResult(
52 testing::Field("ech_config_list",
53 &ConnectionEndpointMetadata::ech_config_list,
54 ech_config_list_matcher_),
55 metadata, result_listener) &&
56 ExplainMatchResult(
57 testing::Field("target_name",
58 &ConnectionEndpointMetadata::target_name,
59 target_name_matcher_),
60 metadata, result_listener);
61 }
62
DescribeTo(std::ostream * os) const63 void DescribeTo(std::ostream* os) const override {
64 *os << "matches ";
65 Describe(*os);
66 }
67
DescribeNegationTo(std::ostream * os) const68 void DescribeNegationTo(std::ostream* os) const override {
69 *os << "does not match ";
70 Describe(*os);
71 }
72
73 private:
Describe(std::ostream & os) const74 void Describe(std::ostream& os) const {
75 os << "ConnectionEndpoint {\nsupported_protocol_alpns: "
76 << testing::PrintToString(supported_protocol_alpns_matcher_)
77 << "\nech_config_list: "
78 << testing::PrintToString(ech_config_list_matcher_)
79 << "\ntarget_name: " << testing::PrintToString(target_name_matcher_)
80 << "\n}";
81 }
82
83 testing::Matcher<std::vector<std::string>> supported_protocol_alpns_matcher_;
84 testing::Matcher<EchConfigList> ech_config_list_matcher_;
85 testing::Matcher<std::string> target_name_matcher_;
86 };
87
88 } // namespace
89
90 testing::Matcher<const ConnectionEndpointMetadata&>
ExpectConnectionEndpointMetadata(testing::Matcher<std::vector<std::string>> supported_protocol_alpns_matcher,testing::Matcher<EchConfigList> ech_config_list_matcher,testing::Matcher<std::string> target_name_matcher)91 ExpectConnectionEndpointMetadata(
92 testing::Matcher<std::vector<std::string>> supported_protocol_alpns_matcher,
93 testing::Matcher<EchConfigList> ech_config_list_matcher,
94 testing::Matcher<std::string> target_name_matcher) {
95 return testing::MakeMatcher(new EndpointMetadataMatcher(
96 std::move(supported_protocol_alpns_matcher),
97 std::move(ech_config_list_matcher), std::move(target_name_matcher)));
98 }
99
operator <<(std::ostream & os,const ConnectionEndpointMetadata & connection_endpoint_metadata)100 std::ostream& operator<<(
101 std::ostream& os,
102 const ConnectionEndpointMetadata& connection_endpoint_metadata) {
103 return os << "ConnectionEndpointMetadata {\nsupported_protocol_alpns: "
104 << testing::PrintToString(
105 connection_endpoint_metadata.supported_protocol_alpns)
106 << "\nech_config_list: "
107 << testing::PrintToString(
108 connection_endpoint_metadata.ech_config_list)
109 << "\ntarget_name: "
110 << testing::PrintToString(connection_endpoint_metadata.target_name)
111 << "\n}";
112 }
113
114 } // namespace net
115