| 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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
| 6 #include "base/strings/string_util.h" |
| 6 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
| 7 #include "net/dns/mock_host_resolver.h" | 8 #include "net/dns/mock_host_resolver.h" |
| 9 #include "net/http/http_auth_challenge_tokenizer.h" |
| 8 #include "net/http/http_auth_handler.h" | 10 #include "net/http/http_auth_handler.h" |
| 9 #include "net/http/http_auth_handler_factory.h" | 11 #include "net/http/http_auth_handler_factory.h" |
| 12 #include "net/http/http_auth_handler_mock.h" |
| 10 #include "net/http/mock_allow_url_security_manager.h" | 13 #include "net/http/mock_allow_url_security_manager.h" |
| 11 #include "net/http/url_security_manager.h" | 14 #include "net/http/url_security_manager.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 13 | 16 |
| 14 namespace net { | 17 namespace net { |
| 15 | 18 |
| 16 namespace { | 19 namespace { |
| 17 | 20 |
| 18 class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { | 21 class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { |
| 19 public: | 22 public: |
| 20 explicit MockHttpAuthHandlerFactory(int return_code) : | 23 explicit MockHttpAuthHandlerFactory(const char* scheme) |
| 21 return_code_(return_code) {} | 24 : scheme_(base::ToLowerASCII(scheme)) {} |
| 22 ~MockHttpAuthHandlerFactory() override {} | 25 ~MockHttpAuthHandlerFactory() override {} |
| 23 | 26 |
| 24 int CreateAuthHandler(const HttpAuthChallengeTokenizer& challenge, | 27 scoped_ptr<HttpAuthHandler> CreateAuthHandlerForScheme( |
| 25 HttpAuth::Target target, | 28 const std::string& scheme) override { |
| 26 const GURL& origin, | 29 EXPECT_EQ(scheme, scheme_) << scheme << " vs. " << scheme_; |
| 27 CreateReason reason, | 30 return make_scoped_ptr(new HttpAuthHandlerMock()); |
| 28 int nonce_count, | 31 } |
| 29 const BoundNetLog& net_log, | 32 scoped_ptr<HttpAuthHandler> CreateAndInitPreemptiveAuthHandler( |
| 30 scoped_ptr<HttpAuthHandler>* handler) override { | 33 HttpAuthCache::Entry* cache_entry, |
| 31 handler->reset(); | 34 const HttpAuthChallengeTokenizer& tokenizer, |
| 32 return return_code_; | 35 HttpAuth::Target target, |
| 36 const BoundNetLog& net_log) override { |
| 37 EXPECT_EQ(cache_entry->scheme(), scheme_) << cache_entry->scheme() |
| 38 << " vs. " << scheme_; |
| 39 return make_scoped_ptr(new HttpAuthHandlerMock()); |
| 33 } | 40 } |
| 34 | 41 |
| 35 private: | 42 private: |
| 36 int return_code_; | 43 std::string scheme_; |
| 37 }; | 44 }; |
| 38 | 45 |
| 39 } // namespace | 46 } // namespace |
| 40 | 47 |
| 41 TEST(HttpAuthHandlerFactoryTest, RegistryFactory) { | 48 TEST(HttpAuthHandlerFactoryTest, RegistryFactory) { |
| 42 HttpAuthHandlerRegistryFactory registry_factory; | 49 HttpAuthHandlerRegistryFactory registry_factory; |
| 43 GURL gurl("www.google.com"); | 50 GURL gurl("www.google.com"); |
| 44 const int kBasicReturnCode = ERR_INVALID_SPDY_STREAM; | |
| 45 MockHttpAuthHandlerFactory* mock_factory_basic = | |
| 46 new MockHttpAuthHandlerFactory(kBasicReturnCode); | |
| 47 | |
| 48 const int kDigestReturnCode = ERR_PAC_SCRIPT_FAILED; | |
| 49 MockHttpAuthHandlerFactory* mock_factory_digest = | |
| 50 new MockHttpAuthHandlerFactory(kDigestReturnCode); | |
| 51 | |
| 52 const int kDigestReturnCodeReplace = ERR_SYN_REPLY_NOT_RECEIVED; | |
| 53 MockHttpAuthHandlerFactory* mock_factory_digest_replace = | |
| 54 new MockHttpAuthHandlerFactory(kDigestReturnCodeReplace); | |
| 55 | 51 |
| 56 scoped_ptr<HttpAuthHandler> handler; | 52 scoped_ptr<HttpAuthHandler> handler; |
| 57 | 53 |
| 58 // No schemes should be supported in the beginning. | 54 // No schemes should be supported in the beginning. |
| 59 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, | 55 handler = registry_factory.CreateAuthHandlerForScheme("basic"); |
| 60 registry_factory.CreateAuthHandlerFromString( | 56 EXPECT_FALSE(handler); |
| 61 "Basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); | |
| 62 | 57 |
| 63 // Test what happens with a single scheme. | 58 // Test what happens with a single scheme. |
| 64 registry_factory.RegisterSchemeFactory("basic", mock_factory_basic); | 59 registry_factory.RegisterSchemeFactory( |
| 65 EXPECT_EQ(kBasicReturnCode, | 60 "basic", new MockHttpAuthHandlerFactory("basic")); |
| 66 registry_factory.CreateAuthHandlerFromString( | 61 handler = registry_factory.CreateAuthHandlerForScheme("basic"); |
| 67 "Basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); | 62 EXPECT_TRUE(handler); |
| 68 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, | 63 handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
| 69 registry_factory.CreateAuthHandlerFromString( | 64 EXPECT_FALSE(handler); |
| 70 "Digest", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), | |
| 71 &handler)); | |
| 72 | 65 |
| 73 // Test multiple schemes | 66 // Test multiple schemes |
| 74 registry_factory.RegisterSchemeFactory("digest", mock_factory_digest); | 67 registry_factory.RegisterSchemeFactory( |
| 75 EXPECT_EQ(kBasicReturnCode, | 68 "digest", new MockHttpAuthHandlerFactory("digest")); |
| 76 registry_factory.CreateAuthHandlerFromString( | 69 handler = registry_factory.CreateAuthHandlerForScheme("basic"); |
| 77 "Basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); | 70 EXPECT_TRUE(handler); |
| 78 EXPECT_EQ(kDigestReturnCode, | 71 handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
| 79 registry_factory.CreateAuthHandlerFromString( | 72 EXPECT_TRUE(handler); |
| 80 "Digest", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), | 73 handler = registry_factory.CreateAuthHandlerForScheme("bogus"); |
| 81 &handler)); | 74 EXPECT_FALSE(handler); |
| 82 | 75 |
| 83 // Test case-insensitivity | 76 // Test replacement of existing schemes. |
| 84 EXPECT_EQ(kBasicReturnCode, | 77 registry_factory.RegisterSchemeFactory("digest", |
| 85 registry_factory.CreateAuthHandlerFromString( | 78 new HttpAuthHandlerMock::Factory()); |
| 86 "basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); | 79 handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
| 87 | 80 EXPECT_FALSE(handler); |
| 88 // Test replacement of existing auth scheme | 81 registry_factory.RegisterSchemeFactory( |
| 89 registry_factory.RegisterSchemeFactory("digest", mock_factory_digest_replace); | 82 "digest", new MockHttpAuthHandlerFactory("digest")); |
| 90 EXPECT_EQ(kBasicReturnCode, | 83 handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
| 91 registry_factory.CreateAuthHandlerFromString( | 84 EXPECT_TRUE(handler); |
| 92 "Basic", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), &handler)); | |
| 93 EXPECT_EQ(kDigestReturnCodeReplace, | |
| 94 registry_factory.CreateAuthHandlerFromString( | |
| 95 "Digest", HttpAuth::AUTH_SERVER, gurl, BoundNetLog(), | |
| 96 &handler)); | |
| 97 } | 85 } |
| 98 | 86 |
| 99 TEST(HttpAuthHandlerFactoryTest, DefaultFactory) { | 87 TEST(HttpAuthHandlerFactoryTest, DefaultFactory) { |
| 100 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); | 88 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); |
| 101 MockAllowURLSecurityManager url_security_manager; | 89 MockAllowURLSecurityManager url_security_manager; |
| 102 scoped_ptr<HttpAuthHandlerRegistryFactory> http_auth_handler_factory( | 90 scoped_ptr<HttpAuthHandlerRegistryFactory> http_auth_handler_factory( |
| 103 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); | 91 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); |
| 104 http_auth_handler_factory->SetURLSecurityManager( | 92 http_auth_handler_factory->SetURLSecurityManager( |
| 105 "negotiate", &url_security_manager); | 93 "negotiate", &url_security_manager); |
| 106 GURL server_origin("http://www.example.com"); | 94 GURL server_origin("http://www.example.com"); |
| 107 GURL proxy_origin("http://cache.example.com:3128"); | 95 GURL proxy_origin("http://cache.example.com:3128"); |
| 108 { | 96 { |
| 109 scoped_ptr<HttpAuthHandler> handler; | 97 std::string challenge = "Basic realm=\"FooBar\""; |
| 110 int rv = http_auth_handler_factory->CreateAuthHandlerFromString( | 98 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
| 111 "Basic realm=\"FooBar\"", | 99 scoped_ptr<HttpAuthHandler> handler = |
| 112 HttpAuth::AUTH_SERVER, | 100 http_auth_handler_factory->CreateAuthHandlerForScheme( |
| 113 server_origin, | 101 tokenizer.NormalizedScheme()); |
| 114 BoundNetLog(), | 102 ASSERT_TRUE(handler); |
| 115 &handler); | 103 int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, |
| 104 server_origin, BoundNetLog()); |
| 116 EXPECT_EQ(OK, rv); | 105 EXPECT_EQ(OK, rv); |
| 117 ASSERT_FALSE(handler.get() == NULL); | 106 ASSERT_FALSE(handler.get() == NULL); |
| 118 EXPECT_EQ("basic", handler->auth_scheme()); | 107 EXPECT_EQ("basic", handler->auth_scheme()); |
| 119 EXPECT_STREQ("FooBar", handler->realm().c_str()); | 108 EXPECT_STREQ("FooBar", handler->realm().c_str()); |
| 120 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); | 109 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); |
| 121 } | 110 } |
| 122 { | 111 { |
| 123 scoped_ptr<HttpAuthHandler> handler; | 112 scoped_ptr<HttpAuthHandler> handler = |
| 124 int rv = http_auth_handler_factory->CreateAuthHandlerFromString( | 113 http_auth_handler_factory->CreateAuthHandlerForScheme("UNSUPPORTED"); |
| 125 "UNSUPPORTED realm=\"FooBar\"", | 114 EXPECT_FALSE(handler); |
| 126 HttpAuth::AUTH_SERVER, | |
| 127 server_origin, | |
| 128 BoundNetLog(), | |
| 129 &handler); | |
| 130 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); | |
| 131 EXPECT_TRUE(handler.get() == NULL); | |
| 132 } | 115 } |
| 133 { | 116 { |
| 134 scoped_ptr<HttpAuthHandler> handler; | 117 std::string challenge = "Digest realm=\"FooBar\", nonce=\"xyz\""; |
| 135 int rv = http_auth_handler_factory->CreateAuthHandlerFromString( | 118 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
| 136 "Digest realm=\"FooBar\", nonce=\"xyz\"", | 119 scoped_ptr<HttpAuthHandler> handler = |
| 137 HttpAuth::AUTH_PROXY, | 120 http_auth_handler_factory->CreateAuthHandlerForScheme( |
| 138 proxy_origin, | 121 tokenizer.NormalizedScheme()); |
| 139 BoundNetLog(), | 122 ASSERT_TRUE(handler); |
| 140 &handler); | 123 int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_PROXY, |
| 124 proxy_origin, BoundNetLog()); |
| 141 EXPECT_EQ(OK, rv); | 125 EXPECT_EQ(OK, rv); |
| 142 ASSERT_FALSE(handler.get() == NULL); | 126 ASSERT_FALSE(handler.get() == NULL); |
| 143 EXPECT_EQ("digest", handler->auth_scheme()); | 127 EXPECT_EQ("digest", handler->auth_scheme()); |
| 144 EXPECT_STREQ("FooBar", handler->realm().c_str()); | 128 EXPECT_STREQ("FooBar", handler->realm().c_str()); |
| 145 EXPECT_EQ(HttpAuth::AUTH_PROXY, handler->target()); | 129 EXPECT_EQ(HttpAuth::AUTH_PROXY, handler->target()); |
| 146 } | 130 } |
| 147 { | 131 { |
| 148 scoped_ptr<HttpAuthHandler> handler; | 132 std::string challenge = "NTLM"; |
| 149 int rv = http_auth_handler_factory->CreateAuthHandlerFromString( | 133 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
| 150 "NTLM", | 134 scoped_ptr<HttpAuthHandler> handler = |
| 151 HttpAuth::AUTH_SERVER, | 135 http_auth_handler_factory->CreateAuthHandlerForScheme( |
| 152 server_origin, | 136 tokenizer.NormalizedScheme()); |
| 153 BoundNetLog(), | 137 ASSERT_TRUE(handler); |
| 154 &handler); | 138 int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, |
| 139 server_origin, BoundNetLog()); |
| 155 EXPECT_EQ(OK, rv); | 140 EXPECT_EQ(OK, rv); |
| 156 ASSERT_FALSE(handler.get() == NULL); | 141 ASSERT_FALSE(handler.get() == NULL); |
| 157 EXPECT_EQ("ntlm", handler->auth_scheme()); | 142 EXPECT_EQ("ntlm", handler->auth_scheme()); |
| 158 EXPECT_STREQ("", handler->realm().c_str()); | 143 EXPECT_STREQ("", handler->realm().c_str()); |
| 159 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); | 144 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); |
| 160 } | 145 } |
| 161 { | 146 { |
| 162 scoped_ptr<HttpAuthHandler> handler; | 147 std::string challenge = "Negotiate"; |
| 163 int rv = http_auth_handler_factory->CreateAuthHandlerFromString( | 148 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
| 164 "Negotiate", | 149 scoped_ptr<HttpAuthHandler> handler = |
| 165 HttpAuth::AUTH_SERVER, | 150 http_auth_handler_factory->CreateAuthHandlerForScheme( |
| 166 server_origin, | 151 tokenizer.NormalizedScheme()); |
| 167 BoundNetLog(), | 152 ASSERT_TRUE(handler); |
| 168 &handler); | 153 int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, |
| 154 server_origin, BoundNetLog()); |
| 169 // Note the default factory doesn't support Kerberos on Android | 155 // Note the default factory doesn't support Kerberos on Android |
| 170 #if defined(USE_KERBEROS) && !defined(OS_ANDROID) | 156 #if defined(USE_KERBEROS) && !defined(OS_ANDROID) |
| 171 EXPECT_EQ(OK, rv); | 157 EXPECT_EQ(OK, rv); |
| 172 ASSERT_FALSE(handler.get() == NULL); | 158 ASSERT_FALSE(handler.get() == NULL); |
| 173 EXPECT_EQ("negotiate", handler->auth_scheme()); | 159 EXPECT_EQ("negotiate", handler->auth_scheme()); |
| 174 EXPECT_STREQ("", handler->realm().c_str()); | 160 EXPECT_STREQ("", handler->realm().c_str()); |
| 175 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); | 161 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); |
| 176 #else | 162 #else |
| 177 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); | 163 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); |
| 178 EXPECT_TRUE(handler.get() == NULL); | 164 EXPECT_TRUE(handler.get() == NULL); |
| 179 #endif // defined(USE_KERBEROS) && !defined(OS_ANDROID) | 165 #endif // defined(USE_KERBEROS) && !defined(OS_ANDROID) |
| 180 } | 166 } |
| 181 } | 167 } |
| 182 | 168 |
| 183 } // namespace net | 169 } // namespace net |
| OLD | NEW |