| 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 <set> | 5 #include <set> |
| 6 #include <string> | 6 #include <string> |
| 7 | 7 |
| 8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
| 9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 42 return new HttpResponseHeaders( | 42 return new HttpResponseHeaders( |
| 43 HttpUtil::AssembleRawHeaders(response.c_str(), response.length())); | 43 HttpUtil::AssembleRawHeaders(response.c_str(), response.length())); |
| 44 } | 44 } |
| 45 | 45 |
| 46 HttpAuth::AuthorizationResult HandleChallengeResponse( | 46 HttpAuth::AuthorizationResult HandleChallengeResponse( |
| 47 bool connection_based, | 47 bool connection_based, |
| 48 const std::string& headers_text, | 48 const std::string& headers_text, |
| 49 std::string* challenge_used) { | 49 std::string* challenge_used) { |
| 50 scoped_ptr<HttpAuthHandlerMock> mock_handler( | 50 scoped_ptr<HttpAuthHandlerMock> mock_handler( |
| 51 CreateMockHandler(connection_based)); | 51 CreateMockHandler(connection_based)); |
| 52 std::set<HttpAuth::Scheme> disabled_schemes; | 52 std::set<std::string> disabled_schemes; |
| 53 scoped_refptr<HttpResponseHeaders> headers( | 53 scoped_refptr<HttpResponseHeaders> headers( |
| 54 HeadersFromResponseText(headers_text)); | 54 HeadersFromResponseText(headers_text)); |
| 55 return HttpAuth::HandleChallengeResponse( | 55 return HttpAuth::HandleChallengeResponse( |
| 56 mock_handler.get(), | 56 mock_handler.get(), |
| 57 headers.get(), | 57 headers.get(), |
| 58 HttpAuth::AUTH_SERVER, | 58 HttpAuth::AUTH_SERVER, |
| 59 disabled_schemes, | 59 disabled_schemes, |
| 60 challenge_used); | 60 challenge_used); |
| 61 } | 61 } |
| 62 | 62 |
| 63 } // namespace | 63 } // namespace |
| 64 | 64 |
| 65 TEST(HttpAuthTest, ChooseBestChallenge) { | 65 TEST(HttpAuthTest, ChooseBestChallenge) { |
| 66 static const struct { | 66 static const struct { |
| 67 const char* headers; | 67 const char* headers; |
| 68 HttpAuth::Scheme challenge_scheme; | 68 const char* challenge_scheme; |
| 69 const char* challenge_realm; | 69 const char* challenge_realm; |
| 70 } tests[] = { | 70 } tests[] = { |
| 71 { | 71 { |
| 72 // Basic is the only challenge type, pick it. | 72 // Basic is the only challenge type, pick it. |
| 73 "Y: Digest realm=\"X\", nonce=\"aaaaaaaaaa\"\n" | 73 "Y: Digest realm=\"X\", nonce=\"aaaaaaaaaa\"\n" |
| 74 "www-authenticate: Basic realm=\"BasicRealm\"\n", | 74 "www-authenticate: Basic realm=\"BasicRealm\"\n", |
| 75 | 75 |
| 76 HttpAuth::AUTH_SCHEME_BASIC, | 76 "basic", |
| 77 "BasicRealm", | 77 "BasicRealm", |
| 78 }, | 78 }, |
| 79 { | 79 { |
| 80 // Fake is the only challenge type, but it is unsupported. | 80 // Fake is the only challenge type, but it is unsupported. |
| 81 "Y: Digest realm=\"FooBar\", nonce=\"aaaaaaaaaa\"\n" | 81 "Y: Digest realm=\"FooBar\", nonce=\"aaaaaaaaaa\"\n" |
| 82 "www-authenticate: Fake realm=\"FooBar\"\n", | 82 "www-authenticate: Fake realm=\"FooBar\"\n", |
| 83 | 83 |
| 84 HttpAuth::AUTH_SCHEME_MAX, | 84 "", |
| 85 "", | 85 "", |
| 86 }, | 86 }, |
| 87 { | 87 { |
| 88 // Pick Digest over Basic. | 88 // Pick Digest over Basic. |
| 89 "www-authenticate: Basic realm=\"FooBar\"\n" | 89 "www-authenticate: Basic realm=\"FooBar\"\n" |
| 90 "www-authenticate: Fake realm=\"FooBar\"\n" | 90 "www-authenticate: Fake realm=\"FooBar\"\n" |
| 91 "www-authenticate: nonce=\"aaaaaaaaaa\"\n" | 91 "www-authenticate: nonce=\"aaaaaaaaaa\"\n" |
| 92 "www-authenticate: Digest realm=\"DigestRealm\", nonce=\"aaaaaaaaaa\"\n", | 92 "www-authenticate: Digest realm=\"DigestRealm\", nonce=\"aaaaaaaaaa\"\n", |
| 93 | 93 |
| 94 HttpAuth::AUTH_SCHEME_DIGEST, | 94 "digest", |
| 95 "DigestRealm", | 95 "DigestRealm", |
| 96 }, | 96 }, |
| 97 { | 97 { |
| 98 // Handle an empty header correctly. | 98 // Handle an empty header correctly. |
| 99 "Y: Digest realm=\"X\", nonce=\"aaaaaaaaaa\"\n" | 99 "Y: Digest realm=\"X\", nonce=\"aaaaaaaaaa\"\n" |
| 100 "www-authenticate:\n", | 100 "www-authenticate:\n", |
| 101 | 101 |
| 102 HttpAuth::AUTH_SCHEME_MAX, | 102 "", |
| 103 "", | 103 "", |
| 104 }, | 104 }, |
| 105 { | 105 { |
| 106 "WWW-Authenticate: Negotiate\n" | 106 "WWW-Authenticate: Negotiate\n" |
| 107 "WWW-Authenticate: NTLM\n", | 107 "WWW-Authenticate: NTLM\n", |
| 108 | 108 |
| 109 #if defined(USE_KERBEROS) | 109 #if defined(USE_KERBEROS) |
| 110 // Choose Negotiate over NTLM on all platforms. | 110 // Choose Negotiate over NTLM on all platforms. |
| 111 // TODO(ahendrickson): This may be flaky on Linux and OSX as it | 111 // TODO(ahendrickson): This may be flaky on Linux and OSX as it |
| 112 // relies on being able to load one of the known .so files | 112 // relies on being able to load one of the known .so files |
| 113 // for gssapi. | 113 // for gssapi. |
| 114 HttpAuth::AUTH_SCHEME_NEGOTIATE, | 114 "negotiate", |
| 115 #else | 115 #else |
| 116 // On systems that don't use Kerberos fall back to NTLM. | 116 // On systems that don't use Kerberos fall back to NTLM. |
| 117 HttpAuth::AUTH_SCHEME_NTLM, | 117 "ntlm", |
| 118 #endif // defined(USE_KERBEROS) | 118 #endif // defined(USE_KERBEROS) |
| 119 "", | 119 "", |
| 120 } | 120 } |
| 121 }; | 121 }; |
| 122 GURL origin("http://www.example.com"); | 122 GURL origin("http://www.example.com"); |
| 123 std::set<HttpAuth::Scheme> disabled_schemes; | 123 std::set<std::string> disabled_schemes; |
| 124 MockAllowURLSecurityManager url_security_manager; | 124 MockAllowURLSecurityManager url_security_manager; |
| 125 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); | 125 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); |
| 126 scoped_ptr<HttpAuthHandlerRegistryFactory> http_auth_handler_factory( | 126 scoped_ptr<HttpAuthHandlerRegistryFactory> http_auth_handler_factory( |
| 127 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); | 127 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); |
| 128 http_auth_handler_factory->SetURLSecurityManager( | 128 http_auth_handler_factory->SetURLSecurityManager( |
| 129 "negotiate", &url_security_manager); | 129 "negotiate", &url_security_manager); |
| 130 | 130 |
| 131 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 131 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 132 // Make a HttpResponseHeaders object. | 132 // Make a HttpResponseHeaders object. |
| 133 std::string headers_with_status_line("HTTP/1.1 401 Unauthorized\n"); | 133 std::string headers_with_status_line("HTTP/1.1 401 Unauthorized\n"); |
| 134 headers_with_status_line += tests[i].headers; | 134 headers_with_status_line += tests[i].headers; |
| 135 scoped_refptr<HttpResponseHeaders> headers( | 135 scoped_refptr<HttpResponseHeaders> headers( |
| 136 HeadersFromResponseText(headers_with_status_line)); | 136 HeadersFromResponseText(headers_with_status_line)); |
| 137 | 137 |
| 138 scoped_ptr<HttpAuthHandler> handler; | 138 scoped_ptr<HttpAuthHandler> handler; |
| 139 HttpAuth::ChooseBestChallenge(http_auth_handler_factory.get(), | 139 HttpAuth::ChooseBestChallenge(http_auth_handler_factory.get(), |
| 140 headers.get(), | 140 headers.get(), |
| 141 HttpAuth::AUTH_SERVER, | 141 HttpAuth::AUTH_SERVER, |
| 142 origin, | 142 origin, |
| 143 disabled_schemes, | 143 disabled_schemes, |
| 144 BoundNetLog(), | 144 BoundNetLog(), |
| 145 &handler); | 145 &handler); |
| 146 | 146 |
| 147 if (handler.get()) { | 147 if (handler.get()) { |
| 148 EXPECT_EQ(tests[i].challenge_scheme, handler->auth_scheme()); | 148 EXPECT_STREQ(tests[i].challenge_scheme, handler->auth_scheme().c_str()); |
| 149 EXPECT_STREQ(tests[i].challenge_realm, handler->realm().c_str()); | 149 EXPECT_STREQ(tests[i].challenge_realm, handler->realm().c_str()); |
| 150 } else { | 150 } else { |
| 151 EXPECT_EQ(HttpAuth::AUTH_SCHEME_MAX, tests[i].challenge_scheme); | 151 EXPECT_STREQ("", tests[i].challenge_scheme); |
| 152 EXPECT_STREQ("", tests[i].challenge_realm); | 152 EXPECT_STREQ("", tests[i].challenge_realm); |
| 153 } | 153 } |
| 154 } | 154 } |
| 155 } | 155 } |
| 156 | 156 |
| 157 TEST(HttpAuthTest, HandleChallengeResponse) { | 157 TEST(HttpAuthTest, HandleChallengeResponse) { |
| 158 std::string challenge_used; | 158 std::string challenge_used; |
| 159 const char* const kMockChallenge = | 159 const char* const kMockChallenge = |
| 160 "HTTP/1.1 401 Unauthorized\n" | 160 "HTTP/1.1 401 Unauthorized\n" |
| 161 "WWW-Authenticate: Mock token_here\n"; | 161 "WWW-Authenticate: Mock token_here\n"; |
| (...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 426 std::string name; | 426 std::string name; |
| 427 | 427 |
| 428 name = HttpAuth::GetAuthorizationHeaderName(HttpAuth::AUTH_SERVER); | 428 name = HttpAuth::GetAuthorizationHeaderName(HttpAuth::AUTH_SERVER); |
| 429 EXPECT_STREQ("Authorization", name.c_str()); | 429 EXPECT_STREQ("Authorization", name.c_str()); |
| 430 | 430 |
| 431 name = HttpAuth::GetAuthorizationHeaderName(HttpAuth::AUTH_PROXY); | 431 name = HttpAuth::GetAuthorizationHeaderName(HttpAuth::AUTH_PROXY); |
| 432 EXPECT_STREQ("Proxy-Authorization", name.c_str()); | 432 EXPECT_STREQ("Proxy-Authorization", name.c_str()); |
| 433 } | 433 } |
| 434 | 434 |
| 435 } // namespace net | 435 } // namespace net |
| OLD | NEW |