#include <string>
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/simple_test_clock.h"
#include "base/test/simple_test_tick_clock.h"
#include "base/time/time.h"
#include "net/base/net_errors.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/base/schemeful_site.h"
#include "net/http/http_auth_cache.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
using base::ASCIIToUTF16;
namespace net {
namespace {
const char kRealm1[] = "Realm1";
const char kRealm2[] = "Realm2";
const char kRealm3[] = "Realm3";
const char kRealm4[] = "Realm4";
const char kRealm5[] = "Realm5";
const std::u16string k123(u"123");
const std::u16string k1234(u"1234");
const std::u16string k12345(u"12345");
const std::u16string kAdmin(u"admin");
const std::u16string kAlice(u"alice");
const std::u16string kAlice2(u"alice2");
const std::u16string kAlice3(u"alice3");
const std::u16string kPassword(u"password");
const std::u16string kRoot(u"root");
const std::u16string kUsername(u"username");
const std::u16string kWileCoyote(u"wilecoyote");
AuthCredentials CreateASCIICredentials(const char* username,
const char* password) {
return AuthCredentials(ASCIIToUTF16(username), ASCIIToUTF16(password));
}
bool DoesUrlMatchFilter(const std::set<std::string>& domains, const GURL& url) {
std::string url_registerable_domain =
registry_controlled_domains::GetDomainAndRegistry(
url, registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
bool found_domain = (domains.find(url_registerable_domain != ""
? url_registerable_domain
: url.GetHost()) != domains.end());
return found_domain;
}
}
TEST(HttpAuthCacheTest, Basic) {
url::SchemeHostPort scheme_host_port(GURL("http://www.google.com"));
url::SchemeHostPort scheme_host_port2(GURL("http://www.foobar.com"));
HttpAuthCache cache(false );
HttpAuthCache::Entry* entry;
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm1",
CreateASCIICredentials("realm1-user", "realm1-password"),
"/foo/bar/index.html");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm2",
CreateASCIICredentials("realm2-user", "realm2-password"),
"/foo2/index.html");
cache.Add(
scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm3",
CreateASCIICredentials("realm3-basic-user", "realm3-basic-password"),
std::string());
cache.Add(
scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey(),
"Digest realm=Realm3",
CreateASCIICredentials("realm3-digest-user", "realm3-digest-password"),
"/baz/index.html");
cache.Add(
scheme_host_port, HttpAuth::AUTH_SERVER, kRealm4,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm4",
CreateASCIICredentials("realm4-basic-user", "realm4-basic-password"),
"/");
cache.Add(scheme_host_port2, HttpAuth::AUTH_SERVER, kRealm5,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm5",
CreateASCIICredentials("realm5-user", "realm5-password"), "/");
cache.Add(
scheme_host_port2, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm3",
CreateASCIICredentials("realm3-basic-user", "realm3-basic-password"),
std::string());
entry = cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm5,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
EXPECT_FALSE(entry);
entry = cache.Lookup(url::SchemeHostPort(GURL("https://www.google.com")),
HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
EXPECT_FALSE(entry);
entry = cache.Lookup(url::SchemeHostPort(GURL("https://www.google.com")),
HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey());
EXPECT_FALSE(entry);
entry = cache.Lookup(url::SchemeHostPort(GURL("http://www.google.com:80")),
HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
ASSERT_TRUE(entry);
EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme());
EXPECT_EQ(kRealm3, entry->realm());
EXPECT_EQ("Basic realm=Realm3", entry->auth_challenge());
EXPECT_EQ(u"realm3-basic-user", entry->credentials().username());
EXPECT_EQ(u"realm3-basic-password", entry->credentials().password());
HttpAuthCache::Entry* entry2 =
cache.Lookup(url::SchemeHostPort(GURL("http://www.foobar.com:80")),
HttpAuth::AUTH_SERVER, kRealm3, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey());
ASSERT_TRUE(entry2);
EXPECT_NE(entry, entry2);
entry = cache.Lookup(url::SchemeHostPort(GURL("http://www.google.com:80")),
HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey());
ASSERT_TRUE(entry);
EXPECT_EQ(HttpAuth::AUTH_SCHEME_DIGEST, entry->scheme());
EXPECT_EQ(kRealm3, entry->realm());
EXPECT_EQ("Digest realm=Realm3", entry->auth_challenge());
EXPECT_EQ(u"realm3-digest-user", entry->credentials().username());
EXPECT_EQ(u"realm3-digest-password", entry->credentials().password());
entry = cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
ASSERT_TRUE(entry);
EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme());
EXPECT_EQ(kRealm2, entry->realm());
EXPECT_EQ("Basic realm=Realm2", entry->auth_challenge());
EXPECT_EQ(u"realm2-user", entry->credentials().username());
EXPECT_EQ(u"realm2-password", entry->credentials().password());
HttpAuthCache::Entry* p_realm2_entry =
cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
HttpAuthCache::Entry* p_realm4_entry =
cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm4,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
EXPECT_TRUE(p_realm2_entry);
EXPECT_TRUE(p_realm4_entry);
HttpAuthCache::Entry realm2_entry = *p_realm2_entry;
HttpAuthCache::Entry realm4_entry = *p_realm4_entry;
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/foo2/index.html");
EXPECT_TRUE(realm2_entry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/foo2/foobar.html");
EXPECT_TRUE(realm2_entry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/foo2/bar/index.html");
EXPECT_TRUE(realm2_entry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/foo2/");
EXPECT_TRUE(realm2_entry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/foo2");
EXPECT_TRUE(realm4_entry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/");
EXPECT_TRUE(realm4_entry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/foo3/index.html");
EXPECT_FALSE(realm2_entry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), std::string());
EXPECT_FALSE(realm2_entry.IsEqualForTesting(*entry));
HttpAuthCache::Entry* p_realm3_digest_entry =
cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey());
EXPECT_TRUE(p_realm3_digest_entry);
HttpAuthCache::Entry realm3_digest_entry = *p_realm3_digest_entry;
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz/index.html");
EXPECT_TRUE(realm3_digest_entry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz/");
EXPECT_TRUE(realm3_digest_entry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz");
EXPECT_FALSE(realm3_digest_entry.IsEqualForTesting(*entry));
HttpAuthCache::Entry* p_realm3DigestEntry =
cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey());
EXPECT_TRUE(p_realm3DigestEntry);
HttpAuthCache::Entry realm3DigestEntry = *p_realm3DigestEntry;
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz/index.html");
EXPECT_TRUE(realm3DigestEntry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz/");
EXPECT_TRUE(realm3DigestEntry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz");
EXPECT_FALSE(realm3DigestEntry.IsEqualForTesting(*entry));
entry = cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), std::string());
EXPECT_TRUE(entry);
EXPECT_EQ(HttpAuth::AUTH_SCHEME_BASIC, entry->scheme());
EXPECT_EQ(kRealm3, entry->realm());
}
TEST(HttpAuthCacheTest, SeparateByTarget) {
const std::u16string kServerUser = u"server_user";
const std::u16string kServerPass = u"server_pass";
const std::u16string kProxyUser = u"proxy_user";
const std::u16string kProxyPass = u"proxy_pass";
const char kServerPath[] = "/foo/bar/index.html";
url::SchemeHostPort scheme_host_port(GURL("http://www.google.com"));
HttpAuthCache cache(false );
HttpAuthCache::Entry* entry;
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm1", AuthCredentials(kServerUser, kServerPass),
kServerPath);
entry = cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kServerUser);
EXPECT_EQ(entry->credentials().password(), kServerPass);
EXPECT_EQ(entry, cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), kServerPath));
EXPECT_FALSE(cache.Lookup(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_FALSE(cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_PROXY,
NetworkAnonymizationKey(), kServerPath));
cache.Add(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm1", AuthCredentials(kProxyUser, kProxyPass), "/");
entry = cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kServerUser);
EXPECT_EQ(entry->credentials().password(), kServerPass);
EXPECT_EQ(entry, cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), kServerPath));
entry = cache.Lookup(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kProxyUser);
EXPECT_EQ(entry->credentials().password(), kProxyPass);
EXPECT_EQ(entry, cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_PROXY,
NetworkAnonymizationKey(), "/"));
EXPECT_TRUE(cache.Remove(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(),
AuthCredentials(kServerUser, kServerPass)));
EXPECT_FALSE(cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_FALSE(cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), kServerPath));
entry = cache.Lookup(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kProxyUser);
EXPECT_EQ(entry->credentials().password(), kProxyPass);
EXPECT_EQ(entry, cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_PROXY,
NetworkAnonymizationKey(), "/"));
EXPECT_TRUE(cache.Remove(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(),
AuthCredentials(kProxyUser, kProxyPass)));
EXPECT_FALSE(cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_FALSE(cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), kServerPath));
EXPECT_FALSE(cache.Lookup(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_FALSE(cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_PROXY,
NetworkAnonymizationKey(), "/"));
}
TEST(HttpAuthCacheTest, SeparateServersByNetworkAnonymizationKey) {
const SchemefulSite kSite1(GURL("https://foo.test/"));
auto kNetworkAnonymizationKey1 =
NetworkAnonymizationKey::CreateSameSite(kSite1);
const SchemefulSite kSite2(GURL("https://bar.test/"));
auto kNetworkAnonymizationKey2 =
NetworkAnonymizationKey::CreateSameSite(kSite2);
url::SchemeHostPort kSchemeHostPort(GURL("http://www.google.com"));
const char kPath[] = "/";
const std::u16string kUser1 = u"user1";
const std::u16string kPass1 = u"pass1";
const std::u16string kUser2 = u"user2";
const std::u16string kPass2 = u"pass2";
for (bool key_entries_by_network_anonymization_key : {false, true}) {
HttpAuthCache cache(key_entries_by_network_anonymization_key);
HttpAuthCache::Entry* entry;
cache.Add(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey1,
"Basic realm=Realm1", AuthCredentials(kUser1, kPass1), kPath);
entry =
cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey1);
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kUser1);
EXPECT_EQ(entry->credentials().password(), kPass1);
EXPECT_EQ(entry, cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_SERVER,
kNetworkAnonymizationKey1, kPath));
if (key_entries_by_network_anonymization_key) {
EXPECT_FALSE(cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
kNetworkAnonymizationKey2));
EXPECT_FALSE(cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_SERVER,
kNetworkAnonymizationKey2, kPath));
} else {
EXPECT_EQ(entry, cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_SERVER,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
kNetworkAnonymizationKey2));
EXPECT_EQ(entry,
cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_SERVER,
kNetworkAnonymizationKey2, kPath));
}
cache.Add(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey2,
"Basic realm=Realm1", AuthCredentials(kUser2, kPass2), kPath);
entry =
cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey2);
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kUser2);
EXPECT_EQ(entry->credentials().password(), kPass2);
EXPECT_EQ(entry, cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_SERVER,
kNetworkAnonymizationKey2, kPath));
entry =
cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey1);
ASSERT_TRUE(entry);
EXPECT_EQ(entry, cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_SERVER,
kNetworkAnonymizationKey1, kPath));
if (key_entries_by_network_anonymization_key) {
EXPECT_EQ(entry->credentials().username(), kUser1);
EXPECT_EQ(entry->credentials().password(), kPass1);
} else {
EXPECT_EQ(entry->credentials().username(), kUser2);
EXPECT_EQ(entry->credentials().password(), kPass2);
}
EXPECT_TRUE(cache.Remove(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
kNetworkAnonymizationKey2,
AuthCredentials(kUser2, kPass2)));
EXPECT_FALSE(cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
kNetworkAnonymizationKey2));
EXPECT_FALSE(cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_SERVER,
kNetworkAnonymizationKey2, kPath));
if (key_entries_by_network_anonymization_key) {
entry =
cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey1);
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kUser1);
EXPECT_EQ(entry->credentials().password(), kPass1);
EXPECT_EQ(entry,
cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_SERVER,
kNetworkAnonymizationKey1, kPath));
} else {
EXPECT_FALSE(cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
kNetworkAnonymizationKey1));
EXPECT_FALSE(cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_SERVER,
kNetworkAnonymizationKey1, kPath));
}
}
}
TEST(HttpAuthCacheTest, NeverSeparateProxiesByNetworkAnonymizationKey) {
const SchemefulSite kSite1(GURL("https://foo.test/"));
auto kNetworkAnonymizationKey1 =
NetworkAnonymizationKey::CreateSameSite(kSite1);
const SchemefulSite kSite2(GURL("https://bar.test/"));
auto kNetworkAnonymizationKey2 =
NetworkAnonymizationKey::CreateSameSite(kSite2);
url::SchemeHostPort kSchemeHostPort(GURL("http://www.google.com"));
const char kPath[] = "/";
const std::u16string kUser1 = u"user1";
const std::u16string kPass1 = u"pass1";
const std::u16string kUser2 = u"user2";
const std::u16string kPass2 = u"pass2";
for (bool key_entries_by_network_anonymization_key : {false, true}) {
HttpAuthCache cache(key_entries_by_network_anonymization_key);
HttpAuthCache::Entry* entry;
cache.Add(kSchemeHostPort, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey1,
"Basic realm=Realm1", AuthCredentials(kUser1, kPass1), kPath);
entry =
cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey1);
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kUser1);
EXPECT_EQ(entry->credentials().password(), kPass1);
EXPECT_EQ(entry, cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_PROXY,
kNetworkAnonymizationKey1, kPath));
EXPECT_EQ(entry, cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_PROXY,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
kNetworkAnonymizationKey2));
EXPECT_EQ(entry, cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_PROXY,
kNetworkAnonymizationKey2, kPath));
cache.Add(kSchemeHostPort, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey2,
"Basic realm=Realm1", AuthCredentials(kUser2, kPass2), kPath);
entry =
cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, kNetworkAnonymizationKey2);
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kUser2);
EXPECT_EQ(entry->credentials().password(), kPass2);
EXPECT_EQ(entry, cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_PROXY,
kNetworkAnonymizationKey2, kPath));
EXPECT_EQ(entry, cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_PROXY,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
kNetworkAnonymizationKey1));
EXPECT_EQ(entry, cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_PROXY,
kNetworkAnonymizationKey1, kPath));
EXPECT_TRUE(cache.Remove(kSchemeHostPort, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(),
AuthCredentials(kUser2, kPass2)));
EXPECT_FALSE(cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
kNetworkAnonymizationKey2));
EXPECT_FALSE(cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_PROXY,
kNetworkAnonymizationKey2, kPath));
EXPECT_FALSE(cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
kNetworkAnonymizationKey1));
EXPECT_FALSE(cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_PROXY,
kNetworkAnonymizationKey1, kPath));
}
}
TEST(HttpAuthCacheTest, SetKeyServerEntriesByNetworkAnonymizationKey) {
const url::SchemeHostPort kSchemeHostPort(GURL("http://www.google.com"));
const char kPath[] = "/";
const std::u16string kUser1 = u"user1";
const std::u16string kPass1 = u"pass1";
const std::u16string kUser2 = u"user2";
const std::u16string kPass2 = u"pass2";
for (bool initially_key_entries_by_network_anonymization_key :
{false, true}) {
for (bool to_key_entries_by_network_anonymization_key : {false, true}) {
HttpAuthCache cache(initially_key_entries_by_network_anonymization_key);
EXPECT_EQ(initially_key_entries_by_network_anonymization_key,
cache.key_server_entries_by_network_anonymization_key());
cache.Add(kSchemeHostPort, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm1", AuthCredentials(kUser1, kPass1), kPath);
cache.Add(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"Basic realm=Realm1", AuthCredentials(kUser2, kPass2), kPath);
EXPECT_TRUE(cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_TRUE(cache.Lookup(kSchemeHostPort, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
cache.SetKeyServerEntriesByNetworkAnonymizationKey(
to_key_entries_by_network_anonymization_key);
EXPECT_EQ(to_key_entries_by_network_anonymization_key,
cache.key_server_entries_by_network_anonymization_key());
HttpAuthCache::Entry* entry =
cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_PROXY,
NetworkAnonymizationKey(), kPath);
ASSERT_TRUE(entry);
EXPECT_EQ(entry->credentials().username(), kUser1);
EXPECT_EQ(entry->credentials().password(), kPass1);
entry = cache.LookupByPath(kSchemeHostPort, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), kPath);
EXPECT_EQ(initially_key_entries_by_network_anonymization_key ==
to_key_entries_by_network_anonymization_key,
!!entry);
if (entry) {
EXPECT_EQ(entry->credentials().username(), kUser2);
EXPECT_EQ(entry->credentials().password(), kPass2);
}
}
}
}
TEST(HttpAuthCacheTest, AddPath) {
HttpAuthCache::Entry entry;
entry.AddPath("/1/2/3/4/5/x.txt");
entry.AddPath("/1/2/3/4/5/y.txt");
entry.AddPath("/1/2/3/4/5/z.txt");
EXPECT_EQ(1U, entry.paths_.size());
EXPECT_EQ("/1/2/3/4/5/", entry.paths_.front());
entry.AddPath("/1/XXX/q");
EXPECT_EQ(2U, entry.paths_.size());
EXPECT_EQ("/1/XXX/", entry.paths_.front());
EXPECT_EQ("/1/2/3/4/5/", entry.paths_.back());
entry.AddPath("/1/2/3/4/x.txt");
EXPECT_EQ(2U, entry.paths_.size());
EXPECT_EQ("/1/2/3/4/", entry.paths_.front());
EXPECT_EQ("/1/XXX/", entry.paths_.back());
entry.AddPath("/1/2/3/x");
EXPECT_EQ(2U, entry.paths_.size());
EXPECT_EQ("/1/2/3/", entry.paths_.front());
EXPECT_EQ("/1/XXX/", entry.paths_.back());
entry.AddPath("/index.html");
EXPECT_EQ(1U, entry.paths_.size());
EXPECT_EQ("/", entry.paths_.front());
}
TEST(HttpAuthCacheTest, AddToExistingEntry) {
HttpAuthCache cache(false );
url::SchemeHostPort scheme_host_port(GURL("http://www.foobar.com:70"));
const std::string kAuthChallenge = "Basic realm=MyRealm";
const std::string kRealm = "MyRealm";
HttpAuthCache::Entry* orig_entry = cache.Add(
scheme_host_port, HttpAuth::AUTH_SERVER, kRealm,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(), kAuthChallenge,
CreateASCIICredentials("user1", "password1"), "/x/y/z/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
kAuthChallenge, CreateASCIICredentials("user2", "password2"),
"/z/y/x/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
kAuthChallenge, CreateASCIICredentials("user3", "password3"),
"/z/y");
HttpAuthCache::Entry* entry =
cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
EXPECT_TRUE(entry == orig_entry);
EXPECT_EQ(u"user3", entry->credentials().username());
EXPECT_EQ(u"password3", entry->credentials().password());
EXPECT_EQ(2U, entry->paths_.size());
EXPECT_EQ("/z/", entry->paths_.front());
EXPECT_EQ("/x/y/z/", entry->paths_.back());
}
TEST(HttpAuthCacheTest, Remove) {
url::SchemeHostPort scheme_host_port(GURL("http://foobar2.com"));
HttpAuthCache cache(false );
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm1", AuthCredentials(kAlice, k123), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm2", CreateASCIICredentials("bob", "princess"),
"/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm3", AuthCredentials(kAdmin, kPassword), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey(),
"digest realm=Realm3", AuthCredentials(kRoot, kWileCoyote), "/");
EXPECT_FALSE(cache.Remove(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm5,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(),
AuthCredentials(kAlice, k123)));
EXPECT_FALSE(
cache.Remove(url::SchemeHostPort(GURL("http://foobar2.com:100")),
HttpAuth::AUTH_SERVER, kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(), AuthCredentials(kAlice, k123)));
EXPECT_FALSE(cache.Remove(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(),
AuthCredentials(kAlice2, k123)));
EXPECT_FALSE(cache.Remove(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(),
AuthCredentials(kAlice, k1234)));
EXPECT_FALSE(cache.Remove(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_DIGEST,
NetworkAnonymizationKey(),
AuthCredentials(kAlice, k123)));
EXPECT_TRUE(cache.Remove(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(),
AuthCredentials(kAlice, k123)));
EXPECT_FALSE(cache.Remove(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(),
AuthCredentials(kAlice, k123)));
EXPECT_TRUE(cache.Remove(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST,
NetworkAnonymizationKey(),
AuthCredentials(kRoot, kWileCoyote)));
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey(),
"digest realm=Realm3", AuthCredentials(kRoot, kWileCoyote), "/");
EXPECT_TRUE(cache.Remove(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey(),
AuthCredentials(kAdmin, kPassword)));
HttpAuthCache::Entry* entry =
cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey());
EXPECT_FALSE(nullptr == entry);
}
TEST(HttpAuthCacheTest, ClearEntriesAddedBetweenNothingToClear) {
HttpAuthCache cache(false );
EXPECT_FALSE(cache.ClearEntriesAddedBetween(
base::Time::Min(), base::Time::Max(),
base::RepeatingCallback<bool(const GURL&)>()));
}
TEST(HttpAuthCacheTest, ClearAllEntriesNothingToClear) {
HttpAuthCache cache(false );
EXPECT_FALSE(cache.ClearAllEntries());
}
TEST(HttpAuthCacheTest, ClearEntriesAddedBetween) {
url::SchemeHostPort scheme_host_port(GURL("http://foobar.com"));
base::Time start_time;
ASSERT_TRUE(base::Time::FromString("30 May 2018 12:00:00", &start_time));
base::SimpleTestClock test_clock;
test_clock.SetNow(start_time);
HttpAuthCache cache(false );
cache.set_clock_for_testing(&test_clock);
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm1", AuthCredentials(kAlice, k123), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm2", AuthCredentials(kRoot, kWileCoyote), "/");
test_clock.Advance(base::Seconds(10));
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm3", AuthCredentials(kAlice2, k1234), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm4,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm4", AuthCredentials(kUsername, kPassword), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm2", AuthCredentials(kAdmin, kPassword), "/baz/");
test_clock.Advance(base::Seconds(10));
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm5,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm5", AuthCredentials(kAlice3, k12345), "/");
base::Time test_time1;
ASSERT_TRUE(base::Time::FromString("30 May 2018 12:00:05", &test_time1));
base::Time test_time2;
ASSERT_TRUE(base::Time::FromString("30 May 2018 12:00:15", &test_time2));
EXPECT_TRUE(cache.ClearEntriesAddedBetween(
test_time1, test_time2, base::RepeatingCallback<bool(const GURL&)>()));
EXPECT_NE(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_NE(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm2, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_NE(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm5, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_NE(nullptr, cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz/"));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm3, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm4, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_FALSE(cache.ClearEntriesAddedBetween(
test_time1, test_time2, base::RepeatingCallback<bool(const GURL&)>()));
EXPECT_TRUE(cache.ClearEntriesAddedBetween(
start_time - base::Seconds(1), base::Time::Max(),
base::RepeatingCallback<bool(const GURL&)>()));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm2, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_EQ(nullptr, cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz/"));
EXPECT_FALSE(cache.ClearEntriesAddedBetween(
start_time - base::Seconds(1), base::Time::Max(),
base::RepeatingCallback<bool(const GURL&)>()));
}
TEST(HttpAuthCacheTest, ClearEntriesAddedBetweenByFilter) {
url::SchemeHostPort scheme_host_port_1(GURL("http://foobar.com"));
url::SchemeHostPort scheme_host_port_2(GURL("http://foobar2.com"));
base::SimpleTestClock test_clock;
test_clock.SetNow(base::Time::Now());
HttpAuthCache cache(false );
cache.set_clock_for_testing(&test_clock);
cache.Add(scheme_host_port_1, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm1", AuthCredentials(kAlice, k123), "/");
cache.Add(scheme_host_port_2, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm1", AuthCredentials(kRoot, kWileCoyote), "/");
EXPECT_TRUE(cache.ClearEntriesAddedBetween(
base::Time::Min(), base::Time::Max(),
base::BindRepeating(&DoesUrlMatchFilter,
std::set<std::string>({scheme_host_port_1.host()}))));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port_1, HttpAuth::AUTH_SERVER,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_NE(nullptr, cache.Lookup(scheme_host_port_2, HttpAuth::AUTH_SERVER,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_FALSE(cache.ClearEntriesAddedBetween(
base::Time::Min(), base::Time::Max(),
base::BindRepeating(&DoesUrlMatchFilter,
std::set<std::string>({scheme_host_port_1.host()}))));
}
TEST(HttpAuthCacheTest, ClearEntriesAddedBetweenWithAllTimeValues) {
url::SchemeHostPort scheme_host_port(GURL("http://foobar.com"));
base::SimpleTestClock test_clock;
test_clock.SetNow(base::Time::Now());
HttpAuthCache cache(false );
cache.set_clock_for_testing(&test_clock);
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm1", AuthCredentials(kAlice, k123), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm2", AuthCredentials(kRoot, kWileCoyote), "/");
test_clock.Advance(base::Seconds(10));
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm3", AuthCredentials(kAlice2, k1234), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm4,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm4", AuthCredentials(kUsername, kPassword), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm2", AuthCredentials(kAdmin, kPassword), "/baz/");
EXPECT_TRUE(cache.ClearEntriesAddedBetween(
base::Time::Min(), base::Time::Max(),
base::RepeatingCallback<bool(const GURL&)>()));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm2, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_EQ(nullptr, cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz/"));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm3, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm4, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_FALSE(cache.ClearEntriesAddedBetween(
base::Time::Min(), base::Time::Max(),
base::RepeatingCallback<bool(const GURL&)>()));
}
TEST(HttpAuthCacheTest, ClearAllEntries) {
url::SchemeHostPort scheme_host_port(GURL("http://foobar.com"));
base::SimpleTestClock test_clock;
test_clock.SetNow(base::Time::Now());
HttpAuthCache cache(false );
cache.set_clock_for_testing(&test_clock);
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm1", AuthCredentials(kAlice, k123), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm2", AuthCredentials(kRoot, kWileCoyote), "/");
test_clock.Advance(base::Seconds(10));
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm3,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm3", AuthCredentials(kAlice2, k1234), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm4,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm4", AuthCredentials(kUsername, kPassword), "/");
cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm2", AuthCredentials(kAdmin, kPassword), "/baz/");
test_clock.Advance(base::Seconds(55));
EXPECT_TRUE(cache.ClearAllEntries());
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm2, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_EQ(nullptr, cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_SERVER,
NetworkAnonymizationKey(), "/baz/"));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm3, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_EQ(nullptr, cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm4, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_FALSE(cache.ClearAllEntries());
}
TEST(HttpAuthCacheTest, UpdateStaleChallenge) {
HttpAuthCache cache(false );
url::SchemeHostPort scheme_host_port(GURL("http://foobar2.com"));
HttpAuthCache::Entry* entry_pre = cache.Add(
scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey(),
"Digest realm=Realm1,"
"nonce=\"s3MzvFhaBAA=4c520af5acd9d8d7ae26947529d18c8eae1e98f4\"",
CreateASCIICredentials("realm-digest-user", "realm-digest-password"),
"/baz/index.html");
ASSERT_TRUE(entry_pre != nullptr);
EXPECT_EQ(2, entry_pre->IncrementNonceCount());
EXPECT_EQ(3, entry_pre->IncrementNonceCount());
EXPECT_EQ(4, entry_pre->IncrementNonceCount());
bool update_success = cache.UpdateStaleChallenge(
scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey(),
"Digest realm=Realm1,"
"nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\","
"stale=\"true\"");
EXPECT_TRUE(update_success);
HttpAuthCache::Entry* entry_post =
cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey());
ASSERT_TRUE(entry_post != nullptr);
EXPECT_EQ(2, entry_post->IncrementNonceCount());
bool update_failure = cache.UpdateStaleChallenge(
scheme_host_port, HttpAuth::AUTH_SERVER, kRealm2,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey(),
"Digest realm=Realm2,"
"nonce=\"claGgoRXBAA=7583377687842fdb7b56ba0555d175baa0b800e3\","
"stale=\"true\"");
EXPECT_FALSE(update_failure);
}
TEST(HttpAuthCacheTest, CopyProxyEntriesFrom) {
url::SchemeHostPort scheme_host_port(GURL("http://example.com"));
std::string path("/some/path");
std::string another_path("/another/path");
HttpAuthCache first_cache(
false );
HttpAuthCache::Entry* entry;
first_cache.Add(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm1", AuthCredentials(kAlice, k123), path);
first_cache.Add(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm2", AuthCredentials(kAlice2, k1234), path);
first_cache.Add(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey(),
"digest realm=Realm3", AuthCredentials(kRoot, kWileCoyote),
path);
entry = first_cache.Add(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST,
NetworkAnonymizationKey(), "digest realm=Realm3",
AuthCredentials(kRoot, kWileCoyote), another_path);
EXPECT_EQ(2, entry->IncrementNonceCount());
first_cache.Add(scheme_host_port, HttpAuth::AUTH_SERVER, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm1", AuthCredentials(kAlice, k123), path);
HttpAuthCache second_cache(
false );
second_cache.Add(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST, NetworkAnonymizationKey(),
"digest realm=Realm3", AuthCredentials(kAlice2, k1234),
path);
second_cache.Add(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm4,
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
"basic realm=Realm4", AuthCredentials(kAdmin, kRoot), path);
second_cache.CopyProxyEntriesFrom(first_cache);
entry = second_cache.Lookup(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm1,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey());
EXPECT_TRUE(nullptr != entry);
EXPECT_EQ(kAlice, entry->credentials().username());
EXPECT_EQ(k123, entry->credentials().password());
entry = second_cache.Lookup(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm2,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey());
EXPECT_TRUE(nullptr != entry);
EXPECT_EQ(kAlice2, entry->credentials().username());
EXPECT_EQ(k1234, entry->credentials().password());
entry = second_cache.Lookup(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm3,
HttpAuth::AUTH_SCHEME_DIGEST,
NetworkAnonymizationKey());
EXPECT_TRUE(nullptr != entry);
EXPECT_EQ(kRoot, entry->credentials().username());
EXPECT_EQ(kWileCoyote, entry->credentials().password());
EXPECT_EQ(3, entry->IncrementNonceCount());
entry = second_cache.LookupByPath(scheme_host_port, HttpAuth::AUTH_PROXY,
NetworkAnonymizationKey(), another_path);
EXPECT_TRUE(nullptr != entry);
EXPECT_EQ(kRoot, entry->credentials().username());
EXPECT_EQ(kWileCoyote, entry->credentials().password());
entry = second_cache.Lookup(scheme_host_port, HttpAuth::AUTH_PROXY, kRealm4,
HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey());
EXPECT_TRUE(nullptr != entry);
EXPECT_EQ(kAdmin, entry->credentials().username());
EXPECT_EQ(kRoot, entry->credentials().password());
EXPECT_TRUE(first_cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
EXPECT_FALSE(second_cache.Lookup(scheme_host_port, HttpAuth::AUTH_SERVER,
kRealm1, HttpAuth::AUTH_SCHEME_BASIC,
NetworkAnonymizationKey()));
}
class HttpAuthCacheEvictionTest : public testing::Test {
protected:
HttpAuthCacheEvictionTest()
: scheme_host_port_(GURL("http://www.google.com")),
cache_(false ) {}
std::string GenerateRealm(int realm_i) {
return base::StringPrintf("Realm %d", realm_i);
}
std::string GeneratePath(int realm_i, int path_i) {
return base::StringPrintf("/%d/%d/x/y", realm_i, path_i);
}
void AddRealm(int realm_i) {
AddPathToRealm(realm_i, 0);
}
void AddPathToRealm(int realm_i, int path_i) {
cache_.Add(scheme_host_port_, HttpAuth::AUTH_SERVER, GenerateRealm(realm_i),
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey(),
std::string(), AuthCredentials(kUsername, kPassword),
GeneratePath(realm_i, path_i));
}
void CheckRealmExistence(int realm_i, bool exists) {
const HttpAuthCache::Entry* entry = cache_.Lookup(
scheme_host_port_, HttpAuth::AUTH_SERVER, GenerateRealm(realm_i),
HttpAuth::AUTH_SCHEME_BASIC, NetworkAnonymizationKey());
if (exists) {
EXPECT_FALSE(entry == nullptr);
EXPECT_EQ(GenerateRealm(realm_i), entry->realm());
} else {
EXPECT_TRUE(entry == nullptr);
}
}
void CheckPathExistence(int realm_i, int path_i, bool exists) {
const HttpAuthCache::Entry* entry = cache_.LookupByPath(
scheme_host_port_, HttpAuth::AUTH_SERVER, NetworkAnonymizationKey(),
GeneratePath(realm_i, path_i));
if (exists) {
EXPECT_FALSE(entry == nullptr);
EXPECT_EQ(GenerateRealm(realm_i), entry->realm());
} else {
EXPECT_TRUE(entry == nullptr);
}
}
url::SchemeHostPort scheme_host_port_;
HttpAuthCache cache_;
static const int kMaxPaths = HttpAuthCache::kMaxNumPathsPerRealmEntry;
static const int kMaxRealms = HttpAuthCache::kMaxNumRealmEntries;
};
TEST_F(HttpAuthCacheEvictionTest, RealmEntryEviction) {
base::SimpleTestTickClock test_clock;
test_clock.SetNowTicks(base::TimeTicks::Now());
cache_.set_tick_clock_for_testing(&test_clock);
for (int i = 0; i < kMaxRealms; ++i) {
AddRealm(i);
test_clock.Advance(base::Seconds(1));
}
for (int i = 0; i < kMaxRealms; ++i) {
CheckRealmExistence(i, true);
test_clock.Advance(base::Seconds(1));
}
for (int i = 0; i < 3; ++i) {
AddRealm(i + kMaxRealms);
test_clock.Advance(base::Seconds(1));
}
for (int i = 0; i < 3; ++i) {
CheckRealmExistence(i, false);
test_clock.Advance(base::Seconds(1));
}
for (int i = 0; i < kMaxRealms; ++i) {
CheckRealmExistence(i + 3, true);
test_clock.Advance(base::Seconds(1));
}
cache_.set_tick_clock_for_testing(nullptr);
}
TEST_F(HttpAuthCacheEvictionTest, RealmPathEviction) {
for (int i = 0; i < kMaxPaths; ++i)
AddPathToRealm(0, i);
for (int i = 1; i < kMaxRealms; ++i)
AddRealm(i);
for (int i = 0; i < 3; ++i)
AddPathToRealm(0, i + kMaxPaths);
for (int i = 0; i < 3; ++i)
CheckPathExistence(0, i, false);
for (int i = 0; i < kMaxPaths; ++i)
CheckPathExistence(0, i + 3, true);
for (int i = 0; i < kMaxRealms; ++i)
CheckRealmExistence(i, true);
}
}