| 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/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "net/base/net_errors.h" | 8 #include "net/base/net_errors.h" |
| 9 #include "net/base/net_log.h" | 9 #include "net/base/net_log.h" |
| 10 #include "net/base/test_completion_callback.h" | 10 #include "net/base/test_completion_callback.h" |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 EXPECT_TRUE(controller->HaveAuth()); | 78 EXPECT_TRUE(controller->HaveAuth()); |
| 79 | 79 |
| 80 TestCompletionCallback callback; | 80 TestCompletionCallback callback; |
| 81 EXPECT_EQ((run_mode == RUN_HANDLER_ASYNC)? ERR_IO_PENDING: | 81 EXPECT_EQ((run_mode == RUN_HANDLER_ASYNC)? ERR_IO_PENDING: |
| 82 expected_controller_rv, | 82 expected_controller_rv, |
| 83 controller->MaybeGenerateAuthToken(&request, callback.callback(), | 83 controller->MaybeGenerateAuthToken(&request, callback.callback(), |
| 84 dummy_log)); | 84 dummy_log)); |
| 85 if (run_mode == RUN_HANDLER_ASYNC) | 85 if (run_mode == RUN_HANDLER_ASYNC) |
| 86 EXPECT_EQ(expected_controller_rv, callback.WaitForResult()); | 86 EXPECT_EQ(expected_controller_rv, callback.WaitForResult()); |
| 87 EXPECT_EQ((scheme_state == SCHEME_IS_DISABLED), | 87 EXPECT_EQ((scheme_state == SCHEME_IS_DISABLED), |
| 88 controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_MOCK)); | 88 controller->IsAuthSchemeDisabled("mock")); |
| 89 } | 89 } |
| 90 | 90 |
| 91 } // namespace | 91 } // namespace |
| 92 | 92 |
| 93 // If an HttpAuthHandler returns an error code that indicates a | 93 // If an HttpAuthHandler returns an error code that indicates a |
| 94 // permanent error, the HttpAuthController should disable the scheme | 94 // permanent error, the HttpAuthController should disable the scheme |
| 95 // used and retry the request. | 95 // used and retry the request. |
| 96 TEST(HttpAuthControllerTest, PermanentErrors) { | 96 TEST(HttpAuthControllerTest, PermanentErrors) { |
| 97 | 97 |
| 98 // Run a synchronous handler that returns | 98 // Run a synchronous handler that returns |
| (...skipping 15 matching lines...) Expand all Loading... |
| 114 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_INVALID_AUTH_CREDENTIALS, | 114 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_INVALID_AUTH_CREDENTIALS, |
| 115 ERR_INVALID_AUTH_CREDENTIALS, SCHEME_IS_ENABLED); | 115 ERR_INVALID_AUTH_CREDENTIALS, SCHEME_IS_ENABLED); |
| 116 } | 116 } |
| 117 | 117 |
| 118 // If an HttpAuthHandler indicates that it doesn't allow explicit | 118 // If an HttpAuthHandler indicates that it doesn't allow explicit |
| 119 // credentials, don't prompt for credentials. | 119 // credentials, don't prompt for credentials. |
| 120 TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) { | 120 TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) { |
| 121 // Modified mock HttpAuthHandler for this test. | 121 // Modified mock HttpAuthHandler for this test. |
| 122 class MockHandler : public HttpAuthHandlerMock { | 122 class MockHandler : public HttpAuthHandlerMock { |
| 123 public: | 123 public: |
| 124 MockHandler(int expected_rv, HttpAuth::Scheme scheme) | 124 MockHandler(int expected_rv, const std::string& scheme) |
| 125 : expected_scheme_(scheme) { | 125 : expected_scheme_(scheme) { |
| 126 SetGenerateExpectation(false, expected_rv); | 126 SetGenerateExpectation(false, expected_rv); |
| 127 } | 127 } |
| 128 | 128 |
| 129 protected: | 129 protected: |
| 130 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) OVERRIDE { | 130 virtual bool Init(HttpAuth::ChallengeTokenizer* challenge) OVERRIDE { |
| 131 HttpAuthHandlerMock::Init(challenge); | 131 HttpAuthHandlerMock::Init(challenge); |
| 132 set_allows_default_credentials(true); | 132 set_allows_default_credentials(true); |
| 133 set_allows_explicit_credentials(false); | 133 set_allows_explicit_credentials(false); |
| 134 set_connection_based(true); | 134 set_connection_based(true); |
| 135 // Pretend to be SCHEME_BASIC so we can test failover logic. | 135 // Pretend to be SCHEME_BASIC so we can test failover logic. |
| 136 if (challenge->scheme() == "Basic") { | 136 if (challenge->scheme() == "Basic") { |
| 137 auth_scheme_ = HttpAuth::AUTH_SCHEME_BASIC; | 137 auth_scheme_ = "basic"; |
| 138 --score_; // Reduce score, so we rank below Mock. | 138 --score_; // Reduce score, so we rank below Mock. |
| 139 set_allows_explicit_credentials(true); | 139 set_allows_explicit_credentials(true); |
| 140 } | 140 } |
| 141 EXPECT_EQ(expected_scheme_, auth_scheme_); | 141 EXPECT_EQ(expected_scheme_, auth_scheme_); |
| 142 return true; | 142 return true; |
| 143 } | 143 } |
| 144 | 144 |
| 145 virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials, | 145 virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials, |
| 146 const HttpRequestInfo* request, | 146 const HttpRequestInfo* request, |
| 147 const CompletionCallback& callback, | 147 const CompletionCallback& callback, |
| 148 std::string* auth_token) OVERRIDE { | 148 std::string* auth_token) OVERRIDE { |
| 149 int result = | 149 int result = |
| 150 HttpAuthHandlerMock::GenerateAuthTokenImpl(credentials, | 150 HttpAuthHandlerMock::GenerateAuthTokenImpl(credentials, |
| 151 request, callback, | 151 request, callback, |
| 152 auth_token); | 152 auth_token); |
| 153 EXPECT_TRUE(result != OK || | 153 EXPECT_TRUE(result != OK || |
| 154 !AllowsExplicitCredentials() || | 154 !AllowsExplicitCredentials() || |
| 155 !credentials->Empty()); | 155 !credentials->Empty()); |
| 156 return result; | 156 return result; |
| 157 } | 157 } |
| 158 | 158 |
| 159 private: | 159 private: |
| 160 HttpAuth::Scheme expected_scheme_; | 160 std::string expected_scheme_; |
| 161 }; | 161 }; |
| 162 | 162 |
| 163 BoundNetLog dummy_log; | 163 BoundNetLog dummy_log; |
| 164 HttpAuthCache dummy_auth_cache; | 164 HttpAuthCache dummy_auth_cache; |
| 165 HttpRequestInfo request; | 165 HttpRequestInfo request; |
| 166 request.method = "GET"; | 166 request.method = "GET"; |
| 167 request.url = GURL("http://example.com"); | 167 request.url = GURL("http://example.com"); |
| 168 | 168 |
| 169 HttpRequestHeaders request_headers; | 169 HttpRequestHeaders request_headers; |
| 170 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( | 170 scoped_refptr<HttpResponseHeaders> headers(HeadersFromString( |
| 171 "HTTP/1.1 401\r\n" | 171 "HTTP/1.1 401\r\n" |
| 172 "WWW-Authenticate: Mock\r\n" | 172 "WWW-Authenticate: Mock\r\n" |
| 173 "WWW-Authenticate: Basic\r\n" | 173 "WWW-Authenticate: Basic\r\n" |
| 174 "\r\n")); | 174 "\r\n")); |
| 175 | 175 |
| 176 HttpAuthHandlerMock::Factory auth_handler_factory; | 176 HttpAuthHandlerMock::Factory auth_handler_factory; |
| 177 | 177 |
| 178 // Handlers for the first attempt at authentication. AUTH_SCHEME_MOCK handler | 178 // Handlers for the first attempt at authentication. AUTH_SCHEME_MOCK handler |
| 179 // accepts the default identity and successfully constructs a token. | 179 // accepts the default identity and successfully constructs a token. |
| 180 auth_handler_factory.AddMockHandler( | 180 auth_handler_factory.AddMockHandler( |
| 181 new MockHandler(OK, HttpAuth::AUTH_SCHEME_MOCK), HttpAuth::AUTH_SERVER); | 181 new MockHandler(OK, "mock"), HttpAuth::AUTH_SERVER); |
| 182 auth_handler_factory.AddMockHandler( | 182 auth_handler_factory.AddMockHandler( |
| 183 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_BASIC), | 183 new MockHandler(ERR_UNEXPECTED, "basic"), |
| 184 HttpAuth::AUTH_SERVER); | 184 HttpAuth::AUTH_SERVER); |
| 185 | 185 |
| 186 // Handlers for the second attempt. Neither should be used to generate a | 186 // Handlers for the second attempt. Neither should be used to generate a |
| 187 // token. Instead the controller should realize that there are no viable | 187 // token. Instead the controller should realize that there are no viable |
| 188 // identities to use with the AUTH_SCHEME_MOCK handler and fail. | 188 // identities to use with the AUTH_SCHEME_MOCK handler and fail. |
| 189 auth_handler_factory.AddMockHandler( | 189 auth_handler_factory.AddMockHandler( |
| 190 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_MOCK), | 190 new MockHandler(ERR_UNEXPECTED, "mock"), |
| 191 HttpAuth::AUTH_SERVER); | 191 HttpAuth::AUTH_SERVER); |
| 192 auth_handler_factory.AddMockHandler( | 192 auth_handler_factory.AddMockHandler( |
| 193 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_BASIC), | 193 new MockHandler(ERR_UNEXPECTED, "basic"), |
| 194 HttpAuth::AUTH_SERVER); | 194 HttpAuth::AUTH_SERVER); |
| 195 | 195 |
| 196 // Fallback handlers for the second attempt. The AUTH_SCHEME_MOCK handler | 196 // Fallback handlers for the second attempt. The AUTH_SCHEME_MOCK handler |
| 197 // should be discarded due to the disabled scheme, and the AUTH_SCHEME_BASIC | 197 // should be discarded due to the disabled scheme, and the AUTH_SCHEME_BASIC |
| 198 // handler should successfully be used to generate a token. | 198 // handler should successfully be used to generate a token. |
| 199 auth_handler_factory.AddMockHandler( | 199 auth_handler_factory.AddMockHandler( |
| 200 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_MOCK), | 200 new MockHandler(ERR_UNEXPECTED, "mock"), |
| 201 HttpAuth::AUTH_SERVER); | 201 HttpAuth::AUTH_SERVER); |
| 202 auth_handler_factory.AddMockHandler( | 202 auth_handler_factory.AddMockHandler( |
| 203 new MockHandler(OK, HttpAuth::AUTH_SCHEME_BASIC), | 203 new MockHandler(OK, "basic"), |
| 204 HttpAuth::AUTH_SERVER); | 204 HttpAuth::AUTH_SERVER); |
| 205 auth_handler_factory.set_do_init_from_challenge(true); | 205 auth_handler_factory.set_do_init_from_challenge(true); |
| 206 | 206 |
| 207 scoped_refptr<HttpAuthController> controller( | 207 scoped_refptr<HttpAuthController> controller( |
| 208 new HttpAuthController(HttpAuth::AUTH_SERVER, | 208 new HttpAuthController(HttpAuth::AUTH_SERVER, |
| 209 GURL("http://example.com"), | 209 GURL("http://example.com"), |
| 210 &dummy_auth_cache, &auth_handler_factory)); | 210 &dummy_auth_cache, &auth_handler_factory)); |
| 211 ASSERT_EQ(OK, | 211 ASSERT_EQ(OK, |
| 212 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 212 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
| 213 ASSERT_TRUE(controller->HaveAuthHandler()); | 213 ASSERT_TRUE(controller->HaveAuthHandler()); |
| 214 controller->ResetAuth(AuthCredentials()); | 214 controller->ResetAuth(AuthCredentials()); |
| 215 EXPECT_TRUE(controller->HaveAuth()); | 215 EXPECT_TRUE(controller->HaveAuth()); |
| 216 | 216 |
| 217 // Should only succeed if we are using the AUTH_SCHEME_MOCK MockHandler. | 217 // Should only succeed if we are using the AUTH_SCHEME_MOCK MockHandler. |
| 218 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 218 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( |
| 219 &request, CompletionCallback(), dummy_log)); | 219 &request, CompletionCallback(), dummy_log)); |
| 220 controller->AddAuthorizationHeader(&request_headers); | 220 controller->AddAuthorizationHeader(&request_headers); |
| 221 | 221 |
| 222 // Once a token is generated, simulate the receipt of a server response | 222 // Once a token is generated, simulate the receipt of a server response |
| 223 // indicating that the authentication attempt was rejected. | 223 // indicating that the authentication attempt was rejected. |
| 224 ASSERT_EQ(OK, | 224 ASSERT_EQ(OK, |
| 225 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 225 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
| 226 ASSERT_TRUE(controller->HaveAuthHandler()); | 226 ASSERT_TRUE(controller->HaveAuthHandler()); |
| 227 controller->ResetAuth(AuthCredentials(ASCIIToUTF16("Hello"), string16())); | 227 controller->ResetAuth(AuthCredentials(ASCIIToUTF16("Hello"), string16())); |
| 228 EXPECT_TRUE(controller->HaveAuth()); | 228 EXPECT_TRUE(controller->HaveAuth()); |
| 229 EXPECT_TRUE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_MOCK)); | 229 EXPECT_TRUE(controller->IsAuthSchemeDisabled("mock")); |
| 230 EXPECT_FALSE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_BASIC)); | 230 EXPECT_FALSE(controller->IsAuthSchemeDisabled("basic")); |
| 231 | 231 |
| 232 // Should only succeed if we are using the AUTH_SCHEME_BASIC MockHandler. | 232 // Should only succeed if we are using the AUTH_SCHEME_BASIC MockHandler. |
| 233 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 233 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( |
| 234 &request, CompletionCallback(), dummy_log)); | 234 &request, CompletionCallback(), dummy_log)); |
| 235 } | 235 } |
| 236 | 236 |
| 237 } // namespace net | 237 } // namespace net |
| OLD | NEW |