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/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" |
11 #include "net/http/http_auth_cache.h" | 11 #include "net/http/http_auth_cache.h" |
12 #include "net/http/http_auth_challenge_tokenizer.h" | 12 #include "net/http/http_auth_challenge_tokenizer.h" |
13 #include "net/http/http_auth_handler_mock.h" | 13 #include "net/http/http_auth_handler_mock.h" |
14 #include "net/http/http_request_info.h" | 14 #include "net/http/http_request_info.h" |
15 #include "net/http/http_response_headers.h" | 15 #include "net/http/http_response_headers.h" |
16 #include "net/http/http_util.h" | 16 #include "net/http/http_util.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
18 | 18 |
19 namespace net { | 19 namespace net { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 enum HandlerRunMode { | 23 enum HandlerRunMode { RUN_HANDLER_SYNC, RUN_HANDLER_ASYNC }; |
24 RUN_HANDLER_SYNC, | |
25 RUN_HANDLER_ASYNC | |
26 }; | |
27 | 24 |
28 enum SchemeState { | 25 enum SchemeState { SCHEME_IS_DISABLED, SCHEME_IS_ENABLED }; |
29 SCHEME_IS_DISABLED, | |
30 SCHEME_IS_ENABLED | |
31 }; | |
32 | 26 |
33 scoped_refptr<HttpResponseHeaders> HeadersFromString(const char* string) { | 27 scoped_refptr<HttpResponseHeaders> HeadersFromString(const char* string) { |
34 std::string raw_string(string); | 28 std::string raw_string(string); |
35 std::string headers_string = HttpUtil::AssembleRawHeaders( | 29 std::string headers_string = |
36 raw_string.c_str(), raw_string.length()); | 30 HttpUtil::AssembleRawHeaders(raw_string.c_str(), raw_string.length()); |
37 scoped_refptr<HttpResponseHeaders> headers( | 31 scoped_refptr<HttpResponseHeaders> headers( |
38 new HttpResponseHeaders(headers_string)); | 32 new HttpResponseHeaders(headers_string)); |
39 return headers; | 33 return headers; |
40 } | 34 } |
41 | 35 |
42 // Runs an HttpAuthController with a single round mock auth handler | 36 // Runs an HttpAuthController with a single round mock auth handler |
43 // that returns |handler_rv| on token generation. The handler runs in | 37 // that returns |handler_rv| on token generation. The handler runs in |
44 // async if |run_mode| is RUN_HANDLER_ASYNC. Upon completion, the | 38 // async if |run_mode| is RUN_HANDLER_ASYNC. Upon completion, the |
45 // return value of the controller is tested against | 39 // return value of the controller is tested against |
46 // |expected_controller_rv|. |scheme_state| indicates whether the | 40 // |expected_controller_rv|. |scheme_state| indicates whether the |
(...skipping 17 matching lines...) Expand all Loading... |
64 HttpAuthHandlerMock::Factory auth_handler_factory; | 58 HttpAuthHandlerMock::Factory auth_handler_factory; |
65 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); | 59 HttpAuthHandlerMock* auth_handler = new HttpAuthHandlerMock(); |
66 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), | 60 auth_handler->SetGenerateExpectation((run_mode == RUN_HANDLER_ASYNC), |
67 handler_rv); | 61 handler_rv); |
68 auth_handler_factory.AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); | 62 auth_handler_factory.AddMockHandler(auth_handler, HttpAuth::AUTH_PROXY); |
69 auth_handler_factory.set_do_init_from_challenge(true); | 63 auth_handler_factory.set_do_init_from_challenge(true); |
70 | 64 |
71 scoped_refptr<HttpAuthController> controller( | 65 scoped_refptr<HttpAuthController> controller( |
72 new HttpAuthController(HttpAuth::AUTH_PROXY, | 66 new HttpAuthController(HttpAuth::AUTH_PROXY, |
73 GURL("http://example.com"), | 67 GURL("http://example.com"), |
74 &dummy_auth_cache, &auth_handler_factory)); | 68 &dummy_auth_cache, |
| 69 &auth_handler_factory)); |
75 ASSERT_EQ(OK, | 70 ASSERT_EQ(OK, |
76 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 71 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
77 ASSERT_TRUE(controller->HaveAuthHandler()); | 72 ASSERT_TRUE(controller->HaveAuthHandler()); |
78 controller->ResetAuth(AuthCredentials()); | 73 controller->ResetAuth(AuthCredentials()); |
79 EXPECT_TRUE(controller->HaveAuth()); | 74 EXPECT_TRUE(controller->HaveAuth()); |
80 | 75 |
81 TestCompletionCallback callback; | 76 TestCompletionCallback callback; |
82 EXPECT_EQ((run_mode == RUN_HANDLER_ASYNC)? ERR_IO_PENDING: | 77 EXPECT_EQ( |
83 expected_controller_rv, | 78 (run_mode == RUN_HANDLER_ASYNC) ? ERR_IO_PENDING : expected_controller_rv, |
84 controller->MaybeGenerateAuthToken(&request, callback.callback(), | 79 controller->MaybeGenerateAuthToken( |
85 dummy_log)); | 80 &request, callback.callback(), dummy_log)); |
86 if (run_mode == RUN_HANDLER_ASYNC) | 81 if (run_mode == RUN_HANDLER_ASYNC) |
87 EXPECT_EQ(expected_controller_rv, callback.WaitForResult()); | 82 EXPECT_EQ(expected_controller_rv, callback.WaitForResult()); |
88 EXPECT_EQ((scheme_state == SCHEME_IS_DISABLED), | 83 EXPECT_EQ((scheme_state == SCHEME_IS_DISABLED), |
89 controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_MOCK)); | 84 controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_MOCK)); |
90 } | 85 } |
91 | 86 |
92 } // namespace | 87 } // namespace |
93 | 88 |
94 // If an HttpAuthHandler returns an error code that indicates a | 89 // If an HttpAuthHandler returns an error code that indicates a |
95 // permanent error, the HttpAuthController should disable the scheme | 90 // permanent error, the HttpAuthController should disable the scheme |
96 // used and retry the request. | 91 // used and retry the request. |
97 TEST(HttpAuthControllerTest, PermanentErrors) { | 92 TEST(HttpAuthControllerTest, PermanentErrors) { |
98 | |
99 // Run a synchronous handler that returns | 93 // Run a synchronous handler that returns |
100 // ERR_UNEXPECTED_SECURITY_LIBRARY_STATUS. We expect a return value | 94 // ERR_UNEXPECTED_SECURITY_LIBRARY_STATUS. We expect a return value |
101 // of OK from the controller so we can retry the request. | 95 // of OK from the controller so we can retry the request. |
102 RunSingleRoundAuthTest(RUN_HANDLER_SYNC, | 96 RunSingleRoundAuthTest(RUN_HANDLER_SYNC, |
103 ERR_UNEXPECTED_SECURITY_LIBRARY_STATUS, | 97 ERR_UNEXPECTED_SECURITY_LIBRARY_STATUS, |
104 OK, SCHEME_IS_DISABLED); | 98 OK, |
| 99 SCHEME_IS_DISABLED); |
105 | 100 |
106 // Now try an async handler that returns | 101 // Now try an async handler that returns |
107 // ERR_MISSING_AUTH_CREDENTIALS. Async and sync handlers invoke | 102 // ERR_MISSING_AUTH_CREDENTIALS. Async and sync handlers invoke |
108 // different code paths in HttpAuthController when generating | 103 // different code paths in HttpAuthController when generating |
109 // tokens. | 104 // tokens. |
110 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_MISSING_AUTH_CREDENTIALS, OK, | 105 RunSingleRoundAuthTest( |
111 SCHEME_IS_DISABLED); | 106 RUN_HANDLER_ASYNC, ERR_MISSING_AUTH_CREDENTIALS, OK, SCHEME_IS_DISABLED); |
112 | 107 |
113 // If a non-permanent error is returned by the handler, then the | 108 // If a non-permanent error is returned by the handler, then the |
114 // controller should report it unchanged. | 109 // controller should report it unchanged. |
115 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, ERR_INVALID_AUTH_CREDENTIALS, | 110 RunSingleRoundAuthTest(RUN_HANDLER_ASYNC, |
116 ERR_INVALID_AUTH_CREDENTIALS, SCHEME_IS_ENABLED); | 111 ERR_INVALID_AUTH_CREDENTIALS, |
| 112 ERR_INVALID_AUTH_CREDENTIALS, |
| 113 SCHEME_IS_ENABLED); |
117 } | 114 } |
118 | 115 |
119 // If an HttpAuthHandler indicates that it doesn't allow explicit | 116 // If an HttpAuthHandler indicates that it doesn't allow explicit |
120 // credentials, don't prompt for credentials. | 117 // credentials, don't prompt for credentials. |
121 TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) { | 118 TEST(HttpAuthControllerTest, NoExplicitCredentialsAllowed) { |
122 // Modified mock HttpAuthHandler for this test. | 119 // Modified mock HttpAuthHandler for this test. |
123 class MockHandler : public HttpAuthHandlerMock { | 120 class MockHandler : public HttpAuthHandlerMock { |
124 public: | 121 public: |
125 MockHandler(int expected_rv, HttpAuth::Scheme scheme) | 122 MockHandler(int expected_rv, HttpAuth::Scheme scheme) |
126 : expected_scheme_(scheme) { | 123 : expected_scheme_(scheme) { |
(...skipping 13 matching lines...) Expand all Loading... |
140 set_allows_explicit_credentials(true); | 137 set_allows_explicit_credentials(true); |
141 } | 138 } |
142 EXPECT_EQ(expected_scheme_, auth_scheme_); | 139 EXPECT_EQ(expected_scheme_, auth_scheme_); |
143 return true; | 140 return true; |
144 } | 141 } |
145 | 142 |
146 virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials, | 143 virtual int GenerateAuthTokenImpl(const AuthCredentials* credentials, |
147 const HttpRequestInfo* request, | 144 const HttpRequestInfo* request, |
148 const CompletionCallback& callback, | 145 const CompletionCallback& callback, |
149 std::string* auth_token) OVERRIDE { | 146 std::string* auth_token) OVERRIDE { |
150 int result = | 147 int result = HttpAuthHandlerMock::GenerateAuthTokenImpl( |
151 HttpAuthHandlerMock::GenerateAuthTokenImpl(credentials, | 148 credentials, request, callback, auth_token); |
152 request, callback, | 149 EXPECT_TRUE(result != OK || !AllowsExplicitCredentials() || |
153 auth_token); | |
154 EXPECT_TRUE(result != OK || | |
155 !AllowsExplicitCredentials() || | |
156 !credentials->Empty()); | 150 !credentials->Empty()); |
157 return result; | 151 return result; |
158 } | 152 } |
159 | 153 |
160 private: | 154 private: |
161 HttpAuth::Scheme expected_scheme_; | 155 HttpAuth::Scheme expected_scheme_; |
162 }; | 156 }; |
163 | 157 |
164 BoundNetLog dummy_log; | 158 BoundNetLog dummy_log; |
165 HttpAuthCache dummy_auth_cache; | 159 HttpAuthCache dummy_auth_cache; |
(...skipping 28 matching lines...) Expand all Loading... |
194 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_BASIC), | 188 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_BASIC), |
195 HttpAuth::AUTH_SERVER); | 189 HttpAuth::AUTH_SERVER); |
196 | 190 |
197 // Fallback handlers for the second attempt. The AUTH_SCHEME_MOCK handler | 191 // Fallback handlers for the second attempt. The AUTH_SCHEME_MOCK handler |
198 // should be discarded due to the disabled scheme, and the AUTH_SCHEME_BASIC | 192 // should be discarded due to the disabled scheme, and the AUTH_SCHEME_BASIC |
199 // handler should successfully be used to generate a token. | 193 // handler should successfully be used to generate a token. |
200 auth_handler_factory.AddMockHandler( | 194 auth_handler_factory.AddMockHandler( |
201 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_MOCK), | 195 new MockHandler(ERR_UNEXPECTED, HttpAuth::AUTH_SCHEME_MOCK), |
202 HttpAuth::AUTH_SERVER); | 196 HttpAuth::AUTH_SERVER); |
203 auth_handler_factory.AddMockHandler( | 197 auth_handler_factory.AddMockHandler( |
204 new MockHandler(OK, HttpAuth::AUTH_SCHEME_BASIC), | 198 new MockHandler(OK, HttpAuth::AUTH_SCHEME_BASIC), HttpAuth::AUTH_SERVER); |
205 HttpAuth::AUTH_SERVER); | |
206 auth_handler_factory.set_do_init_from_challenge(true); | 199 auth_handler_factory.set_do_init_from_challenge(true); |
207 | 200 |
208 scoped_refptr<HttpAuthController> controller( | 201 scoped_refptr<HttpAuthController> controller( |
209 new HttpAuthController(HttpAuth::AUTH_SERVER, | 202 new HttpAuthController(HttpAuth::AUTH_SERVER, |
210 GURL("http://example.com"), | 203 GURL("http://example.com"), |
211 &dummy_auth_cache, &auth_handler_factory)); | 204 &dummy_auth_cache, |
| 205 &auth_handler_factory)); |
212 ASSERT_EQ(OK, | 206 ASSERT_EQ(OK, |
213 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 207 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
214 ASSERT_TRUE(controller->HaveAuthHandler()); | 208 ASSERT_TRUE(controller->HaveAuthHandler()); |
215 controller->ResetAuth(AuthCredentials()); | 209 controller->ResetAuth(AuthCredentials()); |
216 EXPECT_TRUE(controller->HaveAuth()); | 210 EXPECT_TRUE(controller->HaveAuth()); |
217 | 211 |
218 // Should only succeed if we are using the AUTH_SCHEME_MOCK MockHandler. | 212 // Should only succeed if we are using the AUTH_SCHEME_MOCK MockHandler. |
219 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 213 EXPECT_EQ(OK, |
220 &request, CompletionCallback(), dummy_log)); | 214 controller->MaybeGenerateAuthToken( |
| 215 &request, CompletionCallback(), dummy_log)); |
221 controller->AddAuthorizationHeader(&request_headers); | 216 controller->AddAuthorizationHeader(&request_headers); |
222 | 217 |
223 // Once a token is generated, simulate the receipt of a server response | 218 // Once a token is generated, simulate the receipt of a server response |
224 // indicating that the authentication attempt was rejected. | 219 // indicating that the authentication attempt was rejected. |
225 ASSERT_EQ(OK, | 220 ASSERT_EQ(OK, |
226 controller->HandleAuthChallenge(headers, false, false, dummy_log)); | 221 controller->HandleAuthChallenge(headers, false, false, dummy_log)); |
227 ASSERT_TRUE(controller->HaveAuthHandler()); | 222 ASSERT_TRUE(controller->HaveAuthHandler()); |
228 controller->ResetAuth(AuthCredentials(base::ASCIIToUTF16("Hello"), | 223 controller->ResetAuth( |
229 base::string16())); | 224 AuthCredentials(base::ASCIIToUTF16("Hello"), base::string16())); |
230 EXPECT_TRUE(controller->HaveAuth()); | 225 EXPECT_TRUE(controller->HaveAuth()); |
231 EXPECT_TRUE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_MOCK)); | 226 EXPECT_TRUE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_MOCK)); |
232 EXPECT_FALSE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_BASIC)); | 227 EXPECT_FALSE(controller->IsAuthSchemeDisabled(HttpAuth::AUTH_SCHEME_BASIC)); |
233 | 228 |
234 // Should only succeed if we are using the AUTH_SCHEME_BASIC MockHandler. | 229 // Should only succeed if we are using the AUTH_SCHEME_BASIC MockHandler. |
235 EXPECT_EQ(OK, controller->MaybeGenerateAuthToken( | 230 EXPECT_EQ(OK, |
236 &request, CompletionCallback(), dummy_log)); | 231 controller->MaybeGenerateAuthToken( |
| 232 &request, CompletionCallback(), dummy_log)); |
237 } | 233 } |
238 | 234 |
239 } // namespace net | 235 } // namespace net |
OLD | NEW |