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" |
(...skipping 17 matching lines...) Expand all Loading... |
28 { "", "foobar", "Basic OmZvb2Jhcg==" }, | 28 { "", "foobar", "Basic OmZvb2Jhcg==" }, |
29 // Empty password | 29 // Empty password |
30 { "anon", "", "Basic YW5vbjo=" }, | 30 { "anon", "", "Basic YW5vbjo=" }, |
31 // Empty username and empty password. | 31 // Empty username and empty password. |
32 { "", "", "Basic Og==" }, | 32 { "", "", "Basic Og==" }, |
33 }; | 33 }; |
34 GURL origin("http://www.example.com"); | 34 GURL origin("http://www.example.com"); |
35 HttpAuthHandlerBasic::Factory factory; | 35 HttpAuthHandlerBasic::Factory factory; |
36 for (size_t i = 0; i < arraysize(tests); ++i) { | 36 for (size_t i = 0; i < arraysize(tests); ++i) { |
37 std::string challenge = "Basic realm=\"Atlantis\""; | 37 std::string challenge = "Basic realm=\"Atlantis\""; |
38 scoped_ptr<HttpAuthHandler> basic; | 38 scoped_ptr<HttpAuthHandler> basic = |
39 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( | 39 factory.CreateAuthHandlerForScheme("basic"); |
40 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic)); | 40 ASSERT_TRUE(basic); |
| 41 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
| 42 EXPECT_EQ(OK, basic->HandleInitialChallenge( |
| 43 tokenizer, HttpAuth::AUTH_SERVER, origin, BoundNetLog())); |
41 AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username), | 44 AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username), |
42 base::ASCIIToUTF16(tests[i].password)); | 45 base::ASCIIToUTF16(tests[i].password)); |
43 HttpRequestInfo request_info; | 46 HttpRequestInfo request_info; |
44 std::string auth_token; | 47 std::string auth_token; |
45 TestCompletionCallback callback; | 48 TestCompletionCallback callback; |
46 int rv = basic->GenerateAuthToken(&credentials, request_info, | 49 int rv = basic->GenerateAuthToken(&credentials, request_info, |
47 callback.callback(), &auth_token); | 50 callback.callback(), &auth_token); |
48 EXPECT_EQ(OK, rv); | 51 EXPECT_EQ(OK, rv); |
49 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str()); | 52 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str()); |
50 } | 53 } |
(...skipping 29 matching lines...) Expand all Loading... |
80 | 83 |
81 // And this one should be treated as if it was for "Second." | 84 // And this one should be treated as if it was for "Second." |
82 { | 85 { |
83 "basic realm=\"First\",realm=\"Second\"", | 86 "basic realm=\"First\",realm=\"Second\"", |
84 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM | 87 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM |
85 } | 88 } |
86 }; | 89 }; |
87 | 90 |
88 GURL origin("http://www.example.com"); | 91 GURL origin("http://www.example.com"); |
89 HttpAuthHandlerBasic::Factory factory; | 92 HttpAuthHandlerBasic::Factory factory; |
90 scoped_ptr<HttpAuthHandler> basic; | 93 scoped_ptr<HttpAuthHandler> basic = |
91 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( | 94 factory.CreateAuthHandlerForScheme("basic"); |
92 tests[0].challenge, HttpAuth::AUTH_SERVER, origin, | 95 std::string initial_challenge(tests[0].challenge); |
93 BoundNetLog(), &basic)); | 96 HttpAuthChallengeTokenizer tokenizer(initial_challenge.begin(), |
| 97 initial_challenge.end()); |
| 98 EXPECT_EQ(OK, basic->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, |
| 99 origin, BoundNetLog())); |
94 | 100 |
95 for (size_t i = 0; i < arraysize(tests); ++i) { | 101 for (size_t i = 0; i < arraysize(tests); ++i) { |
96 std::string challenge(tests[i].challenge); | 102 std::string challenge(tests[i].challenge); |
97 HttpAuthChallengeTokenizer tok(challenge.begin(), | 103 HttpAuthChallengeTokenizer tok(challenge.begin(), |
98 challenge.end()); | 104 challenge.end()); |
99 EXPECT_EQ(tests[i].expected_rv, basic->HandleAnotherChallenge(tok)); | 105 EXPECT_EQ(tests[i].expected_rv, basic->HandleAnotherChallenge(tok)); |
100 } | 106 } |
101 } | 107 } |
102 | 108 |
103 TEST(HttpAuthHandlerBasicTest, HandleInitialChallenge) { | 109 TEST(HttpAuthHandlerBasicTest, HandleInitialChallenge) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 }, | 159 }, |
154 | 160 |
155 // The parser skips over tokens which aren't known. | 161 // The parser skips over tokens which aren't known. |
156 { | 162 { |
157 "Basic unknown_token=foobar realm=\"test_realm\"", | 163 "Basic unknown_token=foobar realm=\"test_realm\"", |
158 OK, | 164 OK, |
159 "test_realm", | 165 "test_realm", |
160 }, | 166 }, |
161 #endif | 167 #endif |
162 | 168 |
163 // The parser fails when the first token is not "Basic". | |
164 { | |
165 "Negotiate", | |
166 ERR_INVALID_RESPONSE, | |
167 "" | |
168 }, | |
169 | |
170 // Although RFC 2617 isn't explicit about this case, if there is | 169 // Although RFC 2617 isn't explicit about this case, if there is |
171 // more than one realm directive, we pick the last one. | 170 // more than one realm directive, we pick the last one. |
172 { | 171 { |
173 "Basic realm=\"foo\",realm=\"bar\"", | 172 "Basic realm=\"foo\",realm=\"bar\"", |
174 OK, | 173 OK, |
175 "bar", | 174 "bar", |
176 }, | 175 }, |
177 | 176 |
178 // Handle ISO-8859-1 character as part of the realm. The realm is converted | 177 // Handle ISO-8859-1 character as part of the realm. The realm is converted |
179 // to UTF-8. | 178 // to UTF-8. |
180 { | 179 { |
181 "Basic realm=\"foo-\xE5\"", | 180 "Basic realm=\"foo-\xE5\"", |
182 OK, | 181 OK, |
183 "foo-\xC3\xA5", | 182 "foo-\xC3\xA5", |
184 }, | 183 }, |
185 }; | 184 }; |
186 HttpAuthHandlerBasic::Factory factory; | 185 HttpAuthHandlerBasic::Factory factory; |
187 GURL origin("http://www.example.com"); | 186 GURL origin("http://www.example.com"); |
188 for (size_t i = 0; i < arraysize(tests); ++i) { | 187 for (size_t i = 0; i < arraysize(tests); ++i) { |
189 std::string challenge = tests[i].challenge; | 188 std::string challenge = tests[i].challenge; |
190 scoped_ptr<HttpAuthHandler> basic; | 189 scoped_ptr<HttpAuthHandler> basic = |
191 int rv = factory.CreateAuthHandlerFromString( | 190 factory.CreateAuthHandlerForScheme("basic"); |
192 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic); | 191 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
| 192 int rv = basic->HandleInitialChallenge(tokenizer, HttpAuth::AUTH_SERVER, |
| 193 origin, BoundNetLog()); |
193 EXPECT_EQ(tests[i].expected_rv, rv); | 194 EXPECT_EQ(tests[i].expected_rv, rv); |
194 if (rv == OK) | 195 if (rv == OK) |
195 EXPECT_EQ(tests[i].expected_realm, basic->realm()); | 196 EXPECT_EQ(tests[i].expected_realm, basic->realm()); |
196 } | 197 } |
197 } | 198 } |
198 | 199 |
| 200 TEST(HttpAuthHandlerBasicTest, CreateAuthHandlerForScheme) { |
| 201 HttpAuthHandlerBasic::Factory basic_factory; |
| 202 |
| 203 EXPECT_TRUE(basic_factory.CreateAuthHandlerForScheme("basic")); |
| 204 EXPECT_FALSE(basic_factory.CreateAuthHandlerForScheme("bogus")); |
| 205 } |
| 206 |
| 207 TEST(HttpAuthHandlerBasicTest, CreateAndInitPreemptiveAuthHandler_Valid) { |
| 208 HttpAuthHandlerBasic::Factory digest_factory; |
| 209 HttpAuthCache auth_cache; |
| 210 std::string challenge("basic realm=\"Foo\""); |
| 211 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
| 212 |
| 213 HttpAuthCache::Entry* entry = |
| 214 auth_cache.Add(GURL("http://example.com/foo").GetOrigin(), "foo", "basic", |
| 215 challenge, AuthCredentials(), "/foo"); |
| 216 EXPECT_TRUE(digest_factory.CreateAndInitPreemptiveAuthHandler( |
| 217 entry, tokenizer, HttpAuth::AUTH_SERVER, BoundNetLog())); |
| 218 } |
| 219 |
| 220 TEST(HttpAuthHandlerBasicTest, CreateAndInitPreemptiveAuthHandler_Invalid) { |
| 221 HttpAuthHandlerBasic::Factory digest_factory; |
| 222 HttpAuthCache auth_cache; |
| 223 std::string challenge("digest realm=\"foo\""); |
| 224 HttpAuthChallengeTokenizer tokenizer(challenge.begin(), challenge.end()); |
| 225 |
| 226 HttpAuthCache::Entry* entry = |
| 227 auth_cache.Add(GURL("http://example.com").GetOrigin(), "bar", "digest", |
| 228 challenge, AuthCredentials(), "/bar"); |
| 229 EXPECT_FALSE(digest_factory.CreateAndInitPreemptiveAuthHandler( |
| 230 entry, tokenizer, HttpAuth::AUTH_SERVER, BoundNetLog())); |
| 231 } |
| 232 |
199 } // namespace net | 233 } // namespace net |
OLD | NEW |