| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <string> | |
| 6 | |
| 7 #include "base/basictypes.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/strings/string_util.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "net/base/net_errors.h" | |
| 12 #include "net/base/test_completion_callback.h" | |
| 13 #include "net/http/http_auth_challenge_tokenizer.h" | |
| 14 #include "net/http/http_auth_handler_basic.h" | |
| 15 #include "net/http/http_request_info.h" | |
| 16 #include "testing/gtest/include/gtest/gtest.h" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 TEST(HttpAuthHandlerBasicTest, GenerateAuthToken) { | |
| 21 static const struct { | |
| 22 const char* username; | |
| 23 const char* password; | |
| 24 const char* expected_credentials; | |
| 25 } tests[] = { | |
| 26 { "foo", "bar", "Basic Zm9vOmJhcg==" }, | |
| 27 // Empty username | |
| 28 { "", "foobar", "Basic OmZvb2Jhcg==" }, | |
| 29 // Empty password | |
| 30 { "anon", "", "Basic YW5vbjo=" }, | |
| 31 // Empty username and empty password. | |
| 32 { "", "", "Basic Og==" }, | |
| 33 }; | |
| 34 GURL origin("http://www.example.com"); | |
| 35 HttpAuthHandlerBasic::Factory factory; | |
| 36 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 37 std::string challenge = "Basic realm=\"Atlantis\""; | |
| 38 scoped_ptr<HttpAuthHandler> basic; | |
| 39 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( | |
| 40 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic)); | |
| 41 AuthCredentials credentials(base::ASCIIToUTF16(tests[i].username), | |
| 42 base::ASCIIToUTF16(tests[i].password)); | |
| 43 HttpRequestInfo request_info; | |
| 44 std::string auth_token; | |
| 45 TestCompletionCallback callback; | |
| 46 int rv = basic->GenerateAuthToken(&credentials, &request_info, | |
| 47 callback.callback(), &auth_token); | |
| 48 EXPECT_EQ(OK, rv); | |
| 49 EXPECT_STREQ(tests[i].expected_credentials, auth_token.c_str()); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 TEST(HttpAuthHandlerBasicTest, HandleAnotherChallenge) { | |
| 54 static const struct { | |
| 55 const char* challenge; | |
| 56 HttpAuth::AuthorizationResult expected_rv; | |
| 57 } tests[] = { | |
| 58 // The handler is initialized using this challenge. The first | |
| 59 // time HandleAnotherChallenge is called with it should cause it | |
| 60 // to treat the second challenge as a rejection since it is for | |
| 61 // the same realm. | |
| 62 { | |
| 63 "Basic realm=\"First\"", | |
| 64 HttpAuth::AUTHORIZATION_RESULT_REJECT | |
| 65 }, | |
| 66 | |
| 67 // A challenge for a different realm. | |
| 68 { | |
| 69 "Basic realm=\"Second\"", | |
| 70 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM | |
| 71 }, | |
| 72 | |
| 73 // Although RFC 2617 isn't explicit about this case, if there is | |
| 74 // more than one realm directive, we pick the last one. So this | |
| 75 // challenge should be treated as being for "First" realm. | |
| 76 { | |
| 77 "Basic realm=\"Second\",realm=\"First\"", | |
| 78 HttpAuth::AUTHORIZATION_RESULT_REJECT | |
| 79 }, | |
| 80 | |
| 81 // And this one should be treated as if it was for "Second." | |
| 82 { | |
| 83 "basic realm=\"First\",realm=\"Second\"", | |
| 84 HttpAuth::AUTHORIZATION_RESULT_DIFFERENT_REALM | |
| 85 } | |
| 86 }; | |
| 87 | |
| 88 GURL origin("http://www.example.com"); | |
| 89 HttpAuthHandlerBasic::Factory factory; | |
| 90 scoped_ptr<HttpAuthHandler> basic; | |
| 91 EXPECT_EQ(OK, factory.CreateAuthHandlerFromString( | |
| 92 tests[0].challenge, HttpAuth::AUTH_SERVER, origin, | |
| 93 BoundNetLog(), &basic)); | |
| 94 | |
| 95 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 96 std::string challenge(tests[i].challenge); | |
| 97 HttpAuthChallengeTokenizer tok(challenge.begin(), | |
| 98 challenge.end()); | |
| 99 EXPECT_EQ(tests[i].expected_rv, basic->HandleAnotherChallenge(&tok)); | |
| 100 } | |
| 101 } | |
| 102 | |
| 103 TEST(HttpAuthHandlerBasicTest, InitFromChallenge) { | |
| 104 static const struct { | |
| 105 const char* challenge; | |
| 106 int expected_rv; | |
| 107 const char* expected_realm; | |
| 108 } tests[] = { | |
| 109 // No realm (we allow this even though realm is supposed to be required | |
| 110 // according to RFC 2617.) | |
| 111 { | |
| 112 "Basic", | |
| 113 OK, | |
| 114 "", | |
| 115 }, | |
| 116 | |
| 117 // Realm is empty string. | |
| 118 { | |
| 119 "Basic realm=\"\"", | |
| 120 OK, | |
| 121 "", | |
| 122 }, | |
| 123 | |
| 124 // Realm is valid. | |
| 125 { | |
| 126 "Basic realm=\"test_realm\"", | |
| 127 OK, | |
| 128 "test_realm", | |
| 129 }, | |
| 130 | |
| 131 // The parser ignores tokens which aren't known. | |
| 132 { | |
| 133 "Basic realm=\"test_realm\",unknown_token=foobar", | |
| 134 OK, | |
| 135 "test_realm", | |
| 136 }, | |
| 137 | |
| 138 // The parser skips over tokens which aren't known. | |
| 139 { | |
| 140 "Basic unknown_token=foobar,realm=\"test_realm\"", | |
| 141 OK, | |
| 142 "test_realm", | |
| 143 }, | |
| 144 | |
| 145 #if 0 | |
| 146 // TODO(cbentzel): It's unclear what the parser should do in these cases. | |
| 147 // It seems like this should either be treated as invalid, | |
| 148 // or the spaces should be used as a separator. | |
| 149 { | |
| 150 "Basic realm=\"test_realm\" unknown_token=foobar", | |
| 151 OK, | |
| 152 "test_realm", | |
| 153 }, | |
| 154 | |
| 155 // The parser skips over tokens which aren't known. | |
| 156 { | |
| 157 "Basic unknown_token=foobar realm=\"test_realm\"", | |
| 158 OK, | |
| 159 "test_realm", | |
| 160 }, | |
| 161 #endif | |
| 162 | |
| 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 | |
| 171 // more than one realm directive, we pick the last one. | |
| 172 { | |
| 173 "Basic realm=\"foo\",realm=\"bar\"", | |
| 174 OK, | |
| 175 "bar", | |
| 176 }, | |
| 177 | |
| 178 // Handle ISO-8859-1 character as part of the realm. The realm is converted | |
| 179 // to UTF-8. | |
| 180 { | |
| 181 "Basic realm=\"foo-\xE5\"", | |
| 182 OK, | |
| 183 "foo-\xC3\xA5", | |
| 184 }, | |
| 185 }; | |
| 186 HttpAuthHandlerBasic::Factory factory; | |
| 187 GURL origin("http://www.example.com"); | |
| 188 for (size_t i = 0; i < arraysize(tests); ++i) { | |
| 189 std::string challenge = tests[i].challenge; | |
| 190 scoped_ptr<HttpAuthHandler> basic; | |
| 191 int rv = factory.CreateAuthHandlerFromString( | |
| 192 challenge, HttpAuth::AUTH_SERVER, origin, BoundNetLog(), &basic); | |
| 193 EXPECT_EQ(tests[i].expected_rv, rv); | |
| 194 if (rv == OK) | |
| 195 EXPECT_EQ(tests[i].expected_realm, basic->realm()); | |
| 196 } | |
| 197 } | |
| 198 | |
| 199 } // namespace net | |
| OLD | NEW |