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 95 class MockHttpStreamFactoryJob : public HttpStreamFactory::Job { 96 public: 97 MockHttpStreamFactoryJob( 98 HttpStreamFactory::Job::Delegate* delegate, 99 HttpStreamFactory::JobType job_type, 100 HttpNetworkSession* session, 101 const HttpStreamFactory::StreamRequestInfo& request_info, 102 RequestPriority priority, 103 ProxyInfo proxy_info, 104 const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs, 105 url::SchemeHostPort destination, 106 GURL origin_url, 107 NextProto alternative_protocol, 108 quic::ParsedQuicVersion quic_version, 109 bool is_websocket, 110 bool enable_ip_based_pooling, 111 NetLog* net_log); 112 113 ~MockHttpStreamFactoryJob() override; 114 115 MOCK_METHOD0(Resume, void()); 116 117 MOCK_METHOD0(Orphan, void()); 118 119 void DoResume(); 120 }; 121 122 // JobFactory for creating MockHttpStreamFactoryJobs. 123 class TestJobFactory : public HttpStreamFactory::JobFactory { 124 public: 125 TestJobFactory(); 126 ~TestJobFactory() override; 127 128 std::unique_ptr<HttpStreamFactory::Job> CreateJob( 129 HttpStreamFactory::Job::Delegate* delegate, 130 HttpStreamFactory::JobType job_type, 131 HttpNetworkSession* session, 132 const HttpStreamFactory::StreamRequestInfo& request_info, 133 RequestPriority priority, 134 const ProxyInfo& proxy_info, 135 const std::vector<SSLConfig::CertAndStatus>& allowed_bad_certs, 136 url::SchemeHostPort destination, 137 GURL origin_url, 138 bool is_websocket, 139 bool enable_ip_based_pooling, 140 NetLog* net_log, 141 NextProto alternative_protocol, 142 quic::ParsedQuicVersion quic_version) override; 143 main_job()144 MockHttpStreamFactoryJob* main_job() const { return main_job_; } alternative_job()145 MockHttpStreamFactoryJob* alternative_job() const { return alternative_job_; } dns_alpn_h3_job()146 MockHttpStreamFactoryJob* dns_alpn_h3_job() const { return dns_alpn_h3_job_; } 147 148 private: 149 raw_ptr<MockHttpStreamFactoryJob, AcrossTasksDanglingUntriaged> main_job_ = 150 nullptr; 151 raw_ptr<MockHttpStreamFactoryJob, AcrossTasksDanglingUntriaged> 152 alternative_job_ = nullptr; 153 raw_ptr<MockHttpStreamFactoryJob, AcrossTasksDanglingUntriaged> 154 dns_alpn_h3_job_ = nullptr; 155 }; 156 157 } // namespace net 158 159 #endif // NET_HTTP_HTTP_STREAM_FACTORY_TEST_UTIL_H_ 160