OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_handler_negotiate.h" | 5 #include "net/http/http_auth_handler_negotiate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
13 #include "net/dns/mock_host_resolver.h" | 13 #include "net/dns/mock_host_resolver.h" |
14 #include "net/http/http_request_info.h" | 14 #include "net/http/http_request_info.h" |
15 #include "net/http/mock_allow_url_security_manager.h" | 15 #include "net/http/mock_allow_http_auth_preferences.h" |
16 #if defined(OS_ANDROID) | 16 #if defined(OS_ANDROID) |
17 #include "net/android/dummy_spnego_authenticator.h" | 17 #include "net/android/dummy_spnego_authenticator.h" |
18 #elif defined(OS_WIN) | 18 #elif defined(OS_WIN) |
19 #include "net/http/mock_sspi_library_win.h" | 19 #include "net/http/mock_sspi_library_win.h" |
20 #elif defined(OS_POSIX) | 20 #elif defined(OS_POSIX) |
21 #include "net/http/mock_gssapi_library_posix.h" | 21 #include "net/http/mock_gssapi_library_posix.h" |
22 #endif | 22 #endif |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 #include "testing/platform_test.h" | 24 #include "testing/platform_test.h" |
25 | 25 |
26 namespace net { | 26 namespace net { |
27 | 27 |
28 #if defined(OS_ANDROID) | 28 #if defined(OS_ANDROID) |
29 typedef net::android::DummySpnegoAuthenticator MockAuthLibrary; | 29 typedef net::android::DummySpnegoAuthenticator MockAuthLibrary; |
30 #elif defined(OS_WIN) | 30 #elif defined(OS_WIN) |
31 typedef MockSSPILibrary MockAuthLibrary; | 31 typedef MockSSPILibrary MockAuthLibrary; |
32 #elif defined(OS_POSIX) | 32 #elif defined(OS_POSIX) |
33 typedef test::MockGSSAPILibrary MockAuthLibrary; | 33 typedef test::MockGSSAPILibrary MockAuthLibrary; |
34 #endif | 34 #endif |
35 | 35 |
36 class HttpAuthHandlerNegotiateTest : public PlatformTest { | 36 class HttpAuthHandlerNegotiateTest : public PlatformTest { |
37 public: | 37 public: |
38 void SetUp() override { | 38 void SetUp() override { |
39 auth_library_ = new MockAuthLibrary(); | 39 auth_library_ = new MockAuthLibrary(); |
40 resolver_.reset(new MockHostResolver()); | 40 resolver_.reset(new MockHostResolver()); |
41 resolver_->rules()->AddIPLiteralRule("alias", "10.0.0.2", | 41 resolver_->rules()->AddIPLiteralRule("alias", "10.0.0.2", |
42 "canonical.example.com"); | 42 "canonical.example.com"); |
43 | 43 |
44 url_security_manager_.reset(new MockAllowURLSecurityManager()); | 44 http_auth_preferences_.reset(new MockAllowHttpAuthPreferences()); |
45 factory_.reset(new HttpAuthHandlerNegotiate::Factory()); | 45 factory_.reset(new HttpAuthHandlerNegotiate::Factory()); |
46 factory_->set_url_security_manager(url_security_manager_.get()); | 46 factory_->set_http_auth_preferences(http_auth_preferences_.get()); |
47 #if defined(OS_ANDROID) | 47 #if defined(OS_ANDROID) |
48 std::string* authenticator = | 48 http_auth_preferences_->set_auth_android_negotiate_account_type( |
49 new std::string("org.chromium.test.DummySpnegoAuthenticator"); | 49 "org.chromium.test.DummySpnegoAuthenticator"); |
50 factory_->set_library(authenticator); | |
51 MockAuthLibrary::EnsureTestAccountExists(); | 50 MockAuthLibrary::EnsureTestAccountExists(); |
52 #endif | 51 #endif |
53 #if defined(OS_WIN) || (defined(OS_POSIX) && !defined(OS_ANDROID)) | 52 #if defined(OS_WIN) || (defined(OS_POSIX) && !defined(OS_ANDROID)) |
54 factory_->set_library(auth_library_); | 53 factory_->set_library(make_scoped_ptr(auth_library_)); |
55 #endif | 54 #endif |
56 factory_->set_host_resolver(resolver_.get()); | 55 factory_->set_host_resolver(resolver_.get()); |
57 } | 56 } |
58 | 57 |
59 #if defined(OS_ANDROID) | 58 #if defined(OS_ANDROID) |
60 void TearDown() override { MockAuthLibrary::RemoveTestAccounts(); } | 59 void TearDown() override { MockAuthLibrary::RemoveTestAccounts(); } |
61 #endif | 60 #endif |
62 | 61 |
63 void SetupMocks(MockAuthLibrary* mock_library) { | 62 void SetupMocks(MockAuthLibrary* mock_library) { |
64 #if defined(OS_WIN) | 63 #if defined(OS_WIN) |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 query.expected_input_token, | 186 query.expected_input_token, |
188 query.output_token); | 187 query.output_token); |
189 } | 188 } |
190 | 189 |
191 #endif // defined(OS_POSIX) | 190 #endif // defined(OS_POSIX) |
192 | 191 |
193 int CreateHandler(bool disable_cname_lookup, bool use_port, | 192 int CreateHandler(bool disable_cname_lookup, bool use_port, |
194 bool synchronous_resolve_mode, | 193 bool synchronous_resolve_mode, |
195 const std::string& url_string, | 194 const std::string& url_string, |
196 scoped_ptr<HttpAuthHandlerNegotiate>* handler) { | 195 scoped_ptr<HttpAuthHandlerNegotiate>* handler) { |
197 factory_->set_disable_cname_lookup(disable_cname_lookup); | 196 http_auth_preferences_->set_negotiate_disable_cname_lookup( |
198 factory_->set_use_port(use_port); | 197 disable_cname_lookup); |
| 198 http_auth_preferences_->set_negotiate_enable_port(use_port); |
199 resolver_->set_synchronous_mode(synchronous_resolve_mode); | 199 resolver_->set_synchronous_mode(synchronous_resolve_mode); |
200 GURL gurl(url_string); | 200 GURL gurl(url_string); |
201 | 201 |
202 // Note: This is a little tricky because CreateAuthHandlerFromString | 202 // Note: This is a little tricky because CreateAuthHandlerFromString |
203 // expects a scoped_ptr<HttpAuthHandler>* rather than a | 203 // expects a scoped_ptr<HttpAuthHandler>* rather than a |
204 // scoped_ptr<HttpAuthHandlerNegotiate>*. This needs to do the cast | 204 // scoped_ptr<HttpAuthHandlerNegotiate>*. This needs to do the cast |
205 // after creating the handler, and make sure that generic_handler | 205 // after creating the handler, and make sure that generic_handler |
206 // no longer holds on to the HttpAuthHandlerNegotiate object. | 206 // no longer holds on to the HttpAuthHandlerNegotiate object. |
207 scoped_ptr<HttpAuthHandler> generic_handler; | 207 scoped_ptr<HttpAuthHandler> generic_handler; |
208 int rv = factory_->CreateAuthHandlerFromString("Negotiate", | 208 int rv = factory_->CreateAuthHandlerFromString("Negotiate", |
209 HttpAuth::AUTH_SERVER, | 209 HttpAuth::AUTH_SERVER, |
210 gurl, | 210 gurl, |
211 BoundNetLog(), | 211 BoundNetLog(), |
212 &generic_handler); | 212 &generic_handler); |
213 if (rv != OK) | 213 if (rv != OK) |
214 return rv; | 214 return rv; |
215 HttpAuthHandlerNegotiate* negotiate_handler = | 215 HttpAuthHandlerNegotiate* negotiate_handler = |
216 static_cast<HttpAuthHandlerNegotiate*>(generic_handler.release()); | 216 static_cast<HttpAuthHandlerNegotiate*>(generic_handler.release()); |
217 handler->reset(negotiate_handler); | 217 handler->reset(negotiate_handler); |
218 return rv; | 218 return rv; |
219 } | 219 } |
220 | 220 |
221 MockAuthLibrary* AuthLibrary() { return auth_library_; } | 221 MockAuthLibrary* AuthLibrary() { return auth_library_; } |
222 | 222 |
223 private: | 223 private: |
224 #if defined(OS_WIN) | 224 #if defined(OS_WIN) |
225 scoped_ptr<SecPkgInfoW> security_package_; | 225 scoped_ptr<SecPkgInfoW> security_package_; |
226 #endif | 226 #endif |
227 // |auth_library_| is passed to |factory_|, which assumes ownership of it. | 227 // |auth_library_| is passed to |factory_|, which assumes ownership of it, but |
| 228 // can't be a scoped pointer to it since the tests need access when they set |
| 229 // up the mocks after passing ownership. |
228 MockAuthLibrary* auth_library_; | 230 MockAuthLibrary* auth_library_; |
229 scoped_ptr<MockHostResolver> resolver_; | 231 scoped_ptr<MockHostResolver> resolver_; |
230 scoped_ptr<URLSecurityManager> url_security_manager_; | 232 scoped_ptr<MockAllowHttpAuthPreferences> http_auth_preferences_; |
231 scoped_ptr<HttpAuthHandlerNegotiate::Factory> factory_; | 233 scoped_ptr<HttpAuthHandlerNegotiate::Factory> factory_; |
232 }; | 234 }; |
233 | 235 |
234 TEST_F(HttpAuthHandlerNegotiateTest, DisableCname) { | 236 TEST_F(HttpAuthHandlerNegotiateTest, DisableCname) { |
235 SetupMocks(AuthLibrary()); | 237 SetupMocks(AuthLibrary()); |
236 scoped_ptr<HttpAuthHandlerNegotiate> auth_handler; | 238 scoped_ptr<HttpAuthHandlerNegotiate> auth_handler; |
237 EXPECT_EQ(OK, CreateHandler( | 239 EXPECT_EQ(OK, CreateHandler( |
238 true, false, true, "http://alias:500", &auth_handler)); | 240 true, false, true, "http://alias:500", &auth_handler)); |
239 | 241 |
240 ASSERT_TRUE(auth_handler.get() != NULL); | 242 ASSERT_TRUE(auth_handler.get() != NULL); |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 HttpRequestInfo request_info; | 355 HttpRequestInfo request_info; |
354 std::string token; | 356 std::string token; |
355 EXPECT_EQ(ERR_IO_PENDING, auth_handler->GenerateAuthToken( | 357 EXPECT_EQ(ERR_IO_PENDING, auth_handler->GenerateAuthToken( |
356 NULL, &request_info, callback.callback(), &token)); | 358 NULL, &request_info, callback.callback(), &token)); |
357 EXPECT_EQ(ERR_MISSING_AUTH_CREDENTIALS, callback.WaitForResult()); | 359 EXPECT_EQ(ERR_MISSING_AUTH_CREDENTIALS, callback.WaitForResult()); |
358 } | 360 } |
359 | 361 |
360 #if defined(DLOPEN_KERBEROS) | 362 #if defined(DLOPEN_KERBEROS) |
361 TEST_F(HttpAuthHandlerNegotiateTest, MissingGSSAPI) { | 363 TEST_F(HttpAuthHandlerNegotiateTest, MissingGSSAPI) { |
362 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); | 364 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); |
363 MockAllowURLSecurityManager url_security_manager; | 365 MockAllowHttpAuthPreferences http_auth_preferences; |
364 scoped_ptr<HttpAuthHandlerNegotiate::Factory> negotiate_factory( | 366 scoped_ptr<HttpAuthHandlerNegotiate::Factory> negotiate_factory( |
365 new HttpAuthHandlerNegotiate::Factory()); | 367 new HttpAuthHandlerNegotiate::Factory()); |
366 negotiate_factory->set_host_resolver(host_resolver.get()); | 368 negotiate_factory->set_host_resolver(host_resolver); |
367 negotiate_factory->set_url_security_manager(&url_security_manager); | 369 negotiate_factory->set_http_auth_preferences(&http_auth_preferences); |
368 negotiate_factory->set_library( | 370 negotiate_factory->set_library( |
369 new GSSAPISharedLibrary("/this/library/does/not/exist")); | 371 make_scoped_ptr(new GSSAPISharedLibrary("/this/library/does/not/exist"))); |
370 | 372 |
371 GURL gurl("http://www.example.com"); | 373 GURL gurl("http://www.example.com"); |
372 scoped_ptr<HttpAuthHandler> generic_handler; | 374 scoped_ptr<HttpAuthHandler> generic_handler; |
373 int rv = negotiate_factory->CreateAuthHandlerFromString( | 375 int rv = negotiate_factory->CreateAuthHandlerFromString( |
374 "Negotiate", | 376 "Negotiate", |
375 HttpAuth::AUTH_SERVER, | 377 HttpAuth::AUTH_SERVER, |
376 gurl, | 378 gurl, |
377 BoundNetLog(), | 379 BoundNetLog(), |
378 &generic_handler); | 380 &generic_handler); |
379 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); | 381 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); |
380 EXPECT_TRUE(generic_handler.get() == NULL); | 382 EXPECT_TRUE(generic_handler.get() == NULL); |
381 } | 383 } |
382 #endif // defined(DLOPEN_KERBEROS) | 384 #endif // defined(DLOPEN_KERBEROS) |
383 | 385 |
384 #endif // defined(OS_POSIX) | 386 #endif // defined(OS_POSIX) |
385 | 387 |
386 } // namespace net | 388 } // namespace net |
OLD | NEW |