1 // Copyright 2018 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_SOCKET_CONNECT_JOB_TEST_UTIL_H_ 6 #define NET_SOCKET_CONNECT_JOB_TEST_UTIL_H_ 7 8 #include <memory> 9 10 #include "base/memory/scoped_refptr.h" 11 #include "base/run_loop.h" 12 #include "net/base/net_errors.h" 13 #include "net/http/http_auth_controller.h" 14 #include "net/http/http_response_info.h" 15 #include "net/socket/connect_job.h" 16 17 namespace net { 18 19 class StreamSocket; 20 21 class TestConnectJobDelegate : public ConnectJob::Delegate { 22 public: 23 // Whether a socket should be returned. In most cases, no socket is returned 24 // on failure; however, on certain SSL errors, a socket is returned in the 25 // case of error. 26 enum class SocketExpected { 27 ON_SUCCESS_ONLY, 28 ALWAYS, 29 }; 30 31 explicit TestConnectJobDelegate( 32 SocketExpected socket_expected = SocketExpected::ON_SUCCESS_ONLY); 33 34 TestConnectJobDelegate(const TestConnectJobDelegate&) = delete; 35 TestConnectJobDelegate& operator=(const TestConnectJobDelegate&) = delete; 36 37 ~TestConnectJobDelegate() override; 38 39 // ConnectJob::Delegate implementation. 40 void OnConnectJobComplete(int result, ConnectJob* job) override; 41 void OnNeedsProxyAuth(const HttpResponseInfo& response, 42 HttpAuthController* auth_controller, 43 base::OnceClosure restart_with_auth_callback, 44 ConnectJob* job) override; 45 46 // Waits for the specified number of total auth challenges to be seen. Number 47 // includes auth challenges that have already been waited for. Fails the test 48 // if more auth challenges are seen than expected. 49 void WaitForAuthChallenge(int num_auth_challenges_to_wait_for); 50 51 void RunAuthCallback(); 52 53 // Waits for the ConnectJob to complete if it hasn't already and returns the 54 // resulting network error code. 55 int WaitForResult(); 56 num_auth_challenges()57 int num_auth_challenges() const { return num_auth_challenges_; } auth_response_info()58 const HttpResponseInfo& auth_response_info() const { 59 return auth_response_info_; 60 } auth_controller()61 scoped_refptr<HttpAuthController> auth_controller() { 62 return auth_controller_.get(); 63 } 64 65 // Returns true if the ConnectJob has a result. has_result()66 bool has_result() const { return has_result_; } 67 68 void StartJobExpectingResult(ConnectJob* connect_job, 69 net::Error expected_result, 70 bool expect_sync_result); 71 socket()72 StreamSocket* socket() { return socket_.get(); } 73 74 std::unique_ptr<StreamSocket> ReleaseSocket(); 75 76 private: 77 const SocketExpected socket_expected_; 78 bool has_result_ = false; 79 int result_ = ERR_IO_PENDING; 80 std::unique_ptr<StreamSocket> socket_; 81 82 // These values are all updated each time a proxy auth challenge is seen. 83 int num_auth_challenges_ = 0; 84 HttpResponseInfo auth_response_info_; 85 scoped_refptr<HttpAuthController> auth_controller_; 86 base::OnceClosure restart_with_auth_callback_; 87 88 base::RunLoop run_loop_; 89 std::unique_ptr<base::RunLoop> auth_challenge_run_loop_; 90 }; 91 92 } // namespace net 93 94 #endif // NET_SOCKET_CONNECT_JOB_TEST_UTIL_H_ 95