| 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/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
| 8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "net/base/net_errors.h" | 10 #include "net/base/net_errors.h" |
| 11 #include "net/base/test_completion_callback.h" | 11 #include "net/base/test_completion_callback.h" |
| 12 #include "net/http/http_auth_challenge_tokenizer.h" | 12 #include "net/http/http_auth_challenge_tokenizer.h" |
| 13 #include "net/http/http_auth_handler_basic.h" | 13 #include "net/http/http_auth_handler_basic.h" |
| 14 #include "net/http/http_request_info.h" | 14 #include "net/http/http_request_info.h" |
| 15 #include "net/http/http_response_info.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 | 17 |
| 17 namespace net { | 18 namespace net { |
| 18 | 19 |
| 19 TEST(HttpAuthHandlerBasicTest, GenerateAuthToken) { | 20 TEST(HttpAuthHandlerBasicTest, GenerateAuthToken) { |
| 20 static const struct { | 21 static const struct { |
| 21 const char* username; | 22 const char* username; |
| 22 const char* password; | 23 const char* password; |
| 23 const char* expected_credentials; | 24 const char* expected_credentials; |
| 24 } tests[] = { | 25 } tests[] = { |
| 25 { "foo", "bar", "Basic Zm9vOmJhcg==" }, | 26 { "foo", "bar", "Basic Zm9vOmJhcg==" }, |
| 26 // Empty username | 27 // Empty username |
| 27 { "", "foobar", "Basic OmZvb2Jhcg==" }, | 28 { "", "foobar", "Basic OmZvb2Jhcg==" }, |
| 28 // Empty password | 29 // Empty password |
| 29 { "anon", "", "Basic YW5vbjo=" }, | 30 { "anon", "", "Basic YW5vbjo=" }, |
| 30 // Empty username and empty password. | 31 // Empty username and empty password. |
| 31 { "", "", "Basic Og==" }, | 32 { "", "", "Basic Og==" }, |
| 32 }; | 33 }; |
| 33 GURL origin("http://www.example.com"); | 34 GURL origin("http://www.example.com"); |
| 34 HttpAuthHandlerBasic::Factory factory; | 35 HttpAuthHandlerBasic::Factory factory; |
| 35 for (size_t i = 0; i < arraysize(tests); ++i) { | 36 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 36 std::string challenge = "Basic realm=\"Atlantis\""; | 37 std::string challenge = "Basic realm=\"Atlantis\""; |
| 38 HttpResponseInfo null_response_info; |
| 37 scoped_ptr<HttpAuthHandler> basic; | 39 scoped_ptr<HttpAuthHandler> basic; |
| 38 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( | 40 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( |
| 39 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic)); | 41 challenge, HttpAuth::AUTH_SERVER, null_response_info, |
| 42 origin, BoundNetLog(), &basic)); |
| 40 AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username), | 43 AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username), |
| 41 base::ASCIIToUTF16(tests[i].password)); | 44 base::ASCIIToUTF16(tests[i].password)); |
| 42 HttpRequestInfo request_info; | 45 HttpRequestInfo request_info; |
| 43 std::string auth_token; | 46 std::string auth_token; |
| 44 TestCompletionCallback callback; | 47 TestCompletionCallback callback; |
| 45 int rv = basic->GenerateAuthToken(&credentials, &request_info, | 48 int rv = basic->GenerateAuthToken(&credentials, &request_info, |
| 46 callback.callback(), &auth_token); | 49 callback.callback(), &auth_token); |
| 47 EXPECT_EQ(OK, rv); | 50 EXPECT_EQ(OK, rv); |
| 48 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str()); | 51 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str()); |
| 49 } | 52 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 79 | 82 |
| 80 // And this one should be treated as if it was for "Second." | 83 // And this one should be treated as if it was for "Second." |
| 81 { | 84 { |
| 82 "basic realm=\"First\",realm=\"Second\"", | 85 "basic realm=\"First\",realm=\"Second\"", |
| 83 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM | 86 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM |
| 84 } | 87 } |
| 85 }; | 88 }; |
| 86 | 89 |
| 87 GURL origin("http://www.example.com"); | 90 GURL origin("http://www.example.com"); |
| 88 HttpAuthHandlerBasic::Factory factory; | 91 HttpAuthHandlerBasic::Factory factory; |
| 92 HttpResponseInfo null_response_info; |
| 89 scoped_ptr<HttpAuthHandler> basic; | 93 scoped_ptr<HttpAuthHandler> basic; |
| 90 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( | 94 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( |
| 91 tests[0].challenge, HttpAuth::AUTH_SERVER, origin, | 95 tests[0].challenge, HttpAuth::AUTH_SERVER, |
| 92 BoundNetLog(), &basic)); | 96 null_response_info, origin, BoundNetLog(), &basic)); |
| 93 | 97 |
| 94 for (size_t i = 0; i < arraysize(tests); ++i) { | 98 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 95 std::string challenge(tests[i].challenge); | 99 std::string challenge(tests[i].challenge); |
| 96 HttpAuthChallengeTokenizer tok(challenge.begin(), | 100 HttpAuthChallengeTokenizer tok(challenge.begin(), |
| 97 challenge.end()); | 101 challenge.end()); |
| 98 EXPECT_EQ(tests[i].expected_rv, basic->HandleAnotherChallenge(&tok)); | 102 EXPECT_EQ(tests[i].expected_rv, basic->HandleAnotherChallenge(&tok)); |
| 99 } | 103 } |
| 100 } | 104 } |
| 101 | 105 |
| 102 TEST(HttpAuthHandlerBasicTest, InitFromChallenge) { | 106 TEST(HttpAuthHandlerBasicTest, InitFromChallenge) { |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 { | 183 { |
| 180 "Basic realm=\"foo-\xE5\"", | 184 "Basic realm=\"foo-\xE5\"", |
| 181 OK, | 185 OK, |
| 182 "foo-\xC3\xA5", | 186 "foo-\xC3\xA5", |
| 183 }, | 187 }, |
| 184 }; | 188 }; |
| 185 HttpAuthHandlerBasic::Factory factory; | 189 HttpAuthHandlerBasic::Factory factory; |
| 186 GURL origin("http://www.example.com"); | 190 GURL origin("http://www.example.com"); |
| 187 for (size_t i = 0; i < arraysize(tests); ++i) { | 191 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 188 std::string challenge = tests[i].challenge; | 192 std::string challenge = tests[i].challenge; |
| 193 HttpResponseInfo null_response_info; |
| 189 scoped_ptr<HttpAuthHandler> basic; | 194 scoped_ptr<HttpAuthHandler> basic; |
| 190 int rv = factory.CreateAuthHandlerFromString( | 195 int rv = factory.CreateAuthHandlerFromString( |
| 191 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic); | 196 challenge, HttpAuth::AUTH_SERVER, null_response_info, origin, |
| 197 BoundNetLog(), &basic); |
| 192 EXPECT_EQ(tests[i].expected_rv, rv); | 198 EXPECT_EQ(tests[i].expected_rv, rv); |
| 193 if (rv == OK) | 199 if (rv == OK) |
| 194 EXPECT_EQ(tests[i].expected_realm, basic->realm()); | 200 EXPECT_EQ(tests[i].expected_realm, basic->realm()); |
| 195 } | 201 } |
| 196 } | 202 } |
| 197 | 203 |
| 198 } // namespace net | 204 } // namespace net |
| OLD | NEW |