From bdc5c44ac28ba9d7b1fe97fcc68131ffd2ad97dd Mon Sep 17 00:00:00 2001 From: "ivankr@chromium.org" Date: Mon, 3 Oct 2011 12:33:35 +0000 Subject: [cros] Transfer proxy HTTP authentication from login screen into new session. BUG=chromium-os:20992 TEST=net_unittests: HttpAuthCacheTest.*; Manual: enable proxies on shared networks, open login screen, set a proxy that requires user authentication, add new user; upon sign-in, no additional username/password prompt should appear. Review URL: http://codereview.chromium.org/8083004 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@103703 0039d316-1c4b-4281-b951-d872f2087c98 --- net/http/http_auth_cache.cc | 17 ++++++++ net/http/http_auth_cache.h | 3 ++ net/http/http_auth_cache_unittest.cc | 85 ++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+) (limited to 'net') diff --git a/net/http/http_auth_cache.cc b/net/http/http_auth_cache.cc index a9e8274..ac50cec 100644 --- a/net/http/http_auth_cache.cc +++ b/net/http/http_auth_cache.cc @@ -225,4 +225,21 @@ bool HttpAuthCache::UpdateStaleChallenge(const GURL& origin, return true; } +void HttpAuthCache::UpdateAllFrom(const HttpAuthCache& other) { + for (EntryList::const_iterator it = other.entries_.begin(); + it != other.entries_.end(); ++it) { + // Add an Entry with one of the original entry's paths. + DCHECK(it->paths_.size() > 0); + Entry* entry = Add(it->origin(), it->realm(), it->scheme(), + it->auth_challenge(), it->username(), it->password(), + it->paths_.back()); + // Copy all other paths. + for (Entry::PathList::const_reverse_iterator it2 = ++it->paths_.rbegin(); + it2 != it->paths_.rend(); ++it2) + entry->AddPath(*it2); + // Copy nonce count (for digest authentication). + entry->nonce_count_ = it->nonce_count_; + } +} + } // namespace net diff --git a/net/http/http_auth_cache.h b/net/http/http_auth_cache.h index 33ce2a5..a5af644 100644 --- a/net/http/http_auth_cache.h +++ b/net/http/http_auth_cache.h @@ -103,6 +103,9 @@ class NET_EXPORT_PRIVATE HttpAuthCache { HttpAuth::Scheme scheme, const std::string& auth_challenge); + // Copies all entries from |other| cache. + void UpdateAllFrom(const HttpAuthCache& other); + private: typedef std::list EntryList; EntryList entries_; diff --git a/net/http/http_auth_cache_unittest.cc b/net/http/http_auth_cache_unittest.cc index b8eb479..96bd2dd 100644 --- a/net/http/http_auth_cache_unittest.cc +++ b/net/http/http_auth_cache_unittest.cc @@ -418,6 +418,91 @@ TEST(HttpAuthCacheTest, UpdateStaleChallenge) { EXPECT_FALSE(update_failure); } +TEST(HttpAuthCacheTest, UpdateAllFrom) { + GURL origin("http://example.com"); + std::string path("/some/path"); + std::string another_path("/another/path"); + + scoped_ptr realm1_handler( + new MockAuthHandler( + HttpAuth::AUTH_SCHEME_BASIC, kRealm1, HttpAuth::AUTH_SERVER)); + + scoped_ptr realm2_handler( + new MockAuthHandler( + HttpAuth::AUTH_SCHEME_BASIC, kRealm2, HttpAuth::AUTH_PROXY)); + + scoped_ptr realm3_digest_handler( + new MockAuthHandler( + HttpAuth::AUTH_SCHEME_DIGEST, kRealm3, HttpAuth::AUTH_SERVER)); + + scoped_ptr realm4_handler( + new MockAuthHandler( + HttpAuth::AUTH_SCHEME_BASIC, kRealm4, HttpAuth::AUTH_SERVER)); + + HttpAuthCache first_cache; + HttpAuthCache::Entry* entry; + + first_cache.Add(origin, realm1_handler->realm(), + realm1_handler->auth_scheme(), "basic realm=Realm1", + kAlice, k123, path); + first_cache.Add(origin, realm2_handler->realm(), + realm2_handler->auth_scheme(), "basic realm=Realm2", + kAlice2, k1234, path); + first_cache.Add(origin, realm3_digest_handler->realm(), + realm3_digest_handler->auth_scheme(), "digest realm=Realm3", + kRoot, kWileCoyote, path); + entry = first_cache.Add( + origin, realm3_digest_handler->realm(), + realm3_digest_handler->auth_scheme(), "digest realm=Realm3", + kRoot, kWileCoyote, another_path); + + EXPECT_EQ(2, entry->IncrementNonceCount()); + + HttpAuthCache second_cache; + // Will be overwritten by kRoot:kWileCoyote. + second_cache.Add(origin, realm3_digest_handler->realm(), + realm3_digest_handler->auth_scheme(), "digest realm=Realm3", + kAlice2, k1234, path); + // Should be left intact. + second_cache.Add(origin, realm4_handler->realm(), + realm4_handler->auth_scheme(), "basic realm=Realm4", + kAdmin, kRoot, path); + + second_cache.UpdateAllFrom(first_cache); + + // Copied from first_cache. + entry = second_cache.Lookup(origin, kRealm1, HttpAuth::AUTH_SCHEME_BASIC); + EXPECT_TRUE(NULL != entry); + EXPECT_EQ(kAlice, entry->username()); + EXPECT_EQ(k123, entry->password()); + + // Copied from first_cache. + entry = second_cache.Lookup(origin, kRealm2, HttpAuth::AUTH_SCHEME_BASIC); + EXPECT_TRUE(NULL != entry); + EXPECT_EQ(kAlice2, entry->username()); + EXPECT_EQ(k1234, entry->password()); + + // Overwritten from first_cache. + entry = second_cache.Lookup(origin, kRealm3, HttpAuth::AUTH_SCHEME_DIGEST); + EXPECT_TRUE(NULL != entry); + EXPECT_EQ(kRoot, entry->username()); + EXPECT_EQ(kWileCoyote, entry->password()); + // Nonce count should get copied. + EXPECT_EQ(3, entry->IncrementNonceCount()); + + // All paths should get copied. + entry = second_cache.LookupByPath(origin, another_path); + EXPECT_TRUE(NULL != entry); + EXPECT_EQ(kRoot, entry->username()); + EXPECT_EQ(kWileCoyote, entry->password()); + + // Left intact in second_cache. + entry = second_cache.Lookup(origin, kRealm4, HttpAuth::AUTH_SCHEME_BASIC); + EXPECT_TRUE(NULL != entry); + EXPECT_EQ(kAdmin, entry->username()); + EXPECT_EQ(kRoot, entry->password()); +} + // Test fixture class for eviction tests (contains helpers for bulk // insertion and existence testing). class HttpAuthCacheEvictionTest : public testing::Test { -- cgit v1.1