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 #include "net/socket/connect_job_test_util.h"
6
7 #include <utility>
8
9 #include "base/check.h"
10 #include "base/run_loop.h"
11 #include "net/socket/stream_socket.h"
12 #include "net/test/gtest_util.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace net {
16
TestConnectJobDelegate(SocketExpected socket_expected)17 TestConnectJobDelegate::TestConnectJobDelegate(SocketExpected socket_expected)
18 : socket_expected_(socket_expected) {}
19
20 TestConnectJobDelegate::~TestConnectJobDelegate() = default;
21
OnConnectJobComplete(int result,ConnectJob * job)22 void TestConnectJobDelegate::OnConnectJobComplete(int result, ConnectJob* job) {
23 EXPECT_FALSE(has_result_);
24 result_ = result;
25 socket_ = job->PassSocket();
26 EXPECT_EQ(socket_.get() != nullptr,
27 result == OK || socket_expected_ == SocketExpected::ALWAYS);
28 // On success, generally end up with a connected socket. Could theoretically
29 // be racily disconnected before it was returned, but that case isn't tested
30 // with this class.
31 if (result == OK)
32 EXPECT_TRUE(socket_->IsConnected());
33 has_result_ = true;
34 run_loop_.Quit();
35 }
36
OnNeedsProxyAuth(const HttpResponseInfo & response,HttpAuthController * auth_controller,base::OnceClosure restart_with_auth_callback,ConnectJob * job)37 void TestConnectJobDelegate::OnNeedsProxyAuth(
38 const HttpResponseInfo& response,
39 HttpAuthController* auth_controller,
40 base::OnceClosure restart_with_auth_callback,
41 ConnectJob* job) {
42 EXPECT_TRUE(auth_controller);
43 EXPECT_TRUE(restart_with_auth_callback);
44
45 EXPECT_FALSE(has_result_);
46 EXPECT_FALSE(auth_controller_);
47 EXPECT_FALSE(restart_with_auth_callback_);
48
49 num_auth_challenges_++;
50 auth_response_info_ = response;
51 auth_controller_ = auth_controller;
52 restart_with_auth_callback_ = std::move(restart_with_auth_callback);
53 if (auth_challenge_run_loop_)
54 auth_challenge_run_loop_->Quit();
55 }
56
WaitForAuthChallenge(int num_auth_challenges_to_wait_for)57 void TestConnectJobDelegate::WaitForAuthChallenge(
58 int num_auth_challenges_to_wait_for) {
59 // It a bit strange to call this after a job has already complete, and doing
60 // so probably indicates a bug.
61 EXPECT_FALSE(has_result_);
62
63 while (num_auth_challenges_ < num_auth_challenges_to_wait_for) {
64 auth_challenge_run_loop_ = std::make_unique<base::RunLoop>();
65 auth_challenge_run_loop_->Run();
66 auth_challenge_run_loop_.reset();
67 }
68 EXPECT_EQ(num_auth_challenges_to_wait_for, num_auth_challenges_);
69 }
70
RunAuthCallback()71 void TestConnectJobDelegate::RunAuthCallback() {
72 ASSERT_TRUE(restart_with_auth_callback_);
73 auth_controller_ = nullptr;
74 std::move(restart_with_auth_callback_).Run();
75 }
76
WaitForResult()77 int TestConnectJobDelegate::WaitForResult() {
78 run_loop_.Run();
79 DCHECK(has_result_);
80 return result_;
81 }
82
StartJobExpectingResult(ConnectJob * connect_job,net::Error expected_result,bool expect_sync_result)83 void TestConnectJobDelegate::StartJobExpectingResult(ConnectJob* connect_job,
84 net::Error expected_result,
85 bool expect_sync_result) {
86 int rv = connect_job->Connect();
87 if (rv == ERR_IO_PENDING) {
88 EXPECT_FALSE(expect_sync_result);
89 EXPECT_THAT(WaitForResult(), test::IsError(expected_result));
90 } else {
91 EXPECT_TRUE(expect_sync_result);
92 // The callback should not have been invoked.
93 ASSERT_FALSE(has_result_);
94 OnConnectJobComplete(rv, connect_job);
95 EXPECT_THAT(result_, test::IsError(expected_result));
96 }
97 }
98
ReleaseSocket()99 std::unique_ptr<StreamSocket> TestConnectJobDelegate::ReleaseSocket() {
100 return std::move(socket_);
101 }
102
103 } // namespace net
104