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 "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "url/origin.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 url::Origin 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 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( | 40 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( |
40 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic)); | 41 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic)); |
41 AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username), | 42 AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username), |
42 base::ASCIIToUTF16(tests[i].password)); | 43 base::ASCIIToUTF16(tests[i].password)); |
43 HttpRequestInfo request_info; | 44 HttpRequestInfo request_info; |
44 std::string auth_token; | 45 std::string auth_token; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 HttpAuth::AUTHORIZATION_RESULT_REJECT | 79 HttpAuth::AUTHORIZATION_RESULT_REJECT |
79 }, | 80 }, |
80 | 81 |
81 // And this one should be treated as if it was for "Second." | 82 // And this one should be treated as if it was for "Second." |
82 { | 83 { |
83 "basic realm=\"First\",realm=\"Second\"", | 84 "basic realm=\"First\",realm=\"Second\"", |
84 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM | 85 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM |
85 } | 86 } |
86 }; | 87 }; |
87 | 88 |
88 GURL origin("http://www.example.com"); | 89 url::Origin origin("http://www.example.com"); |
89 HttpAuthHandlerBasic::Factory factory; | 90 HttpAuthHandlerBasic::Factory factory; |
90 scoped_ptr<HttpAuthHandler> basic; | 91 scoped_ptr<HttpAuthHandler> basic; |
91 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( | 92 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( |
92 tests[0].challenge, HttpAuth::AUTH_SERVER, origin, | 93 tests[0].challenge, HttpAuth::AUTH_SERVER, origin, |
93 BoundNetLog(), &basic)); | 94 BoundNetLog(), &basic)); |
94 | 95 |
95 for (size_t i = 0; i < arraysize(tests); ++i) { | 96 for (size_t i = 0; i < arraysize(tests); ++i) { |
96 std::string challenge(tests[i].challenge); | 97 std::string challenge(tests[i].challenge); |
97 HttpAuthChallengeTokenizer tok(challenge.begin(), | 98 HttpAuthChallengeTokenizer tok(challenge.begin(), |
98 challenge.end()); | 99 challenge.end()); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 | 178 |
178 // Handle ISO-8859-1 character as part of the realm. The realm is converted | 179 // Handle ISO-8859-1 character as part of the realm. The realm is converted |
179 // to UTF-8. | 180 // to UTF-8. |
180 { | 181 { |
181 "Basic realm=\"foo-\xE5\"", | 182 "Basic realm=\"foo-\xE5\"", |
182 OK, | 183 OK, |
183 "foo-\xC3\xA5", | 184 "foo-\xC3\xA5", |
184 }, | 185 }, |
185 }; | 186 }; |
186 HttpAuthHandlerBasic::Factory factory; | 187 HttpAuthHandlerBasic::Factory factory; |
187 GURL origin("http://www.example.com"); | 188 url::Origin origin("http://www.example.com"); |
188 for (size_t i = 0; i < arraysize(tests); ++i) { | 189 for (size_t i = 0; i < arraysize(tests); ++i) { |
189 std::string challenge = tests[i].challenge; | 190 std::string challenge = tests[i].challenge; |
190 scoped_ptr<HttpAuthHandler> basic; | 191 scoped_ptr<HttpAuthHandler> basic; |
191 int rv = factory.CreateAuthHandlerFromString( | 192 int rv = factory.CreateAuthHandlerFromString( |
192 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic); | 193 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic); |
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 |
199 } // namespace net | 200 } // namespace net |
OLD | NEW |