OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "jingle/glue/proxy_resolving_client_socket.h" | 5 #include "jingle/glue/proxy_resolving_client_socket.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/macros.h" | 8 #include "base/macros.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 11 #include "base/strings/utf_string_conversions.h" |
11 #include "base/threading/thread_task_runner_handle.h" | 12 #include "base/threading/thread_task_runner_handle.h" |
12 #include "net/base/test_completion_callback.h" | 13 #include "net/base/test_completion_callback.h" |
13 #include "net/dns/mock_host_resolver.h" | 14 #include "net/dns/mock_host_resolver.h" |
14 #include "net/proxy/proxy_service.h" | 15 #include "net/proxy/proxy_service.h" |
15 #include "net/socket/socket_test_util.h" | 16 #include "net/socket/socket_test_util.h" |
| 17 #include "net/test/gtest_util.h" |
16 #include "net/url_request/url_request_context_getter.h" | 18 #include "net/url_request/url_request_context_getter.h" |
17 #include "net/url_request/url_request_test_util.h" | 19 #include "net/url_request/url_request_test_util.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 | 21 |
20 namespace { | 22 namespace { |
21 | 23 |
22 class MyTestURLRequestContext : public net::TestURLRequestContext { | 24 class MyTestURLRequestContext : public net::TestURLRequestContext { |
23 public: | 25 public: |
24 MyTestURLRequestContext() : TestURLRequestContext(true) { | 26 MyTestURLRequestContext() : TestURLRequestContext(true) { |
25 context_storage_.set_proxy_service( | 27 context_storage_.set_proxy_service( |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 net::URLRequestContext* context = | 111 net::URLRequestContext* context = |
110 url_request_context_getter_->GetURLRequestContext(); | 112 url_request_context_getter_->GetURLRequestContext(); |
111 const net::ProxyRetryInfoMap& retry_info = | 113 const net::ProxyRetryInfoMap& retry_info = |
112 context->proxy_service()->proxy_retry_info(); | 114 context->proxy_service()->proxy_retry_info(); |
113 | 115 |
114 EXPECT_EQ(1u, retry_info.size()); | 116 EXPECT_EQ(1u, retry_info.size()); |
115 net::ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); | 117 net::ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); |
116 EXPECT_TRUE(iter != retry_info.end()); | 118 EXPECT_TRUE(iter != retry_info.end()); |
117 } | 119 } |
118 | 120 |
| 121 TEST_F(ProxyResolvingClientSocketTest, ReusesHTTPAuthCache_Lookup) { |
| 122 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter( |
| 123 new net::TestURLRequestContextGetter( |
| 124 base::ThreadTaskRunnerHandle::Get(), |
| 125 std::unique_ptr<net::TestURLRequestContext>( |
| 126 new MyTestURLRequestContext))); |
| 127 net::MockClientSocketFactory socket_factory; |
| 128 net::HostPortPair dest("example.com", 443); |
| 129 |
| 130 // Initial connect without credentials. The server responds with a 407. |
| 131 net::MockWrite kConnectWrites1[] = { |
| 132 net::MockWrite("CONNECT example.com:443 HTTP/1.1\r\n" |
| 133 "Host: example.com:443\r\n" |
| 134 "Proxy-Connection: keep-alive\r\n" |
| 135 "\r\n")}; |
| 136 net::MockRead kConnectReads1[] = { |
| 137 net::MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 138 "Proxy-Authenticate: Basic realm=\"test_realm\"\r\n" |
| 139 "\r\n")}; |
| 140 |
| 141 // Second connect attempt includes credentials. |
| 142 net::MockWrite kConnectWrites2[] = { |
| 143 net::MockWrite("CONNECT example.com:443 HTTP/1.1\r\n" |
| 144 "Host: example.com:443\r\n" |
| 145 "Proxy-Connection: keep-alive\r\n" |
| 146 "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" |
| 147 "\r\n")}; |
| 148 net::MockRead kConnectReads2[] = { |
| 149 net::MockRead("HTTP/1.1 200 Success\r\n\r\n")}; |
| 150 |
| 151 net::StaticSocketDataProvider kSocketData1( |
| 152 kConnectReads1, arraysize(kConnectReads1), kConnectWrites1, |
| 153 arraysize(kConnectWrites1)); |
| 154 socket_factory.AddSocketDataProvider(&kSocketData1); |
| 155 |
| 156 net::StaticSocketDataProvider kSocketData2( |
| 157 kConnectReads2, arraysize(kConnectReads2), kConnectWrites2, |
| 158 arraysize(kConnectWrites2)); |
| 159 socket_factory.AddSocketDataProvider(&kSocketData2); |
| 160 |
| 161 net::HttpAuthCache* auth_cache = |
| 162 url_request_context_getter->GetURLRequestContext() |
| 163 ->http_transaction_factory() |
| 164 ->GetSession() |
| 165 ->http_auth_cache(); |
| 166 |
| 167 // We are adding these credentials at an empty path so that it won't be picked |
| 168 // up by the preemptive authentication step and will only be picked up via |
| 169 // origin + realm + scheme lookup. |
| 170 auth_cache->Add(GURL("http://bad:99"), "test_realm", |
| 171 net::HttpAuth::AUTH_SCHEME_BASIC, |
| 172 "Basic realm=\"test_realm\"", |
| 173 net::AuthCredentials(base::ASCIIToUTF16("user"), |
| 174 base::ASCIIToUTF16("password")), |
| 175 std::string()); |
| 176 |
| 177 ProxyResolvingClientSocket proxy_resolving_socket( |
| 178 &socket_factory, url_request_context_getter, net::SSLConfig(), dest); |
| 179 |
| 180 net::TestCompletionCallback callback; |
| 181 int status = proxy_resolving_socket.Connect(callback.callback()); |
| 182 EXPECT_THAT(callback.GetResult(status), net::test::IsOk()); |
| 183 } |
| 184 |
| 185 TEST_F(ProxyResolvingClientSocketTest, ReusesHTTPAuthCache_Preemptive) { |
| 186 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter( |
| 187 new net::TestURLRequestContextGetter( |
| 188 base::ThreadTaskRunnerHandle::Get(), |
| 189 std::unique_ptr<net::TestURLRequestContext>( |
| 190 new MyTestURLRequestContext))); |
| 191 net::MockClientSocketFactory socket_factory; |
| 192 net::HostPortPair dest("example.com", 443); |
| 193 |
| 194 // Initial connect uses preemptive credentials. That is all. |
| 195 net::MockWrite kConnectWrites[] = { |
| 196 net::MockWrite("CONNECT example.com:443 HTTP/1.1\r\n" |
| 197 "Host: example.com:443\r\n" |
| 198 "Proxy-Connection: keep-alive\r\n" |
| 199 "Proxy-Authorization: Basic dXNlcjpwYXNzd29yZA==\r\n" |
| 200 "\r\n")}; |
| 201 net::MockRead kConnectReads[] = { |
| 202 net::MockRead("HTTP/1.1 200 Success\r\n\r\n")}; |
| 203 |
| 204 net::StaticSocketDataProvider kSocketData( |
| 205 kConnectReads, arraysize(kConnectReads), kConnectWrites, |
| 206 arraysize(kConnectWrites)); |
| 207 socket_factory.AddSocketDataProvider(&kSocketData); |
| 208 |
| 209 net::HttpAuthCache* auth_cache = |
| 210 url_request_context_getter->GetURLRequestContext() |
| 211 ->http_transaction_factory() |
| 212 ->GetSession() |
| 213 ->http_auth_cache(); |
| 214 |
| 215 auth_cache->Add(GURL("http://bad:99"), "test_realm", |
| 216 net::HttpAuth::AUTH_SCHEME_BASIC, |
| 217 "Basic realm=\"test_realm\"", |
| 218 net::AuthCredentials(base::ASCIIToUTF16("user"), |
| 219 base::ASCIIToUTF16("password")), |
| 220 "/"); |
| 221 |
| 222 ProxyResolvingClientSocket proxy_resolving_socket( |
| 223 &socket_factory, url_request_context_getter, net::SSLConfig(), dest); |
| 224 |
| 225 net::TestCompletionCallback callback; |
| 226 int status = proxy_resolving_socket.Connect(callback.callback()); |
| 227 EXPECT_THAT(callback.GetResult(status), net::test::IsOk()); |
| 228 } |
| 229 |
| 230 TEST_F(ProxyResolvingClientSocketTest, ReusesHTTPAuthCache_NoCredentials) { |
| 231 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter( |
| 232 new net::TestURLRequestContextGetter( |
| 233 base::ThreadTaskRunnerHandle::Get(), |
| 234 std::unique_ptr<net::TestURLRequestContext>( |
| 235 new MyTestURLRequestContext))); |
| 236 net::MockClientSocketFactory socket_factory; |
| 237 net::HostPortPair dest("example.com", 443); |
| 238 |
| 239 // Initial connect uses preemptive credentials. That is all. |
| 240 net::MockWrite kConnectWrites[] = { |
| 241 net::MockWrite("CONNECT example.com:443 HTTP/1.1\r\n" |
| 242 "Host: example.com:443\r\n" |
| 243 "Proxy-Connection: keep-alive\r\n" |
| 244 "\r\n")}; |
| 245 net::MockRead kConnectReads[] = { |
| 246 net::MockRead("HTTP/1.1 407 Proxy Authentication Required\r\n" |
| 247 "Proxy-Authenticate: Basic realm=\"test_realm\"\r\n" |
| 248 "\r\n")}; |
| 249 |
| 250 net::StaticSocketDataProvider kSocketData( |
| 251 kConnectReads, arraysize(kConnectReads), kConnectWrites, |
| 252 arraysize(kConnectWrites)); |
| 253 socket_factory.AddSocketDataProvider(&kSocketData); |
| 254 |
| 255 ProxyResolvingClientSocket proxy_resolving_socket( |
| 256 &socket_factory, url_request_context_getter, net::SSLConfig(), dest); |
| 257 |
| 258 net::TestCompletionCallback callback; |
| 259 int status = proxy_resolving_socket.Connect(callback.callback()); |
| 260 EXPECT_THAT(callback.GetResult(status), net::ERR_PROXY_AUTH_REQUESTED); |
| 261 } |
| 262 |
119 // TODO(sanjeevr): Add more unit-tests. | 263 // TODO(sanjeevr): Add more unit-tests. |
120 } // namespace jingle_glue | 264 } // namespace jingle_glue |
OLD | NEW |