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 <string> | 5 #include <string> |
6 | 6 |
7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
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/http/http_auth_challenge_tokenizer.h" | 13 #include "net/http/http_auth_challenge_tokenizer.h" |
14 #include "net/http/http_auth_handler_basic.h" | 14 #include "net/http/http_auth_handler_basic.h" |
15 #include "net/http/http_request_info.h" | 15 #include "net/http/http_request_info.h" |
| 16 #include "net/http/http_response_info.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 | 20 |
20 TEST(HttpAuthHandlerBasicTest, GenerateAuthToken) { | 21 TEST(HttpAuthHandlerBasicTest, GenerateAuthToken) { |
21 static const struct { | 22 static const struct { |
22 const char* username; | 23 const char* username; |
23 const char* password; | 24 const char* password; |
24 const char* expected_credentials; | 25 const char* expected_credentials; |
25 } tests[] = { | 26 } tests[] = { |
26 { "foo", "bar", "Basic Zm9vOmJhcg==" }, | 27 { "foo", "bar", "Basic Zm9vOmJhcg==" }, |
27 // Empty username | 28 // Empty username |
28 { "", "foobar", "Basic OmZvb2Jhcg==" }, | 29 { "", "foobar", "Basic OmZvb2Jhcg==" }, |
29 // Empty password | 30 // Empty password |
30 { "anon", "", "Basic YW5vbjo=" }, | 31 { "anon", "", "Basic YW5vbjo=" }, |
31 // Empty username and empty password. | 32 // Empty username and empty password. |
32 { "", "", "Basic Og==" }, | 33 { "", "", "Basic Og==" }, |
33 }; | 34 }; |
34 GURL origin("http://www.example.com"); | 35 GURL origin("http://www.example.com"); |
35 HttpAuthHandlerBasic::Factory factory; | 36 HttpAuthHandlerBasic::Factory factory; |
36 for (size_t i = 0; i < arraysize(tests); ++i) { | 37 for (size_t i = 0; i < arraysize(tests); ++i) { |
37 std::string challenge = "Basic realm=\"Atlantis\""; | 38 std::string challenge = "Basic realm=\"Atlantis\""; |
38 scoped_ptr<HttpAuthHandler> basic = | 39 scoped_ptr<HttpAuthHandler> basic = |
39 factory.CreateAuthHandlerForScheme("basic"); | 40 factory.CreateAuthHandlerForScheme("basic"); |
40 ASSERT_TRUE(basic); | 41 ASSERT_TRUE(basic); |
41 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); | 42 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
42 EXPECT_EQ(OK, basic->HandleInitialChallenge( | 43 HttpResponseInfo response_info; |
43 tokenizer, HttpAuth::AUTH_SERVER, origin, BoundNetLog())); | 44 TestCompletionCallback callback; |
| 45 int rv = basic->HandleInitialChallenge(tokenizer, response_info, |
| 46 HttpAuth::AUTH_SERVER, origin, |
| 47 BoundNetLog(), callback.callback()); |
| 48 EXPECT_EQ(OK, callback.GetResult(rv)); |
44 AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username), | 49 AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username), |
45 base::ASCIIToUTF16(tests[i].password)); | 50 base::ASCIIToUTF16(tests[i].password)); |
46 HttpRequestInfo request_info; | 51 HttpRequestInfo request_info; |
47 std::string auth_token; | 52 std::string auth_token; |
48 TestCompletionCallback callback; | 53 rv = basic->GenerateAuthToken(&credentials, request_info, |
49 int rv = basic->GenerateAuthToken(&credentials, request_info, | 54 callback.callback(), &auth_token); |
50 callback.callback(), &auth_token); | 55 EXPECT_EQ(OK, callback.GetResult(rv)); |
51 EXPECT_EQ(OK, rv); | |
52 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str()); | 56 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str()); |
53 } | 57 } |
54 } | 58 } |
55 | 59 |
56 TEST(HttpAuthHandlerBasicTest, HandleAnotherChallenge) { | 60 TEST(HttpAuthHandlerBasicTest, HandleAnotherChallenge) { |
57 static const struct { | 61 static const struct { |
58 const char* challenge; | 62 const char* challenge; |
59 HttpAuth::AuthorizationResult expected_rv; | 63 HttpAuth::AuthorizationResult expected_rv; |
60 } tests[] = { | 64 } tests[] = { |
61 // The handler is initialized using this challenge. The first | 65 // The handler is initialized using this challenge. The first |
(...skipping 26 matching lines...) Expand all Loading... |
88 } | 92 } |
89 }; | 93 }; |
90 | 94 |
91 GURL origin("http://www.example.com"); | 95 GURL origin("http://www.example.com"); |
92 HttpAuthHandlerBasic::Factory factory; | 96 HttpAuthHandlerBasic::Factory factory; |
93 scoped_ptr<HttpAuthHandler> basic = | 97 scoped_ptr<HttpAuthHandler> basic = |
94 factory.CreateAuthHandlerForScheme("basic"); | 98 factory.CreateAuthHandlerForScheme("basic"); |
95 std::string initial_challenge(tests[0].challenge); | 99 std::string initial_challenge(tests[0].challenge); |
96 HttpAuthChallengeTokenizer tokenizer(initial_challenge.begin(), | 100 HttpAuthChallengeTokenizer tokenizer(initial_challenge.begin(), |
97 initial_challenge.end()); | 101 initial_challenge.end()); |
98 EXPECT_EQ(OK, basic->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, | 102 HttpResponseInfo response_info; |
99 origin, BoundNetLog())); | 103 TestCompletionCallback callback; |
| 104 int rv = basic->HandleInitialChallenge(tokenizer, response_info, |
| 105 HttpAuth::AUTH_SERVER, origin, |
| 106 BoundNetLog(), callback.callback()); |
| 107 EXPECT_EQ(OK, callback.GetResult(rv)); |
100 | 108 |
101 for (size_t i = 0; i < arraysize(tests); ++i) { | 109 for (size_t i = 0; i < arraysize(tests); ++i) { |
102 std::string challenge(tests[i].challenge); | 110 std::string challenge(tests[i].challenge); |
103 HttpAuthChallengeTokenizer tok(challenge.begin(), | 111 HttpAuthChallengeTokenizer tok(challenge.begin(), |
104 challenge.end()); | 112 challenge.end()); |
105 EXPECT_EQ(tests[i].expected_rv, basic->HandleAnotherChallenge(tok)); | 113 EXPECT_EQ(tests[i].expected_rv, basic->HandleAnotherChallenge(tok)); |
106 } | 114 } |
107 } | 115 } |
108 | 116 |
109 TEST(HttpAuthHandlerBasicTest, HandleInitialChallenge) { | 117 TEST(HttpAuthHandlerBasicTest, HandleInitialChallenge) { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 "foo-\xC3\xA5", | 190 "foo-\xC3\xA5", |
183 }, | 191 }, |
184 }; | 192 }; |
185 HttpAuthHandlerBasic::Factory factory; | 193 HttpAuthHandlerBasic::Factory factory; |
186 GURL origin("http://www.example.com"); | 194 GURL origin("http://www.example.com"); |
187 for (size_t i = 0; i < arraysize(tests); ++i) { | 195 for (size_t i = 0; i < arraysize(tests); ++i) { |
188 std::string challenge = tests[i].challenge; | 196 std::string challenge = tests[i].challenge; |
189 scoped_ptr<HttpAuthHandler> basic = | 197 scoped_ptr<HttpAuthHandler> basic = |
190 factory.CreateAuthHandlerForScheme("basic"); | 198 factory.CreateAuthHandlerForScheme("basic"); |
191 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); | 199 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
192 int rv = basic->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, | 200 HttpResponseInfo response_info; |
193 origin, BoundNetLog()); | 201 TestCompletionCallback callback; |
194 EXPECT_EQ(tests[i].expected_rv, rv); | 202 int rv = basic->HandleInitialChallenge(tokenizer, response_info, |
| 203 HttpAuth::AUTH_SERVER, origin, |
| 204 BoundNetLog(), callback.callback()); |
| 205 EXPECT_EQ(tests[i].expected_rv, callback.GetResult(rv)); |
195 if (rv == OK) | 206 if (rv == OK) |
196 EXPECT_EQ(tests[i].expected_realm, basic->realm()); | 207 EXPECT_EQ(tests[i].expected_realm, basic->realm()); |
197 } | 208 } |
198 } | 209 } |
199 | 210 |
200 TEST(HttpAuthHandlerBasicTest, CreateAuthHandlerForScheme) { | 211 TEST(HttpAuthHandlerBasicTest, CreateAuthHandlerForScheme) { |
201 HttpAuthHandlerBasic::Factory basic_factory; | 212 HttpAuthHandlerBasic::Factory basic_factory; |
202 | 213 |
203 EXPECT_TRUE(basic_factory.CreateAuthHandlerForScheme("basic")); | 214 EXPECT_TRUE(basic_factory.CreateAuthHandlerForScheme("basic")); |
204 EXPECT_FALSE(basic_factory.CreateAuthHandlerForScheme("bogus")); | 215 EXPECT_FALSE(basic_factory.CreateAuthHandlerForScheme("bogus")); |
205 } | 216 } |
206 | 217 |
207 TEST(HttpAuthHandlerBasicTest, CreateAndInitPreemptiveAuthHandler_Valid) { | 218 TEST(HttpAuthHandlerBasicTest, CreateAndInitPreemptiveAuthHandler_Valid) { |
208 HttpAuthHandlerBasic::Factory digest_factory; | 219 HttpAuthHandlerBasic::Factory digest_factory; |
209 HttpAuthCache auth_cache; | 220 HttpAuthCache auth_cache; |
210 std::string challenge("basic realm=\"Foo\""); | 221 std::string challenge("basic realm=\"Foo\""); |
211 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); | |
212 | 222 |
213 HttpAuthCache::Entry* entry = | 223 HttpAuthCache::Entry* entry = |
214 auth_cache.Add(GURL("http://example.com/foo").GetOrigin(), "foo", "basic", | 224 auth_cache.Add(GURL("http://example.com/foo").GetOrigin(), "foo", "basic", |
215 challenge, AuthCredentials(), "/foo"); | 225 challenge, AuthCredentials(), "/foo"); |
216 EXPECT_TRUE(digest_factory.CreateAndInitPreemptiveAuthHandler( | 226 EXPECT_TRUE(digest_factory.CreateAndInitPreemptiveAuthHandler( |
217 entry, tokenizer, HttpAuth::AUTH_SERVER, BoundNetLog())); | 227 entry, HttpAuth::AUTH_SERVER, BoundNetLog())); |
218 } | 228 } |
219 | 229 |
220 TEST(HttpAuthHandlerBasicTest, CreateAndInitPreemptiveAuthHandler_Invalid) { | 230 TEST(HttpAuthHandlerBasicTest, CreateAndInitPreemptiveAuthHandler_Invalid) { |
221 HttpAuthHandlerBasic::Factory digest_factory; | 231 HttpAuthHandlerBasic::Factory digest_factory; |
222 HttpAuthCache auth_cache; | 232 HttpAuthCache auth_cache; |
223 std::string challenge("digest realm=\"foo\""); | 233 std::string challenge("digest realm=\"foo\""); |
224 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); | |
225 | 234 |
226 HttpAuthCache::Entry* entry = | 235 HttpAuthCache::Entry* entry = |
227 auth_cache.Add(GURL("http://example.com").GetOrigin(), "bar", "digest", | 236 auth_cache.Add(GURL("http://example.com").GetOrigin(), "bar", "digest", |
228 challenge, AuthCredentials(), "/bar"); | 237 challenge, AuthCredentials(), "/bar"); |
229 EXPECT_FALSE(digest_factory.CreateAndInitPreemptiveAuthHandler( | 238 EXPECT_FALSE(digest_factory.CreateAndInitPreemptiveAuthHandler( |
230 entry, tokenizer, HttpAuth::AUTH_SERVER, BoundNetLog())); | 239 entry, HttpAuth::AUTH_SERVER, BoundNetLog())); |
231 } | 240 } |
232 | 241 |
233 } // namespace net | 242 } // namespace net |
OLD | NEW |