| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <math.h> // ceil | 5 #include <math.h> // ceil |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "net/base/completion_callback.h" | 8 #include "net/base/completion_callback.h" |
| 9 #include "net/base/mock_host_resolver.h" | 9 #include "net/base/mock_host_resolver.h" |
| 10 #include "net/base/ssl_info.h" | 10 #include "net/base/ssl_info.h" |
| (...skipping 1476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1487 EXPECT_EQ(ERR_IO_PENDING, rv); | 1487 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1488 | 1488 |
| 1489 rv = callback3.WaitForResult(); | 1489 rv = callback3.WaitForResult(); |
| 1490 EXPECT_EQ(OK, rv); | 1490 EXPECT_EQ(OK, rv); |
| 1491 | 1491 |
| 1492 response = trans->GetResponseInfo(); | 1492 response = trans->GetResponseInfo(); |
| 1493 EXPECT_TRUE(response->auth_challenge.get() == NULL); | 1493 EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1494 EXPECT_EQ(100, response->headers->GetContentLength()); | 1494 EXPECT_EQ(100, response->headers->GetContentLength()); |
| 1495 } | 1495 } |
| 1496 | 1496 |
| 1497 // For the NTLM implementation using SSPI, we skip the NTLM tests since we |
| 1498 // can't hook into its internals to cause it to generate predictable NTLM |
| 1499 // authorization headers. |
| 1500 #if defined(NTLM_PORTABLE) |
| 1497 // The NTLM authentication unit tests were generated by capturing the HTTP | 1501 // The NTLM authentication unit tests were generated by capturing the HTTP |
| 1498 // requests and responses using Fiddler 2 and inspecting the generated random | 1502 // requests and responses using Fiddler 2 and inspecting the generated random |
| 1499 // bytes in the debugger. | 1503 // bytes in the debugger. |
| 1500 | 1504 |
| 1501 // Enter the correct password and authenticate successfully. | 1505 // Enter the correct password and authenticate successfully. |
| 1502 TEST_F(HttpNetworkTransactionTest, NTLMAuth1) { | 1506 TEST_F(HttpNetworkTransactionTest, NTLMAuth1) { |
| 1503 HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1, | 1507 HttpAuthHandlerNTLM::ScopedProcSetter proc_setter(MockGenerateRandom1, |
| 1504 MockGetHostName); | 1508 MockGetHostName); |
| 1505 SessionDependencies session_deps; | 1509 SessionDependencies session_deps; |
| 1506 scoped_ptr<HttpTransaction> trans( | 1510 scoped_ptr<HttpTransaction> trans( |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1815 rv = trans->RestartWithAuth(L"testing-ntlm", L"testing-ntlm", &callback5); | 1819 rv = trans->RestartWithAuth(L"testing-ntlm", L"testing-ntlm", &callback5); |
| 1816 EXPECT_EQ(ERR_IO_PENDING, rv); | 1820 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1817 | 1821 |
| 1818 rv = callback5.WaitForResult(); | 1822 rv = callback5.WaitForResult(); |
| 1819 EXPECT_EQ(OK, rv); | 1823 EXPECT_EQ(OK, rv); |
| 1820 | 1824 |
| 1821 response = trans->GetResponseInfo(); | 1825 response = trans->GetResponseInfo(); |
| 1822 EXPECT_TRUE(response->auth_challenge.get() == NULL); | 1826 EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 1823 EXPECT_EQ(13, response->headers->GetContentLength()); | 1827 EXPECT_EQ(13, response->headers->GetContentLength()); |
| 1824 } | 1828 } |
| 1829 #endif // NTLM_PORTABLE |
| 1825 | 1830 |
| 1826 // Test reading a server response which has only headers, and no body. | 1831 // Test reading a server response which has only headers, and no body. |
| 1827 // After some maximum number of bytes is consumed, the transaction should | 1832 // After some maximum number of bytes is consumed, the transaction should |
| 1828 // fail with ERR_RESPONSE_HEADERS_TOO_BIG. | 1833 // fail with ERR_RESPONSE_HEADERS_TOO_BIG. |
| 1829 TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { | 1834 TEST_F(HttpNetworkTransactionTest, LargeHeadersNoBody) { |
| 1830 SessionDependencies session_deps; | 1835 SessionDependencies session_deps; |
| 1831 scoped_ptr<HttpTransaction> trans( | 1836 scoped_ptr<HttpTransaction> trans( |
| 1832 new HttpNetworkTransaction( | 1837 new HttpNetworkTransaction( |
| 1833 CreateSession(&session_deps), | 1838 CreateSession(&session_deps), |
| 1834 &session_deps.socket_factory)); | 1839 &session_deps.socket_factory)); |
| (...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2118 std::string response_data; | 2123 std::string response_data; |
| 2119 rv = ReadTransaction(trans.get(), &response_data); | 2124 rv = ReadTransaction(trans.get(), &response_data); |
| 2120 EXPECT_EQ(OK, rv); | 2125 EXPECT_EQ(OK, rv); |
| 2121 EXPECT_EQ(kExpectedResponseData[i], response_data); | 2126 EXPECT_EQ(kExpectedResponseData[i], response_data); |
| 2122 } | 2127 } |
| 2123 } | 2128 } |
| 2124 | 2129 |
| 2125 // Test the request-challenge-retry sequence for basic auth when there is | 2130 // Test the request-challenge-retry sequence for basic auth when there is |
| 2126 // an identity in the URL. The request should be sent as normal, but when | 2131 // an identity in the URL. The request should be sent as normal, but when |
| 2127 // it fails the identity from the URL is used to answer the challenge. | 2132 // it fails the identity from the URL is used to answer the challenge. |
| 2128 TEST_F(HttpNetworkTransactionTest, AuthIdentityInUrl) { | 2133 TEST_F(HttpNetworkTransactionTest, AuthIdentityInURL) { |
| 2129 SessionDependencies session_deps; | 2134 SessionDependencies session_deps; |
| 2130 scoped_ptr<HttpTransaction> trans( | 2135 scoped_ptr<HttpTransaction> trans( |
| 2131 new HttpNetworkTransaction( | 2136 new HttpNetworkTransaction( |
| 2132 CreateSession(&session_deps), | 2137 CreateSession(&session_deps), |
| 2133 &session_deps.socket_factory)); | 2138 &session_deps.socket_factory)); |
| 2134 | 2139 |
| 2135 HttpRequestInfo request; | 2140 HttpRequestInfo request; |
| 2136 request.method = "GET"; | 2141 request.method = "GET"; |
| 2137 // Note: the URL has a username:password in it. | 2142 // Note: the URL has a username:password in it. |
| 2138 request.url = GURL("http://foo:bar@www.google.com/"); | 2143 request.url = GURL("http://foo:bar@www.google.com/"); |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2192 | 2197 |
| 2193 // There is no challenge info, since the identity in URL worked. | 2198 // There is no challenge info, since the identity in URL worked. |
| 2194 EXPECT_TRUE(response->auth_challenge.get() == NULL); | 2199 EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2195 | 2200 |
| 2196 EXPECT_EQ(100, response->headers->GetContentLength()); | 2201 EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2197 | 2202 |
| 2198 // Empty the current queue. | 2203 // Empty the current queue. |
| 2199 MessageLoop::current()->RunAllPending(); | 2204 MessageLoop::current()->RunAllPending(); |
| 2200 } | 2205 } |
| 2201 | 2206 |
| 2207 // Test the request-challenge-retry sequence for basic auth when there is |
| 2208 // an incorrect identity in the URL. The identity from the URL should be used |
| 2209 // only once. |
| 2210 TEST_F(HttpNetworkTransactionTest, WrongAuthIdentityInURL) { |
| 2211 SessionDependencies session_deps; |
| 2212 scoped_ptr<HttpTransaction> trans( |
| 2213 new HttpNetworkTransaction( |
| 2214 CreateSession(&session_deps), |
| 2215 &session_deps.socket_factory)); |
| 2216 |
| 2217 HttpRequestInfo request; |
| 2218 request.method = "GET"; |
| 2219 // Note: the URL has a username:password in it. The password "baz" is |
| 2220 // wrong (should be "bar"). |
| 2221 request.url = GURL("http://foo:baz@www.google.com/"); |
| 2222 |
| 2223 request.load_flags = 0; |
| 2224 |
| 2225 MockWrite data_writes1[] = { |
| 2226 MockWrite("GET / HTTP/1.1\r\n" |
| 2227 "Host: www.google.com\r\n" |
| 2228 "Connection: keep-alive\r\n\r\n"), |
| 2229 }; |
| 2230 |
| 2231 MockRead data_reads1[] = { |
| 2232 MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2233 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2234 MockRead("Content-Length: 10\r\n\r\n"), |
| 2235 MockRead(false, ERR_FAILED), |
| 2236 }; |
| 2237 |
| 2238 // After the challenge above, the transaction will be restarted using the |
| 2239 // identity from the url (foo, baz) to answer the challenge. |
| 2240 MockWrite data_writes2[] = { |
| 2241 MockWrite("GET / HTTP/1.1\r\n" |
| 2242 "Host: www.google.com\r\n" |
| 2243 "Connection: keep-alive\r\n" |
| 2244 "Authorization: Basic Zm9vOmJheg==\r\n\r\n"), |
| 2245 }; |
| 2246 |
| 2247 MockRead data_reads2[] = { |
| 2248 MockRead("HTTP/1.0 401 Unauthorized\r\n"), |
| 2249 MockRead("WWW-Authenticate: Basic realm=\"MyRealm1\"\r\n"), |
| 2250 MockRead("Content-Length: 10\r\n\r\n"), |
| 2251 MockRead(false, ERR_FAILED), |
| 2252 }; |
| 2253 |
| 2254 // After the challenge above, the transaction will be restarted using the |
| 2255 // identity supplied by the user (foo, bar) to answer the challenge. |
| 2256 MockWrite data_writes3[] = { |
| 2257 MockWrite("GET / HTTP/1.1\r\n" |
| 2258 "Host: www.google.com\r\n" |
| 2259 "Connection: keep-alive\r\n" |
| 2260 "Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), |
| 2261 }; |
| 2262 |
| 2263 MockRead data_reads3[] = { |
| 2264 MockRead("HTTP/1.0 200 OK\r\n"), |
| 2265 MockRead("Content-Length: 100\r\n\r\n"), |
| 2266 MockRead(false, OK), |
| 2267 }; |
| 2268 |
| 2269 StaticMockSocket data1(data_reads1, data_writes1); |
| 2270 StaticMockSocket data2(data_reads2, data_writes2); |
| 2271 StaticMockSocket data3(data_reads3, data_writes3); |
| 2272 session_deps.socket_factory.AddMockSocket(&data1); |
| 2273 session_deps.socket_factory.AddMockSocket(&data2); |
| 2274 session_deps.socket_factory.AddMockSocket(&data3); |
| 2275 |
| 2276 TestCompletionCallback callback1; |
| 2277 |
| 2278 int rv = trans->Start(&request, &callback1); |
| 2279 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2280 |
| 2281 rv = callback1.WaitForResult(); |
| 2282 EXPECT_EQ(OK, rv); |
| 2283 |
| 2284 EXPECT_TRUE(trans->IsReadyToRestartForAuth()); |
| 2285 TestCompletionCallback callback2; |
| 2286 rv = trans->RestartWithAuth(std::wstring(), std::wstring(), &callback2); |
| 2287 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2288 rv = callback2.WaitForResult(); |
| 2289 EXPECT_EQ(OK, rv); |
| 2290 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2291 |
| 2292 const HttpResponseInfo* response = trans->GetResponseInfo(); |
| 2293 EXPECT_FALSE(response == NULL); |
| 2294 // The password prompt info should have been set in response->auth_challenge. |
| 2295 EXPECT_FALSE(response->auth_challenge.get() == NULL); |
| 2296 |
| 2297 EXPECT_EQ(L"www.google.com:80", response->auth_challenge->host_and_port); |
| 2298 EXPECT_EQ(L"MyRealm1", response->auth_challenge->realm); |
| 2299 EXPECT_EQ(L"basic", response->auth_challenge->scheme); |
| 2300 |
| 2301 TestCompletionCallback callback3; |
| 2302 rv = trans->RestartWithAuth(L"foo", L"bar", &callback3); |
| 2303 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 2304 rv = callback3.WaitForResult(); |
| 2305 EXPECT_EQ(OK, rv); |
| 2306 EXPECT_FALSE(trans->IsReadyToRestartForAuth()); |
| 2307 |
| 2308 response = trans->GetResponseInfo(); |
| 2309 EXPECT_FALSE(response == NULL); |
| 2310 |
| 2311 // There is no challenge info, since the identity worked. |
| 2312 EXPECT_TRUE(response->auth_challenge.get() == NULL); |
| 2313 |
| 2314 EXPECT_EQ(100, response->headers->GetContentLength()); |
| 2315 |
| 2316 // Empty the current queue. |
| 2317 MessageLoop::current()->RunAllPending(); |
| 2318 } |
| 2319 |
| 2202 // Test that previously tried username/passwords for a realm get re-used. | 2320 // Test that previously tried username/passwords for a realm get re-used. |
| 2203 TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { | 2321 TEST_F(HttpNetworkTransactionTest, BasicAuthCacheAndPreauth) { |
| 2204 SessionDependencies session_deps; | 2322 SessionDependencies session_deps; |
| 2205 scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); | 2323 scoped_refptr<HttpNetworkSession> session = CreateSession(&session_deps); |
| 2206 | 2324 |
| 2207 // Transaction 1: authenticate (foo, bar) on MyRealm1 | 2325 // Transaction 1: authenticate (foo, bar) on MyRealm1 |
| 2208 { | 2326 { |
| 2209 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( | 2327 scoped_ptr<HttpTransaction> trans(new HttpNetworkTransaction( |
| 2210 session, &session_deps.socket_factory)); | 2328 session, &session_deps.socket_factory)); |
| 2211 | 2329 |
| (...skipping 1332 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3544 rv = trans->Start(&request, &callback); | 3662 rv = trans->Start(&request, &callback); |
| 3545 ASSERT_EQ(ERR_IO_PENDING, rv); | 3663 ASSERT_EQ(ERR_IO_PENDING, rv); |
| 3546 rv = callback.WaitForResult(); | 3664 rv = callback.WaitForResult(); |
| 3547 | 3665 |
| 3548 // If we bypassed the cache, we would have gotten a failure while resolving | 3666 // If we bypassed the cache, we would have gotten a failure while resolving |
| 3549 // "www.google.com". | 3667 // "www.google.com". |
| 3550 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); | 3668 EXPECT_EQ(ERR_NAME_NOT_RESOLVED, rv); |
| 3551 } | 3669 } |
| 3552 | 3670 |
| 3553 } // namespace net | 3671 } // namespace net |
| OLD | NEW |