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" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 HttpRequestInfo request; | 55 HttpRequestInfo request; |
56 request.method = "GET"; | 56 request.method = "GET"; |
57 request.url = GURL("http://example.com"); | 57 request.url = GURL("http://example.com"); |
58 | 58 |
59 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( | 59 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( |
60 "HTTP/1.1 407\r\n" | 60 "HTTP/1.1 407\r\n" |
61 "Proxy-Authenticate: MOCK foo\r\n" | 61 "Proxy-Authenticate: MOCK foo\r\n" |
62 "\r\n")); | 62 "\r\n")); |
63 | 63 |
64 HttpAuthHandlerMock::Factory auth_handler_factory; | 64 HttpAuthHandlerMock::Factory auth_handler_factory; |
65 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); | 65 scoped_ptr<HttpAuthHandlerMock> auth_handler(new HttpAuthHandlerMock()); |
66 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), | 66 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), |
67 handler_rv); | 67 handler_rv); |
68 auth_handler_factory.AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); | 68 auth_handler_factory.AddMockHandler(auth_handler.Pass(), |
69 auth_handler_factory.set_do_init_from_challenge(true); | 69 HttpAuthHandlerFactory::CREATE_CHALLENGE, |
| 70 HttpAuth::AUTH_PROXY); |
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 ASSERT_EQ(OK, |
76 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 77 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
77 ASSERT_TRUE(controller->HaveAuthHandler()); | 78 ASSERT_TRUE(controller->HaveAuthHandler()); |
78 controller->ResetAuth(AuthCredentials()); | 79 controller->ResetAuth( |
| 80 AuthCredentials(base::ASCIIToUTF16("a"), base::ASCIIToUTF16("b"))); |
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) |
87 EXPECT_EQ(expected_controller_rv, callback.WaitForResult()); | 89 EXPECT_EQ(expected_controller_rv, callback.WaitForResult()); |
88 EXPECT_EQ((scheme_state == SCHEME_IS_DISABLED), | 90 EXPECT_EQ((scheme_state == SCHEME_IS_DISABLED), |
(...skipping 23 matching lines...) Expand all Loading... |
112 | 114 |
113 // If a non-permanent error is returned by the handler, then the | 115 // If a non-permanent error is returned by the handler, then the |
114 // controller should report it unchanged. | 116 // controller should report it unchanged. |
115 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_INVALID_AUTH_CREDENTIALS, | 117 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_INVALID_AUTH_CREDENTIALS, |
116 ERR_INVALID_AUTH_CREDENTIALS, SCHEME_IS_ENABLED); | 118 ERR_INVALID_AUTH_CREDENTIALS, SCHEME_IS_ENABLED); |
117 } | 119 } |
118 | 120 |
119 // If an HttpAuthHandler indicates that it doesn't allow explicit | 121 // If an HttpAuthHandler indicates that it doesn't allow explicit |
120 // credentials, don't prompt for credentials. | 122 // credentials, don't prompt for credentials. |
121 TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) { | 123 TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) { |
122 // Modified mock HttpAuthHandler for this test. | |
123 class MockHandler : public HttpAuthHandlerMock { | |
124 public: | |
125 MockHandler(int expected_rv, const std::string& scheme) | |
126 : expected_scheme_(scheme) { | |
127 SetGenerateExpectation(false, expected_rv); | |
128 } | |
129 | |
130 protected: | |
131 bool Init(HttpAuthChallengeTokenizer* challenge) override { | |
132 HttpAuthHandlerMock::Init(challenge); | |
133 set_allows_default_credentials(true); | |
134 set_allows_explicit_credentials(false); | |
135 // Pretend to be Bert so we can test failover logic. | |
136 if (challenge->SchemeIs("bert")) { | |
137 auth_scheme_ = "bert"; | |
138 set_allows_explicit_credentials(true); | |
139 } else { | |
140 auth_scheme_ = "ernie"; | |
141 } | |
142 EXPECT_EQ(expected_scheme_, auth_scheme_); | |
143 return true; | |
144 } | |
145 | |
146 int GenerateAuthTokenImpl(const AuthCredentials* credentials, | |
147 const HttpRequestInfo* request, | |
148 const CompletionCallback& callback, | |
149 std::string* auth_token) override { | |
150 int result = | |
151 HttpAuthHandlerMock::GenerateAuthTokenImpl(credentials, | |
152 request, callback, | |
153 auth_token); | |
154 EXPECT_TRUE(result != OK || | |
155 !AllowsExplicitCredentials() || | |
156 !credentials->Empty()); | |
157 return result; | |
158 } | |
159 | |
160 private: | |
161 std::string expected_scheme_; | |
162 }; | |
163 | |
164 BoundNetLog dummy_log; | 124 BoundNetLog dummy_log; |
165 HttpAuthCache dummy_auth_cache; | 125 HttpAuthCache dummy_auth_cache; |
166 HttpRequestInfo request; | 126 HttpRequestInfo request; |
167 request.method = "GET"; | 127 request.method = "GET"; |
168 request.url = GURL("http://example.com"); | 128 request.url = GURL("http://example.com"); |
169 | 129 |
170 HttpRequestHeaders request_headers; | 130 HttpRequestHeaders request_headers; |
171 scoped_refptr<HttpResponseHeaders> headers( | 131 scoped_refptr<HttpResponseHeaders> headers( |
172 HeadersFromString("HTTP/1.1 401\r\n" | 132 HeadersFromString("HTTP/1.1 401\r\n" |
173 "WWW-Authenticate: Ernie\r\n" | 133 "WWW-Authenticate: Ernie\r\n" |
174 "WWW-Authenticate: Bert\r\n" | 134 "WWW-Authenticate: Bert\r\n" |
175 "\r\n")); | 135 "\r\n")); |
176 | 136 |
177 HttpAuthHandlerMock::Factory auth_handler_factory; | 137 HttpAuthHandlerMock::Factory auth_handler_factory; |
178 auth_handler_factory.set_do_init_from_challenge(true); | |
179 | 138 |
180 // Handler for the first attempt at authentication. "Ernie" handler accepts | 139 // Handler for the first attempt at authentication. "Ernie" handler accepts |
181 // the default identity and successfully constructs a token. | 140 // the default identity and successfully constructs a token. Handler for the |
182 auth_handler_factory.AddMockHandler(new MockHandler(OK, "ernie"), | 141 scoped_ptr<HttpAuthHandlerMock> auth_handler(new HttpAuthHandlerMock()); |
| 142 auth_handler->set_allows_default_credentials(true); |
| 143 auth_handler->set_allows_explicit_credentials(false); |
| 144 auth_handler->set_expected_auth_scheme("ernie"); |
| 145 auth_handler_factory.AddMockHandler(auth_handler.Pass(), |
| 146 HttpAuthHandlerFactory::CREATE_CHALLENGE, |
183 HttpAuth::AUTH_SERVER); | 147 HttpAuth::AUTH_SERVER); |
184 | 148 |
185 scoped_refptr<HttpAuthController> controller( | 149 scoped_refptr<HttpAuthController> controller( |
186 new HttpAuthController(HttpAuth::AUTH_SERVER, | 150 new HttpAuthController(HttpAuth::AUTH_SERVER, |
187 GURL("http://example.com"), | 151 GURL("http://example.com"), |
188 &dummy_auth_cache, &auth_handler_factory)); | 152 &dummy_auth_cache, &auth_handler_factory)); |
189 ASSERT_EQ(OK, | 153 ASSERT_EQ(OK, |
190 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 154 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
191 ASSERT_TRUE(controller->HaveAuthHandler()); | 155 ASSERT_TRUE(controller->HaveAuthHandler()); |
192 controller->ResetAuth(AuthCredentials()); | 156 controller->ResetAuth(AuthCredentials()); |
193 EXPECT_TRUE(controller->HaveAuth()); | 157 EXPECT_TRUE(controller->HaveAuth()); |
194 EXPECT_FALSE(auth_handler_factory.HaveAuthHandlers(HttpAuth::AUTH_SERVER)); | 158 EXPECT_FALSE(auth_handler_factory.HaveAuthHandlers(HttpAuth::AUTH_SERVER)); |
195 | 159 |
196 // Should only succeed if we are using the "Ernie" MockHandler. | 160 // Should only succeed if we are using the "Ernie" MockHandler. |
197 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 161 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( |
198 &request, CompletionCallback(), dummy_log)); | 162 &request, CompletionCallback(), dummy_log)); |
199 controller->AddAuthorizationHeader(&request_headers); | 163 controller->AddAuthorizationHeader(&request_headers); |
200 | 164 |
201 // Handlers for the second attempt. Neither should be used to generate a | 165 // Handlers for the second attempt. Neither should be used to generate a |
202 // token. Instead the controller should realize that there are no viable | 166 // token. Instead the controller should realize that there are no viable |
203 // identities to use with the "Ernie" handler and fail. | 167 // identities to use with the "Ernie" handler and fail. |
204 auth_handler_factory.AddMockHandler(new MockHandler(ERR_UNEXPECTED, "ernie"), | 168 auth_handler.reset(new HttpAuthHandlerMock()); |
| 169 auth_handler->set_allows_default_credentials(true); |
| 170 auth_handler->set_allows_explicit_credentials(false); |
| 171 auth_handler->set_expected_auth_scheme("ernie"); |
| 172 auth_handler_factory.AddMockHandler(auth_handler.Pass(), |
| 173 HttpAuthHandlerFactory::CREATE_CHALLENGE, |
205 HttpAuth::AUTH_SERVER); | 174 HttpAuth::AUTH_SERVER); |
206 | 175 |
207 // Fallback handlers for the second attempt. The "Ernie" handler should be | 176 // Fallback handlers for the second attempt. The "Ernie" handler should be |
208 // discarded due to the disabled scheme, and the "Bert" handler should | 177 // discarded due to the disabled scheme, and the "Bert" handler should |
209 // successfully be used to generate a token. | 178 // successfully be used to generate a token. |
210 auth_handler_factory.AddMockHandler(new MockHandler(OK, "bert"), | 179 auth_handler.reset(new HttpAuthHandlerMock()); |
| 180 auth_handler->set_allows_default_credentials(false); |
| 181 auth_handler->set_allows_explicit_credentials(true); |
| 182 auth_handler->set_expected_auth_scheme("bert"); |
| 183 auth_handler_factory.AddMockHandler(auth_handler.Pass(), |
| 184 HttpAuthHandlerFactory::CREATE_CHALLENGE, |
211 HttpAuth::AUTH_SERVER); | 185 HttpAuth::AUTH_SERVER); |
212 | 186 |
213 // Once a token is generated, simulate the receipt of a server response | 187 // Once a token is generated, simulate the receipt of a server response |
214 // indicating that the authentication attempt was rejected. | 188 // indicating that the authentication attempt was rejected. |
215 ASSERT_EQ(OK, | 189 ASSERT_EQ(OK, |
216 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 190 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
217 ASSERT_TRUE(controller->HaveAuthHandler()); | 191 ASSERT_TRUE(controller->HaveAuthHandler()); |
218 controller->ResetAuth(AuthCredentials(base::ASCIIToUTF16("Hello"), | 192 controller->ResetAuth(AuthCredentials(base::ASCIIToUTF16("Hello"), |
219 base::string16())); | 193 base::string16())); |
220 EXPECT_TRUE(controller->HaveAuth()); | 194 EXPECT_TRUE(controller->HaveAuth()); |
221 EXPECT_TRUE(controller->IsAuthSchemeDisabled("ernie")); | 195 EXPECT_TRUE(controller->IsAuthSchemeDisabled("ernie")); |
222 EXPECT_FALSE(controller->IsAuthSchemeDisabled("bert")); | 196 EXPECT_FALSE(controller->IsAuthSchemeDisabled("bert")); |
223 | 197 |
224 // Should only succeed if we are using the "Bert" MockHandler. | 198 // Should only succeed if we are using the "Bert" MockHandler. |
225 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 199 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( |
226 &request, CompletionCallback(), dummy_log)); | 200 &request, CompletionCallback(), dummy_log)); |
227 } | 201 } |
228 | 202 |
229 } // namespace net | 203 } // namespace net |
OLD | NEW |