| 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/string16.h" | 7 #include "base/string16.h" |
| 8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
| 9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "net/http/http_auth_cache.h" | 12 #include "net/http/http_auth_cache.h" |
| 13 #include "net/http/http_auth_handler.h" | 13 #include "net/http/http_auth_handler.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 15 |
| 16 namespace net { | 16 namespace net { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 class MockAuthHandler : public HttpAuthHandler { | 20 class MockAuthHandler : public HttpAuthHandler { |
| 21 public: | 21 public: |
| 22 MockAuthHandler(HttpAuth::Scheme scheme, | 22 MockAuthHandler(const std::string& scheme, |
| 23 const std::string& realm, | 23 const std::string& realm, |
| 24 HttpAuth::Target target) { | 24 HttpAuth::Target target) { |
| 25 // Can't use initializer list since these are members of the base class. | 25 // Can't use initializer list since these are members of the base class. |
| 26 auth_scheme_ = scheme; | 26 auth_scheme_ = scheme; |
| 27 realm_ = realm; | 27 realm_ = realm; |
| 28 score_ = 1; | 28 score_ = 1; |
| 29 target_ = target; | 29 target_ = target; |
| 30 properties_ = 0; | 30 properties_ = 0; |
| 31 } | 31 } |
| 32 | 32 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 // Test adding and looking-up cache entries (both by realm and by path). | 78 // Test adding and looking-up cache entries (both by realm and by path). |
| 79 TEST(HttpAuthCacheTest, Basic) { | 79 TEST(HttpAuthCacheTest, Basic) { |
| 80 GURL origin("http://www.google.com"); | 80 GURL origin("http://www.google.com"); |
| 81 HttpAuthCache cache; | 81 HttpAuthCache cache; |
| 82 HttpAuthCache::Entry* entry; | 82 HttpAuthCache::Entry* entry; |
| 83 | 83 |
| 84 // Add cache entries for 4 realms: "Realm1", "Realm2", "Realm3" and | 84 // Add cache entries for 4 realms: "Realm1", "Realm2", "Realm3" and |
| 85 // "Realm4" | 85 // "Realm4" |
| 86 | 86 |
| 87 scoped_ptr<HttpAuthHandler> realm1_handler( | 87 scoped_ptr<HttpAuthHandler> realm1_handler( |
| 88 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, | 88 new MockAuthHandler("basic", |
| 89 kRealm1, | 89 kRealm1, |
| 90 HttpAuth::AUTH_SERVER)); | 90 HttpAuth::AUTH_SERVER)); |
| 91 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), | 91 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), |
| 92 "Basic realm=Realm1", | 92 "Basic realm=Realm1", |
| 93 CreateASCIICredentials("realm1-user", "realm1-password"), | 93 CreateASCIICredentials("realm1-user", "realm1-password"), |
| 94 "/foo/bar/index.html"); | 94 "/foo/bar/index.html"); |
| 95 | 95 |
| 96 scoped_ptr<HttpAuthHandler> realm2_handler( | 96 scoped_ptr<HttpAuthHandler> realm2_handler( |
| 97 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, | 97 new MockAuthHandler("basic", |
| 98 kRealm2, | 98 kRealm2, |
| 99 HttpAuth::AUTH_SERVER)); | 99 HttpAuth::AUTH_SERVER)); |
| 100 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), | 100 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), |
| 101 "Basic realm=Realm2", | 101 "Basic realm=Realm2", |
| 102 CreateASCIICredentials("realm2-user", "realm2-password"), | 102 CreateASCIICredentials("realm2-user", "realm2-password"), |
| 103 "/foo2/index.html"); | 103 "/foo2/index.html"); |
| 104 | 104 |
| 105 scoped_ptr<HttpAuthHandler> realm3_basic_handler( | 105 scoped_ptr<HttpAuthHandler> realm3_basic_handler( |
| 106 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, | 106 new MockAuthHandler("basic", |
| 107 kRealm3, | 107 kRealm3, |
| 108 HttpAuth::AUTH_PROXY)); | 108 HttpAuth::AUTH_PROXY)); |
| 109 cache.Add(origin, realm3_basic_handler->realm(), | 109 cache.Add(origin, realm3_basic_handler->realm(), |
| 110 realm3_basic_handler->auth_scheme(), "Basic realm=Realm3", | 110 realm3_basic_handler->auth_scheme(), "Basic realm=Realm3", |
| 111 CreateASCIICredentials("realm3-basic-user", | 111 CreateASCIICredentials("realm3-basic-user", |
| 112 "realm3-basic-password"), | 112 "realm3-basic-password"), |
| 113 ""); | 113 ""); |
| 114 | 114 |
| 115 scoped_ptr<HttpAuthHandler> realm3_digest_handler( | 115 scoped_ptr<HttpAuthHandler> realm3_digest_handler( |
| 116 new MockAuthHandler(HttpAuth::AUTH_SCHEME_DIGEST, | 116 new MockAuthHandler("digest", |
| 117 kRealm3, | 117 kRealm3, |
| 118 HttpAuth::AUTH_PROXY)); | 118 HttpAuth::AUTH_PROXY)); |
| 119 cache.Add(origin, realm3_digest_handler->realm(), | 119 cache.Add(origin, realm3_digest_handler->realm(), |
| 120 realm3_digest_handler->auth_scheme(), "Digest realm=Realm3", | 120 realm3_digest_handler->auth_scheme(), "Digest realm=Realm3", |
| 121 CreateASCIICredentials("realm3-digest-user", | 121 CreateASCIICredentials("realm3-digest-user", |
| 122 "realm3-digest-password"), | 122 "realm3-digest-password"), |
| 123 "/baz/index.html"); | 123 "/baz/index.html"); |
| 124 | 124 |
| 125 scoped_ptr<HttpAuthHandler> realm4_basic_handler( | 125 scoped_ptr<HttpAuthHandler> realm4_basic_handler( |
| 126 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, | 126 new MockAuthHandler("basic", |
| 127 kRealm4, | 127 kRealm4, |
| 128 HttpAuth::AUTH_SERVER)); | 128 HttpAuth::AUTH_SERVER)); |
| 129 cache.Add(origin, realm4_basic_handler->realm(), | 129 cache.Add(origin, realm4_basic_handler->realm(), |
| 130 realm4_basic_handler->auth_scheme(), "Basic realm=Realm4", | 130 realm4_basic_handler->auth_scheme(), "Basic realm=Realm4", |
| 131 CreateASCIICredentials("realm4-basic-user", | 131 CreateASCIICredentials("realm4-basic-user", |
| 132 "realm4-basic-password"), | 132 "realm4-basic-password"), |
| 133 "/"); | 133 "/"); |
| 134 | 134 |
| 135 // There is no Realm5 | 135 // There is no Realm5 |
| 136 entry = cache.Lookup(origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC); | 136 entry = cache.Lookup(origin, kRealm5, "basic"); |
| 137 EXPECT_TRUE(NULL == entry); | 137 EXPECT_TRUE(NULL == entry); |
| 138 | 138 |
| 139 // While Realm3 does exist, the origin scheme is wrong. | 139 // While Realm3 does exist, the origin scheme is wrong. |
| 140 entry = cache.Lookup(GURL("https://www.google.com"), kRealm3, | 140 entry = cache.Lookup(GURL("https://www.google.com"), kRealm3, |
| 141 HttpAuth::AUTH_SCHEME_BASIC); | 141 "basic"); |
| 142 EXPECT_TRUE(NULL == entry); | 142 EXPECT_TRUE(NULL == entry); |
| 143 | 143 |
| 144 // Realm, origin scheme ok, authentication scheme wrong | 144 // Realm, origin scheme ok, authentication scheme wrong |
| 145 entry = cache.Lookup | 145 entry = cache.Lookup |
| 146 (GURL("http://www.google.com"), kRealm1, HttpAuth::AUTH_SCHEME_DIGEST); | 146 (GURL("http://www.google.com"), kRealm1, "digest"); |
| 147 EXPECT_TRUE(NULL == entry); | 147 EXPECT_TRUE(NULL == entry); |
| 148 | 148 |
| 149 // Valid lookup by origin, realm, scheme. | 149 // Valid lookup by origin, realm, scheme. |
| 150 entry = cache.Lookup( | 150 entry = cache.Lookup( |
| 151 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_BASIC); | 151 GURL("http://www.google.com:80"), kRealm3, "basic"); |
| 152 ASSERT_FALSE(NULL == entry); | 152 ASSERT_FALSE(NULL == entry); |
| 153 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); | 153 EXPECT_EQ("basic", entry->scheme()); |
| 154 EXPECT_EQ(kRealm3, entry->realm()); | 154 EXPECT_EQ(kRealm3, entry->realm()); |
| 155 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge()); | 155 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge()); |
| 156 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->credentials().username()); | 156 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->credentials().username()); |
| 157 EXPECT_EQ(ASCIIToUTF16("realm3-basic-password"), | 157 EXPECT_EQ(ASCIIToUTF16("realm3-basic-password"), |
| 158 entry->credentials().password()); | 158 entry->credentials().password()); |
| 159 | 159 |
| 160 // Valid lookup by origin, realm, scheme when there's a duplicate | 160 // Valid lookup by origin, realm, scheme when there's a duplicate |
| 161 // origin, realm in the cache | 161 // origin, realm in the cache |
| 162 entry = cache.Lookup( | 162 entry = cache.Lookup( |
| 163 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); | 163 GURL("http://www.google.com:80"), kRealm3, "digest"); |
| 164 ASSERT_FALSE(NULL == entry); | 164 ASSERT_FALSE(NULL == entry); |
| 165 EXPECT_EQ(HttpAuth::AUTH_SCHEME_DIGEST, entry->scheme()); | 165 EXPECT_EQ("digest", entry->scheme()); |
| 166 EXPECT_EQ(kRealm3, entry->realm()); | 166 EXPECT_EQ(kRealm3, entry->realm()); |
| 167 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge()); | 167 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge()); |
| 168 EXPECT_EQ(ASCIIToUTF16("realm3-digest-user"), | 168 EXPECT_EQ(ASCIIToUTF16("realm3-digest-user"), |
| 169 entry->credentials().username()); | 169 entry->credentials().username()); |
| 170 EXPECT_EQ(ASCIIToUTF16("realm3-digest-password"), | 170 EXPECT_EQ(ASCIIToUTF16("realm3-digest-password"), |
| 171 entry->credentials().password()); | 171 entry->credentials().password()); |
| 172 | 172 |
| 173 // Valid lookup by realm. | 173 // Valid lookup by realm. |
| 174 entry = cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); | 174 entry = cache.Lookup(origin, kRealm2, "basic"); |
| 175 ASSERT_FALSE(NULL == entry); | 175 ASSERT_FALSE(NULL == entry); |
| 176 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); | 176 EXPECT_EQ("basic", entry->scheme()); |
| 177 EXPECT_EQ(kRealm2, entry->realm()); | 177 EXPECT_EQ(kRealm2, entry->realm()); |
| 178 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge()); | 178 EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge()); |
| 179 EXPECT_EQ(ASCIIToUTF16("realm2-user"), entry->credentials().username()); | 179 EXPECT_EQ(ASCIIToUTF16("realm2-user"), entry->credentials().username()); |
| 180 EXPECT_EQ(ASCIIToUTF16("realm2-password"), entry->credentials().password()); | 180 EXPECT_EQ(ASCIIToUTF16("realm2-password"), entry->credentials().password()); |
| 181 | 181 |
| 182 // Check that subpaths are recognized. | 182 // Check that subpaths are recognized. |
| 183 HttpAuthCache::Entry* realm2_entry = cache.Lookup( | 183 HttpAuthCache::Entry* realm2_entry = cache.Lookup( |
| 184 origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); | 184 origin, kRealm2, "basic"); |
| 185 HttpAuthCache::Entry* realm4_entry = cache.Lookup( | 185 HttpAuthCache::Entry* realm4_entry = cache.Lookup( |
| 186 origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC); | 186 origin, kRealm4, "basic"); |
| 187 EXPECT_FALSE(NULL == realm2_entry); | 187 EXPECT_FALSE(NULL == realm2_entry); |
| 188 EXPECT_FALSE(NULL == realm4_entry); | 188 EXPECT_FALSE(NULL == realm4_entry); |
| 189 // Realm4 applies to '/' and Realm2 applies to '/foo2/'. | 189 // Realm4 applies to '/' and Realm2 applies to '/foo2/'. |
| 190 // LookupByPath() should return the closest enclosing path. | 190 // LookupByPath() should return the closest enclosing path. |
| 191 // Positive tests: | 191 // Positive tests: |
| 192 entry = cache.LookupByPath(origin, "/foo2/index.html"); | 192 entry = cache.LookupByPath(origin, "/foo2/index.html"); |
| 193 EXPECT_TRUE(realm2_entry == entry); | 193 EXPECT_TRUE(realm2_entry == entry); |
| 194 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); | 194 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); |
| 195 EXPECT_TRUE(realm2_entry == entry); | 195 EXPECT_TRUE(realm2_entry == entry); |
| 196 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); | 196 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); |
| 197 EXPECT_TRUE(realm2_entry == entry); | 197 EXPECT_TRUE(realm2_entry == entry); |
| 198 entry = cache.LookupByPath(origin, "/foo2/"); | 198 entry = cache.LookupByPath(origin, "/foo2/"); |
| 199 EXPECT_TRUE(realm2_entry == entry); | 199 EXPECT_TRUE(realm2_entry == entry); |
| 200 entry = cache.LookupByPath(origin, "/foo2"); | 200 entry = cache.LookupByPath(origin, "/foo2"); |
| 201 EXPECT_TRUE(realm4_entry == entry); | 201 EXPECT_TRUE(realm4_entry == entry); |
| 202 entry = cache.LookupByPath(origin, "/"); | 202 entry = cache.LookupByPath(origin, "/"); |
| 203 EXPECT_TRUE(realm4_entry == entry); | 203 EXPECT_TRUE(realm4_entry == entry); |
| 204 | 204 |
| 205 // Negative tests: | 205 // Negative tests: |
| 206 entry = cache.LookupByPath(origin, "/foo3/index.html"); | 206 entry = cache.LookupByPath(origin, "/foo3/index.html"); |
| 207 EXPECT_FALSE(realm2_entry == entry); | 207 EXPECT_FALSE(realm2_entry == entry); |
| 208 entry = cache.LookupByPath(origin, ""); | 208 entry = cache.LookupByPath(origin, ""); |
| 209 EXPECT_FALSE(realm2_entry == entry); | 209 EXPECT_FALSE(realm2_entry == entry); |
| 210 | 210 |
| 211 // Confirm we find the same realm, different auth scheme by path lookup | 211 // Confirm we find the same realm, different auth scheme by path lookup |
| 212 HttpAuthCache::Entry* realm3_digest_entry = | 212 HttpAuthCache::Entry* realm3_digest_entry = |
| 213 cache.Lookup(origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); | 213 cache.Lookup(origin, kRealm3, "digest"); |
| 214 EXPECT_FALSE(NULL == realm3_digest_entry); | 214 EXPECT_FALSE(NULL == realm3_digest_entry); |
| 215 entry = cache.LookupByPath(origin, "/baz/index.html"); | 215 entry = cache.LookupByPath(origin, "/baz/index.html"); |
| 216 EXPECT_TRUE(realm3_digest_entry == entry); | 216 EXPECT_TRUE(realm3_digest_entry == entry); |
| 217 entry = cache.LookupByPath(origin, "/baz/"); | 217 entry = cache.LookupByPath(origin, "/baz/"); |
| 218 EXPECT_TRUE(realm3_digest_entry == entry); | 218 EXPECT_TRUE(realm3_digest_entry == entry); |
| 219 entry = cache.LookupByPath(origin, "/baz"); | 219 entry = cache.LookupByPath(origin, "/baz"); |
| 220 EXPECT_FALSE(realm3_digest_entry == entry); | 220 EXPECT_FALSE(realm3_digest_entry == entry); |
| 221 | 221 |
| 222 // Confirm we find the same realm, different auth scheme by path lookup | 222 // Confirm we find the same realm, different auth scheme by path lookup |
| 223 HttpAuthCache::Entry* realm3DigestEntry = | 223 HttpAuthCache::Entry* realm3DigestEntry = |
| 224 cache.Lookup(origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); | 224 cache.Lookup(origin, kRealm3, "digest"); |
| 225 EXPECT_FALSE(NULL == realm3DigestEntry); | 225 EXPECT_FALSE(NULL == realm3DigestEntry); |
| 226 entry = cache.LookupByPath(origin, "/baz/index.html"); | 226 entry = cache.LookupByPath(origin, "/baz/index.html"); |
| 227 EXPECT_TRUE(realm3DigestEntry == entry); | 227 EXPECT_TRUE(realm3DigestEntry == entry); |
| 228 entry = cache.LookupByPath(origin, "/baz/"); | 228 entry = cache.LookupByPath(origin, "/baz/"); |
| 229 EXPECT_TRUE(realm3DigestEntry == entry); | 229 EXPECT_TRUE(realm3DigestEntry == entry); |
| 230 entry = cache.LookupByPath(origin, "/baz"); | 230 entry = cache.LookupByPath(origin, "/baz"); |
| 231 EXPECT_FALSE(realm3DigestEntry == entry); | 231 EXPECT_FALSE(realm3DigestEntry == entry); |
| 232 | 232 |
| 233 // Lookup using empty path (may be used for proxy). | 233 // Lookup using empty path (may be used for proxy). |
| 234 entry = cache.LookupByPath(origin, ""); | 234 entry = cache.LookupByPath(origin, ""); |
| 235 EXPECT_FALSE(NULL == entry); | 235 EXPECT_FALSE(NULL == entry); |
| 236 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); | 236 EXPECT_EQ("basic", entry->scheme()); |
| 237 EXPECT_EQ(kRealm3, entry->realm()); | 237 EXPECT_EQ(kRealm3, entry->realm()); |
| 238 } | 238 } |
| 239 | 239 |
| 240 TEST(HttpAuthCacheTest, AddPath) { | 240 TEST(HttpAuthCacheTest, AddPath) { |
| 241 HttpAuthCache::Entry entry; | 241 HttpAuthCache::Entry entry; |
| 242 | 242 |
| 243 // All of these paths have a common root /1/2/2/4/5/ | 243 // All of these paths have a common root /1/2/2/4/5/ |
| 244 entry.AddPath("/1/2/3/4/5/x.txt"); | 244 entry.AddPath("/1/2/3/4/5/x.txt"); |
| 245 entry.AddPath("/1/2/3/4/5/y.txt"); | 245 entry.AddPath("/1/2/3/4/5/y.txt"); |
| 246 entry.AddPath("/1/2/3/4/5/z.txt"); | 246 entry.AddPath("/1/2/3/4/5/z.txt"); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 271 | 271 |
| 272 // Calling Add when the realm entry already exists, should append that | 272 // Calling Add when the realm entry already exists, should append that |
| 273 // path. | 273 // path. |
| 274 TEST(HttpAuthCacheTest, AddToExistingEntry) { | 274 TEST(HttpAuthCacheTest, AddToExistingEntry) { |
| 275 HttpAuthCache cache; | 275 HttpAuthCache cache; |
| 276 GURL origin("http://www.foobar.com:70"); | 276 GURL origin("http://www.foobar.com:70"); |
| 277 const std::string auth_challenge = "Basic realm=MyRealm"; | 277 const std::string auth_challenge = "Basic realm=MyRealm"; |
| 278 | 278 |
| 279 scoped_ptr<HttpAuthHandler> handler( | 279 scoped_ptr<HttpAuthHandler> handler( |
| 280 new MockAuthHandler( | 280 new MockAuthHandler( |
| 281 HttpAuth::AUTH_SCHEME_BASIC, "MyRealm", HttpAuth::AUTH_SERVER)); | 281 "basic", "MyRealm", HttpAuth::AUTH_SERVER)); |
| 282 HttpAuthCache::Entry* orig_entry = cache.Add( | 282 HttpAuthCache::Entry* orig_entry = cache.Add( |
| 283 origin, handler->realm(), handler->auth_scheme(), auth_challenge, | 283 origin, handler->realm(), handler->auth_scheme(), auth_challenge, |
| 284 CreateASCIICredentials("user1", "password1"), "/x/y/z/"); | 284 CreateASCIICredentials("user1", "password1"), "/x/y/z/"); |
| 285 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, | 285 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, |
| 286 CreateASCIICredentials("user2", "password2"), "/z/y/x/"); | 286 CreateASCIICredentials("user2", "password2"), "/z/y/x/"); |
| 287 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, | 287 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, |
| 288 CreateASCIICredentials("user3", "password3"), "/z/y"); | 288 CreateASCIICredentials("user3", "password3"), "/z/y"); |
| 289 | 289 |
| 290 HttpAuthCache::Entry* entry = cache.Lookup( | 290 HttpAuthCache::Entry* entry = cache.Lookup( |
| 291 origin, "MyRealm", HttpAuth::AUTH_SCHEME_BASIC); | 291 origin, "MyRealm", "basic"); |
| 292 | 292 |
| 293 EXPECT_TRUE(entry == orig_entry); | 293 EXPECT_TRUE(entry == orig_entry); |
| 294 EXPECT_EQ(ASCIIToUTF16("user3"), entry->credentials().username()); | 294 EXPECT_EQ(ASCIIToUTF16("user3"), entry->credentials().username()); |
| 295 EXPECT_EQ(ASCIIToUTF16("password3"), entry->credentials().password()); | 295 EXPECT_EQ(ASCIIToUTF16("password3"), entry->credentials().password()); |
| 296 | 296 |
| 297 EXPECT_EQ(2U, entry->paths_.size()); | 297 EXPECT_EQ(2U, entry->paths_.size()); |
| 298 EXPECT_EQ("/z/", entry->paths_.front()); | 298 EXPECT_EQ("/z/", entry->paths_.front()); |
| 299 EXPECT_EQ("/x/y/z/", entry->paths_.back()); | 299 EXPECT_EQ("/x/y/z/", entry->paths_.back()); |
| 300 } | 300 } |
| 301 | 301 |
| 302 TEST(HttpAuthCacheTest, Remove) { | 302 TEST(HttpAuthCacheTest, Remove) { |
| 303 GURL origin("http://foobar2.com"); | 303 GURL origin("http://foobar2.com"); |
| 304 | 304 |
| 305 scoped_ptr<HttpAuthHandler> realm1_handler( | 305 scoped_ptr<HttpAuthHandler> realm1_handler( |
| 306 new MockAuthHandler( | 306 new MockAuthHandler( |
| 307 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER)); | 307 "basic", kRealm1, HttpAuth::AUTH_SERVER)); |
| 308 | 308 |
| 309 scoped_ptr<HttpAuthHandler> realm2_handler( | 309 scoped_ptr<HttpAuthHandler> realm2_handler( |
| 310 new MockAuthHandler( | 310 new MockAuthHandler( |
| 311 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_SERVER)); | 311 "basic", kRealm2, HttpAuth::AUTH_SERVER)); |
| 312 | 312 |
| 313 scoped_ptr<HttpAuthHandler> realm3_basic_handler( | 313 scoped_ptr<HttpAuthHandler> realm3_basic_handler( |
| 314 new MockAuthHandler( | 314 new MockAuthHandler( |
| 315 HttpAuth::AUTH_SCHEME_BASIC, kRealm3, HttpAuth::AUTH_SERVER)); | 315 "basic", kRealm3, HttpAuth::AUTH_SERVER)); |
| 316 | 316 |
| 317 scoped_ptr<HttpAuthHandler> realm3_digest_handler( | 317 scoped_ptr<HttpAuthHandler> realm3_digest_handler( |
| 318 new MockAuthHandler( | 318 new MockAuthHandler( |
| 319 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER)); | 319 "digest", kRealm3, HttpAuth::AUTH_SERVER)); |
| 320 | 320 |
| 321 HttpAuthCache cache; | 321 HttpAuthCache cache; |
| 322 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), | 322 cache.Add(origin, realm1_handler->realm(), realm1_handler->auth_scheme(), |
| 323 "basic realm=Realm1", AuthCredentials(kAlice, k123), "/"); | 323 "basic realm=Realm1", AuthCredentials(kAlice, k123), "/"); |
| 324 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), | 324 cache.Add(origin, realm2_handler->realm(), realm2_handler->auth_scheme(), |
| 325 "basic realm=Realm2", CreateASCIICredentials("bob", "princess"), | 325 "basic realm=Realm2", CreateASCIICredentials("bob", "princess"), |
| 326 "/"); | 326 "/"); |
| 327 cache.Add(origin, realm3_basic_handler->realm(), | 327 cache.Add(origin, realm3_basic_handler->realm(), |
| 328 realm3_basic_handler->auth_scheme(), "basic realm=Realm3", | 328 realm3_basic_handler->auth_scheme(), "basic realm=Realm3", |
| 329 AuthCredentials(kAdmin, kPassword), "/"); | 329 AuthCredentials(kAdmin, kPassword), "/"); |
| 330 cache.Add(origin, realm3_digest_handler->realm(), | 330 cache.Add(origin, realm3_digest_handler->realm(), |
| 331 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", | 331 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", |
| 332 AuthCredentials(kRoot, kWileCoyote), "/"); | 332 AuthCredentials(kRoot, kWileCoyote), "/"); |
| 333 | 333 |
| 334 // Fails, because there is no realm "Realm5". | 334 // Fails, because there is no realm "Realm5". |
| 335 EXPECT_FALSE(cache.Remove( | 335 EXPECT_FALSE(cache.Remove( |
| 336 origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC, | 336 origin, kRealm5, "basic", |
| 337 AuthCredentials(kAlice, k123))); | 337 AuthCredentials(kAlice, k123))); |
| 338 | 338 |
| 339 // Fails because the origin is wrong. | 339 // Fails because the origin is wrong. |
| 340 EXPECT_FALSE(cache.Remove(GURL("http://foobar2.com:100"), | 340 EXPECT_FALSE(cache.Remove(GURL("http://foobar2.com:100"), |
| 341 kRealm1, | 341 kRealm1, |
| 342 HttpAuth::AUTH_SCHEME_BASIC, | 342 "basic", |
| 343 AuthCredentials(kAlice, k123))); | 343 AuthCredentials(kAlice, k123))); |
| 344 | 344 |
| 345 // Fails because the username is wrong. | 345 // Fails because the username is wrong. |
| 346 EXPECT_FALSE(cache.Remove( | 346 EXPECT_FALSE(cache.Remove( |
| 347 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, | 347 origin, kRealm1, "basic", |
| 348 AuthCredentials(kAlice2, k123))); | 348 AuthCredentials(kAlice2, k123))); |
| 349 | 349 |
| 350 // Fails because the password is wrong. | 350 // Fails because the password is wrong. |
| 351 EXPECT_FALSE(cache.Remove( | 351 EXPECT_FALSE(cache.Remove( |
| 352 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, | 352 origin, kRealm1, "basic", |
| 353 AuthCredentials(kAlice, k1234))); | 353 AuthCredentials(kAlice, k1234))); |
| 354 | 354 |
| 355 // Fails because the authentication type is wrong. | 355 // Fails because the authentication type is wrong. |
| 356 EXPECT_FALSE(cache.Remove( | 356 EXPECT_FALSE(cache.Remove( |
| 357 origin, kRealm1, HttpAuth::AUTH_SCHEME_DIGEST, | 357 origin, kRealm1, "digest", |
| 358 AuthCredentials(kAlice, k123))); | 358 AuthCredentials(kAlice, k123))); |
| 359 | 359 |
| 360 // Succeeds. | 360 // Succeeds. |
| 361 EXPECT_TRUE(cache.Remove( | 361 EXPECT_TRUE(cache.Remove( |
| 362 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, | 362 origin, kRealm1, "basic", |
| 363 AuthCredentials(kAlice, k123))); | 363 AuthCredentials(kAlice, k123))); |
| 364 | 364 |
| 365 // Fails because we just deleted the entry! | 365 // Fails because we just deleted the entry! |
| 366 EXPECT_FALSE(cache.Remove( | 366 EXPECT_FALSE(cache.Remove( |
| 367 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, | 367 origin, kRealm1, "basic", |
| 368 AuthCredentials(kAlice, k123))); | 368 AuthCredentials(kAlice, k123))); |
| 369 | 369 |
| 370 // Succeed when there are two authentication types for the same origin,realm. | 370 // Succeed when there are two authentication types for the same origin,realm. |
| 371 EXPECT_TRUE(cache.Remove( | 371 EXPECT_TRUE(cache.Remove( |
| 372 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST, | 372 origin, kRealm3, "digest", |
| 373 AuthCredentials(kRoot, kWileCoyote))); | 373 AuthCredentials(kRoot, kWileCoyote))); |
| 374 | 374 |
| 375 // Succeed as above, but when entries were added in opposite order | 375 // Succeed as above, but when entries were added in opposite order |
| 376 cache.Add(origin, realm3_digest_handler->realm(), | 376 cache.Add(origin, realm3_digest_handler->realm(), |
| 377 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", | 377 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", |
| 378 AuthCredentials(kRoot, kWileCoyote), "/"); | 378 AuthCredentials(kRoot, kWileCoyote), "/"); |
| 379 EXPECT_TRUE(cache.Remove( | 379 EXPECT_TRUE(cache.Remove( |
| 380 origin, kRealm3, HttpAuth::AUTH_SCHEME_BASIC, | 380 origin, kRealm3, "basic", |
| 381 AuthCredentials(kAdmin, kPassword))); | 381 AuthCredentials(kAdmin, kPassword))); |
| 382 | 382 |
| 383 // Make sure that removing one entry still leaves the other available for | 383 // Make sure that removing one entry still leaves the other available for |
| 384 // lookup. | 384 // lookup. |
| 385 HttpAuthCache::Entry* entry = cache.Lookup( | 385 HttpAuthCache::Entry* entry = cache.Lookup( |
| 386 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); | 386 origin, kRealm3, "digest"); |
| 387 EXPECT_FALSE(NULL == entry); | 387 EXPECT_FALSE(NULL == entry); |
| 388 } | 388 } |
| 389 | 389 |
| 390 TEST(HttpAuthCacheTest, UpdateStaleChallenge) { | 390 TEST(HttpAuthCacheTest, UpdateStaleChallenge) { |
| 391 HttpAuthCache cache; | 391 HttpAuthCache cache; |
| 392 GURL origin("http://foobar2.com"); | 392 GURL origin("http://foobar2.com"); |
| 393 scoped_ptr<HttpAuthHandler> digest_handler( | 393 scoped_ptr<HttpAuthHandler> digest_handler( |
| 394 new MockAuthHandler( | 394 new MockAuthHandler( |
| 395 HttpAuth::AUTH_SCHEME_DIGEST, kRealm1, HttpAuth::AUTH_PROXY)); | 395 "digest", kRealm1, HttpAuth::AUTH_PROXY)); |
| 396 HttpAuthCache::Entry* entry_pre = cache.Add( | 396 HttpAuthCache::Entry* entry_pre = cache.Add( |
| 397 origin, | 397 origin, |
| 398 digest_handler->realm(), | 398 digest_handler->realm(), |
| 399 digest_handler->auth_scheme(), | 399 digest_handler->auth_scheme(), |
| 400 "Digest realm=Realm1," | 400 "Digest realm=Realm1," |
| 401 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"", | 401 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"", |
| 402 CreateASCIICredentials("realm-digest-user", "realm-digest-password"), | 402 CreateASCIICredentials("realm-digest-user", "realm-digest-password"), |
| 403 "/baz/index.html"); | 403 "/baz/index.html"); |
| 404 ASSERT_TRUE(entry_pre != NULL); | 404 ASSERT_TRUE(entry_pre != NULL); |
| 405 | 405 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 436 EXPECT_FALSE(update_failure); | 436 EXPECT_FALSE(update_failure); |
| 437 } | 437 } |
| 438 | 438 |
| 439 TEST(HttpAuthCacheTest, UpdateAllFrom) { | 439 TEST(HttpAuthCacheTest, UpdateAllFrom) { |
| 440 GURL origin("http://example.com"); | 440 GURL origin("http://example.com"); |
| 441 std::string path("/some/path"); | 441 std::string path("/some/path"); |
| 442 std::string another_path("/another/path"); | 442 std::string another_path("/another/path"); |
| 443 | 443 |
| 444 scoped_ptr<HttpAuthHandler> realm1_handler( | 444 scoped_ptr<HttpAuthHandler> realm1_handler( |
| 445 new MockAuthHandler( | 445 new MockAuthHandler( |
| 446 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER)); | 446 "basic", kRealm1, HttpAuth::AUTH_SERVER)); |
| 447 | 447 |
| 448 scoped_ptr<HttpAuthHandler> realm2_handler( | 448 scoped_ptr<HttpAuthHandler> realm2_handler( |
| 449 new MockAuthHandler( | 449 new MockAuthHandler( |
| 450 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_PROXY)); | 450 "basic", kRealm2, HttpAuth::AUTH_PROXY)); |
| 451 | 451 |
| 452 scoped_ptr<HttpAuthHandler> realm3_digest_handler( | 452 scoped_ptr<HttpAuthHandler> realm3_digest_handler( |
| 453 new MockAuthHandler( | 453 new MockAuthHandler( |
| 454 HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER)); | 454 "digest", kRealm3, HttpAuth::AUTH_SERVER)); |
| 455 | 455 |
| 456 scoped_ptr<HttpAuthHandler> realm4_handler( | 456 scoped_ptr<HttpAuthHandler> realm4_handler( |
| 457 new MockAuthHandler( | 457 new MockAuthHandler( |
| 458 HttpAuth::AUTH_SCHEME_BASIC, kRealm4, HttpAuth::AUTH_SERVER)); | 458 "basic", kRealm4, HttpAuth::AUTH_SERVER)); |
| 459 | 459 |
| 460 HttpAuthCache first_cache; | 460 HttpAuthCache first_cache; |
| 461 HttpAuthCache::Entry* entry; | 461 HttpAuthCache::Entry* entry; |
| 462 | 462 |
| 463 first_cache.Add(origin, realm1_handler->realm(), | 463 first_cache.Add(origin, realm1_handler->realm(), |
| 464 realm1_handler->auth_scheme(), "basic realm=Realm1", | 464 realm1_handler->auth_scheme(), "basic realm=Realm1", |
| 465 AuthCredentials(kAlice, k123), path); | 465 AuthCredentials(kAlice, k123), path); |
| 466 first_cache.Add(origin, realm2_handler->realm(), | 466 first_cache.Add(origin, realm2_handler->realm(), |
| 467 realm2_handler->auth_scheme(), "basic realm=Realm2", | 467 realm2_handler->auth_scheme(), "basic realm=Realm2", |
| 468 AuthCredentials(kAlice2, k1234), path); | 468 AuthCredentials(kAlice2, k1234), path); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 482 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", | 482 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", |
| 483 AuthCredentials(kAlice2, k1234), path); | 483 AuthCredentials(kAlice2, k1234), path); |
| 484 // Should be left intact. | 484 // Should be left intact. |
| 485 second_cache.Add(origin, realm4_handler->realm(), | 485 second_cache.Add(origin, realm4_handler->realm(), |
| 486 realm4_handler->auth_scheme(), "basic realm=Realm4", | 486 realm4_handler->auth_scheme(), "basic realm=Realm4", |
| 487 AuthCredentials(kAdmin, kRoot), path); | 487 AuthCredentials(kAdmin, kRoot), path); |
| 488 | 488 |
| 489 second_cache.UpdateAllFrom(first_cache); | 489 second_cache.UpdateAllFrom(first_cache); |
| 490 | 490 |
| 491 // Copied from first_cache. | 491 // Copied from first_cache. |
| 492 entry = second_cache.Lookup(origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC); | 492 entry = second_cache.Lookup(origin, kRealm1, "basic"); |
| 493 EXPECT_TRUE(NULL != entry); | 493 EXPECT_TRUE(NULL != entry); |
| 494 EXPECT_EQ(kAlice, entry->credentials().username()); | 494 EXPECT_EQ(kAlice, entry->credentials().username()); |
| 495 EXPECT_EQ(k123, entry->credentials().password()); | 495 EXPECT_EQ(k123, entry->credentials().password()); |
| 496 | 496 |
| 497 // Copied from first_cache. | 497 // Copied from first_cache. |
| 498 entry = second_cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); | 498 entry = second_cache.Lookup(origin, kRealm2, "basic"); |
| 499 EXPECT_TRUE(NULL != entry); | 499 EXPECT_TRUE(NULL != entry); |
| 500 EXPECT_EQ(kAlice2, entry->credentials().username()); | 500 EXPECT_EQ(kAlice2, entry->credentials().username()); |
| 501 EXPECT_EQ(k1234, entry->credentials().password()); | 501 EXPECT_EQ(k1234, entry->credentials().password()); |
| 502 | 502 |
| 503 // Overwritten from first_cache. | 503 // Overwritten from first_cache. |
| 504 entry = second_cache.Lookup(origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); | 504 entry = second_cache.Lookup(origin, kRealm3, "digest"); |
| 505 EXPECT_TRUE(NULL != entry); | 505 EXPECT_TRUE(NULL != entry); |
| 506 EXPECT_EQ(kRoot, entry->credentials().username()); | 506 EXPECT_EQ(kRoot, entry->credentials().username()); |
| 507 EXPECT_EQ(kWileCoyote, entry->credentials().password()); | 507 EXPECT_EQ(kWileCoyote, entry->credentials().password()); |
| 508 // Nonce count should get copied. | 508 // Nonce count should get copied. |
| 509 EXPECT_EQ(3, entry->IncrementNonceCount()); | 509 EXPECT_EQ(3, entry->IncrementNonceCount()); |
| 510 | 510 |
| 511 // All paths should get copied. | 511 // All paths should get copied. |
| 512 entry = second_cache.LookupByPath(origin, another_path); | 512 entry = second_cache.LookupByPath(origin, another_path); |
| 513 EXPECT_TRUE(NULL != entry); | 513 EXPECT_TRUE(NULL != entry); |
| 514 EXPECT_EQ(kRoot, entry->credentials().username()); | 514 EXPECT_EQ(kRoot, entry->credentials().username()); |
| 515 EXPECT_EQ(kWileCoyote, entry->credentials().password()); | 515 EXPECT_EQ(kWileCoyote, entry->credentials().password()); |
| 516 | 516 |
| 517 // Left intact in second_cache. | 517 // Left intact in second_cache. |
| 518 entry = second_cache.Lookup(origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC); | 518 entry = second_cache.Lookup(origin, kRealm4, "basic"); |
| 519 EXPECT_TRUE(NULL != entry); | 519 EXPECT_TRUE(NULL != entry); |
| 520 EXPECT_EQ(kAdmin, entry->credentials().username()); | 520 EXPECT_EQ(kAdmin, entry->credentials().username()); |
| 521 EXPECT_EQ(kRoot, entry->credentials().password()); | 521 EXPECT_EQ(kRoot, entry->credentials().password()); |
| 522 } | 522 } |
| 523 | 523 |
| 524 // Test fixture class for eviction tests (contains helpers for bulk | 524 // Test fixture class for eviction tests (contains helpers for bulk |
| 525 // insertion and existence testing). | 525 // insertion and existence testing). |
| 526 class HttpAuthCacheEvictionTest : public testing::Test { | 526 class HttpAuthCacheEvictionTest : public testing::Test { |
| 527 protected: | 527 protected: |
| 528 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } | 528 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } |
| 529 | 529 |
| 530 std::string GenerateRealm(int realm_i) { | 530 std::string GenerateRealm(int realm_i) { |
| 531 return base::StringPrintf("Realm %d", realm_i); | 531 return base::StringPrintf("Realm %d", realm_i); |
| 532 } | 532 } |
| 533 | 533 |
| 534 std::string GeneratePath(int realm_i, int path_i) { | 534 std::string GeneratePath(int realm_i, int path_i) { |
| 535 return base::StringPrintf("/%d/%d/x/y", realm_i, path_i); | 535 return base::StringPrintf("/%d/%d/x/y", realm_i, path_i); |
| 536 } | 536 } |
| 537 | 537 |
| 538 void AddRealm(int realm_i) { | 538 void AddRealm(int realm_i) { |
| 539 AddPathToRealm(realm_i, 0); | 539 AddPathToRealm(realm_i, 0); |
| 540 } | 540 } |
| 541 | 541 |
| 542 void AddPathToRealm(int realm_i, int path_i) { | 542 void AddPathToRealm(int realm_i, int path_i) { |
| 543 cache_.Add(origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC, "", | 543 cache_.Add(origin_, GenerateRealm(realm_i), "basic", "", |
| 544 AuthCredentials(kUsername, kPassword), | 544 AuthCredentials(kUsername, kPassword), |
| 545 GeneratePath(realm_i, path_i)); | 545 GeneratePath(realm_i, path_i)); |
| 546 } | 546 } |
| 547 | 547 |
| 548 void CheckRealmExistence(int realm_i, bool exists) { | 548 void CheckRealmExistence(int realm_i, bool exists) { |
| 549 const HttpAuthCache::Entry* entry = | 549 const HttpAuthCache::Entry* entry = |
| 550 cache_.Lookup( | 550 cache_.Lookup( |
| 551 origin_, GenerateRealm(realm_i), HttpAuth::AUTH_SCHEME_BASIC); | 551 origin_, GenerateRealm(realm_i), "basic"); |
| 552 if (exists) { | 552 if (exists) { |
| 553 EXPECT_FALSE(entry == NULL); | 553 EXPECT_FALSE(entry == NULL); |
| 554 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); | 554 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); |
| 555 } else { | 555 } else { |
| 556 EXPECT_TRUE(entry == NULL); | 556 EXPECT_TRUE(entry == NULL); |
| 557 } | 557 } |
| 558 } | 558 } |
| 559 | 559 |
| 560 void CheckPathExistence(int realm_i, int path_i, bool exists) { | 560 void CheckPathExistence(int realm_i, int path_i, bool exists) { |
| 561 const HttpAuthCache::Entry* entry = | 561 const HttpAuthCache::Entry* entry = |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 CheckPathExistence(0, i, false); | 612 CheckPathExistence(0, i, false); |
| 613 | 613 |
| 614 for (int i = 0; i < kMaxPaths; ++i) | 614 for (int i = 0; i < kMaxPaths; ++i) |
| 615 CheckPathExistence(0, i + 3, true); | 615 CheckPathExistence(0, i + 3, true); |
| 616 | 616 |
| 617 for (int i = 0; i < kMaxRealms; ++i) | 617 for (int i = 0; i < kMaxRealms; ++i) |
| 618 CheckRealmExistence(i, true); | 618 CheckRealmExistence(i, true); |
| 619 } | 619 } |
| 620 | 620 |
| 621 } // namespace net | 621 } // namespace net |
| OLD | NEW |