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 "base/strings/string_util.h" |
7 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
| 8 #include "net/base/test_completion_callback.h" |
8 #include "net/dns/mock_host_resolver.h" | 9 #include "net/dns/mock_host_resolver.h" |
9 #include "net/http/http_auth_challenge_tokenizer.h" | 10 #include "net/http/http_auth_challenge_tokenizer.h" |
10 #include "net/http/http_auth_handler.h" | 11 #include "net/http/http_auth_handler.h" |
11 #include "net/http/http_auth_handler_factory.h" | 12 #include "net/http/http_auth_handler_factory.h" |
12 #include "net/http/http_auth_handler_mock.h" | 13 #include "net/http/http_auth_handler_mock.h" |
| 14 #include "net/http/http_response_info.h" |
13 #include "net/http/mock_allow_url_security_manager.h" | 15 #include "net/http/mock_allow_url_security_manager.h" |
14 #include "net/http/url_security_manager.h" | 16 #include "net/http/url_security_manager.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
16 | 18 |
17 namespace net { | 19 namespace net { |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { | 23 class MockHttpAuthHandlerFactory : public HttpAuthHandlerFactory { |
22 public: | 24 public: |
23 explicit MockHttpAuthHandlerFactory(const char* scheme) | 25 explicit MockHttpAuthHandlerFactory(const char* scheme) |
24 : scheme_(base::ToLowerASCII(scheme)) {} | 26 : scheme_(base::ToLowerASCII(scheme)) {} |
25 ~MockHttpAuthHandlerFactory() override {} | 27 ~MockHttpAuthHandlerFactory() override {} |
26 | 28 |
27 scoped_ptr<HttpAuthHandler> CreateAuthHandlerForScheme( | 29 scoped_ptr<HttpAuthHandler> CreateAuthHandlerForScheme( |
28 const std::string& scheme) override { | 30 const std::string& scheme) override { |
| 31 if (scheme_.empty()) |
| 32 return scoped_ptr<HttpAuthHandler>(); |
29 EXPECT_EQ(scheme, scheme_) << scheme << " vs. " << scheme_; | 33 EXPECT_EQ(scheme, scheme_) << scheme << " vs. " << scheme_; |
30 return make_scoped_ptr(new HttpAuthHandlerMock()); | 34 return make_scoped_ptr(new HttpAuthHandlerMock()); |
31 } | 35 } |
32 scoped_ptr<HttpAuthHandler> CreateAndInitPreemptiveAuthHandler( | 36 scoped_ptr<HttpAuthHandler> CreateAndInitPreemptiveAuthHandler( |
33 HttpAuthCache::Entry* cache_entry, | 37 HttpAuthCache::Entry* cache_entry, |
34 const HttpAuthChallengeTokenizer& tokenizer, | |
35 HttpAuth::Target target, | 38 HttpAuth::Target target, |
36 const BoundNetLog& net_log) override { | 39 const BoundNetLog& net_log) override { |
37 EXPECT_EQ(cache_entry->scheme(), scheme_) << cache_entry->scheme() | 40 EXPECT_EQ(cache_entry->scheme(), scheme_) << cache_entry->scheme() |
38 << " vs. " << scheme_; | 41 << " vs. " << scheme_; |
39 return make_scoped_ptr(new HttpAuthHandlerMock()); | 42 return make_scoped_ptr(new HttpAuthHandlerMock()); |
40 } | 43 } |
41 | 44 |
42 private: | 45 private: |
43 std::string scheme_; | 46 std::string scheme_; |
44 }; | 47 }; |
(...skipping 23 matching lines...) Expand all Loading... |
68 "digest", new MockHttpAuthHandlerFactory("digest")); | 71 "digest", new MockHttpAuthHandlerFactory("digest")); |
69 handler = registry_factory.CreateAuthHandlerForScheme("basic"); | 72 handler = registry_factory.CreateAuthHandlerForScheme("basic"); |
70 EXPECT_TRUE(handler); | 73 EXPECT_TRUE(handler); |
71 handler = registry_factory.CreateAuthHandlerForScheme("digest"); | 74 handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
72 EXPECT_TRUE(handler); | 75 EXPECT_TRUE(handler); |
73 handler = registry_factory.CreateAuthHandlerForScheme("bogus"); | 76 handler = registry_factory.CreateAuthHandlerForScheme("bogus"); |
74 EXPECT_FALSE(handler); | 77 EXPECT_FALSE(handler); |
75 | 78 |
76 // Test replacement of existing schemes. | 79 // Test replacement of existing schemes. |
77 registry_factory.RegisterSchemeFactory("digest", | 80 registry_factory.RegisterSchemeFactory("digest", |
78 new HttpAuthHandlerMock::Factory()); | 81 new MockHttpAuthHandlerFactory("")); |
79 handler = registry_factory.CreateAuthHandlerForScheme("digest"); | 82 handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
80 EXPECT_FALSE(handler); | 83 EXPECT_FALSE(handler); |
81 registry_factory.RegisterSchemeFactory( | 84 registry_factory.RegisterSchemeFactory( |
82 "digest", new MockHttpAuthHandlerFactory("digest")); | 85 "digest", new MockHttpAuthHandlerFactory("digest")); |
83 handler = registry_factory.CreateAuthHandlerForScheme("digest"); | 86 handler = registry_factory.CreateAuthHandlerForScheme("digest"); |
84 EXPECT_TRUE(handler); | 87 EXPECT_TRUE(handler); |
85 } | 88 } |
86 | 89 |
87 TEST(HttpAuthHandlerFactoryTest, DefaultFactory) { | 90 TEST(HttpAuthHandlerFactoryTest, DefaultFactory) { |
88 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); | 91 scoped_ptr<HostResolver> host_resolver(new MockHostResolver()); |
89 MockAllowURLSecurityManager url_security_manager; | 92 MockAllowURLSecurityManager url_security_manager; |
90 scoped_ptr<HttpAuthHandlerRegistryFactory> http_auth_handler_factory( | 93 scoped_ptr<HttpAuthHandlerRegistryFactory> http_auth_handler_factory( |
91 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); | 94 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())); |
92 http_auth_handler_factory->SetURLSecurityManager( | 95 http_auth_handler_factory->SetURLSecurityManager( |
93 "negotiate", &url_security_manager); | 96 "negotiate", &url_security_manager); |
94 GURL server_origin("http://www.example.com"); | 97 GURL server_origin("http://www.example.com"); |
95 GURL proxy_origin("http://cache.example.com:3128"); | 98 GURL proxy_origin("http://cache.example.com:3128"); |
| 99 TestCompletionCallback callback; |
| 100 HttpResponseInfo response_info; |
96 { | 101 { |
97 std::string challenge = "Basic realm=\"FooBar\""; | 102 std::string challenge = "Basic realm=\"FooBar\""; |
98 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); | 103 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
99 scoped_ptr<HttpAuthHandler> handler = | 104 scoped_ptr<HttpAuthHandler> handler = |
100 http_auth_handler_factory->CreateAuthHandlerForScheme( | 105 http_auth_handler_factory->CreateAuthHandlerForScheme( |
101 tokenizer.NormalizedScheme()); | 106 tokenizer.NormalizedScheme()); |
102 ASSERT_TRUE(handler); | 107 ASSERT_TRUE(handler); |
103 int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, | 108 int rv = handler->HandleInitialChallenge( |
104 server_origin, BoundNetLog()); | 109 tokenizer, response_info, HttpAuth::AUTH_SERVER, server_origin, |
105 EXPECT_EQ(OK, rv); | 110 BoundNetLog(), callback.callback()); |
106 ASSERT_FALSE(handler.get() == NULL); | 111 EXPECT_EQ(OK, callback.GetResult(rv)); |
107 EXPECT_EQ("basic", handler->auth_scheme()); | 112 EXPECT_EQ("basic", handler->auth_scheme()); |
108 EXPECT_STREQ("FooBar", handler->realm().c_str()); | 113 EXPECT_STREQ("FooBar", handler->realm().c_str()); |
109 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); | 114 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); |
110 } | 115 } |
111 { | 116 { |
112 scoped_ptr<HttpAuthHandler> handler = | 117 scoped_ptr<HttpAuthHandler> handler = |
113 http_auth_handler_factory->CreateAuthHandlerForScheme("UNSUPPORTED"); | 118 http_auth_handler_factory->CreateAuthHandlerForScheme("UNSUPPORTED"); |
114 EXPECT_FALSE(handler); | 119 EXPECT_FALSE(handler); |
115 } | 120 } |
116 { | 121 { |
117 std::string challenge = "Digest realm=\"FooBar\", nonce=\"xyz\""; | 122 std::string challenge = "Digest realm=\"FooBar\", nonce=\"xyz\""; |
118 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); | 123 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
119 scoped_ptr<HttpAuthHandler> handler = | 124 scoped_ptr<HttpAuthHandler> handler = |
120 http_auth_handler_factory->CreateAuthHandlerForScheme( | 125 http_auth_handler_factory->CreateAuthHandlerForScheme( |
121 tokenizer.NormalizedScheme()); | 126 tokenizer.NormalizedScheme()); |
122 ASSERT_TRUE(handler); | 127 ASSERT_TRUE(handler); |
123 int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_PROXY, | 128 int rv = handler->HandleInitialChallenge( |
124 proxy_origin, BoundNetLog()); | 129 tokenizer, response_info, HttpAuth::AUTH_PROXY, proxy_origin, |
125 EXPECT_EQ(OK, rv); | 130 BoundNetLog(), callback.callback()); |
126 ASSERT_FALSE(handler.get() == NULL); | 131 EXPECT_EQ(OK, callback.GetResult(rv)); |
127 EXPECT_EQ("digest", handler->auth_scheme()); | 132 EXPECT_EQ("digest", handler->auth_scheme()); |
128 EXPECT_STREQ("FooBar", handler->realm().c_str()); | 133 EXPECT_STREQ("FooBar", handler->realm().c_str()); |
129 EXPECT_EQ(HttpAuth::AUTH_PROXY, handler->target()); | 134 EXPECT_EQ(HttpAuth::AUTH_PROXY, handler->target()); |
130 } | 135 } |
131 { | 136 { |
132 std::string challenge = "NTLM"; | 137 std::string challenge = "NTLM"; |
133 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); | 138 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
134 scoped_ptr<HttpAuthHandler> handler = | 139 scoped_ptr<HttpAuthHandler> handler = |
135 http_auth_handler_factory->CreateAuthHandlerForScheme( | 140 http_auth_handler_factory->CreateAuthHandlerForScheme( |
136 tokenizer.NormalizedScheme()); | 141 tokenizer.NormalizedScheme()); |
137 ASSERT_TRUE(handler); | 142 ASSERT_TRUE(handler); |
138 int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, | 143 int rv = handler->HandleInitialChallenge( |
139 server_origin, BoundNetLog()); | 144 tokenizer, response_info, HttpAuth::AUTH_SERVER, server_origin, |
140 EXPECT_EQ(OK, rv); | 145 BoundNetLog(), callback.callback()); |
| 146 EXPECT_EQ(OK, callback.GetResult(rv)); |
141 ASSERT_FALSE(handler.get() == NULL); | 147 ASSERT_FALSE(handler.get() == NULL); |
142 EXPECT_EQ("ntlm", handler->auth_scheme()); | 148 EXPECT_EQ("ntlm", handler->auth_scheme()); |
143 EXPECT_STREQ("", handler->realm().c_str()); | 149 EXPECT_STREQ("", handler->realm().c_str()); |
144 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); | 150 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); |
145 } | 151 } |
146 { | 152 { |
147 std::string challenge = "Negotiate"; | 153 std::string challenge = "Negotiate"; |
148 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); | 154 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
149 scoped_ptr<HttpAuthHandler> handler = | 155 scoped_ptr<HttpAuthHandler> handler = |
150 http_auth_handler_factory->CreateAuthHandlerForScheme( | 156 http_auth_handler_factory->CreateAuthHandlerForScheme( |
151 tokenizer.NormalizedScheme()); | 157 tokenizer.NormalizedScheme()); |
152 ASSERT_TRUE(handler); | 158 ASSERT_TRUE(handler); |
153 int rv = handler->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, | 159 int rv = handler->HandleInitialChallenge( |
154 server_origin, BoundNetLog()); | 160 tokenizer, response_info, HttpAuth::AUTH_SERVER, server_origin, |
| 161 BoundNetLog(), callback.callback()); |
| 162 rv = callback.GetResult(rv); |
155 // Note the default factory doesn't support Kerberos on Android | 163 // Note the default factory doesn't support Kerberos on Android |
156 #if defined(USE_KERBEROS) && !defined(OS_ANDROID) | 164 #if defined(USE_KERBEROS) && !defined(OS_ANDROID) |
157 EXPECT_EQ(OK, rv); | 165 EXPECT_EQ(OK, rv); |
158 ASSERT_FALSE(handler.get() == NULL); | 166 ASSERT_FALSE(handler.get() == NULL); |
159 EXPECT_EQ("negotiate", handler->auth_scheme()); | 167 EXPECT_EQ("negotiate", handler->auth_scheme()); |
160 EXPECT_STREQ("", handler->realm().c_str()); | 168 EXPECT_STREQ("", handler->realm().c_str()); |
161 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); | 169 EXPECT_EQ(HttpAuth::AUTH_SERVER, handler->target()); |
162 #else | 170 #else |
163 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); | 171 EXPECT_EQ(ERR_UNSUPPORTED_AUTH_SCHEME, rv); |
164 EXPECT_TRUE(handler.get() == NULL); | 172 EXPECT_TRUE(handler.get() == NULL); |
165 #endif // defined(USE_KERBEROS) && !defined(OS_ANDROID) | 173 #endif // defined(USE_KERBEROS) && !defined(OS_ANDROID) |
166 } | 174 } |
167 } | 175 } |
168 | 176 |
169 } // namespace net | 177 } // namespace net |
OLD | NEW |