Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: net/http/http_auth_cache_unittest.cc

Issue 1151843002: DO NOT LAND Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: More. Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/strings/string16.h" 7 #include "base/strings/string16.h"
8 #include "base/strings/string_util.h" 8 #include "base/strings/string_util.h"
9 #include "base/strings/stringprintf.h" 9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 72
73 AuthCredentials CreateASCIICredentials(const char* username, 73 AuthCredentials CreateASCIICredentials(const char* username,
74 const char* password) { 74 const char* password) {
75 return AuthCredentials(ASCIIToUTF16(username), ASCIIToUTF16(password)); 75 return AuthCredentials(ASCIIToUTF16(username), ASCIIToUTF16(password));
76 } 76 }
77 77
78 } // namespace 78 } // namespace
79 79
80 // Test adding and looking-up cache entries (both by realm and by path). 80 // Test adding and looking-up cache entries (both by realm and by path).
81 TEST(HttpAuthCacheTest, Basic) { 81 TEST(HttpAuthCacheTest, Basic) {
82 GURL origin("http://www.google.com"); 82 url::Origin origin("http://www.google.com");
83 HttpAuthCache cache; 83 HttpAuthCache cache;
84 HttpAuthCache::Entry* entry; 84 HttpAuthCache::Entry* entry;
85 85
86 // Add cache entries for 4 realms: "Realm1", "Realm2", "Realm3" and 86 // Add cache entries for 4 realms: "Realm1", "Realm2", "Realm3" and
87 // "Realm4" 87 // "Realm4"
88 88
89 scoped_ptr<HttpAuthHandler> realm1_handler( 89 scoped_ptr<HttpAuthHandler> realm1_handler(
90 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC, 90 new MockAuthHandler(HttpAuth::AUTH_SCHEME_BASIC,
91 kRealm1, 91 kRealm1,
92 HttpAuth::AUTH_SERVER)); 92 HttpAuth::AUTH_SERVER));
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 realm4_basic_handler->auth_scheme(), "Basic realm=Realm4", 134 realm4_basic_handler->auth_scheme(), "Basic realm=Realm4",
135 CreateASCIICredentials("realm4-basic-user", 135 CreateASCIICredentials("realm4-basic-user",
136 "realm4-basic-password"), 136 "realm4-basic-password"),
137 "/"); 137 "/");
138 138
139 // There is no Realm5 139 // There is no Realm5
140 entry = cache.Lookup(origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC); 140 entry = cache.Lookup(origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC);
141 EXPECT_TRUE(NULL == entry); 141 EXPECT_TRUE(NULL == entry);
142 142
143 // While Realm3 does exist, the origin scheme is wrong. 143 // While Realm3 does exist, the origin scheme is wrong.
144 entry = cache.Lookup(GURL("https://www.google.com"), kRealm3, 144 entry = cache.Lookup(url::Origin("https://www.google.com"), kRealm3,
145 HttpAuth::AUTH_SCHEME_BASIC); 145 HttpAuth::AUTH_SCHEME_BASIC);
146 EXPECT_TRUE(NULL == entry); 146 EXPECT_TRUE(NULL == entry);
147 147
148 // Realm, origin scheme ok, authentication scheme wrong 148 // Realm, origin scheme ok, authentication scheme wrong
149 entry = cache.Lookup 149 entry = cache.Lookup(url::Origin("http://www.google.com"), kRealm1,
150 (GURL("http://www.google.com"), kRealm1, HttpAuth::AUTH_SCHEME_DIGEST); 150 HttpAuth::AUTH_SCHEME_DIGEST);
151 EXPECT_TRUE(NULL == entry); 151 EXPECT_TRUE(NULL == entry);
152 152
153 // Valid lookup by origin, realm, scheme. 153 // Valid lookup by origin, realm, scheme.
154 entry = cache.Lookup( 154 entry = cache.Lookup(url::Origin("http://www.google.com:80"), kRealm3,
155 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_BASIC); 155 HttpAuth::AUTH_SCHEME_BASIC);
156 ASSERT_FALSE(NULL == entry); 156 ASSERT_FALSE(NULL == entry);
157 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme()); 157 EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme());
158 EXPECT_EQ(kRealm3, entry->realm()); 158 EXPECT_EQ(kRealm3, entry->realm());
159 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge()); 159 EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge());
160 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->credentials().username()); 160 EXPECT_EQ(ASCIIToUTF16("realm3-basic-user"), entry->credentials().username());
161 EXPECT_EQ(ASCIIToUTF16("realm3-basic-password"), 161 EXPECT_EQ(ASCIIToUTF16("realm3-basic-password"),
162 entry->credentials().password()); 162 entry->credentials().password());
163 163
164 // Valid lookup by origin, realm, scheme when there's a duplicate 164 // Valid lookup by origin, realm, scheme when there's a duplicate
165 // origin, realm in the cache 165 // origin, realm in the cache
166 entry = cache.Lookup( 166 entry = cache.Lookup(url::Origin("http://www.google.com:80"), kRealm3,
167 GURL("http://www.google.com:80"), kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); 167 HttpAuth::AUTH_SCHEME_DIGEST);
168 ASSERT_FALSE(NULL == entry); 168 ASSERT_FALSE(NULL == entry);
169 EXPECT_EQ(HttpAuth::AUTH_SCHEME_DIGEST, entry->scheme()); 169 EXPECT_EQ(HttpAuth::AUTH_SCHEME_DIGEST, entry->scheme());
170 EXPECT_EQ(kRealm3, entry->realm()); 170 EXPECT_EQ(kRealm3, entry->realm());
171 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge()); 171 EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge());
172 EXPECT_EQ(ASCIIToUTF16("realm3-digest-user"), 172 EXPECT_EQ(ASCIIToUTF16("realm3-digest-user"),
173 entry->credentials().username()); 173 entry->credentials().username());
174 EXPECT_EQ(ASCIIToUTF16("realm3-digest-password"), 174 EXPECT_EQ(ASCIIToUTF16("realm3-digest-password"),
175 entry->credentials().password()); 175 entry->credentials().password());
176 176
177 // Valid lookup by realm. 177 // Valid lookup by realm.
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
270 270
271 entry.AddPath("/index.html"); 271 entry.AddPath("/index.html");
272 EXPECT_EQ(1U, entry.paths_.size()); 272 EXPECT_EQ(1U, entry.paths_.size());
273 EXPECT_EQ("/", entry.paths_.front()); 273 EXPECT_EQ("/", entry.paths_.front());
274 } 274 }
275 275
276 // Calling Add when the realm entry already exists, should append that 276 // Calling Add when the realm entry already exists, should append that
277 // path. 277 // path.
278 TEST(HttpAuthCacheTest, AddToExistingEntry) { 278 TEST(HttpAuthCacheTest, AddToExistingEntry) {
279 HttpAuthCache cache; 279 HttpAuthCache cache;
280 GURL origin("http://www.foobar.com:70"); 280 url::Origin origin("http://www.foobar.com:70");
281 const std::string auth_challenge = "Basic realm=MyRealm"; 281 const std::string auth_challenge = "Basic realm=MyRealm";
282 282
283 scoped_ptr<HttpAuthHandler> handler( 283 scoped_ptr<HttpAuthHandler> handler(
284 new MockAuthHandler( 284 new MockAuthHandler(
285 HttpAuth::AUTH_SCHEME_BASIC, "MyRealm", HttpAuth::AUTH_SERVER)); 285 HttpAuth::AUTH_SCHEME_BASIC, "MyRealm", HttpAuth::AUTH_SERVER));
286 HttpAuthCache::Entry* orig_entry = cache.Add( 286 HttpAuthCache::Entry* orig_entry = cache.Add(
287 origin, handler->realm(), handler->auth_scheme(), auth_challenge, 287 origin, handler->realm(), handler->auth_scheme(), auth_challenge,
288 CreateASCIICredentials("user1", "password1"), "/x/y/z/"); 288 CreateASCIICredentials("user1", "password1"), "/x/y/z/");
289 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, 289 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge,
290 CreateASCIICredentials("user2", "password2"), "/z/y/x/"); 290 CreateASCIICredentials("user2", "password2"), "/z/y/x/");
291 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge, 291 cache.Add(origin, handler->realm(), handler->auth_scheme(), auth_challenge,
292 CreateASCIICredentials("user3", "password3"), "/z/y"); 292 CreateASCIICredentials("user3", "password3"), "/z/y");
293 293
294 HttpAuthCache::Entry* entry = cache.Lookup( 294 HttpAuthCache::Entry* entry = cache.Lookup(
295 origin, "MyRealm", HttpAuth::AUTH_SCHEME_BASIC); 295 origin, "MyRealm", HttpAuth::AUTH_SCHEME_BASIC);
296 296
297 EXPECT_TRUE(entry == orig_entry); 297 EXPECT_TRUE(entry == orig_entry);
298 EXPECT_EQ(ASCIIToUTF16("user3"), entry->credentials().username()); 298 EXPECT_EQ(ASCIIToUTF16("user3"), entry->credentials().username());
299 EXPECT_EQ(ASCIIToUTF16("password3"), entry->credentials().password()); 299 EXPECT_EQ(ASCIIToUTF16("password3"), entry->credentials().password());
300 300
301 EXPECT_EQ(2U, entry->paths_.size()); 301 EXPECT_EQ(2U, entry->paths_.size());
302 EXPECT_EQ("/z/", entry->paths_.front()); 302 EXPECT_EQ("/z/", entry->paths_.front());
303 EXPECT_EQ("/x/y/z/", entry->paths_.back()); 303 EXPECT_EQ("/x/y/z/", entry->paths_.back());
304 } 304 }
305 305
306 TEST(HttpAuthCacheTest, Remove) { 306 TEST(HttpAuthCacheTest, Remove) {
307 GURL origin("http://foobar2.com"); 307 url::Origin origin("http://foobar2.com");
308 308
309 scoped_ptr<HttpAuthHandler> realm1_handler( 309 scoped_ptr<HttpAuthHandler> realm1_handler(
310 new MockAuthHandler( 310 new MockAuthHandler(
311 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER)); 311 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER));
312 312
313 scoped_ptr<HttpAuthHandler> realm2_handler( 313 scoped_ptr<HttpAuthHandler> realm2_handler(
314 new MockAuthHandler( 314 new MockAuthHandler(
315 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_SERVER)); 315 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_SERVER));
316 316
317 scoped_ptr<HttpAuthHandler> realm3_basic_handler( 317 scoped_ptr<HttpAuthHandler> realm3_basic_handler(
(...skipping 16 matching lines...) Expand all
334 cache.Add(origin, realm3_digest_handler->realm(), 334 cache.Add(origin, realm3_digest_handler->realm(),
335 realm3_digest_handler->auth_scheme(), "digest realm=Realm3", 335 realm3_digest_handler->auth_scheme(), "digest realm=Realm3",
336 AuthCredentials(kRoot, kWileCoyote), "/"); 336 AuthCredentials(kRoot, kWileCoyote), "/");
337 337
338 // Fails, because there is no realm "Realm5". 338 // Fails, because there is no realm "Realm5".
339 EXPECT_FALSE(cache.Remove( 339 EXPECT_FALSE(cache.Remove(
340 origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC, 340 origin, kRealm5, HttpAuth::AUTH_SCHEME_BASIC,
341 AuthCredentials(kAlice, k123))); 341 AuthCredentials(kAlice, k123)));
342 342
343 // Fails because the origin is wrong. 343 // Fails because the origin is wrong.
344 EXPECT_FALSE(cache.Remove(GURL("http://foobar2.com:100"), 344 EXPECT_FALSE(cache.Remove(url::Origin("http://foobar2.com:100"), kRealm1,
345 kRealm1,
346 HttpAuth::AUTH_SCHEME_BASIC, 345 HttpAuth::AUTH_SCHEME_BASIC,
347 AuthCredentials(kAlice, k123))); 346 AuthCredentials(kAlice, k123)));
348 347
349 // Fails because the username is wrong. 348 // Fails because the username is wrong.
350 EXPECT_FALSE(cache.Remove( 349 EXPECT_FALSE(cache.Remove(
351 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC, 350 origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
352 AuthCredentials(kAlice2, k123))); 351 AuthCredentials(kAlice2, k123)));
353 352
354 // Fails because the password is wrong. 353 // Fails because the password is wrong.
355 EXPECT_FALSE(cache.Remove( 354 EXPECT_FALSE(cache.Remove(
(...skipping 30 matching lines...) Expand all
386 385
387 // Make sure that removing one entry still leaves the other available for 386 // Make sure that removing one entry still leaves the other available for
388 // lookup. 387 // lookup.
389 HttpAuthCache::Entry* entry = cache.Lookup( 388 HttpAuthCache::Entry* entry = cache.Lookup(
390 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); 389 origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST);
391 EXPECT_FALSE(NULL == entry); 390 EXPECT_FALSE(NULL == entry);
392 } 391 }
393 392
394 TEST(HttpAuthCacheTest, UpdateStaleChallenge) { 393 TEST(HttpAuthCacheTest, UpdateStaleChallenge) {
395 HttpAuthCache cache; 394 HttpAuthCache cache;
396 GURL origin("http://foobar2.com"); 395 url::Origin origin("http://foobar2.com");
397 scoped_ptr<HttpAuthHandler> digest_handler( 396 scoped_ptr<HttpAuthHandler> digest_handler(
398 new MockAuthHandler( 397 new MockAuthHandler(
399 HttpAuth::AUTH_SCHEME_DIGEST, kRealm1, HttpAuth::AUTH_PROXY)); 398 HttpAuth::AUTH_SCHEME_DIGEST, kRealm1, HttpAuth::AUTH_PROXY));
400 HttpAuthCache::Entry* entry_pre = cache.Add( 399 HttpAuthCache::Entry* entry_pre = cache.Add(
401 origin, 400 origin,
402 digest_handler->realm(), 401 digest_handler->realm(),
403 digest_handler->auth_scheme(), 402 digest_handler->auth_scheme(),
404 "Digest realm=Realm1," 403 "Digest realm=Realm1,"
405 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"", 404 "nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"",
406 CreateASCIICredentials("realm-digest-user", "realm-digest-password"), 405 CreateASCIICredentials("realm-digest-user", "realm-digest-password"),
(...skipping 27 matching lines...) Expand all
434 origin, 433 origin,
435 kRealm2, 434 kRealm2,
436 digest_handler->auth_scheme(), 435 digest_handler->auth_scheme(),
437 "Digest realm=Realm2," 436 "Digest realm=Realm2,"
438 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\"," 437 "nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\","
439 "stale=\"true\""); 438 "stale=\"true\"");
440 EXPECT_FALSE(update_failure); 439 EXPECT_FALSE(update_failure);
441 } 440 }
442 441
443 TEST(HttpAuthCacheTest, UpdateAllFrom) { 442 TEST(HttpAuthCacheTest, UpdateAllFrom) {
444 GURL origin("http://example.com"); 443 url::Origin origin("http://example.com");
445 std::string path("/some/path"); 444 std::string path("/some/path");
446 std::string another_path("/another/path"); 445 std::string another_path("/another/path");
447 446
448 scoped_ptr<HttpAuthHandler> realm1_handler( 447 scoped_ptr<HttpAuthHandler> realm1_handler(
449 new MockAuthHandler( 448 new MockAuthHandler(
450 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER)); 449 HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER));
451 450
452 scoped_ptr<HttpAuthHandler> realm2_handler( 451 scoped_ptr<HttpAuthHandler> realm2_handler(
453 new MockAuthHandler( 452 new MockAuthHandler(
454 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_PROXY)); 453 HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_PROXY));
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
568 const HttpAuthCache::Entry* entry = 567 const HttpAuthCache::Entry* entry =
569 cache_.LookupByPath(origin_, GeneratePath(realm_i, path_i)); 568 cache_.LookupByPath(origin_, GeneratePath(realm_i, path_i));
570 if (exists) { 569 if (exists) {
571 EXPECT_FALSE(entry == NULL); 570 EXPECT_FALSE(entry == NULL);
572 EXPECT_EQ(GenerateRealm(realm_i), entry->realm()); 571 EXPECT_EQ(GenerateRealm(realm_i), entry->realm());
573 } else { 572 } else {
574 EXPECT_TRUE(entry == NULL); 573 EXPECT_TRUE(entry == NULL);
575 } 574 }
576 } 575 }
577 576
578 GURL origin_; 577 url::Origin origin_;
579 HttpAuthCache cache_; 578 HttpAuthCache cache_;
580 579
581 static const int kMaxPaths = HttpAuthCache::kMaxNumPathsPerRealmEntry; 580 static const int kMaxPaths = HttpAuthCache::kMaxNumPathsPerRealmEntry;
582 static const int kMaxRealms = HttpAuthCache::kMaxNumRealmEntries; 581 static const int kMaxRealms = HttpAuthCache::kMaxNumRealmEntries;
583 }; 582 };
584 583
585 // Add the maxinim number of realm entries to the cache. Each of these entries 584 // Add the maxinim number of realm entries to the cache. Each of these entries
586 // must still be retrievable. Next add three more entries -- since the cache is 585 // must still be retrievable. Next add three more entries -- since the cache is
587 // full this causes FIFO eviction of the first three entries. 586 // full this causes FIFO eviction of the first three entries.
588 TEST_F(HttpAuthCacheEvictionTest, RealmEntryEviction) { 587 TEST_F(HttpAuthCacheEvictionTest, RealmEntryEviction) {
(...skipping 30 matching lines...) Expand all
619 CheckPathExistence(0, i, false); 618 CheckPathExistence(0, i, false);
620 619
621 for (int i = 0; i < kMaxPaths; ++i) 620 for (int i = 0; i < kMaxPaths; ++i)
622 CheckPathExistence(0, i + 3, true); 621 CheckPathExistence(0, i + 3, true);
623 622
624 for (int i = 0; i < kMaxRealms; ++i) 623 for (int i = 0; i < kMaxRealms; ++i)
625 CheckRealmExistence(i, true); 624 CheckRealmExistence(i, true);
626 } 625 }
627 626
628 } // namespace net 627 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698