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_response_info.h" |
15 #include "net/http/http_util.h" | 16 #include "net/http/http_util.h" |
16 #include "net/log/net_log.h" | 17 #include "net/log/net_log.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, |
(...skipping 24 matching lines...) Expand all Loading... |
49 int handler_rv, | 50 int handler_rv, |
50 int expected_controller_rv, | 51 int expected_controller_rv, |
51 SchemeState scheme_state) { | 52 SchemeState scheme_state) { |
52 BoundNetLog dummy_log; | 53 BoundNetLog dummy_log; |
53 HttpAuthCache dummy_auth_cache; | 54 HttpAuthCache dummy_auth_cache; |
54 | 55 |
55 HttpRequestInfo request; | 56 HttpRequestInfo request; |
56 request.method = "GET"; | 57 request.method = "GET"; |
57 request.url = GURL("http://example.com"); | 58 request.url = GURL("http://example.com"); |
58 | 59 |
59 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( | 60 HttpResponseInfo response_info; |
| 61 response_info.headers = HeadersFromString( |
60 "HTTP/1.1 407\r\n" | 62 "HTTP/1.1 407\r\n" |
61 "Proxy-Authenticate: MOCK foo\r\n" | 63 "Proxy-Authenticate: MOCK foo\r\n" |
62 "\r\n")); | 64 "\r\n"); |
63 | 65 |
64 HttpAuthHandlerMock::Factory auth_handler_factory; | 66 HttpAuthHandlerMock::Factory auth_handler_factory; |
65 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); | 67 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
66 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), | 68 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), |
67 handler_rv); | 69 handler_rv); |
68 auth_handler_factory.AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); | 70 auth_handler_factory.AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
69 auth_handler_factory.set_do_init_from_challenge(true); | 71 auth_handler_factory.set_do_init_from_challenge(true); |
70 | 72 |
71 scoped_refptr<HttpAuthController> controller( | 73 scoped_refptr<HttpAuthController> controller( |
72 new HttpAuthController(HttpAuth::AUTH_PROXY, | 74 new HttpAuthController(HttpAuth::AUTH_PROXY, |
73 GURL("http://example.com"), | 75 GURL("http://example.com"), |
74 &dummy_auth_cache, &auth_handler_factory)); | 76 &dummy_auth_cache, &auth_handler_factory)); |
75 ASSERT_EQ(OK, | 77 ASSERT_EQ(OK, controller->HandleAuthChallenge(response_info, false, false, |
76 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 78 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 HttpResponseInfo& response_info) override { |
| 135 HttpAuthHandlerMock::Init(challenge, response_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 17 matching lines...) Expand all Loading... |
160 private: | 163 private: |
161 HttpAuth::Scheme expected_scheme_; | 164 HttpAuth::Scheme expected_scheme_; |
162 }; | 165 }; |
163 | 166 |
164 BoundNetLog dummy_log; | 167 BoundNetLog dummy_log; |
165 HttpAuthCache dummy_auth_cache; | 168 HttpAuthCache dummy_auth_cache; |
166 HttpRequestInfo request; | 169 HttpRequestInfo request; |
167 request.method = "GET"; | 170 request.method = "GET"; |
168 request.url = GURL("http://example.com"); | 171 request.url = GURL("http://example.com"); |
169 | 172 |
170 HttpRequestHeaders request_headers; | 173 HttpResponseInfo response_info; |
171 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( | 174 response_info.headers = HeadersFromString( |
172 "HTTP/1.1 401\r\n" | 175 "HTTP/1.1 401\r\n" |
173 "WWW-Authenticate: Mock\r\n" | 176 "WWW-Authenticate: Mock\r\n" |
174 "WWW-Authenticate: Basic\r\n" | 177 "WWW-Authenticate: Basic\r\n" |
175 "\r\n")); | 178 "\r\n"); |
176 | 179 |
177 HttpAuthHandlerMock::Factory auth_handler_factory; | 180 HttpAuthHandlerMock::Factory auth_handler_factory; |
178 | 181 |
179 // Handlers for the first attempt at authentication. AUTH_SCHEME_MOCK handler | 182 // Handlers for the first attempt at authentication. AUTH_SCHEME_MOCK handler |
180 // accepts the default identity and successfully constructs a token. | 183 // accepts the default identity and successfully constructs a token. |
181 auth_handler_factory.AddMockHandler( | 184 auth_handler_factory.AddMockHandler( |
182 new MockHandler(OK, HttpAuth::AUTH_SCHEME_MOCK), HttpAuth::AUTH_SERVER); | 185 new MockHandler(OK, HttpAuth::AUTH_SCHEME_MOCK), HttpAuth::AUTH_SERVER); |
183 auth_handler_factory.AddMockHandler( | 186 auth_handler_factory.AddMockHandler( |
184 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_BASIC), | 187 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_BASIC), |
185 HttpAuth::AUTH_SERVER); | 188 HttpAuth::AUTH_SERVER); |
(...skipping 16 matching lines...) Expand all 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 ASSERT_EQ(OK, controller->HandleAuthChallenge(response_info, false, false, |
213 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 216 dummy_log)); |
214 ASSERT_TRUE(controller->HaveAuthHandler()); | 217 ASSERT_TRUE(controller->HaveAuthHandler()); |
215 controller->ResetAuth(AuthCredentials()); | 218 controller->ResetAuth(AuthCredentials()); |
216 EXPECT_TRUE(controller->HaveAuth()); | 219 EXPECT_TRUE(controller->HaveAuth()); |
217 | 220 |
218 // Should only succeed if we are using the AUTH_SCHEME_MOCK MockHandler. | 221 // Should only succeed if we are using the AUTH_SCHEME_MOCK MockHandler. |
219 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 222 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( |
220 &request, CompletionCallback(), dummy_log)); | 223 &request, CompletionCallback(), dummy_log)); |
| 224 HttpRequestHeaders request_headers; |
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(response_info, false, false, |
226 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 230 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 |