OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "net/http/http_auth_controller.h" | 5 #include "net/http/http_auth_controller.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
9 #include "net/base/test_completion_callback.h" | 9 #include "net/base/test_completion_callback.h" |
10 #include "net/http/http_auth_cache.h" | 10 #include "net/http/http_auth_cache.h" |
11 #include "net/http/http_auth_challenge_tokenizer.h" | 11 #include "net/http/http_auth_challenge_tokenizer.h" |
12 #include "net/http/http_auth_handler_mock.h" | 12 #include "net/http/http_auth_handler_mock.h" |
13 #include "net/http/http_request_info.h" | 13 #include "net/http/http_request_info.h" |
14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
15 #include "net/http/http_util.h" | 15 #include "net/http/http_util.h" |
16 #include "net/log/net_log.h" | 16 #include "net/log/net_log.h" |
| 17 #include "net/ssl/ssl_info.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 enum HandlerRunMode { | 24 enum HandlerRunMode { |
24 RUN_HANDLER_SYNC, | 25 RUN_HANDLER_SYNC, |
25 RUN_HANDLER_ASYNC | 26 RUN_HANDLER_ASYNC |
26 }; | 27 }; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); | 66 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
66 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), | 67 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), |
67 handler_rv); | 68 handler_rv); |
68 auth_handler_factory.AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); | 69 auth_handler_factory.AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
69 auth_handler_factory.set_do_init_from_challenge(true); | 70 auth_handler_factory.set_do_init_from_challenge(true); |
70 | 71 |
71 scoped_refptr<HttpAuthController> controller( | 72 scoped_refptr<HttpAuthController> controller( |
72 new HttpAuthController(HttpAuth::AUTH_PROXY, | 73 new HttpAuthController(HttpAuth::AUTH_PROXY, |
73 GURL("http://example.com"), | 74 GURL("http://example.com"), |
74 &dummy_auth_cache, &auth_handler_factory)); | 75 &dummy_auth_cache, &auth_handler_factory)); |
75 ASSERT_EQ(OK, | 76 SSLInfo null_ssl_info; |
76 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 77 ASSERT_EQ(OK, controller->HandleAuthChallenge(headers, null_ssl_info, false, |
| 78 false, dummy_log)); |
77 ASSERT_TRUE(controller->HaveAuthHandler()); | 79 ASSERT_TRUE(controller->HaveAuthHandler()); |
78 controller->ResetAuth(AuthCredentials()); | 80 controller->ResetAuth(AuthCredentials()); |
79 EXPECT_TRUE(controller->HaveAuth()); | 81 EXPECT_TRUE(controller->HaveAuth()); |
80 | 82 |
81 TestCompletionCallback callback; | 83 TestCompletionCallback callback; |
82 EXPECT_EQ((run_mode == RUN_HANDLER_ASYNC)? ERR_IO_PENDING: | 84 EXPECT_EQ((run_mode == RUN_HANDLER_ASYNC)? ERR_IO_PENDING: |
83 expected_controller_rv, | 85 expected_controller_rv, |
84 controller->MaybeGenerateAuthToken(&request, callback.callback(), | 86 controller->MaybeGenerateAuthToken(&request, callback.callback(), |
85 dummy_log)); | 87 dummy_log)); |
86 if (run_mode == RUN_HANDLER_ASYNC) | 88 if (run_mode == RUN_HANDLER_ASYNC) |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) { | 123 TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) { |
122 // Modified mock HttpAuthHandler for this test. | 124 // Modified mock HttpAuthHandler for this test. |
123 class MockHandler : public HttpAuthHandlerMock { | 125 class MockHandler : public HttpAuthHandlerMock { |
124 public: | 126 public: |
125 MockHandler(int expected_rv, HttpAuth::Scheme scheme) | 127 MockHandler(int expected_rv, HttpAuth::Scheme scheme) |
126 : expected_scheme_(scheme) { | 128 : expected_scheme_(scheme) { |
127 SetGenerateExpectation(false, expected_rv); | 129 SetGenerateExpectation(false, expected_rv); |
128 } | 130 } |
129 | 131 |
130 protected: | 132 protected: |
131 bool Init(HttpAuthChallengeTokenizer* challenge) override { | 133 bool Init(HttpAuthChallengeTokenizer* challenge, |
132 HttpAuthHandlerMock::Init(challenge); | 134 const SSLInfo& ssl_info) override { |
| 135 HttpAuthHandlerMock::Init(challenge, ssl_info); |
133 set_allows_default_credentials(true); | 136 set_allows_default_credentials(true); |
134 set_allows_explicit_credentials(false); | 137 set_allows_explicit_credentials(false); |
135 set_connection_based(true); | 138 set_connection_based(true); |
136 // Pretend to be SCHEME_BASIC so we can test failover logic. | 139 // Pretend to be SCHEME_BASIC so we can test failover logic. |
137 if (challenge->scheme() == "Basic") { | 140 if (challenge->scheme() == "Basic") { |
138 auth_scheme_ = HttpAuth::AUTH_SCHEME_BASIC; | 141 auth_scheme_ = HttpAuth::AUTH_SCHEME_BASIC; |
139 --score_; // Reduce score, so we rank below Mock. | 142 --score_; // Reduce score, so we rank below Mock. |
140 set_allows_explicit_credentials(true); | 143 set_allows_explicit_credentials(true); |
141 } | 144 } |
142 EXPECT_EQ(expected_scheme_, auth_scheme_); | 145 EXPECT_EQ(expected_scheme_, auth_scheme_); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 HttpAuth::AUTH_SERVER); | 205 HttpAuth::AUTH_SERVER); |
203 auth_handler_factory.AddMockHandler( | 206 auth_handler_factory.AddMockHandler( |
204 new MockHandler(OK, HttpAuth::AUTH_SCHEME_BASIC), | 207 new MockHandler(OK, HttpAuth::AUTH_SCHEME_BASIC), |
205 HttpAuth::AUTH_SERVER); | 208 HttpAuth::AUTH_SERVER); |
206 auth_handler_factory.set_do_init_from_challenge(true); | 209 auth_handler_factory.set_do_init_from_challenge(true); |
207 | 210 |
208 scoped_refptr<HttpAuthController> controller( | 211 scoped_refptr<HttpAuthController> controller( |
209 new HttpAuthController(HttpAuth::AUTH_SERVER, | 212 new HttpAuthController(HttpAuth::AUTH_SERVER, |
210 GURL("http://example.com"), | 213 GURL("http://example.com"), |
211 &dummy_auth_cache, &auth_handler_factory)); | 214 &dummy_auth_cache, &auth_handler_factory)); |
212 ASSERT_EQ(OK, | 215 SSLInfo null_ssl_info; |
213 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 216 ASSERT_EQ(OK, controller->HandleAuthChallenge(headers, null_ssl_info, false, |
| 217 false, dummy_log)); |
214 ASSERT_TRUE(controller->HaveAuthHandler()); | 218 ASSERT_TRUE(controller->HaveAuthHandler()); |
215 controller->ResetAuth(AuthCredentials()); | 219 controller->ResetAuth(AuthCredentials()); |
216 EXPECT_TRUE(controller->HaveAuth()); | 220 EXPECT_TRUE(controller->HaveAuth()); |
217 | 221 |
218 // Should only succeed if we are using the AUTH_SCHEME_MOCK MockHandler. | 222 // Should only succeed if we are using the AUTH_SCHEME_MOCK MockHandler. |
219 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 223 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( |
220 &request, CompletionCallback(), dummy_log)); | 224 &request, CompletionCallback(), dummy_log)); |
221 controller->AddAuthorizationHeader(&request_headers); | 225 controller->AddAuthorizationHeader(&request_headers); |
222 | 226 |
223 // Once a token is generated, simulate the receipt of a server response | 227 // Once a token is generated, simulate the receipt of a server response |
224 // indicating that the authentication attempt was rejected. | 228 // indicating that the authentication attempt was rejected. |
225 ASSERT_EQ(OK, | 229 ASSERT_EQ(OK, controller->HandleAuthChallenge(headers, null_ssl_info, false, |
226 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 230 false, dummy_log)); |
227 ASSERT_TRUE(controller->HaveAuthHandler()); | 231 ASSERT_TRUE(controller->HaveAuthHandler()); |
228 controller->ResetAuth(AuthCredentials(base::ASCIIToUTF16("Hello"), | 232 controller->ResetAuth(AuthCredentials(base::ASCIIToUTF16("Hello"), |
229 base::string16())); | 233 base::string16())); |
230 EXPECT_TRUE(controller->HaveAuth()); | 234 EXPECT_TRUE(controller->HaveAuth()); |
231 EXPECT_TRUE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_MOCK)); | 235 EXPECT_TRUE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_MOCK)); |
232 EXPECT_FALSE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_BASIC)); | 236 EXPECT_FALSE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_BASIC)); |
233 | 237 |
234 // Should only succeed if we are using the AUTH_SCHEME_BASIC MockHandler. | 238 // Should only succeed if we are using the AUTH_SCHEME_BASIC MockHandler. |
235 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 239 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( |
236 &request, CompletionCallback(), dummy_log)); | 240 &request, CompletionCallback(), dummy_log)); |
237 } | 241 } |
238 | 242 |
239 } // namespace net | 243 } // namespace net |
OLD | NEW |