| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 "base/string_util.h" | 5 #include "base/string_util.h" |
| 6 #include "net/base/net_errors.h" | 6 #include "net/base/net_errors.h" |
| 7 #include "net/http/http_auth_cache.h" | 7 #include "net/http/http_auth_cache.h" |
| 8 | 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 62 scoped_refptr<HttpAuthHandler> realm1_handler = | 62 scoped_refptr<HttpAuthHandler> realm1_handler = |
| 63 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER); | 63 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER); |
| 64 cache.Add(origin, realm1_handler, L"realm1-user", L"realm1-password", | 64 cache.Add(origin, realm1_handler, L"realm1-user", L"realm1-password", |
| 65 "/foo/bar/index.html"); | 65 "/foo/bar/index.html"); |
| 66 | 66 |
| 67 scoped_refptr<HttpAuthHandler> realm2_handler = | 67 scoped_refptr<HttpAuthHandler> realm2_handler = |
| 68 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER); | 68 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER); |
| 69 cache.Add(origin, realm2_handler, L"realm2-user", L"realm2-password", | 69 cache.Add(origin, realm2_handler, L"realm2-user", L"realm2-password", |
| 70 "/foo2/index.html"); | 70 "/foo2/index.html"); |
| 71 | 71 |
| 72 scoped_refptr<HttpAuthHandler> realm3_handler = | 72 scoped_refptr<HttpAuthHandler> realm3_basic_handler = |
| 73 new MockAuthHandler("basic", "Realm3", HttpAuth::AUTH_PROXY); | 73 new MockAuthHandler("basic", "Realm3", HttpAuth::AUTH_PROXY); |
| 74 cache.Add(origin, realm3_handler, L"realm3-user", L"realm3-password", ""); | 74 cache.Add(origin, realm3_basic_handler, L"realm3-basic-user", |
| 75 L"realm3-basic-password", ""); |
| 76 |
| 77 scoped_refptr<HttpAuthHandler> realm3_digest_handler = |
| 78 new MockAuthHandler("digest", "Realm3", HttpAuth::AUTH_PROXY); |
| 79 cache.Add(origin, realm3_digest_handler, L"realm3-digest-user", |
| 80 L"realm3-digest-password", "/baz/index.html"); |
| 75 | 81 |
| 76 // There is no Realm4 | 82 // There is no Realm4 |
| 77 entry = cache.LookupByRealm(origin, "Realm4"); | 83 entry = cache.Lookup(origin, "Realm4", "basic"); |
| 78 EXPECT_TRUE(NULL == entry); | 84 EXPECT_TRUE(NULL == entry); |
| 79 | 85 |
| 80 // While Realm3 does exist, the origin scheme is wrong. | 86 // While Realm3 does exist, the origin scheme is wrong. |
| 81 entry = cache.LookupByRealm(GURL("https://www.google.com"), "Realm3"); | 87 entry = cache.Lookup(GURL("https://www.google.com"), "Realm3", |
| 88 "basic"); |
| 82 EXPECT_TRUE(NULL == entry); | 89 EXPECT_TRUE(NULL == entry); |
| 83 | 90 |
| 84 // Valid lookup by realm. | 91 // Realm, origin scheme ok, authentication scheme wrong |
| 85 entry = cache.LookupByRealm(GURL("http://www.google.com:80"), "Realm3"); | 92 entry = cache.Lookup(GURL("http://www.google.com"), "Realm1", "digest"); |
| 93 EXPECT_TRUE(NULL == entry); |
| 94 |
| 95 // Valid lookup by origin, realm, scheme. |
| 96 entry = cache.Lookup(GURL("http://www.google.com:80"), "Realm3", "basic"); |
| 86 EXPECT_FALSE(NULL == entry); | 97 EXPECT_FALSE(NULL == entry); |
| 87 EXPECT_TRUE(entry->handler() == realm3_handler.get()); | 98 EXPECT_TRUE(entry->handler() == realm3_basic_handler.get()); |
| 88 EXPECT_EQ(L"realm3-user", entry->username()); | 99 EXPECT_EQ(L"realm3-basic-user", entry->username()); |
| 89 EXPECT_EQ(L"realm3-password", entry->password()); | 100 EXPECT_EQ(L"realm3-basic-password", entry->password()); |
| 101 |
| 102 // Valid lookup by origin, realm, scheme when there's a duplicate |
| 103 // origin, realm in the cache |
| 104 entry = cache.Lookup(GURL("http://www.google.com:80"), "Realm3", "digest"); |
| 105 EXPECT_FALSE(NULL == entry); |
| 106 EXPECT_TRUE(entry->handler() == realm3_digest_handler.get()); |
| 107 EXPECT_EQ(L"realm3-digest-user", entry->username()); |
| 108 EXPECT_EQ(L"realm3-digest-password", entry->password()); |
| 90 | 109 |
| 91 // Valid lookup by realm. | 110 // Valid lookup by realm. |
| 92 entry = cache.LookupByRealm(origin, "Realm2"); | 111 entry = cache.Lookup(origin, "Realm2", "basic"); |
| 93 EXPECT_FALSE(NULL == entry); | 112 EXPECT_FALSE(NULL == entry); |
| 94 EXPECT_TRUE(entry->handler() == realm2_handler.get()); | 113 EXPECT_TRUE(entry->handler() == realm2_handler.get()); |
| 95 EXPECT_EQ(L"realm2-user", entry->username()); | 114 EXPECT_EQ(L"realm2-user", entry->username()); |
| 96 EXPECT_EQ(L"realm2-password", entry->password()); | 115 EXPECT_EQ(L"realm2-password", entry->password()); |
| 97 | 116 |
| 98 // Check that subpaths are recognized. | 117 // Check that subpaths are recognized. |
| 99 HttpAuthCache::Entry* realm2Entry = cache.LookupByRealm(origin, "Realm2"); | 118 HttpAuthCache::Entry* realm2_entry = cache.Lookup(origin, "Realm2", "basic"); |
| 100 EXPECT_FALSE(NULL == realm2Entry); | 119 EXPECT_FALSE(NULL == realm2_entry); |
| 101 // Positive tests: | 120 // Positive tests: |
| 102 entry = cache.LookupByPath(origin, "/foo2/index.html"); | 121 entry = cache.LookupByPath(origin, "/foo2/index.html"); |
| 103 EXPECT_TRUE(realm2Entry == entry); | 122 EXPECT_TRUE(realm2_entry == entry); |
| 104 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); | 123 entry = cache.LookupByPath(origin, "/foo2/foobar.html"); |
| 105 EXPECT_TRUE(realm2Entry == entry); | 124 EXPECT_TRUE(realm2_entry == entry); |
| 106 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); | 125 entry = cache.LookupByPath(origin, "/foo2/bar/index.html"); |
| 107 EXPECT_TRUE(realm2Entry == entry); | 126 EXPECT_TRUE(realm2_entry == entry); |
| 108 entry = cache.LookupByPath(origin, "/foo2/"); | 127 entry = cache.LookupByPath(origin, "/foo2/"); |
| 109 EXPECT_TRUE(realm2Entry == entry); | 128 EXPECT_TRUE(realm2_entry == entry); |
| 129 |
| 110 // Negative tests: | 130 // Negative tests: |
| 111 entry = cache.LookupByPath(origin, "/foo2"); | 131 entry = cache.LookupByPath(origin, "/foo2"); |
| 112 EXPECT_FALSE(realm2Entry == entry); | 132 EXPECT_FALSE(realm2_entry == entry); |
| 113 entry = cache.LookupByPath(origin, "/foo3/index.html"); | 133 entry = cache.LookupByPath(origin, "/foo3/index.html"); |
| 114 EXPECT_FALSE(realm2Entry == entry); | 134 EXPECT_FALSE(realm2_entry == entry); |
| 115 entry = cache.LookupByPath(origin, ""); | 135 entry = cache.LookupByPath(origin, ""); |
| 116 EXPECT_FALSE(realm2Entry == entry); | 136 EXPECT_FALSE(realm2_entry == entry); |
| 117 entry = cache.LookupByPath(origin, "/"); | 137 entry = cache.LookupByPath(origin, "/"); |
| 118 EXPECT_FALSE(realm2Entry == entry); | 138 EXPECT_FALSE(realm2_entry == entry); |
| 139 |
| 140 // Confirm we find the same realm, different auth scheme by path lookup |
| 141 HttpAuthCache::Entry* realm3_digest_entry = |
| 142 cache.Lookup(origin, "Realm3", "digest"); |
| 143 EXPECT_FALSE(NULL == realm3_digest_entry); |
| 144 entry = cache.LookupByPath(origin, "/baz/index.html"); |
| 145 EXPECT_TRUE(realm3_digest_entry == entry); |
| 146 entry = cache.LookupByPath(origin, "/baz/"); |
| 147 EXPECT_TRUE(realm3_digest_entry == entry); |
| 148 entry = cache.LookupByPath(origin, "/baz"); |
| 149 EXPECT_FALSE(realm3_digest_entry == entry); |
| 119 | 150 |
| 120 // Lookup using empty path (may be used for proxy). | 151 // Lookup using empty path (may be used for proxy). |
| 121 entry = cache.LookupByPath(origin, ""); | 152 entry = cache.LookupByPath(origin, ""); |
| 122 EXPECT_FALSE(NULL == entry); | 153 EXPECT_FALSE(NULL == entry); |
| 123 EXPECT_TRUE(entry->handler() == realm3_handler.get()); | 154 EXPECT_TRUE(entry->handler() == realm3_basic_handler.get()); |
| 124 EXPECT_EQ("Realm3", entry->realm()); | 155 EXPECT_EQ("Realm3", entry->realm()); |
| 125 } | 156 } |
| 126 | 157 |
| 127 TEST(HttpAuthCacheTest, AddPath) { | 158 TEST(HttpAuthCacheTest, AddPath) { |
| 128 HttpAuthCache::Entry entry; | 159 HttpAuthCache::Entry entry; |
| 129 | 160 |
| 130 // All of these paths have a common root /1/2/2/4/5/ | 161 // All of these paths have a common root /1/2/2/4/5/ |
| 131 entry.AddPath("/1/2/3/4/5/x.txt"); | 162 entry.AddPath("/1/2/3/4/5/x.txt"); |
| 132 entry.AddPath("/1/2/3/4/5/y.txt"); | 163 entry.AddPath("/1/2/3/4/5/y.txt"); |
| 133 entry.AddPath("/1/2/3/4/5/z.txt"); | 164 entry.AddPath("/1/2/3/4/5/z.txt"); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 163 GURL origin("http://www.foobar.com:70"); | 194 GURL origin("http://www.foobar.com:70"); |
| 164 | 195 |
| 165 scoped_refptr<HttpAuthHandler> handler = | 196 scoped_refptr<HttpAuthHandler> handler = |
| 166 new MockAuthHandler("basic", "MyRealm", HttpAuth::AUTH_SERVER); | 197 new MockAuthHandler("basic", "MyRealm", HttpAuth::AUTH_SERVER); |
| 167 | 198 |
| 168 HttpAuthCache::Entry* orig_entry = cache.Add( | 199 HttpAuthCache::Entry* orig_entry = cache.Add( |
| 169 origin, handler, L"user1", L"password1", "/x/y/z/"); | 200 origin, handler, L"user1", L"password1", "/x/y/z/"); |
| 170 cache.Add(origin, handler, L"user2", L"password2", "/z/y/x/"); | 201 cache.Add(origin, handler, L"user2", L"password2", "/z/y/x/"); |
| 171 cache.Add(origin, handler, L"user3", L"password3", "/z/y"); | 202 cache.Add(origin, handler, L"user3", L"password3", "/z/y"); |
| 172 | 203 |
| 173 HttpAuthCache::Entry* entry = cache.LookupByRealm(origin, "MyRealm"); | 204 HttpAuthCache::Entry* entry = cache.Lookup(origin, "MyRealm", "basic"); |
| 174 | 205 |
| 175 EXPECT_TRUE(entry == orig_entry); | 206 EXPECT_TRUE(entry == orig_entry); |
| 176 EXPECT_EQ(L"user3", entry->username()); | 207 EXPECT_EQ(L"user3", entry->username()); |
| 177 EXPECT_EQ(L"password3", entry->password()); | 208 EXPECT_EQ(L"password3", entry->password()); |
| 178 | 209 |
| 179 EXPECT_EQ(2U, entry->paths_.size()); | 210 EXPECT_EQ(2U, entry->paths_.size()); |
| 180 EXPECT_EQ("/z/", entry->paths_.front()); | 211 EXPECT_EQ("/z/", entry->paths_.front()); |
| 181 EXPECT_EQ("/x/y/z/", entry->paths_.back()); | 212 EXPECT_EQ("/x/y/z/", entry->paths_.back()); |
| 182 } | 213 } |
| 183 | 214 |
| 184 TEST(HttpAuthCacheTest, Remove) { | 215 TEST(HttpAuthCacheTest, Remove) { |
| 185 GURL origin("http://foobar2.com"); | 216 GURL origin("http://foobar2.com"); |
| 186 | 217 |
| 187 scoped_refptr<HttpAuthHandler> realm1_handler = | 218 scoped_refptr<HttpAuthHandler> realm1_handler = |
| 188 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER); | 219 new MockAuthHandler("basic", "Realm1", HttpAuth::AUTH_SERVER); |
| 189 | 220 |
| 190 scoped_refptr<HttpAuthHandler> realm2_handler = | 221 scoped_refptr<HttpAuthHandler> realm2_handler = |
| 191 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER); | 222 new MockAuthHandler("basic", "Realm2", HttpAuth::AUTH_SERVER); |
| 192 | 223 |
| 193 scoped_refptr<HttpAuthHandler> realm3_handler = | 224 scoped_refptr<HttpAuthHandler> realm3_basic_handler = |
| 194 new MockAuthHandler("basic", "Realm3", HttpAuth::AUTH_SERVER); | 225 new MockAuthHandler("basic", "Realm3", HttpAuth::AUTH_SERVER); |
| 195 | 226 |
| 227 scoped_refptr<HttpAuthHandler> realm3_digest_handler = |
| 228 new MockAuthHandler("digest", "Realm3", HttpAuth::AUTH_SERVER); |
| 229 |
| 196 HttpAuthCache cache; | 230 HttpAuthCache cache; |
| 197 cache.Add(origin, realm1_handler, L"alice", L"123", "/"); | 231 cache.Add(origin, realm1_handler, L"alice", L"123", "/"); |
| 198 cache.Add(origin, realm2_handler, L"bob", L"princess", "/"); | 232 cache.Add(origin, realm2_handler, L"bob", L"princess", "/"); |
| 199 cache.Add(origin, realm3_handler, L"admin", L"password", "/"); | 233 cache.Add(origin, realm3_basic_handler, L"admin", L"password", "/"); |
| 234 cache.Add(origin, realm3_digest_handler, L"root", L"wilecoyote", "/"); |
| 200 | 235 |
| 201 // Fails, because there is no realm "Realm4". | 236 // Fails, because there is no realm "Realm4". |
| 202 EXPECT_FALSE(cache.Remove(origin, "Realm4", L"alice", L"123")); | 237 EXPECT_FALSE(cache.Remove(origin, "Realm4", "basic", L"alice", L"123")); |
| 203 | 238 |
| 204 // Fails because the origin is wrong. | 239 // Fails because the origin is wrong. |
| 205 EXPECT_FALSE(cache.Remove( | 240 EXPECT_FALSE(cache.Remove( |
| 206 GURL("http://foobar2.com:100"), "Realm1", L"alice", L"123")); | 241 GURL("http://foobar2.com:100"), "Realm1", "basic", L"alice", L"123")); |
| 207 | 242 |
| 208 // Fails because the username is wrong. | 243 // Fails because the username is wrong. |
| 209 EXPECT_FALSE(cache.Remove(origin, "Realm1", L"alice2", L"123")); | 244 EXPECT_FALSE(cache.Remove(origin, "Realm1", "basic", L"alice2", L"123")); |
| 210 | 245 |
| 211 // Fails because the password is wrong. | 246 // Fails because the password is wrong. |
| 212 EXPECT_FALSE(cache.Remove(origin, "Realm1", L"alice", L"1234")); | 247 EXPECT_FALSE(cache.Remove(origin, "Realm1", "basic", L"alice", L"1234")); |
| 248 |
| 249 // Fails because the authentication type is wrong. |
| 250 EXPECT_FALSE(cache.Remove(origin, "Realm1", "digest", L"alice", L"123")); |
| 213 | 251 |
| 214 // Succeeds. | 252 // Succeeds. |
| 215 EXPECT_TRUE(cache.Remove(origin, "Realm1", L"alice", L"123")); | 253 EXPECT_TRUE(cache.Remove(origin, "Realm1", "basic", L"alice", L"123")); |
| 216 | 254 |
| 217 // Fails because we just deleted the entry! | 255 // Fails because we just deleted the entry! |
| 218 EXPECT_FALSE(cache.Remove(origin, "Realm1", L"alice", L"123")); | 256 EXPECT_FALSE(cache.Remove(origin, "Realm1", "basic", L"alice", L"123")); |
| 257 |
| 258 // Succeed when there are two authentication types for the same origin,realm. |
| 259 EXPECT_TRUE(cache.Remove(origin, "Realm3", "digest", L"root", L"wilecoyote")); |
| 260 |
| 261 // Succeed as above, but when entries were added in opposite order |
| 262 cache.Add(origin, realm3_digest_handler, L"root", L"wilecoyote", "/"); |
| 263 EXPECT_TRUE(cache.Remove(origin, "Realm3", "basic", L"admin", L"password")); |
| 264 |
| 265 // Make sure that removing one entry still leaves the other available |
| 266 // for lookup |
| 267 HttpAuthCache::Entry* entry = cache.Lookup(origin, "Realm3", "digest"); |
| 268 EXPECT_FALSE(NULL == entry); |
| 219 } | 269 } |
| 220 | 270 |
| 221 // Test fixture class for eviction tests (contains helpers for bulk | 271 // Test fixture class for eviction tests (contains helpers for bulk |
| 222 // insertion and existence testing). | 272 // insertion and existence testing). |
| 223 class HttpAuthCacheEvictionTest : public testing::Test { | 273 class HttpAuthCacheEvictionTest : public testing::Test { |
| 224 protected: | 274 protected: |
| 225 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } | 275 HttpAuthCacheEvictionTest() : origin_("http://www.google.com") { } |
| 226 | 276 |
| 227 std::string GenerateRealm(int realm_i) { | 277 std::string GenerateRealm(int realm_i) { |
| 228 return StringPrintf("Realm %d", realm_i); | 278 return StringPrintf("Realm %d", realm_i); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 239 void AddPathToRealm(int realm_i, int path_i) { | 289 void AddPathToRealm(int realm_i, int path_i) { |
| 240 scoped_refptr<HttpAuthHandler> handler = new MockAuthHandler( | 290 scoped_refptr<HttpAuthHandler> handler = new MockAuthHandler( |
| 241 "basic", | 291 "basic", |
| 242 GenerateRealm(realm_i), HttpAuth::AUTH_SERVER); | 292 GenerateRealm(realm_i), HttpAuth::AUTH_SERVER); |
| 243 std::string path = GeneratePath(realm_i, path_i); | 293 std::string path = GeneratePath(realm_i, path_i); |
| 244 cache_.Add(origin_, handler, L"username", L"password", path); | 294 cache_.Add(origin_, handler, L"username", L"password", path); |
| 245 } | 295 } |
| 246 | 296 |
| 247 void CheckRealmExistence(int realm_i, bool exists) { | 297 void CheckRealmExistence(int realm_i, bool exists) { |
| 248 const HttpAuthCache::Entry* entry = | 298 const HttpAuthCache::Entry* entry = |
| 249 cache_.LookupByRealm(origin_, GenerateRealm(realm_i)); | 299 cache_.Lookup(origin_, GenerateRealm(realm_i), "basic"); |
| 250 if (exists) { | 300 if (exists) { |
| 251 EXPECT_FALSE(entry == NULL); | 301 EXPECT_FALSE(entry == NULL); |
| 252 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); | 302 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); |
| 253 } else { | 303 } else { |
| 254 EXPECT_TRUE(entry == NULL); | 304 EXPECT_TRUE(entry == NULL); |
| 255 } | 305 } |
| 256 } | 306 } |
| 257 | 307 |
| 258 void CheckPathExistence(int realm_i, int path_i, bool exists) { | 308 void CheckPathExistence(int realm_i, int path_i, bool exists) { |
| 259 const HttpAuthCache::Entry* entry = | 309 const HttpAuthCache::Entry* entry = |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 CheckPathExistence(0, i, false); | 360 CheckPathExistence(0, i, false); |
| 311 | 361 |
| 312 for (int i = 0; i < kMaxPaths; ++i) | 362 for (int i = 0; i < kMaxPaths; ++i) |
| 313 CheckPathExistence(0, i + 3, true); | 363 CheckPathExistence(0, i + 3, true); |
| 314 | 364 |
| 315 for (int i = 0; i < kMaxRealms; ++i) | 365 for (int i = 0; i < kMaxRealms; ++i) |
| 316 CheckRealmExistence(i, true); | 366 CheckRealmExistence(i, true); |
| 317 } | 367 } |
| 318 | 368 |
| 319 } // namespace net | 369 } // namespace net |
| OLD | NEW |