1 // Copyright 2016 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 #ifndef NET_HTTP_HTTP_STREAM_FACTORY_TEST_UTIL_H_ 6 #define NET_HTTP_HTTP_STREAM_FACTORY_TEST_UTIL_H_ 7 8 #include <memory> 9 #include <vector> 10 11 #include "base/memory/ptr_util.h" 12 #include "base/memory/raw_ptr.h" 13 #include "net/http/http_stream.h" 14 #include "net/http/http_stream_factory.h" 15 #include "net/http/http_stream_factory_job.h" 16 #include "net/http/http_stream_factory_job_controller.h" 17 #include "net/http/http_stream_request.h" 18 #include "net/proxy_resolution/proxy_info.h" 19 #include "net/socket/next_proto.h" 20 #include "net/ssl/ssl_config.h" 21 #include "testing/gmock/include/gmock/gmock.h" 22 #include "url/scheme_host_port.h" 23 24 using testing::_; 25 using testing::Invoke; 26 27 namespace net { 28 29 class HttpStreamFactoryPeer { 30 public: AddJobController(HttpStreamFactory * factory,std::unique_ptr<HttpStreamFactory::JobController> job_controller)31 static void AddJobController( 32 HttpStreamFactory* factory, 33 std::unique_ptr<HttpStreamFactory::JobController> job_controller) { 34 factory->job_controller_set_.insert(std::move(job_controller)); 35 } 36 IsJobControllerDeleted(HttpStreamFactory * factory)37 static bool IsJobControllerDeleted(HttpStreamFactory* factory) { 38 return factory->job_controller_set_.empty(); 39 } 40 GetDefaultJobFactory(HttpStreamFactory * factory)41 static HttpStreamFactory::JobFactory* GetDefaultJobFactory( 42 HttpStreamFactory* factory) { 43 return factory->job_factory_.get(); 44 } 45 }; 46 47 // This delegate does nothing when called. 48 class MockHttpStreamRequestDelegate : public HttpStreamRequest::Delegate { 49 public: 50 MockHttpStreamRequestDelegate(); 51 52 MockHttpStreamRequestDelegate(const MockHttpStreamRequestDelegate&) = delete; 53 MockHttpStreamRequestDelegate& operator=( 54 const MockHttpStreamRequestDelegate&) = delete; 55 56 ~MockHttpStreamRequestDelegate() override; 57 58 // std::unique_ptr is not copyable and therefore cannot be mocked. 59 MOCK_METHOD2(OnStreamReadyImpl, 60 void(const ProxyInfo& used_proxy_info, HttpStream* stream)); 61 OnStreamReady(const ProxyInfo & used_proxy_info,std::unique_ptr<HttpStream> stream)62 void OnStreamReady(const ProxyInfo& used_proxy_info, 63 std::unique_ptr<HttpStream> stream) override { 64 OnStreamReadyImpl(used_proxy_info, stream.get()); 65 } 66 67 // std::unique_ptr is not copyable and therefore cannot be mocked. OnBidirectionalStreamImplReady(const ProxyInfo & used_proxy_info,std::unique_ptr<BidirectionalStreamImpl> stream)68 void OnBidirectionalStreamImplReady( 69 const ProxyInfo& used_proxy_info, 70 std::unique_ptr<BidirectionalStreamImpl> stream) override {} 71 72 // std::unique_ptr is not copyable and therefore cannot be mocked. OnWebSocketHandshakeStreamReady(const ProxyInfo & used_proxy_info,std::unique_ptr<WebSocketHandshakeStreamBase> stream)73 void OnWebSocketHandshakeStreamReady( 74 const ProxyInfo& used_proxy_info, 75 std::unique_ptr<WebSocketHandshakeStreamBase> stream) override {} 76 77 MOCK_METHOD4(OnStreamFailed, 78 void(int status, 79 const NetErrorDetails& net_error_details, 80 const ProxyInfo& used_proxy_info, 81 ResolveErrorInfo resolve_error_info)); 82 83 MOCK_METHOD2(OnCertificateError, void(int status, const SSLInfo& ssl_info)); 84 85 MOCK_METHOD3(OnNeedsProxyAuth, 86 void(const HttpResponseInfo& proxy_response, 87 const ProxyInfo& used_proxy_info, 88 HttpAuthController* auth_controller)); 89 90 MOCK_METHOD1(OnNeedsClientAuth, void(SSLCertRequestInfo* cert_info)); 91 92 MOCK_METHOD0(OnQuicBroken, void()); 93 94 // `switching_info` is not copyable and therefore cannot be mocked. 95 MOCK_METHOD1(OnSwitchesToHttpStreamPoolImpl, 96 void(HttpStreamPoolRequestInfo& request_info)); 97 OnSwitchesToHttpStreamPool(HttpStreamPoolRequestInfo request_info)98 void OnSwitchesToHttpStreamPool( 99 HttpStreamPoolRequestInfo request_info) override { 100 OnSwitchesToHttpStreamPoolImpl(request_info); 101 } 102 }; 103 104 class MockHttpStreamFactoryJob : public HttpStreamFactory::Job { 105 public: 106 MockHttpStreamFactoryJob( 107 HttpStreamFactory::Job::Delegate* delegate, 108 HttpStreamFactory::JobType job_type, 109 HttpNetworkSession* session, 110 const HttpStreamFactory::StreamRequestInfo& request_info, 111 RequestPriority priority, 112 ProxyInfo proxy_info, 113 const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs, 114 url::SchemeHostPort destination, 115 GURL origin_url, 116 NextProto alternative_protocol, 117 quic::ParsedQuicVersion quic_version, 118 bool is_websocket, 119 bool enable_ip_based_pooling, 120 NetLog* net_log); 121 122 ~MockHttpStreamFactoryJob() override; 123 124 MOCK_METHOD0(Resume, void()); 125 126 MOCK_METHOD0(Orphan, void()); 127 128 void DoResume(); 129 }; 130 131 // JobFactory for creating MockHttpStreamFactoryJobs. 132 class TestJobFactory : public HttpStreamFactory::JobFactory { 133 public: 134 TestJobFactory(); 135 ~TestJobFactory() override; 136 137 std::unique_ptr<HttpStreamFactory::Job> CreateJob( 138 HttpStreamFactory::Job::Delegate* delegate, 139 HttpStreamFactory::JobType job_type, 140 HttpNetworkSession* session, 141 const HttpStreamFactory::StreamRequestInfo& request_info, 142 RequestPriority priority, 143 const ProxyInfo& proxy_info, 144 const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs, 145 url::SchemeHostPort destination, 146 GURL origin_url, 147 bool is_websocket, 148 bool enable_ip_based_pooling, 149 NetLog* net_log, 150 NextProto alternative_protocol, 151 quic::ParsedQuicVersion quic_version) override; 152 main_job()153 MockHttpStreamFactoryJob* main_job() const { return main_job_; } alternative_job()154 MockHttpStreamFactoryJob* alternative_job() const { return alternative_job_; } dns_alpn_h3_job()155 MockHttpStreamFactoryJob* dns_alpn_h3_job() const { return dns_alpn_h3_job_; } 156 157 private: 158 raw_ptr<MockHttpStreamFactoryJob, AcrossTasksDanglingUntriaged> main_job_ = 159 nullptr; 160 raw_ptr<MockHttpStreamFactoryJob, AcrossTasksDanglingUntriaged> 161 alternative_job_ = nullptr; 162 raw_ptr<MockHttpStreamFactoryJob, AcrossTasksDanglingUntriaged> 163 dns_alpn_h3_job_ = nullptr; 164 }; 165 166 } // namespace net 167 168 #endif // NET_HTTP_HTTP_STREAM_FACTORY_TEST_UTIL_H_ 169