Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 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 "system-proxy/proxy_connect_job.h" |
| 6 | |
| 7 | #include <netinet/in.h> |
| 8 | #include <sys/socket.h> |
| 9 | #include <sys/types.h> |
| 10 | |
| 11 | #include <gmock/gmock.h> |
| 12 | #include <gtest/gtest.h> |
| 13 | #include <utility> |
| 14 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 15 | #include <base/bind.h> |
| 16 | #include <base/bind_helpers.h> |
| 17 | #include <base/callback_helpers.h> |
| 18 | #include <base/files/file_util.h> |
| 19 | #include <base/files/scoped_file.h> |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 20 | #include <base/task/single_thread_task_executor.h> |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 21 | #include <base/test/test_mock_time_task_runner.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 22 | #include <brillo/message_loops/base_message_loop.h> |
Garrick Evans | cd8c297 | 2020-04-14 14:35:52 +0900 | [diff] [blame] | 23 | #include <chromeos/patchpanel/socket.h> |
| 24 | #include <chromeos/patchpanel/socket_forwarder.h> |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 25 | |
| 26 | #include "bindings/worker_common.pb.h" |
| 27 | #include "system-proxy/protobuf_util.h" |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 28 | #include "system-proxy/test_http_server.h" |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 29 | |
| 30 | namespace { |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 31 | |
| 32 | constexpr char kProxyServerUrl[] = "http://127.0.0.1:3128"; |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 33 | constexpr char kCredentials[] = "username:pwd"; |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 34 | constexpr char kValidConnectRequest[] = |
| 35 | "CONNECT www.example.server.com:443 HTTP/1.1\r\n\r\n"; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 36 | } // namespace |
| 37 | |
| 38 | namespace system_proxy { |
| 39 | |
| 40 | using ::testing::_; |
| 41 | using ::testing::Return; |
| 42 | |
| 43 | class ProxyConnectJobTest : public ::testing::Test { |
| 44 | public: |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 45 | struct HttpAuthEntry { |
| 46 | HttpAuthEntry(const std::string& origin, |
| 47 | const std::string& scheme, |
| 48 | const std::string& realm, |
| 49 | const std::string& credentials) |
| 50 | : origin(origin), |
| 51 | scheme(scheme), |
| 52 | realm(realm), |
| 53 | credentials(credentials) {} |
| 54 | std::string origin; |
| 55 | std::string scheme; |
| 56 | std::string realm; |
| 57 | std::string credentials; |
| 58 | }; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 59 | ProxyConnectJobTest() = default; |
| 60 | ProxyConnectJobTest(const ProxyConnectJobTest&) = delete; |
| 61 | ProxyConnectJobTest& operator=(const ProxyConnectJobTest&) = delete; |
| 62 | ~ProxyConnectJobTest() = default; |
| 63 | |
| 64 | void SetUp() override { |
| 65 | int fds[2]; |
| 66 | ASSERT_NE(-1, |
| 67 | socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, |
| 68 | 0 /* protocol */, fds)); |
| 69 | cros_client_socket_ = |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 70 | std::make_unique<patchpanel::Socket>(base::ScopedFD(fds[1])); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 71 | |
| 72 | connect_job_ = std::make_unique<ProxyConnectJob>( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 73 | std::make_unique<patchpanel::Socket>(base::ScopedFD(fds[0])), "", |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 74 | base::BindOnce(&ProxyConnectJobTest::ResolveProxy, |
| 75 | base::Unretained(this)), |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 76 | base::BindRepeating(&ProxyConnectJobTest::OnAuthCredentialsRequired, |
| 77 | base::Unretained(this)), |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 78 | base::BindOnce(&ProxyConnectJobTest::OnConnectionSetupFinished, |
| 79 | base::Unretained(this))); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 80 | } |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 81 | void AddHttpAuthEntry(const std::string& origin, |
| 82 | const std::string& scheme, |
| 83 | const std::string& realm, |
| 84 | const std::string& credentials) { |
| 85 | http_auth_cache_.push_back( |
| 86 | HttpAuthEntry(origin, scheme, realm, credentials)); |
| 87 | } |
| 88 | |
| 89 | bool AuthRequested() { return auth_requested_; } |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 90 | |
| 91 | protected: |
| 92 | void ResolveProxy( |
| 93 | const std::string& target_url, |
| 94 | base::OnceCallback<void(const std::list<std::string>&)> callback) { |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 95 | std::move(callback).Run({remote_proxy_url_}); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 98 | void OnAuthCredentialsRequired( |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 99 | const std::string& proxy_url, |
| 100 | const std::string& scheme, |
| 101 | const std::string& realm, |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 102 | const std::string& bad_credentials, |
| 103 | base::RepeatingCallback<void(const std::string&)> callback) { |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 104 | auth_requested_ = true; |
| 105 | for (const auto& auth_entry : http_auth_cache_) { |
| 106 | if (auth_entry.origin == proxy_url && auth_entry.realm == realm && |
| 107 | auth_entry.scheme == scheme) { |
| 108 | std::move(callback).Run(auth_entry.credentials); |
| 109 | return; |
| 110 | } |
| 111 | } |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 112 | if (invoke_authentication_callback_) { |
| 113 | std::move(callback).Run(/* credentials = */ ""); |
| 114 | } |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 115 | } |
| 116 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 117 | void OnConnectionSetupFinished( |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 118 | std::unique_ptr<patchpanel::SocketForwarder> fwd, |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 119 | ProxyConnectJob* connect_job) { |
| 120 | ASSERT_EQ(connect_job, connect_job_.get()); |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 121 | if (fwd) { |
| 122 | forwarder_created_ = true; |
| 123 | } |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 124 | } |
| 125 | |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 126 | std::string remote_proxy_url_ = kProxyServerUrl; |
| 127 | bool forwarder_created_ = false; |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 128 | // Used to simulate time-outs while waiting for credentials from the Browser. |
| 129 | bool invoke_authentication_callback_ = true; |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 130 | std::unique_ptr<ProxyConnectJob> connect_job_; |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 131 | base::SingleThreadTaskExecutor task_executor_{base::MessagePumpType::IO}; |
| 132 | std::unique_ptr<brillo::BaseMessageLoop> brillo_loop_{ |
| 133 | std::make_unique<brillo::BaseMessageLoop>(task_executor_.task_runner())}; |
Garrick Evans | 3388a03 | 2020-03-24 11:25:55 +0900 | [diff] [blame] | 134 | std::unique_ptr<patchpanel::Socket> cros_client_socket_; |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 135 | |
| 136 | private: |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 137 | std::vector<HttpAuthEntry> http_auth_cache_; |
| 138 | bool auth_requested_ = false; |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 139 | FRIEND_TEST(ProxyConnectJobTest, ClientConnectTimeoutJobCanceled); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | TEST_F(ProxyConnectJobTest, SuccessfulConnection) { |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 143 | HttpTestServer http_test_server; |
| 144 | http_test_server.AddHttpConnectReply(HttpTestServer::HttpConnectReply::kOk); |
| 145 | http_test_server.Start(); |
| 146 | remote_proxy_url_ = http_test_server.GetUrl(); |
| 147 | |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 148 | connect_job_->Start(); |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 149 | cros_client_socket_->SendTo(kValidConnectRequest, |
| 150 | std::strlen(kValidConnectRequest)); |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 151 | brillo_loop_->RunOnce(false); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 152 | |
Andreea Costinas | a224659 | 2020-04-12 23:24:01 +0200 | [diff] [blame] | 153 | EXPECT_EQ("www.example.server.com:443", connect_job_->target_url_); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 154 | EXPECT_EQ(1, connect_job_->proxy_servers_.size()); |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 155 | EXPECT_EQ(http_test_server.GetUrl(), connect_job_->proxy_servers_.front()); |
| 156 | EXPECT_TRUE(forwarder_created_); |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 157 | ASSERT_FALSE(AuthRequested()); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 158 | } |
| 159 | |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 160 | TEST_F(ProxyConnectJobTest, TunnelFailedBadGatewayFromRemote) { |
| 161 | HttpTestServer http_test_server; |
| 162 | http_test_server.AddHttpConnectReply( |
| 163 | HttpTestServer::HttpConnectReply::kBadGateway); |
| 164 | http_test_server.Start(); |
| 165 | remote_proxy_url_ = http_test_server.GetUrl(); |
| 166 | |
| 167 | connect_job_->Start(); |
| 168 | cros_client_socket_->SendTo(kValidConnectRequest, |
| 169 | std::strlen(kValidConnectRequest)); |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 170 | brillo_loop_->RunOnce(false); |
Andreea Costinas | f90a4c0 | 2020-06-12 22:30:51 +0200 | [diff] [blame] | 171 | |
| 172 | EXPECT_FALSE(forwarder_created_); |
| 173 | |
| 174 | std::string expected_server_reply = |
| 175 | "HTTP/1.1 502 Error creating tunnel - Origin: local proxy\r\n\r\n"; |
| 176 | std::vector<char> buf(expected_server_reply.size()); |
| 177 | ASSERT_TRUE(cros_client_socket_->RecvFrom(buf.data(), buf.size())); |
| 178 | std::string actual_server_reply(buf.data(), buf.size()); |
| 179 | |
| 180 | EXPECT_EQ(expected_server_reply, actual_server_reply); |
| 181 | } |
| 182 | |
Andreea Costinas | ed3f978 | 2020-05-20 17:09:46 +0200 | [diff] [blame] | 183 | TEST_F(ProxyConnectJobTest, SuccessfulConnectionAltEnding) { |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 184 | HttpTestServer http_test_server; |
| 185 | http_test_server.AddHttpConnectReply(HttpTestServer::HttpConnectReply::kOk); |
| 186 | http_test_server.Start(); |
| 187 | remote_proxy_url_ = http_test_server.GetUrl(); |
| 188 | |
Andreea Costinas | ed3f978 | 2020-05-20 17:09:46 +0200 | [diff] [blame] | 189 | connect_job_->Start(); |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 190 | char validConnRequest[] = "CONNECT www.example.server.com:443 HTTP/1.1\r\n\n"; |
Andreea Costinas | ed3f978 | 2020-05-20 17:09:46 +0200 | [diff] [blame] | 191 | cros_client_socket_->SendTo(validConnRequest, std::strlen(validConnRequest)); |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 192 | brillo_loop_->RunOnce(false); |
Andreea Costinas | ed3f978 | 2020-05-20 17:09:46 +0200 | [diff] [blame] | 193 | |
| 194 | EXPECT_EQ("www.example.server.com:443", connect_job_->target_url_); |
| 195 | EXPECT_EQ(1, connect_job_->proxy_servers_.size()); |
Andreea Costinas | 054fbb5 | 2020-06-12 20:46:22 +0200 | [diff] [blame] | 196 | EXPECT_EQ(http_test_server.GetUrl(), connect_job_->proxy_servers_.front()); |
| 197 | EXPECT_TRUE(forwarder_created_); |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 198 | ASSERT_FALSE(AuthRequested()); |
Andreea Costinas | ed3f978 | 2020-05-20 17:09:46 +0200 | [diff] [blame] | 199 | } |
| 200 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 201 | TEST_F(ProxyConnectJobTest, BadHttpRequestWrongMethod) { |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 202 | connect_job_->Start(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 203 | char badConnRequest[] = "GET www.example.server.com:443 HTTP/1.1\r\n\r\n"; |
| 204 | cros_client_socket_->SendTo(badConnRequest, std::strlen(badConnRequest)); |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 205 | brillo_loop_->RunOnce(false); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 206 | |
| 207 | EXPECT_EQ("", connect_job_->target_url_); |
| 208 | EXPECT_EQ(0, connect_job_->proxy_servers_.size()); |
| 209 | const std::string expected_http_response = |
| 210 | "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n"; |
| 211 | std::vector<char> buf(expected_http_response.size()); |
| 212 | ASSERT_TRUE( |
| 213 | base::ReadFromFD(cros_client_socket_->fd(), buf.data(), buf.size())); |
| 214 | std::string actual_response(buf.data(), buf.size()); |
| 215 | EXPECT_EQ(expected_http_response, actual_response); |
| 216 | } |
| 217 | |
| 218 | TEST_F(ProxyConnectJobTest, BadHttpRequestNoEmptyLine) { |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 219 | connect_job_->Start(); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 220 | // No empty line after http message. |
| 221 | char badConnRequest[] = "CONNECT www.example.server.com:443 HTTP/1.1\r\n"; |
| 222 | cros_client_socket_->SendTo(badConnRequest, std::strlen(badConnRequest)); |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 223 | brillo_loop_->RunOnce(false); |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 224 | |
| 225 | EXPECT_EQ("", connect_job_->target_url_); |
| 226 | EXPECT_EQ(0, connect_job_->proxy_servers_.size()); |
| 227 | const std::string expected_http_response = |
| 228 | "HTTP/1.1 400 Bad Request - Origin: local proxy\r\n\r\n"; |
| 229 | std::vector<char> buf(expected_http_response.size()); |
| 230 | ASSERT_TRUE( |
| 231 | base::ReadFromFD(cros_client_socket_->fd(), buf.data(), buf.size())); |
| 232 | std::string actual_response(buf.data(), buf.size()); |
| 233 | EXPECT_EQ(expected_http_response, actual_response); |
| 234 | } |
| 235 | |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 236 | TEST_F(ProxyConnectJobTest, WaitClientConnectTimeout) { |
| 237 | // Add a TaskRunner where we can control time. |
| 238 | scoped_refptr<base::TestMockTimeTaskRunner> task_runner{ |
| 239 | new base::TestMockTimeTaskRunner()}; |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 240 | brillo_loop_ = nullptr; |
| 241 | brillo_loop_ = std::make_unique<brillo::BaseMessageLoop>(task_runner); |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 242 | base::TestMockTimeTaskRunner::ScopedContext scoped_context(task_runner.get()); |
| 243 | |
| 244 | connect_job_->Start(); |
| 245 | |
| 246 | EXPECT_EQ(1, task_runner->GetPendingTaskCount()); |
| 247 | // Move the time ahead so that the client connection timeout callback is |
| 248 | // triggered. |
| 249 | task_runner->FastForwardBy(task_runner->NextPendingTaskDelay()); |
| 250 | |
| 251 | const std::string expected_http_response = |
| 252 | "HTTP/1.1 408 Request Timeout - Origin: local proxy\r\n\r\n"; |
| 253 | std::vector<char> buf(expected_http_response.size()); |
| 254 | ASSERT_TRUE( |
| 255 | base::ReadFromFD(cros_client_socket_->fd(), buf.data(), buf.size())); |
| 256 | std::string actual_response(buf.data(), buf.size()); |
| 257 | |
| 258 | EXPECT_EQ(expected_http_response, actual_response); |
| 259 | } |
| 260 | |
| 261 | // Check that the client connect timeout callback is not fired if the owning |
| 262 | // proxy connect job is destroyed. |
| 263 | TEST_F(ProxyConnectJobTest, ClientConnectTimeoutJobCanceled) { |
| 264 | // Add a TaskRunner where we can control time. |
| 265 | scoped_refptr<base::TestMockTimeTaskRunner> task_runner{ |
| 266 | new base::TestMockTimeTaskRunner()}; |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 267 | brillo_loop_ = nullptr; |
| 268 | brillo_loop_ = std::make_unique<brillo::BaseMessageLoop>(task_runner); |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 269 | base::TestMockTimeTaskRunner::ScopedContext scoped_context(task_runner.get()); |
| 270 | |
| 271 | // Create a proxy connect job and start the client connect timeout counter. |
| 272 | { |
| 273 | int fds[2]; |
| 274 | ASSERT_NE(-1, |
| 275 | socketpair(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, |
| 276 | 0 /* protocol */, fds)); |
| 277 | auto client_socket = |
| 278 | std::make_unique<patchpanel::Socket>(base::ScopedFD(fds[1])); |
| 279 | |
| 280 | auto connect_job = std::make_unique<ProxyConnectJob>( |
| 281 | std::make_unique<patchpanel::Socket>(base::ScopedFD(fds[0])), "", |
| 282 | base::BindOnce(&ProxyConnectJobTest::ResolveProxy, |
| 283 | base::Unretained(this)), |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 284 | base::BindRepeating(&ProxyConnectJobTest::OnAuthCredentialsRequired, |
| 285 | base::Unretained(this)), |
Andreea Costinas | 08a5d18 | 2020-04-29 22:12:47 +0200 | [diff] [blame] | 286 | base::BindOnce(&ProxyConnectJobTest::OnConnectionSetupFinished, |
| 287 | base::Unretained(this))); |
| 288 | // Post the timeout task. |
| 289 | connect_job->Start(); |
| 290 | EXPECT_TRUE(task_runner->HasPendingTask()); |
| 291 | } |
| 292 | // Check that the task was canceled. |
| 293 | EXPECT_FALSE(task_runner->HasPendingTask()); |
| 294 | } |
| 295 | |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 296 | // Test that the the CONNECT request is sent again after acquiring credentials. |
| 297 | TEST_F(ProxyConnectJobTest, ResendWithCredentials) { |
| 298 | // Start the test server |
| 299 | HttpTestServer http_test_server; |
| 300 | http_test_server.AddHttpConnectReply( |
| 301 | HttpTestServer::HttpConnectReply::kAuthRequiredBasic); |
| 302 | http_test_server.AddHttpConnectReply(HttpTestServer::HttpConnectReply::kOk); |
| 303 | http_test_server.Start(); |
| 304 | remote_proxy_url_ = http_test_server.GetUrl(); |
| 305 | |
| 306 | AddHttpAuthEntry(remote_proxy_url_, "Basic", "\"My Proxy\"", kCredentials); |
| 307 | connect_job_->Start(); |
| 308 | |
| 309 | cros_client_socket_->SendTo(kValidConnectRequest, |
| 310 | std::strlen(kValidConnectRequest)); |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 311 | brillo_loop_->RunOnce(false); |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 312 | |
| 313 | ASSERT_TRUE(AuthRequested()); |
| 314 | EXPECT_TRUE(forwarder_created_); |
| 315 | EXPECT_EQ(kCredentials, connect_job_->credentials_); |
| 316 | EXPECT_EQ(200, connect_job_->http_response_code_); |
| 317 | } |
| 318 | |
| 319 | // Test that the proxy auth required status is forwarded to the client if |
| 320 | // credentials are missing. |
| 321 | TEST_F(ProxyConnectJobTest, NoCredentials) { |
| 322 | // Start the test server |
| 323 | HttpTestServer http_test_server; |
| 324 | http_test_server.AddHttpConnectReply( |
| 325 | HttpTestServer::HttpConnectReply::kAuthRequiredBasic); |
| 326 | http_test_server.Start(); |
| 327 | remote_proxy_url_ = http_test_server.GetUrl(); |
| 328 | |
| 329 | connect_job_->Start(); |
| 330 | |
| 331 | cros_client_socket_->SendTo(kValidConnectRequest, |
| 332 | std::strlen(kValidConnectRequest)); |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 333 | brillo_loop_->RunOnce(false); |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 334 | |
| 335 | ASSERT_TRUE(AuthRequested()); |
| 336 | EXPECT_EQ("", connect_job_->credentials_); |
| 337 | EXPECT_EQ(407, connect_job_->http_response_code_); |
| 338 | } |
| 339 | |
| 340 | // Test that the proxy auth required status is forwarded to the client if the |
| 341 | // server chose Kerberos as an authentication method. |
| 342 | TEST_F(ProxyConnectJobTest, KerberosAuth) { |
| 343 | // Start the test server |
| 344 | HttpTestServer http_test_server; |
| 345 | http_test_server.AddHttpConnectReply( |
| 346 | HttpTestServer::HttpConnectReply::kAuthRequiredKerberos); |
| 347 | http_test_server.Start(); |
| 348 | remote_proxy_url_ = http_test_server.GetUrl(); |
| 349 | |
| 350 | connect_job_->Start(); |
| 351 | |
| 352 | cros_client_socket_->SendTo(kValidConnectRequest, |
| 353 | std::strlen(kValidConnectRequest)); |
Qijiang Fan | 3401467 | 2020-07-20 16:05:38 +0900 | [diff] [blame] | 354 | brillo_loop_->RunOnce(false); |
Andreea Costinas | bb2aa02 | 2020-06-13 00:03:23 +0200 | [diff] [blame] | 355 | |
| 356 | ASSERT_FALSE(AuthRequested()); |
| 357 | EXPECT_EQ("", connect_job_->credentials_); |
| 358 | EXPECT_EQ(407, connect_job_->http_response_code_); |
| 359 | } |
| 360 | |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame^] | 361 | // Test that the connection times out while waiting for credentials from the |
| 362 | // Browser. |
| 363 | TEST_F(ProxyConnectJobTest, AuthenticationTimeout) { |
| 364 | // Add a TaskRunner where we can control time. |
| 365 | scoped_refptr<base::TestMockTimeTaskRunner> task_runner{ |
| 366 | new base::TestMockTimeTaskRunner()}; |
| 367 | brillo_loop_ = nullptr; |
| 368 | brillo_loop_ = std::make_unique<brillo::BaseMessageLoop>(task_runner); |
| 369 | base::TestMockTimeTaskRunner::ScopedContext scoped_context(task_runner.get()); |
| 370 | |
| 371 | invoke_authentication_callback_ = false; |
| 372 | |
| 373 | // Start the test server |
| 374 | HttpTestServer http_test_server; |
| 375 | http_test_server.AddHttpConnectReply( |
| 376 | HttpTestServer::HttpConnectReply::kAuthRequiredBasic); |
| 377 | http_test_server.Start(); |
| 378 | remote_proxy_url_ = http_test_server.GetUrl(); |
| 379 | |
| 380 | connect_job_->Start(); |
| 381 | |
| 382 | cros_client_socket_->SendTo(kValidConnectRequest, |
| 383 | std::strlen(kValidConnectRequest)); |
| 384 | task_runner->RunUntilIdle(); |
| 385 | // We need to manually invoke the method which reads from the client socket |
| 386 | // because |task_runner| will not execute the FileDescriptorWatcher's tasks. |
| 387 | connect_job_->OnClientReadReady(); |
| 388 | |
| 389 | // Check that an authentication request was sent. |
| 390 | ASSERT_TRUE(AuthRequested()); |
| 391 | EXPECT_EQ(1, task_runner->GetPendingTaskCount()); |
| 392 | // Move the time ahead so that the client connection timeout callback is |
| 393 | // triggered. |
| 394 | task_runner->FastForwardBy(task_runner->NextPendingTaskDelay()); |
| 395 | |
| 396 | const std::string expected_http_response = |
| 397 | "HTTP/1.1 407 Credentials required - Origin: local proxy\r\n\r\n"; |
| 398 | std::vector<char> buf(expected_http_response.size()); |
| 399 | ASSERT_TRUE( |
| 400 | base::ReadFromFD(cros_client_socket_->fd(), buf.data(), buf.size())); |
| 401 | std::string actual_response(buf.data(), buf.size()); |
| 402 | |
| 403 | // Check that the auth failure was forwarded to the client. |
| 404 | EXPECT_EQ(expected_http_response, actual_response); |
| 405 | } |
| 406 | |
| 407 | // Verifies that the receiving the same bad credentials twice will send an auth |
| 408 | // failure to the Chrome OS local client. |
| 409 | TEST_F(ProxyConnectJobTest, CancelIfBadCredentials) { |
| 410 | // Start the test server |
| 411 | HttpTestServer http_test_server; |
| 412 | http_test_server.AddHttpConnectReply( |
| 413 | HttpTestServer::HttpConnectReply::kAuthRequiredBasic); |
| 414 | http_test_server.AddHttpConnectReply( |
| 415 | HttpTestServer::HttpConnectReply::kAuthRequiredBasic); |
| 416 | http_test_server.Start(); |
| 417 | remote_proxy_url_ = http_test_server.GetUrl(); |
| 418 | AddHttpAuthEntry(remote_proxy_url_, "Basic", "\"My Proxy\"", kCredentials); |
| 419 | |
| 420 | connect_job_->Start(); |
| 421 | |
| 422 | cros_client_socket_->SendTo(kValidConnectRequest, |
| 423 | std::strlen(kValidConnectRequest)); |
| 424 | brillo_loop_->RunOnce(false); |
| 425 | |
| 426 | ASSERT_TRUE(AuthRequested()); |
| 427 | |
| 428 | const std::string expected_http_response = |
| 429 | "HTTP/1.1 407 Credentials required - Origin: local proxy\r\n\r\n"; |
| 430 | std::vector<char> buf(expected_http_response.size()); |
| 431 | ASSERT_TRUE( |
| 432 | base::ReadFromFD(cros_client_socket_->fd(), buf.data(), buf.size())); |
| 433 | std::string actual_response(buf.data(), buf.size()); |
| 434 | |
| 435 | EXPECT_EQ(expected_http_response, actual_response); |
| 436 | } |
| 437 | |
Andreea Costinas | e45d54b | 2020-03-10 09:21:14 +0100 | [diff] [blame] | 438 | } // namespace system_proxy |