1 // Copyright 2012 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_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ 6 #define NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ 7 8 #include <stdint.h> 9 #include <stdlib.h> 10 11 #include <map> 12 #include <memory> 13 #include <string> 14 #include <utility> 15 #include <vector> 16 17 #include "base/compiler_specific.h" 18 #include "base/memory/raw_ptr.h" 19 #include "base/memory/scoped_refptr.h" 20 #include "base/path_service.h" 21 #include "base/strings/string_util.h" 22 #include "base/strings/utf_string_conversions.h" 23 #include "base/task/single_thread_task_runner.h" 24 #include "net/base/io_buffer.h" 25 #include "net/base/load_timing_info.h" 26 #include "net/base/net_errors.h" 27 #include "net/base/network_delegate_impl.h" 28 #include "net/base/request_priority.h" 29 #include "net/base/transport_info.h" 30 #include "net/cert/cert_verifier.h" 31 #include "net/cert/ct_policy_enforcer.h" 32 #include "net/cookies/cookie_inclusion_status.h" 33 #include "net/cookies/cookie_monster.h" 34 #include "net/cookies/cookie_setting_override.h" 35 #include "net/disk_cache/disk_cache.h" 36 #include "net/first_party_sets/first_party_set_metadata.h" 37 #include "net/http/http_auth_handler_factory.h" 38 #include "net/http/http_cache.h" 39 #include "net/http/http_network_layer.h" 40 #include "net/http/http_network_session.h" 41 #include "net/http/http_request_headers.h" 42 #include "net/ssl/ssl_config_service_defaults.h" 43 #include "net/test/embedded_test_server/default_handlers.h" 44 #include "net/test/embedded_test_server/embedded_test_server.h" 45 #include "net/test/embedded_test_server/embedded_test_server_connection_listener.h" 46 #include "net/url_request/redirect_info.h" 47 #include "net/url_request/url_request.h" 48 #include "net/url_request/url_request_context.h" 49 #include "net/url_request/url_request_context_getter.h" 50 #include "net/url_request/url_request_interceptor.h" 51 #include "third_party/abseil-cpp/absl/types/optional.h" 52 #include "url/url_util.h" 53 54 namespace net { 55 56 class URLRequestContextBuilder; 57 58 //----------------------------------------------------------------------------- 59 60 // Creates a URLRequestContextBuilder with some members configured for the 61 // testing purpose. 62 std::unique_ptr<URLRequestContextBuilder> CreateTestURLRequestContextBuilder(); 63 64 //----------------------------------------------------------------------------- 65 // Used to return a dummy context, which lives on the message loop 66 // given in the constructor. 67 class TestURLRequestContextGetter : public URLRequestContextGetter { 68 public: 69 // |network_task_runner| must not be NULL. 70 explicit TestURLRequestContextGetter( 71 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner); 72 73 // Use to pass a pre-initialized |context|. 74 TestURLRequestContextGetter( 75 const scoped_refptr<base::SingleThreadTaskRunner>& network_task_runner, 76 std::unique_ptr<URLRequestContext> context); 77 78 // URLRequestContextGetter implementation. 79 URLRequestContext* GetURLRequestContext() override; 80 scoped_refptr<base::SingleThreadTaskRunner> GetNetworkTaskRunner() 81 const override; 82 83 // see NotifyContextShuttingDown() in the base class. 84 void NotifyContextShuttingDown(); 85 86 protected: 87 ~TestURLRequestContextGetter() override; 88 89 private: 90 const scoped_refptr<base::SingleThreadTaskRunner> network_task_runner_; 91 std::unique_ptr<URLRequestContext> context_; 92 bool is_shut_down_ = false; 93 }; 94 95 //----------------------------------------------------------------------------- 96 97 class TestDelegate : public URLRequest::Delegate { 98 public: 99 TestDelegate(); 100 ~TestDelegate() override; 101 102 // Helpers to create a RunLoop, set |on_<event>| from it, then Run() it. 103 void RunUntilComplete(); 104 void RunUntilRedirect(); 105 // Enables quitting the message loop in response to auth requests, as opposed 106 // to returning credentials or cancelling the request. 107 void RunUntilAuthRequired(); 108 109 // Sets the closure to be run on completion, for tests which need more fine- 110 // grained control than RunUntilComplete(). set_on_complete(base::OnceClosure on_complete)111 void set_on_complete(base::OnceClosure on_complete) { 112 use_legacy_on_complete_ = false; 113 on_complete_ = std::move(on_complete); 114 } 115 116 // Sets the result returned by subsequent calls to OnConnected(). set_on_connected_result(int result)117 void set_on_connected_result(int result) { on_connected_result_ = result; } 118 set_cancel_in_received_redirect(bool val)119 void set_cancel_in_received_redirect(bool val) { cancel_in_rr_ = val; } set_cancel_in_response_started(bool val)120 void set_cancel_in_response_started(bool val) { cancel_in_rs_ = val; } set_cancel_in_received_data(bool val)121 void set_cancel_in_received_data(bool val) { cancel_in_rd_ = val; } set_cancel_in_received_data_pending(bool val)122 void set_cancel_in_received_data_pending(bool val) { 123 cancel_in_rd_pending_ = val; 124 } 125 set_allow_certificate_errors(bool val)126 void set_allow_certificate_errors(bool val) { 127 allow_certificate_errors_ = val; 128 } set_credentials(const AuthCredentials & credentials)129 void set_credentials(const AuthCredentials& credentials) { 130 credentials_ = credentials; 131 } 132 133 // If true, the delegate will asynchronously run the callback passed in from 134 // URLRequest with `on_connected_result_` set_on_connected_run_callback(bool run_callback)135 void set_on_connected_run_callback(bool run_callback) { 136 on_connected_run_callback_ = run_callback; 137 } 138 139 // Returns the list of arguments with which OnConnected() was called. 140 // The arguments are listed in the same order as the calls were received. transports()141 const std::vector<TransportInfo>& transports() const { return transports_; } 142 143 // query state data_received()144 const std::string& data_received() const { return data_received_; } bytes_received()145 int bytes_received() const { return static_cast<int>(data_received_.size()); } response_started_count()146 int response_started_count() const { return response_started_count_; } received_bytes_count()147 int received_bytes_count() const { return received_bytes_count_; } received_redirect_count()148 int received_redirect_count() const { return received_redirect_count_; } received_data_before_response()149 bool received_data_before_response() const { 150 return received_data_before_response_; 151 } redirect_info()152 RedirectInfo redirect_info() { return redirect_info_; } request_failed()153 bool request_failed() const { return request_failed_; } have_certificate_errors()154 bool have_certificate_errors() const { return have_certificate_errors_; } certificate_errors_are_fatal()155 bool certificate_errors_are_fatal() const { 156 return certificate_errors_are_fatal_; 157 } certificate_net_error()158 int certificate_net_error() const { return certificate_net_error_; } auth_required_called()159 bool auth_required_called() const { return auth_required_; } response_completed()160 bool response_completed() const { return response_completed_; } request_status()161 int request_status() const { return request_status_; } 162 163 // URLRequest::Delegate: 164 int OnConnected(URLRequest* request, 165 const TransportInfo& info, 166 CompletionOnceCallback callback) override; 167 void OnReceivedRedirect(URLRequest* request, 168 const RedirectInfo& redirect_info, 169 bool* defer_redirect) override; 170 void OnAuthRequired(URLRequest* request, 171 const AuthChallengeInfo& auth_info) override; 172 // NOTE: |fatal| causes |certificate_errors_are_fatal_| to be set to true. 173 // (Unit tests use this as a post-condition.) But for policy, this method 174 // consults |allow_certificate_errors_|. 175 void OnSSLCertificateError(URLRequest* request, 176 int net_error, 177 const SSLInfo& ssl_info, 178 bool fatal) override; 179 void OnResponseStarted(URLRequest* request, int net_error) override; 180 void OnReadCompleted(URLRequest* request, int bytes_read) override; 181 182 private: 183 static const int kBufferSize = 4096; 184 185 virtual void OnResponseCompleted(URLRequest* request); 186 187 // options for controlling behavior 188 int on_connected_result_ = OK; 189 bool cancel_in_rr_ = false; 190 bool cancel_in_rs_ = false; 191 bool cancel_in_rd_ = false; 192 bool cancel_in_rd_pending_ = false; 193 bool allow_certificate_errors_ = false; 194 AuthCredentials credentials_; 195 196 // True if legacy on-complete behaviour, using QuitCurrent*Deprecated(), is 197 // enabled. This is cleared if any of the Until*() APIs are used. 198 bool use_legacy_on_complete_ = true; 199 200 // Used to register RunLoop quit closures, to implement the Until*() closures. 201 base::OnceClosure on_complete_; 202 base::OnceClosure on_redirect_; 203 base::OnceClosure on_auth_required_; 204 205 // tracks status of callbacks 206 std::vector<TransportInfo> transports_; 207 int response_started_count_ = 0; 208 int received_bytes_count_ = 0; 209 int received_redirect_count_ = 0; 210 bool received_data_before_response_ = false; 211 bool request_failed_ = false; 212 bool have_certificate_errors_ = false; 213 bool certificate_errors_are_fatal_ = false; 214 int certificate_net_error_ = 0; 215 bool auth_required_ = false; 216 std::string data_received_; 217 bool response_completed_ = false; 218 219 // tracks status of request 220 int request_status_ = ERR_IO_PENDING; 221 222 // our read buffer 223 scoped_refptr<IOBuffer> buf_; 224 225 RedirectInfo redirect_info_; 226 227 bool on_connected_run_callback_ = false; 228 }; 229 230 //----------------------------------------------------------------------------- 231 232 class TestNetworkDelegate : public NetworkDelegateImpl { 233 public: 234 enum Options { 235 NO_GET_COOKIES = 1 << 0, 236 NO_SET_COOKIE = 1 << 1, 237 }; 238 239 TestNetworkDelegate(); 240 ~TestNetworkDelegate() override; 241 242 // Writes the LoadTimingInfo during the most recent call to OnBeforeRedirect. 243 bool GetLoadTimingInfoBeforeRedirect( 244 LoadTimingInfo* load_timing_info_before_redirect) const; 245 246 // Will redirect once to the given URL when the next set of headers are 247 // received. set_redirect_on_headers_received_url(GURL redirect_on_headers_received_url)248 void set_redirect_on_headers_received_url( 249 GURL redirect_on_headers_received_url) { 250 redirect_on_headers_received_url_ = redirect_on_headers_received_url; 251 } 252 253 // Adds a X-Network-Delegate header to the first OnHeadersReceived call, but 254 // not subsequent ones. set_add_header_to_first_response(bool add_header_to_first_response)255 void set_add_header_to_first_response(bool add_header_to_first_response) { 256 add_header_to_first_response_ = add_header_to_first_response; 257 } 258 set_preserve_fragment_on_redirect_url(const absl::optional<GURL> & preserve_fragment_on_redirect_url)259 void set_preserve_fragment_on_redirect_url( 260 const absl::optional<GURL>& preserve_fragment_on_redirect_url) { 261 preserve_fragment_on_redirect_url_ = preserve_fragment_on_redirect_url; 262 } 263 set_cookie_options(int o)264 void set_cookie_options(int o) {cookie_options_bit_mask_ = o; } 265 last_error()266 int last_error() const { return last_error_; } error_count()267 int error_count() const { return error_count_; } created_requests()268 int created_requests() const { return created_requests_; } destroyed_requests()269 int destroyed_requests() const { return destroyed_requests_; } completed_requests()270 int completed_requests() const { return completed_requests_; } canceled_requests()271 int canceled_requests() const { return canceled_requests_; } blocked_annotate_cookies_count()272 int blocked_annotate_cookies_count() const { 273 return blocked_annotate_cookies_count_; 274 } blocked_set_cookie_count()275 int blocked_set_cookie_count() const { return blocked_set_cookie_count_; } set_cookie_count()276 int set_cookie_count() const { return set_cookie_count_; } 277 set_cancel_request_with_policy_violating_referrer(bool val)278 void set_cancel_request_with_policy_violating_referrer(bool val) { 279 cancel_request_with_policy_violating_referrer_ = val; 280 } 281 before_start_transaction_count()282 int before_start_transaction_count() const { 283 return before_start_transaction_count_; 284 } 285 headers_received_count()286 int headers_received_count() const { return headers_received_count_; } 287 set_before_start_transaction_fails()288 void set_before_start_transaction_fails() { 289 before_start_transaction_fails_ = true; 290 } 291 cookie_setting_overrides_records()292 const std::vector<CookieSettingOverrides>& cookie_setting_overrides_records() 293 const { 294 return cookie_setting_overrides_records_; 295 } 296 297 protected: 298 // NetworkDelegate: 299 int OnBeforeURLRequest(URLRequest* request, 300 CompletionOnceCallback callback, 301 GURL* new_url) override; 302 int OnBeforeStartTransaction( 303 URLRequest* request, 304 const HttpRequestHeaders& headers, 305 OnBeforeStartTransactionCallback callback) override; 306 int OnHeadersReceived( 307 URLRequest* request, 308 CompletionOnceCallback callback, 309 const HttpResponseHeaders* original_response_headers, 310 scoped_refptr<HttpResponseHeaders>* override_response_headers, 311 const IPEndPoint& endpoint, 312 absl::optional<GURL>* preserve_fragment_on_redirect_url) override; 313 void OnBeforeRedirect(URLRequest* request, const GURL& new_location) override; 314 void OnResponseStarted(URLRequest* request, int net_error) override; 315 void OnCompleted(URLRequest* request, bool started, int net_error) override; 316 void OnURLRequestDestroyed(URLRequest* request) override; 317 bool OnAnnotateAndMoveUserBlockedCookies( 318 const URLRequest& request, 319 const net::FirstPartySetMetadata& first_party_set_metadata, 320 net::CookieAccessResultList& maybe_included_cookies, 321 net::CookieAccessResultList& excluded_cookies) override; 322 NetworkDelegate::PrivacySetting OnForcePrivacyMode( 323 const URLRequest& request) const override; 324 bool OnCanSetCookie( 325 const URLRequest& request, 326 const net::CanonicalCookie& cookie, 327 CookieOptions* options, 328 const net::FirstPartySetMetadata& first_party_set_metadata, 329 CookieInclusionStatus* inclusion_status) override; 330 bool OnCancelURLRequestWithPolicyViolatingReferrerHeader( 331 const URLRequest& request, 332 const GURL& target_url, 333 const GURL& referrer_url) const override; 334 335 void InitRequestStatesIfNew(int request_id); 336 337 // Gets a request ID if it already has one, assigns a new one and returns that 338 // if not. 339 int GetRequestId(URLRequest* request); 340 RecordCookieSettingOverrides(CookieSettingOverrides overrides)341 void RecordCookieSettingOverrides(CookieSettingOverrides overrides) const { 342 cookie_setting_overrides_records_.push_back(overrides); 343 } 344 345 GURL redirect_on_headers_received_url_; 346 // URL to mark as retaining its fragment if redirected to at the 347 // OnHeadersReceived() stage. 348 absl::optional<GURL> preserve_fragment_on_redirect_url_; 349 350 int last_error_ = 0; 351 int error_count_ = 0; 352 int created_requests_ = 0; 353 int destroyed_requests_ = 0; 354 int completed_requests_ = 0; 355 int canceled_requests_ = 0; 356 int cookie_options_bit_mask_ = 0; 357 int blocked_annotate_cookies_count_ = 0; 358 int blocked_set_cookie_count_ = 0; 359 int set_cookie_count_ = 0; 360 int before_start_transaction_count_ = 0; 361 int headers_received_count_ = 0; 362 363 // NetworkDelegate callbacks happen in a particular order (e.g. 364 // OnBeforeURLRequest is always called before OnBeforeStartTransaction). 365 // This bit-set indicates for each request id (key) what events may be sent 366 // next. 367 std::map<int, int> next_states_; 368 369 // A log that records for each request id (key) the order in which On... 370 // functions were called. 371 std::map<int, std::string> event_order_; 372 373 LoadTimingInfo load_timing_info_before_redirect_; 374 bool has_load_timing_info_before_redirect_ = false; 375 376 bool cancel_request_with_policy_violating_referrer_ = 377 false; // false by default 378 bool before_start_transaction_fails_ = false; 379 bool add_header_to_first_response_ = false; 380 int next_request_id_ = 0; 381 382 mutable std::vector<CookieSettingOverrides> cookie_setting_overrides_records_; 383 }; 384 385 // ---------------------------------------------------------------------------- 386 387 class FilteringTestNetworkDelegate : public TestNetworkDelegate { 388 public: 389 FilteringTestNetworkDelegate(); 390 ~FilteringTestNetworkDelegate() override; 391 392 bool OnCanSetCookie( 393 const URLRequest& request, 394 const net::CanonicalCookie& cookie, 395 CookieOptions* options, 396 const net::FirstPartySetMetadata& first_party_set_metadata, 397 CookieInclusionStatus* inclusion_status) override; 398 SetCookieFilter(std::string filter)399 void SetCookieFilter(std::string filter) { 400 cookie_name_filter_ = std::move(filter); 401 } 402 set_cookie_called_count()403 int set_cookie_called_count() { return set_cookie_called_count_; } 404 blocked_set_cookie_count()405 int blocked_set_cookie_count() { return blocked_set_cookie_count_; } 406 ResetSetCookieCalledCount()407 void ResetSetCookieCalledCount() { set_cookie_called_count_ = 0; } 408 ResetBlockedSetCookieCount()409 void ResetBlockedSetCookieCount() { blocked_set_cookie_count_ = 0; } 410 411 bool OnAnnotateAndMoveUserBlockedCookies( 412 const URLRequest& request, 413 const net::FirstPartySetMetadata& first_party_set_metadata, 414 net::CookieAccessResultList& maybe_included_cookies, 415 net::CookieAccessResultList& excluded_cookies) override; 416 417 NetworkDelegate::PrivacySetting OnForcePrivacyMode( 418 const URLRequest& request) const override; 419 set_block_annotate_cookies()420 void set_block_annotate_cookies() { block_annotate_cookies_ = true; } 421 unset_block_annotate_cookies()422 void unset_block_annotate_cookies() { block_annotate_cookies_ = false; } 423 annotate_cookies_called_count()424 int annotate_cookies_called_count() const { 425 return annotate_cookies_called_count_; 426 } 427 blocked_annotate_cookies_count()428 int blocked_annotate_cookies_count() const { 429 return blocked_annotate_cookies_count_; 430 } 431 ResetAnnotateCookiesCalledCount()432 void ResetAnnotateCookiesCalledCount() { annotate_cookies_called_count_ = 0; } 433 ResetBlockedAnnotateCookiesCount()434 void ResetBlockedAnnotateCookiesCount() { 435 blocked_annotate_cookies_count_ = 0; 436 } 437 set_block_get_cookies_by_name(bool block)438 void set_block_get_cookies_by_name(bool block) { 439 block_get_cookies_by_name_ = block; 440 } 441 set_force_privacy_mode(bool enabled)442 void set_force_privacy_mode(bool enabled) { force_privacy_mode_ = enabled; } 443 set_partitioned_state_allowed(bool allowed)444 void set_partitioned_state_allowed(bool allowed) { 445 partitioned_state_allowed_ = allowed; 446 } 447 448 private: 449 std::string cookie_name_filter_ = ""; 450 int set_cookie_called_count_ = 0; 451 int blocked_set_cookie_count_ = 0; 452 453 bool block_annotate_cookies_ = false; 454 int annotate_cookies_called_count_ = 0; 455 int blocked_annotate_cookies_count_ = 0; 456 bool block_get_cookies_by_name_ = false; 457 458 bool force_privacy_mode_ = false; 459 bool partitioned_state_allowed_ = false; 460 }; 461 462 // ---------------------------------------------------------------------------- 463 464 // Less verbose way of running a simple testserver. 465 class HttpTestServer : public EmbeddedTestServer { 466 public: HttpTestServer(const base::FilePath & document_root)467 explicit HttpTestServer(const base::FilePath& document_root) { 468 AddDefaultHandlers(document_root); 469 } 470 HttpTestServer()471 HttpTestServer() { RegisterDefaultHandlers(this); } 472 }; 473 //----------------------------------------------------------------------------- 474 475 class TestScopedURLInterceptor { 476 public: 477 // Sets up a URLRequestInterceptor that intercepts a single request for |url|, 478 // returning the provided job. 479 // 480 // On destruction, cleans makes sure the job was removed, and cleans up the 481 // interceptor. Other interceptors for the same URL may not be created until 482 // the interceptor is deleted. 483 TestScopedURLInterceptor(const GURL& url, 484 std::unique_ptr<URLRequestJob> intercept_job); 485 ~TestScopedURLInterceptor(); 486 487 private: 488 class TestRequestInterceptor; 489 490 GURL url_; 491 492 // This is owned by the URLFilter. 493 raw_ptr<TestRequestInterceptor> interceptor_ = nullptr; 494 }; 495 496 } // namespace net 497 498 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_UTIL_H_ 499