#include "net/dns/host_cache.h"
#include <algorithm>
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/json/json_writer.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/values.h"
#include "net/base/connection_endpoint_metadata.h"
#include "net/base/host_port_pair.h"
#include "net/base/ip_address.h"
#include "net/base/ip_endpoint.h"
#include "net/base/network_anonymization_key.h"
#include "net/base/schemeful_site.h"
#include "net/dns/host_resolver_internal_result.h"
#include "net/dns/host_resolver_results_test_util.h"
#include "net/dns/https_record_rdata.h"
#include "net/dns/public/host_resolver_results.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/scheme_host_port.h"
#include "url/url_constants.h"
using ::testing::_;
using ::testing::ElementsAre;
using ::testing::ElementsAreArray;
using ::testing::IsEmpty;
using ::testing::Optional;
using ::testing::Pair;
using ::testing::Pointee;
using ::testing::Property;
using ::testing::UnorderedElementsAre;
namespace net {
namespace {
const int kMaxCacheEntries = 10;
HostCache::Key Key(const std::string& hostname) {
return HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, hostname, 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
}
bool FoobarIndexIsOdd(const std::string& foobarx_com) {
return (foobarx_com[6] - '0') % 2 == 1;
}
class MockPersistenceDelegate : public HostCache::PersistenceDelegate {
public:
void ScheduleWrite() override { ++num_changes_; }
int num_changes() const { return num_changes_; }
private:
int num_changes_ = 0;
};
MATCHER_P(EntryContentsEqual,
entry,
base::StrCat({"contents ", negation ? "!=" : "==", " contents of ",
testing::PrintToString(entry)})) {
return arg.ContentsEqual(entry);
}
IPAddress MakeIP(std::string_view literal) {
IPAddress ret;
CHECK(ret.AssignFromIPLiteral(literal));
return ret;
}
std::vector<IPEndPoint> MakeEndpoints(std::vector<std::string> my_addresses) {
std::vector<IPEndPoint> out(my_addresses.size());
std::ranges::transform(my_addresses, out.begin(),
[](auto& s) { return IPEndPoint(MakeIP(s), 0); });
return out;
}
}
TEST(HostCacheTest, Basic) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Entry entry =
HostCache::Entry(OK, {}, {"foobar.com"},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0U, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key1, now)->second.error() == entry.error());
EXPECT_EQ(1U, cache.size());
now += base::Seconds(5);
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2U, cache.size());
now += base::Seconds(4);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now));
now += base::Seconds(1);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
cache.Set(key1, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(2U, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
now += base::Seconds(10);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
}
TEST(HostCacheTest, GetEndpoints) {
std::vector<IPEndPoint> ip_endpoints = {IPEndPoint(IPAddress(1, 1, 1, 1), 0),
IPEndPoint(IPAddress(2, 2, 2, 2), 0)};
HostCache::Entry entry(OK, ip_endpoints, {},
HostCache::Entry::SOURCE_DNS);
EXPECT_THAT(entry.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ip_endpoints)));
}
TEST(HostCacheTest, GetEmptyEndpoints) {
HostCache::Entry entry(ERR_NAME_NOT_RESOLVED, {},
{}, HostCache::Entry::SOURCE_DNS);
EXPECT_THAT(entry.GetEndpoints(), IsEmpty());
}
TEST(HostCacheTest, GetEmptyEndpointsWithMetadata) {
HostCache::Entry entry(ERR_NAME_NOT_RESOLVED, {},
{}, HostCache::Entry::SOURCE_DNS);
ConnectionEndpointMetadata metadata;
metadata.supported_protocol_alpns = {"h3", "h2"};
HostCache::Entry metadata_entry(
OK,
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>{
{1u, metadata}},
HostCache::Entry::SOURCE_DNS);
auto merged_entry = HostCache::Entry::MergeEntries(entry, metadata_entry);
EXPECT_THAT(merged_entry.GetEndpoints(), IsEmpty());
}
TEST(HostCacheTest, GetMissingEndpoints) {
HostCache::Entry entry(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS);
EXPECT_THAT(entry.GetEndpoints(), IsEmpty());
}
TEST(HostCacheTest, GetMissingEndpointsWithMetadata) {
HostCache::Entry entry(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS);
ConnectionEndpointMetadata metadata;
metadata.supported_protocol_alpns = {"h3", "h2"};
HostCache::Entry metadata_entry(
OK,
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>{
{1u, metadata}},
HostCache::Entry::SOURCE_DNS);
auto merged_entry = HostCache::Entry::MergeEntries(entry, metadata_entry);
EXPECT_THAT(merged_entry.GetEndpoints(), IsEmpty());
}
TEST(HostCacheTest, HandlesKeysWithoutScheme) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key("host1.test", DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, NetworkAnonymizationKey());
HostCache::Key key_with_scheme(
url::SchemeHostPort(url::kHttpsScheme, "host1.test", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
ASSERT_NE(key, key_with_scheme);
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
ASSERT_EQ(0U, cache.size());
ASSERT_FALSE(cache.Lookup(key, now));
ASSERT_FALSE(cache.Lookup(key_with_scheme, now));
cache.Set(key, entry, now, kTTL);
EXPECT_EQ(1U, cache.size());
EXPECT_TRUE(cache.Lookup(key, now));
EXPECT_FALSE(cache.Lookup(key_with_scheme, now));
cache.Set(key_with_scheme, entry, now, kTTL);
EXPECT_EQ(2U, cache.size());
EXPECT_TRUE(cache.Lookup(key, now));
EXPECT_TRUE(cache.Lookup(key_with_scheme, now));
cache.clear();
ASSERT_EQ(0U, cache.size());
ASSERT_FALSE(cache.Lookup(key, now));
ASSERT_FALSE(cache.Lookup(key_with_scheme, now));
cache.Set(key_with_scheme, entry, now, kTTL);
EXPECT_EQ(1U, cache.size());
EXPECT_FALSE(cache.Lookup(key, now));
EXPECT_TRUE(cache.Lookup(key_with_scheme, now));
cache.Set(key, entry, now, kTTL);
EXPECT_EQ(2U, cache.size());
EXPECT_TRUE(cache.Lookup(key, now));
EXPECT_TRUE(cache.Lookup(key_with_scheme, now));
}
TEST(HostCacheTest, NetworkAnonymizationKey) {
const url::SchemeHostPort kHost(url::kHttpsScheme, "hostname.test", 443);
const base::TimeDelta kTTL = base::Seconds(10);
const SchemefulSite kSite1(GURL("https://site1.test/"));
const auto kNetworkAnonymizationKey1 =
NetworkAnonymizationKey::CreateSameSite(kSite1);
const SchemefulSite kSite2(GURL("https://site2.test/"));
const auto kNetworkAnonymizationKey2 =
NetworkAnonymizationKey::CreateSameSite(kSite2);
HostCache::Key key1(kHost, DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, kNetworkAnonymizationKey1);
HostCache::Key key2(kHost, DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, kNetworkAnonymizationKey2);
HostCache::Entry entry1 =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
HostCache::Entry entry2 =
HostCache::Entry(ERR_FAILED, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
EXPECT_EQ(0U, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry1, now, kTTL);
const std::pair<const HostCache::Key, HostCache::Entry>* result =
cache.Lookup(key1, now);
ASSERT_TRUE(result);
EXPECT_EQ(kNetworkAnonymizationKey1, result->first.network_anonymization_key);
EXPECT_EQ(OK, result->second.error());
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_EQ(1U, cache.size());
cache.Set(key2, entry2, now, 3 * kTTL);
result = cache.Lookup(key1, now);
ASSERT_TRUE(result);
EXPECT_EQ(kNetworkAnonymizationKey1, result->first.network_anonymization_key);
EXPECT_EQ(OK, result->second.error());
result = cache.Lookup(key2, now);
ASSERT_TRUE(result);
EXPECT_EQ(kNetworkAnonymizationKey2, result->first.network_anonymization_key);
EXPECT_EQ(ERR_FAILED, result->second.error());
EXPECT_EQ(2U, cache.size());
now += 2 * kTTL;
EXPECT_FALSE(cache.Lookup(key1, now));
result = cache.Lookup(key2, now);
ASSERT_TRUE(result);
EXPECT_EQ(kNetworkAnonymizationKey2, result->first.network_anonymization_key);
EXPECT_EQ(ERR_FAILED, result->second.error());
}
TEST(HostCacheTest, NoCacheZeroTTL) {
const base::TimeDelta kSuccessEntryTTL = base::Seconds(10);
const base::TimeDelta kFailureEntryTTL = base::Seconds(0);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kFailureEntryTTL);
EXPECT_EQ(1U, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kFailureEntryTTL);
EXPECT_FALSE(cache.Lookup(key1, now));
}
TEST(HostCacheTest, CacheNegativeEntry) {
const base::TimeDelta kFailureEntryTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0U, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kFailureEntryTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(1U, cache.size());
now += base::Seconds(5);
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry, now, kFailureEntryTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2U, cache.size());
now += base::Seconds(4);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
now += base::Seconds(1);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
cache.Set(key1, entry, now, kFailureEntryTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(2U, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
now += base::Seconds(10);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
}
TEST(HostCacheTest, DnsQueryTypeIsPartOfKey) {
const base::TimeDelta kSuccessEntryTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key1(url::SchemeHostPort(url::kHttpScheme, "foobar.com", 80),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
HostCache::Key key2(url::SchemeHostPort(url::kHttpScheme, "foobar.com", 80),
DnsQueryType::A, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0U, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(1U, cache.size());
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2U, cache.size());
EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now));
}
TEST(HostCacheTest, HostResolverFlagsArePartOfKey) {
const url::SchemeHostPort kHost(url::kHttpsScheme, "foobar.test", 443);
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key1(kHost, DnsQueryType::A, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
HostCache::Key key2(kHost, DnsQueryType::A, HOST_RESOLVER_CANONNAME,
HostResolverSource::ANY, NetworkAnonymizationKey());
HostCache::Key key3(kHost, DnsQueryType::A, HOST_RESOLVER_LOOPBACK_ONLY,
HostResolverSource::ANY, NetworkAnonymizationKey());
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0U, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(1U, cache.size());
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2U, cache.size());
EXPECT_FALSE(cache.Lookup(key3, now));
cache.Set(key3, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key3, now));
EXPECT_EQ(3U, cache.size());
EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now));
EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key3, now));
EXPECT_NE(cache.Lookup(key2, now), cache.Lookup(key3, now));
}
TEST(HostCacheTest, HostResolverSourceIsPartOfKey) {
const url::SchemeHostPort kHost(url::kHttpsScheme, "foobar.test", 443);
const base::TimeDelta kSuccessEntryTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key1(kHost, DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, NetworkAnonymizationKey());
HostCache::Key key2(kHost, DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::DNS, NetworkAnonymizationKey());
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0U, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(1U, cache.size());
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2U, cache.size());
EXPECT_NE(cache.Lookup(key1, now), cache.Lookup(key2, now));
}
TEST(HostCacheTest, SecureIsPartOfKey) {
const url::SchemeHostPort kHost(url::kHttpsScheme, "foobar.test", 443);
const base::TimeDelta kSuccessEntryTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::EntryStaleness stale;
HostCache::Key key1(kHost, DnsQueryType::A, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
key1.secure = true;
HostCache::Key key2(kHost, DnsQueryType::A, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
key2.secure = false;
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0U, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(1U, cache.size());
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.LookupStale(key2, now, &stale));
const std::pair<const HostCache::Key, HostCache::Entry>* result;
result = cache.Lookup(key2, now, true );
EXPECT_TRUE(result);
EXPECT_TRUE(result->first.secure);
result = cache.LookupStale(key2, now, &stale, true );
EXPECT_TRUE(result);
EXPECT_TRUE(result->first.secure);
cache.Set(key2, entry, now, kSuccessEntryTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_TRUE(cache.LookupStale(key2, now, &stale));
EXPECT_EQ(2U, cache.size());
}
TEST(HostCacheTest, PreferLessStaleMoreSecure) {
const url::SchemeHostPort kHost(url::kHttpsScheme, "foobar.test", 443);
const base::TimeDelta kSuccessEntryTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::EntryStaleness stale;
HostCache::Key insecure_key(kHost, DnsQueryType::A, 0,
HostResolverSource::ANY,
NetworkAnonymizationKey());
HostCache::Key secure_key(kHost, DnsQueryType::A, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
secure_key.secure = true;
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0U, cache.size());
cache.Set(insecure_key, entry, now, kSuccessEntryTTL);
cache.Set(secure_key, entry, now, kSuccessEntryTTL);
EXPECT_EQ(insecure_key, cache.Lookup(insecure_key, now)->first);
EXPECT_EQ(secure_key, cache.Lookup(secure_key, now)->first);
EXPECT_EQ(secure_key,
cache.Lookup(insecure_key, now, true )->first);
EXPECT_EQ(secure_key,
cache.Lookup(insecure_key, now, true )->first);
cache.Invalidate();
cache.Set(insecure_key, entry, now, kSuccessEntryTTL);
EXPECT_EQ(insecure_key, cache.Lookup(insecure_key, now)->first);
EXPECT_FALSE(cache.Lookup(secure_key, now));
EXPECT_EQ(secure_key, cache.LookupStale(secure_key, now, &stale)->first);
EXPECT_EQ(
insecure_key,
cache.LookupStale(secure_key, now, &stale, true )
->first);
cache.clear();
cache.Set(insecure_key, entry, now, base::Seconds(20));
cache.Set(secure_key, entry, now, kSuccessEntryTTL);
now += base::Seconds(15);
EXPECT_EQ(insecure_key, cache.Lookup(insecure_key, now)->first);
EXPECT_FALSE(cache.Lookup(secure_key, now));
EXPECT_EQ(secure_key, cache.LookupStale(secure_key, now, &stale)->first);
EXPECT_EQ(
insecure_key,
cache.LookupStale(secure_key, now, &stale, true )
->first);
}
TEST(HostCacheTest, NoCache) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(0);
EXPECT_TRUE(cache.caching_is_disabled());
base::TimeTicks now;
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now));
cache.Set(Key("foobar.com"), entry, now, kTTL);
EXPECT_FALSE(cache.Lookup(Key("foobar.com"), now));
EXPECT_EQ(0U, cache.size());
}
TEST(HostCacheTest, Clear) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0u, cache.size());
cache.Set(Key("foobar1.com"), entry, now, kTTL);
cache.Set(Key("foobar2.com"), entry, now, kTTL);
cache.Set(Key("foobar3.com"), entry, now, kTTL);
EXPECT_EQ(3u, cache.size());
cache.clear();
EXPECT_EQ(0u, cache.size());
}
TEST(HostCacheTest, ClearForHosts) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0u, cache.size());
cache.Set(Key("foobar1.com"), entry, now, kTTL);
cache.Set(Key("foobar2.com"), entry, now, kTTL);
cache.Set(Key("foobar3.com"), entry, now, kTTL);
cache.Set(Key("foobar4.com"), entry, now, kTTL);
cache.Set(Key("foobar5.com"), entry, now, kTTL);
EXPECT_EQ(5u, cache.size());
cache.ClearForHosts(base::BindRepeating(&FoobarIndexIsOdd));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(Key("foobar2.com"), now));
EXPECT_TRUE(cache.Lookup(Key("foobar4.com"), now));
cache.ClearForHosts(base::NullCallback());
EXPECT_EQ(0u, cache.size());
}
TEST(HostCacheTest, Evict) {
HostCache cache(2);
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Key key3 = Key("foobar3.com");
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
cache.Set(key1, entry, now, base::Seconds(10));
cache.Set(key2, entry, now, base::Seconds(5));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
cache.Set(key3, entry, now, base::Seconds(10));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_TRUE(cache.Lookup(key3, now));
}
TEST(HostCacheTest, Stale) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::EntryStaleness stale;
HostCache::Key key = Key("foobar.com");
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0U, cache.size());
EXPECT_FALSE(cache.Lookup(key, now));
EXPECT_FALSE(cache.LookupStale(key, now, &stale));
cache.Set(key, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_FALSE(stale.is_stale());
EXPECT_EQ(0, stale.stale_hits);
EXPECT_EQ(1U, cache.size());
now += base::Seconds(5);
EXPECT_TRUE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_FALSE(stale.is_stale());
EXPECT_EQ(0, stale.stale_hits);
now += base::Seconds(10);
EXPECT_FALSE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_TRUE(stale.is_stale());
EXPECT_EQ(base::Seconds(5), stale.expired_by);
EXPECT_EQ(0, stale.network_changes);
EXPECT_EQ(1, stale.stale_hits);
now += base::Seconds(5);
EXPECT_FALSE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_TRUE(stale.is_stale());
EXPECT_EQ(base::Seconds(10), stale.expired_by);
EXPECT_EQ(0, stale.network_changes);
EXPECT_EQ(2, stale.stale_hits);
cache.Invalidate();
EXPECT_FALSE(cache.Lookup(key, now));
EXPECT_TRUE(cache.LookupStale(key, now, &stale));
EXPECT_TRUE(stale.is_stale());
EXPECT_EQ(base::Seconds(10), stale.expired_by);
EXPECT_EQ(1, stale.network_changes);
EXPECT_EQ(3, stale.stale_hits);
}
TEST(HostCacheTest, EvictStale) {
HostCache cache(2);
base::TimeTicks now;
HostCache::EntryStaleness stale;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Key key3 = Key("foobar3.com");
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
cache.Set(key1, entry, now, base::Seconds(10));
EXPECT_EQ(1u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
cache.Invalidate();
EXPECT_EQ(1u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.LookupStale(key1, now, &stale));
EXPECT_EQ(1, stale.network_changes);
now += base::Seconds(1);
cache.Set(key2, entry, now, base::Seconds(5));
EXPECT_EQ(2u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.LookupStale(key1, now, &stale));
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_FALSE(cache.Lookup(key3, now));
cache.Set(key3, entry, now, base::Seconds(1));
EXPECT_EQ(2u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.LookupStale(key1, now, &stale));
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_TRUE(cache.Lookup(key3, now));
now += base::Seconds(5);
cache.Set(key1, entry, now, base::Seconds(10));
EXPECT_EQ(2u, cache.size());
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
EXPECT_TRUE(cache.LookupStale(key2, now, &stale));
EXPECT_FALSE(cache.Lookup(key3, now));
EXPECT_FALSE(cache.LookupStale(key3, now, &stale));
}
TEST(HostCacheTest, NoEvictPinned) {
HostCache cache(2);
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Key key3 = Key("foobar3.com");
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
entry.set_pinning(true);
cache.Set(key1, entry, now, base::Seconds(5));
now += base::Seconds(10);
cache.Set(key2, entry, now, base::Seconds(5));
now += base::Seconds(10);
cache.Set(key3, entry, now, base::Seconds(5));
EXPECT_EQ(3u, cache.size());
EXPECT_TRUE(cache.LookupStale(key1, now, nullptr));
EXPECT_TRUE(cache.LookupStale(key2, now, nullptr));
EXPECT_TRUE(cache.Lookup(key3, now));
}
TEST(HostCacheTest, EvictObsoletePinned) {
HostCache cache(2);
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Key key3 = Key("foobar3.com");
HostCache::Key key4 = Key("foobar4.com");
HostCache::Entry entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
entry.set_pinning(true);
cache.Set(key1, entry, now, base::Seconds(5));
cache.Set(key2, entry, now, base::Seconds(10));
cache.Set(key3, entry, now, base::Seconds(5));
EXPECT_EQ(3u, cache.size());
cache.Invalidate();
EXPECT_EQ(3u, cache.size());
cache.Set(key4, entry, now, base::Seconds(2));
EXPECT_EQ(2u, cache.size());
EXPECT_FALSE(cache.LookupStale(key1, now, nullptr));
EXPECT_TRUE(cache.LookupStale(key2, now, nullptr));
EXPECT_FALSE(cache.LookupStale(key3, now, nullptr));
EXPECT_TRUE(cache.LookupStale(key4, now, nullptr));
}
TEST(HostCacheTest, PreserveActivePin) {
HostCache cache(2);
base::TimeTicks now;
IPEndPoint endpoint1(IPAddress(192, 0, 2, 1), 0);
IPEndPoint endpoint2(IPAddress(192, 0, 2, 2), 0);
HostCache::Entry entry1 = HostCache::Entry(OK, {endpoint1}, {},
HostCache::Entry::SOURCE_UNKNOWN);
HostCache::Entry entry2 = HostCache::Entry(OK, {endpoint2}, {},
HostCache::Entry::SOURCE_UNKNOWN);
entry1.set_pinning(true);
HostCache::Key key = Key("foobar.com");
cache.Set(key, entry1, now, base::Seconds(10));
const auto* pair1 = cache.Lookup(key, now);
ASSERT_TRUE(pair1);
const HostCache::Entry& result1 = pair1->second;
EXPECT_THAT(result1.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ElementsAre(endpoint1))));
EXPECT_THAT(result1.pinning(), Optional(true));
cache.Set(key, entry2, now, base::Seconds(10));
const auto* pair2 = cache.Lookup(key, now);
ASSERT_TRUE(pair2);
const HostCache::Entry& result2 = pair2->second;
EXPECT_THAT(result2.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ElementsAre(endpoint2))));
EXPECT_THAT(result2.pinning(), Optional(true));
}
TEST(HostCacheTest, DontPreserveObsoletePin) {
HostCache cache(2);
base::TimeTicks now;
IPEndPoint endpoint1(IPAddress(192, 0, 2, 1), 0);
IPEndPoint endpoint2(IPAddress(192, 0, 2, 2), 0);
HostCache::Entry entry1 = HostCache::Entry(OK, {endpoint1}, {},
HostCache::Entry::SOURCE_UNKNOWN);
HostCache::Entry entry2 = HostCache::Entry(OK, {endpoint2}, {},
HostCache::Entry::SOURCE_UNKNOWN);
entry1.set_pinning(true);
HostCache::Key key = Key("foobar.com");
cache.Set(key, entry1, now, base::Seconds(10));
const auto* pair1 = cache.Lookup(key, now);
ASSERT_TRUE(pair1);
const HostCache::Entry& result1 = pair1->second;
EXPECT_THAT(result1.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ElementsAre(endpoint1))));
EXPECT_THAT(result1.pinning(), Optional(true));
cache.Invalidate();
cache.Set(key, entry2, now, base::Seconds(10));
const auto* pair2 = cache.Lookup(key, now);
ASSERT_TRUE(pair2);
const HostCache::Entry& result2 = pair2->second;
EXPECT_THAT(result2.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ElementsAre(endpoint2))));
EXPECT_THAT(result2.pinning(), Optional(false));
}
TEST(HostCacheTest, Unpin) {
HostCache cache(2);
base::TimeTicks now;
IPEndPoint endpoint1(IPAddress(192, 0, 2, 1), 0);
IPEndPoint endpoint2(IPAddress(192, 0, 2, 2), 0);
HostCache::Entry entry1 = HostCache::Entry(OK, {endpoint1}, {},
HostCache::Entry::SOURCE_UNKNOWN);
HostCache::Entry entry2 = HostCache::Entry(OK, {endpoint2}, {},
HostCache::Entry::SOURCE_UNKNOWN);
entry1.set_pinning(true);
entry2.set_pinning(false);
HostCache::Key key = Key("foobar.com");
cache.Set(key, entry1, now, base::Seconds(10));
const auto* pair1 = cache.Lookup(key, now);
ASSERT_TRUE(pair1);
const HostCache::Entry& result1 = pair1->second;
EXPECT_THAT(result1.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ElementsAre(endpoint1))));
EXPECT_THAT(result1.pinning(), Optional(true));
cache.Set(key, entry2, now, base::Seconds(10));
const auto* pair2 = cache.Lookup(key, now);
ASSERT_TRUE(pair2);
const HostCache::Entry& result2 = pair2->second;
EXPECT_THAT(result2.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ElementsAre(endpoint2))));
EXPECT_THAT(result2.pinning(), Optional(false));
}
TEST(HostCacheTest, KeyComparators) {
struct CacheTestParameters {
CacheTestParameters(const HostCache::Key key1,
const HostCache::Key key2,
int expected_comparison)
: key1(key1), key2(key2), expected_comparison(expected_comparison) {}
HostCache::Key key1;
HostCache::Key key2;
int expected_comparison;
};
std::vector<CacheTestParameters> tests = {
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
0},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::A, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::A, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
-1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host2", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
-1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::A, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host2", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host2", 443),
DnsQueryType::A, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
-1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, HOST_RESOLVER_CANONNAME,
HostResolverSource::ANY, NetworkAnonymizationKey()),
-1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, HOST_RESOLVER_CANONNAME,
HostResolverSource::ANY, NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, HOST_RESOLVER_CANONNAME,
HostResolverSource::ANY, NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host2", 443),
DnsQueryType::UNSPECIFIED, HOST_RESOLVER_CANONNAME,
HostResolverSource::ANY, NetworkAnonymizationKey()),
-1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 1544),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
-1},
{HostCache::Key("host1", DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, NetworkAnonymizationKey()),
HostCache::Key("host1", DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, NetworkAnonymizationKey()),
0},
{HostCache::Key("host1", DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, NetworkAnonymizationKey()),
HostCache::Key("host2", DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, NetworkAnonymizationKey()),
-1},
{HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey()),
HostCache::Key("host1", DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, NetworkAnonymizationKey()),
-1},
};
HostCache::Key insecure_key =
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
HostCache::Key secure_key =
HostCache::Key(url::SchemeHostPort(url::kHttpsScheme, "host1", 443),
DnsQueryType::UNSPECIFIED, 0, HostResolverSource::ANY,
NetworkAnonymizationKey());
secure_key.secure = true;
tests.emplace_back(insecure_key, secure_key, -1);
for (size_t i = 0; i < std::size(tests); ++i) {
SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]", i));
const HostCache::Key& key1 = tests[i].key1;
const HostCache::Key& key2 = tests[i].key2;
switch (tests[i].expected_comparison) {
case -1:
EXPECT_TRUE(key1 < key2);
EXPECT_FALSE(key2 < key1);
break;
case 0:
EXPECT_FALSE(key1 < key2);
EXPECT_FALSE(key2 < key1);
break;
case 1:
EXPECT_FALSE(key1 < key2);
EXPECT_TRUE(key2 < key1);
break;
default:
FAIL() << "Invalid expectation. Can be only -1, 0, 1";
}
}
}
TEST(HostCacheTest, SerializeAndDeserializeWithExpirations) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key expire_by_time_key = Key("expire.by.time.test");
HostCache::Key expire_by_changes_key = Key("expire.by.changes.test");
IPEndPoint endpoint(IPAddress(1, 2, 3, 4), 0);
HostCache::Entry entry = HostCache::Entry(OK, {endpoint}, {},
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0u, cache.size());
EXPECT_FALSE(cache.Lookup(expire_by_time_key, now));
cache.Set(expire_by_time_key, entry, now, kTTL);
EXPECT_THAT(cache.Lookup(expire_by_time_key, now),
Pointee(Pair(expire_by_time_key, EntryContentsEqual(entry))));
EXPECT_EQ(1u, cache.size());
now += base::Seconds(5);
EXPECT_FALSE(cache.Lookup(expire_by_changes_key, now));
cache.Set(expire_by_changes_key, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(expire_by_changes_key, now));
EXPECT_EQ(2u, cache.size());
EXPECT_EQ(0u, cache.last_restore_size());
now += base::Seconds(7);
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false ,
HostCache::SerializationType::kRestorable);
HostCache restored_cache(kMaxCacheEntries);
EXPECT_TRUE(restored_cache.RestoreFromListValue(serialized_cache));
HostCache::EntryStaleness stale;
EXPECT_FALSE(restored_cache.Lookup(expire_by_time_key, now));
EXPECT_THAT(restored_cache.LookupStale(expire_by_time_key, now, &stale),
Pointee(Pair(expire_by_time_key, EntryContentsEqual(entry))));
EXPECT_EQ(1, stale.network_changes);
EXPECT_GT(base::Milliseconds(100),
(base::Seconds(2) - stale.expired_by).magnitude());
EXPECT_FALSE(restored_cache.Lookup(expire_by_changes_key, now));
EXPECT_THAT(restored_cache.LookupStale(expire_by_changes_key, now, &stale),
Pointee(Pair(expire_by_changes_key, EntryContentsEqual(entry))));
EXPECT_EQ(1, stale.network_changes);
EXPECT_GT(base::Milliseconds(100),
(base::Seconds(-3) - stale.expired_by).magnitude());
EXPECT_EQ(2u, restored_cache.last_restore_size());
}
TEST(HostCacheTest, SerializeAndDeserializeWithChanges) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key to_serialize_key1 = Key("to.serialize1.test");
HostCache::Key to_serialize_key2 = Key("to.serialize2.test");
HostCache::Key other_key = Key("other.test");
IPEndPoint endpoint(IPAddress(1, 1, 1, 1), 0);
HostCache::Entry serialized_entry = HostCache::Entry(
OK, {endpoint}, {}, HostCache::Entry::SOURCE_UNKNOWN);
IPEndPoint replacement_endpoint(IPAddress(2, 2, 2, 2), 0);
HostCache::Entry replacement_entry =
HostCache::Entry(OK, {replacement_endpoint}, {},
HostCache::Entry::SOURCE_UNKNOWN);
IPEndPoint other_endpoint(IPAddress(3, 3, 3, 3), 0);
HostCache::Entry other_entry = HostCache::Entry(
OK, {other_endpoint}, {}, HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0u, cache.size());
EXPECT_FALSE(cache.Lookup(to_serialize_key1, now));
cache.Set(to_serialize_key1, serialized_entry, now, kTTL);
EXPECT_THAT(
cache.Lookup(to_serialize_key1, now),
Pointee(Pair(to_serialize_key1, EntryContentsEqual(serialized_entry))));
EXPECT_FALSE(cache.Lookup(to_serialize_key2, now));
cache.Set(to_serialize_key2, serialized_entry, now, kTTL);
EXPECT_THAT(
cache.Lookup(to_serialize_key2, now),
Pointee(Pair(to_serialize_key2, EntryContentsEqual(serialized_entry))));
EXPECT_EQ(2u, cache.size());
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false ,
HostCache::SerializationType::kRestorable);
HostCache restored_cache(kMaxCacheEntries);
EXPECT_FALSE(restored_cache.Lookup(to_serialize_key1, now));
restored_cache.Set(to_serialize_key1, replacement_entry, now, kTTL);
EXPECT_THAT(
restored_cache.Lookup(to_serialize_key1, now),
Pointee(Pair(to_serialize_key1, EntryContentsEqual(replacement_entry))));
EXPECT_EQ(1u, restored_cache.size());
EXPECT_FALSE(restored_cache.Lookup(other_key, now));
restored_cache.Set(other_key, other_entry, now, kTTL);
EXPECT_THAT(restored_cache.Lookup(other_key, now),
Pointee(Pair(other_key, EntryContentsEqual(other_entry))));
EXPECT_EQ(2u, restored_cache.size());
EXPECT_EQ(0u, restored_cache.last_restore_size());
EXPECT_TRUE(restored_cache.RestoreFromListValue(serialized_cache));
EXPECT_EQ(1u, restored_cache.last_restore_size());
HostCache::EntryStaleness stale;
EXPECT_THAT(
restored_cache.Lookup(to_serialize_key1, now),
Pointee(Pair(to_serialize_key1, EntryContentsEqual(replacement_entry))));
EXPECT_THAT(
restored_cache.LookupStale(to_serialize_key2, now, &stale),
Pointee(Pair(to_serialize_key2, EntryContentsEqual(serialized_entry))));
EXPECT_THAT(restored_cache.Lookup(other_key, now),
Pointee(Pair(other_key, EntryContentsEqual(other_entry))));
}
TEST(HostCacheTest, SerializeAndDeserializeAddresses) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key1 = Key("foobar.com");
key1.secure = true;
HostCache::Key key2 = Key("foobar2.com");
HostCache::Key key3 = Key("foobar3.com");
HostCache::Key key4 = Key("foobar4.com");
IPAddress address_ipv4(1, 2, 3, 4);
IPAddress address_ipv6(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
IPEndPoint endpoint_ipv4(address_ipv4, 0);
IPEndPoint endpoint_ipv6(address_ipv6, 0);
HostCache::Entry entry1 = HostCache::Entry(
OK, {endpoint_ipv4}, {}, HostCache::Entry::SOURCE_UNKNOWN);
HostCache::Entry entry2 =
HostCache::Entry(OK, {endpoint_ipv6, endpoint_ipv4}, {},
HostCache::Entry::SOURCE_UNKNOWN);
HostCache::Entry entry3 = HostCache::Entry(
OK, {endpoint_ipv6}, {}, HostCache::Entry::SOURCE_UNKNOWN);
HostCache::Entry entry4 = HostCache::Entry(
OK, {endpoint_ipv4}, {}, HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(0u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, entry1, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key1, now)->second.error() == entry1.error());
EXPECT_EQ(1u, cache.size());
now += base::Seconds(5);
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, entry2, now, kTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2u, cache.size());
EXPECT_FALSE(cache.Lookup(key3, now));
cache.Set(key3, entry3, now, kTTL);
EXPECT_TRUE(cache.Lookup(key3, now));
EXPECT_EQ(3u, cache.size());
EXPECT_EQ(0u, cache.last_restore_size());
now += base::Seconds(7);
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false ,
HostCache::SerializationType::kRestorable);
HostCache restored_cache(kMaxCacheEntries);
EXPECT_FALSE(restored_cache.Lookup(key3, now));
restored_cache.Set(key3, entry1, now, kTTL);
EXPECT_TRUE(restored_cache.Lookup(key3, now));
EXPECT_EQ(1u, restored_cache.size());
EXPECT_FALSE(restored_cache.Lookup(key4, now));
restored_cache.Set(key4, entry4, now, kTTL);
EXPECT_TRUE(restored_cache.Lookup(key4, now));
EXPECT_EQ(2u, restored_cache.size());
EXPECT_EQ(0u, restored_cache.last_restore_size());
EXPECT_TRUE(restored_cache.RestoreFromListValue(serialized_cache));
HostCache::EntryStaleness stale;
EXPECT_FALSE(restored_cache.Lookup(key1, now));
const std::pair<const HostCache::Key, HostCache::Entry>* result1 =
restored_cache.LookupStale(key1, now, &stale);
EXPECT_TRUE(result1);
EXPECT_TRUE(result1->first.secure);
EXPECT_THAT(result1->second.text_records(), IsEmpty());
EXPECT_THAT(result1->second.hostnames(), IsEmpty());
EXPECT_EQ(1u, result1->second.ip_endpoints().size());
EXPECT_EQ(endpoint_ipv4, result1->second.ip_endpoints().front());
EXPECT_EQ(1, stale.network_changes);
EXPECT_GT(base::Milliseconds(100),
(base::Seconds(2) - stale.expired_by).magnitude());
EXPECT_FALSE(restored_cache.Lookup(key2, now));
const std::pair<const HostCache::Key, HostCache::Entry>* result2 =
restored_cache.LookupStale(key2, now, &stale);
EXPECT_TRUE(result2);
EXPECT_FALSE(result2->first.secure);
EXPECT_EQ(2u, result2->second.ip_endpoints().size());
EXPECT_EQ(endpoint_ipv6, result2->second.ip_endpoints().front());
EXPECT_EQ(endpoint_ipv4, result2->second.ip_endpoints().back());
EXPECT_EQ(1, stale.network_changes);
EXPECT_GT(base::Milliseconds(100),
(base::Seconds(-3) - stale.expired_by).magnitude());
const std::pair<const HostCache::Key, HostCache::Entry>* result3 =
restored_cache.Lookup(key3, now);
EXPECT_TRUE(result3);
EXPECT_EQ(1u, result3->second.ip_endpoints().size());
EXPECT_EQ(endpoint_ipv4, result3->second.ip_endpoints().front());
const std::pair<const HostCache::Key, HostCache::Entry>* result4 =
restored_cache.Lookup(key4, now);
EXPECT_TRUE(result4);
EXPECT_EQ(1u, result4->second.ip_endpoints().size());
EXPECT_EQ(endpoint_ipv4, result4->second.ip_endpoints().front());
EXPECT_EQ(2u, restored_cache.last_restore_size());
}
TEST(HostCacheTest, SerializeAndDeserializeEntryWithoutScheme) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache::Key key("host.test", DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, NetworkAnonymizationKey());
HostCache::Entry entry =
HostCache::Entry(OK, {},
{}, HostCache::Entry::SOURCE_UNKNOWN);
base::TimeTicks now;
HostCache cache(kMaxCacheEntries);
cache.Set(key, entry, now, kTTL);
ASSERT_TRUE(cache.Lookup(key, now));
ASSERT_EQ(cache.size(), 1u);
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false,
HostCache::SerializationType::kRestorable);
HostCache restored_cache(kMaxCacheEntries);
EXPECT_TRUE(restored_cache.RestoreFromListValue(serialized_cache));
EXPECT_EQ(restored_cache.size(), 1u);
HostCache::EntryStaleness staleness;
EXPECT_THAT(restored_cache.LookupStale(key, now, &staleness),
Pointee(Pair(key, EntryContentsEqual(entry))));
}
TEST(HostCacheTest, SerializeAndDeserializeWithNetworkAnonymizationKey) {
const url::SchemeHostPort kHost =
url::SchemeHostPort(url::kHttpsScheme, "hostname.test", 443);
const base::TimeDelta kTTL = base::Seconds(10);
const SchemefulSite kSite(GURL("https://site.test/"));
const auto kNetworkAnonymizationKey =
NetworkAnonymizationKey::CreateSameSite(kSite);
const SchemefulSite kOpaqueSite;
const auto kOpaqueNetworkAnonymizationKey =
NetworkAnonymizationKey::CreateSameSite(kOpaqueSite);
HostCache::Key key1(kHost, DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, kNetworkAnonymizationKey);
HostCache::Key key2(kHost, DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, kOpaqueNetworkAnonymizationKey);
IPEndPoint endpoint(IPAddress(1, 2, 3, 4), 0);
HostCache::Entry entry = HostCache::Entry(OK, {endpoint}, {},
HostCache::Entry::SOURCE_UNKNOWN);
base::TimeTicks now;
HostCache cache(kMaxCacheEntries);
cache.Set(key1, entry, now, kTTL);
cache.Set(key2, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(kNetworkAnonymizationKey,
cache.Lookup(key1, now)->first.network_anonymization_key);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(kOpaqueNetworkAnonymizationKey,
cache.Lookup(key2, now)->first.network_anonymization_key);
EXPECT_EQ(2u, cache.size());
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false ,
HostCache::SerializationType::kRestorable);
HostCache restored_cache(kMaxCacheEntries);
EXPECT_TRUE(restored_cache.RestoreFromListValue(serialized_cache));
EXPECT_EQ(1u, restored_cache.size());
HostCache::EntryStaleness stale;
EXPECT_THAT(restored_cache.LookupStale(key1, now, &stale),
Pointee(Pair(key1, EntryContentsEqual(entry))));
EXPECT_FALSE(restored_cache.Lookup(key2, now));
}
TEST(HostCacheTest, SerializeForDebugging) {
const url::SchemeHostPort kHost(url::kHttpsScheme, "hostname.test", 443);
const base::TimeDelta kTTL = base::Seconds(10);
const NetworkAnonymizationKey kNetworkAnonymizationKey =
NetworkAnonymizationKey::CreateTransient();
HostCache::Key key(kHost, DnsQueryType::UNSPECIFIED, 0,
HostResolverSource::ANY, kNetworkAnonymizationKey);
IPEndPoint endpoint(IPAddress(1, 2, 3, 4), 0);
HostCache::Entry entry = HostCache::Entry(OK, {endpoint}, {},
HostCache::Entry::SOURCE_UNKNOWN);
base::TimeTicks now;
HostCache cache(kMaxCacheEntries);
cache.Set(key, entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key, now));
EXPECT_EQ(kNetworkAnonymizationKey,
cache.Lookup(key, now)->first.network_anonymization_key);
EXPECT_EQ(1u, cache.size());
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false ,
HostCache::SerializationType::kDebug);
HostCache restored_cache(kMaxCacheEntries);
EXPECT_FALSE(restored_cache.RestoreFromListValue(serialized_cache));
ASSERT_EQ(1u, serialized_cache.size());
ASSERT_TRUE(serialized_cache[0].is_dict());
const std::string* nak_string =
serialized_cache[0].GetDict().FindString("network_anonymization_key");
ASSERT_TRUE(nak_string);
ASSERT_EQ(kNetworkAnonymizationKey.ToDebugString(), *nak_string);
}
TEST(HostCacheTest, SerializeAndDeserialize_Text) {
base::TimeTicks now;
base::TimeDelta ttl = base::Seconds(99);
std::vector<std::string> text_records({"foo", "bar"});
HostCache::Key key(url::SchemeHostPort(url::kHttpsScheme, "example.com", 443),
DnsQueryType::A, 0, HostResolverSource::DNS,
NetworkAnonymizationKey());
key.secure = true;
HostCache::Entry entry(OK, text_records, HostCache::Entry::SOURCE_DNS, ttl);
EXPECT_THAT(entry.text_records(), Not(IsEmpty()));
HostCache cache(kMaxCacheEntries);
cache.Set(key, entry, now, ttl);
EXPECT_EQ(1u, cache.size());
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false ,
HostCache::SerializationType::kRestorable);
HostCache restored_cache(kMaxCacheEntries);
EXPECT_TRUE(restored_cache.RestoreFromListValue(serialized_cache));
ASSERT_EQ(1u, serialized_cache.size());
ASSERT_EQ(1u, restored_cache.size());
HostCache::EntryStaleness stale;
const std::pair<const HostCache::Key, HostCache::Entry>* result =
restored_cache.LookupStale(key, now, &stale);
EXPECT_THAT(result, Pointee(Pair(key, EntryContentsEqual(entry))));
EXPECT_THAT(result->second.text_records(), text_records);
}
TEST(HostCacheTest, SerializeAndDeserialize_Hostname) {
base::TimeTicks now;
base::TimeDelta ttl = base::Seconds(99);
std::vector<HostPortPair> hostnames(
{HostPortPair("example.com", 95), HostPortPair("chromium.org", 122)});
HostCache::Key key(url::SchemeHostPort(url::kHttpsScheme, "example.com", 443),
DnsQueryType::A, 0, HostResolverSource::DNS,
NetworkAnonymizationKey());
HostCache::Entry entry(OK, hostnames, HostCache::Entry::SOURCE_DNS, ttl);
EXPECT_THAT(entry.hostnames(), Not(IsEmpty()));
HostCache cache(kMaxCacheEntries);
cache.Set(key, entry, now, ttl);
EXPECT_EQ(1u, cache.size());
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false ,
HostCache::SerializationType::kRestorable);
HostCache restored_cache(kMaxCacheEntries);
EXPECT_TRUE(restored_cache.RestoreFromListValue(serialized_cache));
ASSERT_EQ(1u, restored_cache.size());
HostCache::EntryStaleness stale;
const std::pair<const HostCache::Key, HostCache::Entry>* result =
restored_cache.LookupStale(key, now, &stale);
EXPECT_THAT(result, Pointee(Pair(key, EntryContentsEqual(entry))));
EXPECT_THAT(result->second.hostnames(), hostnames);
}
TEST(HostCacheTest, SerializeAndDeserializeEndpointResult) {
base::TimeTicks now;
base::TimeDelta ttl = base::Seconds(99);
HostCache::Key key(url::SchemeHostPort(url::kHttpsScheme, "example.com", 443),
DnsQueryType::A, 0, HostResolverSource::DNS,
NetworkAnonymizationKey());
IPEndPoint ipv6_endpoint(
IPAddress(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4), 110);
IPEndPoint ipv4_endpoint1(IPAddress(1, 1, 1, 1), 80);
IPEndPoint ipv4_endpoint2(IPAddress(2, 2, 2, 2), 90);
IPEndPoint other_ipv4_endpoint(IPAddress(3, 3, 3, 3), 100);
std::string ipv6_alias = "ipv6_alias.test";
std::string ipv4_alias = "ipv4_alias.test";
std::string other_alias = "other_alias.test";
std::vector<IPEndPoint> ip_endpoints = {ipv6_endpoint, ipv4_endpoint1,
ipv4_endpoint2, other_ipv4_endpoint};
std::set<std::string> aliases = {ipv6_alias, ipv4_alias, other_alias};
HostCache::Entry entry(OK, ip_endpoints, aliases,
HostCache::Entry::SOURCE_DNS, ttl);
std::set<std::string> canonical_names = {ipv6_alias, ipv4_alias};
entry.set_canonical_names(canonical_names);
EXPECT_THAT(entry.GetEndpoints(), Not(IsEmpty()));
ConnectionEndpointMetadata metadata1;
metadata1.supported_protocol_alpns = {"h3", "h2"};
metadata1.ech_config_list = {'f', 'o', 'o'};
metadata1.target_name = ipv6_alias;
ConnectionEndpointMetadata metadata2;
metadata2.supported_protocol_alpns = {"h2", "h4"};
metadata2.target_name = ipv4_alias;
HostCache::Entry metadata_entry(
OK,
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>{
{1u, metadata1}, {2u, metadata2}},
HostCache::Entry::SOURCE_DNS);
auto merged_entry = HostCache::Entry::MergeEntries(entry, metadata_entry);
EXPECT_THAT(merged_entry.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ip_endpoints)));
EXPECT_THAT(
merged_entry.GetMetadatas(),
testing::ElementsAre(
ExpectConnectionEndpointMetadata(testing::ElementsAre("h3", "h2"),
testing::ElementsAre('f', 'o', 'o'),
ipv6_alias),
ExpectConnectionEndpointMetadata(testing::ElementsAre("h2", "h4"),
IsEmpty(), ipv4_alias)));
EXPECT_THAT(merged_entry.canonical_names(),
UnorderedElementsAre(ipv4_alias, ipv6_alias));
HostCache cache(kMaxCacheEntries);
cache.Set(key, merged_entry, now, ttl);
EXPECT_EQ(1u, cache.size());
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false ,
HostCache::SerializationType::kRestorable);
HostCache restored_cache(kMaxCacheEntries);
EXPECT_TRUE(restored_cache.RestoreFromListValue(serialized_cache));
std::string json;
EXPECT_TRUE(base::JSONWriter::Write(serialized_cache, &json));
ASSERT_EQ(1u, restored_cache.size());
HostCache::EntryStaleness stale;
const std::pair<const HostCache::Key, HostCache::Entry>* result =
restored_cache.LookupStale(key, now, &stale);
ASSERT_TRUE(result);
EXPECT_THAT(result, Pointee(Pair(key, EntryContentsEqual(merged_entry))));
EXPECT_THAT(result->second.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ip_endpoints)));
EXPECT_THAT(
result->second.GetMetadatas(),
testing::ElementsAre(
ExpectConnectionEndpointMetadata(testing::ElementsAre("h3", "h2"),
testing::ElementsAre('f', 'o', 'o'),
ipv6_alias),
ExpectConnectionEndpointMetadata(testing::ElementsAre("h2", "h4"),
IsEmpty(), ipv4_alias)));
EXPECT_THAT(result->second.canonical_names(),
UnorderedElementsAre(ipv4_alias, ipv6_alias));
EXPECT_EQ(result->second.aliases(), aliases);
}
TEST(HostCacheTest, DeserializeNoEndpointNoAliase) {
base::TimeDelta ttl = base::Seconds(99);
std::string expiration_time_str = base::NumberToString(
(base::Time::Now() + ttl).since_origin().InMicroseconds());
auto dict = base::JSONReader::Read(base::StringPrintf(
R"(
[ {
"dns_query_type": 1,
"expiration": "%s",
"flags": 0,
"host_resolver_source": 2,
"hostname": "example.com",
"network_anonymization_key": [ ],
"port": 443,
"scheme": "https",
"secure": false
} ]
)",
expiration_time_str.c_str()),
base::JSON_PARSE_CHROMIUM_EXTENSIONS);
ASSERT_TRUE(dict);
HostCache restored_cache(kMaxCacheEntries);
ASSERT_TRUE(dict->is_list());
EXPECT_TRUE(restored_cache.RestoreFromListValue(dict->GetList()));
ASSERT_EQ(1u, restored_cache.size());
HostCache::Key key(url::SchemeHostPort(url::kHttpsScheme, "example.com", 443),
DnsQueryType::A, 0, HostResolverSource::DNS,
NetworkAnonymizationKey());
HostCache::EntryStaleness stale;
const std::pair<const HostCache::Key, HostCache::Entry>* result =
restored_cache.LookupStale(key, base::TimeTicks::Now(), &stale);
ASSERT_TRUE(result);
EXPECT_THAT(result->second.aliases(), ElementsAre());
EXPECT_THAT(result->second.ip_endpoints(), ElementsAre());
}
TEST(HostCacheTest, DeserializeLegacyAddresses) {
base::TimeDelta ttl = base::Seconds(99);
std::string expiration_time_str = base::NumberToString(
(base::Time::Now() + ttl).since_origin().InMicroseconds());
auto dict = base::JSONReader::Read(base::StringPrintf(
R"(
[ {
"addresses": [ "2000::", "1.2.3.4" ],
"dns_query_type": 1,
"expiration": "%s",
"flags": 0,
"host_resolver_source": 2,
"hostname": "example.com",
"network_anonymization_key": [ ],
"port": 443,
"scheme": "https",
"secure": false
} ]
)",
expiration_time_str.c_str()),
base::JSON_PARSE_CHROMIUM_EXTENSIONS);
ASSERT_TRUE(dict);
HostCache restored_cache(kMaxCacheEntries);
ASSERT_TRUE(dict->is_list());
EXPECT_TRUE(restored_cache.RestoreFromListValue(dict->GetList()));
ASSERT_EQ(1u, restored_cache.size());
HostCache::Key key(url::SchemeHostPort(url::kHttpsScheme, "example.com", 443),
DnsQueryType::A, 0, HostResolverSource::DNS,
NetworkAnonymizationKey());
HostCache::EntryStaleness stale;
const std::pair<const HostCache::Key, HostCache::Entry>* result =
restored_cache.LookupStale(key, base::TimeTicks::Now(), &stale);
ASSERT_TRUE(result);
EXPECT_THAT(result->second.ip_endpoints(),
ElementsAreArray(MakeEndpoints({"2000::", "1.2.3.4"})));
EXPECT_THAT(result->second.aliases(), ElementsAre());
}
TEST(HostCacheTest, DeserializeInvalidQueryTypeIntegrity) {
base::TimeDelta ttl = base::Seconds(99);
std::string expiration_time_str = base::NumberToString(
(base::Time::Now() + ttl).since_origin().InMicroseconds());
auto dict = base::JSONReader::Read(base::StringPrintf(
R"(
[ {
"addresses": [ "2000::", "1.2.3.4" ],
"dns_query_type": 6,
"expiration": "%s",
"flags": 0,
"host_resolver_source": 2,
"hostname": "example.com",
"network_anonymization_key": [ ],
"port": 443,
"scheme": "https",
"secure": false
} ]
)",
expiration_time_str.c_str()),
base::JSON_PARSE_CHROMIUM_EXTENSIONS);
ASSERT_TRUE(dict);
HostCache restored_cache(kMaxCacheEntries);
ASSERT_TRUE(dict->is_list());
EXPECT_FALSE(restored_cache.RestoreFromListValue(dict->GetList()));
ASSERT_EQ(0u, restored_cache.size());
}
TEST(HostCacheTest, DeserializeInvalidQueryTypeHttpsExperimental) {
base::TimeDelta ttl = base::Seconds(99);
std::string expiration_time_str = base::NumberToString(
(base::Time::Now() + ttl).since_origin().InMicroseconds());
auto dict = base::JSONReader::Read(base::StringPrintf(
R"(
[ {
"addresses": [ "2000::", "1.2.3.4" ],
"dns_query_type": 8,
"expiration": "%s",
"flags": 0,
"host_resolver_source": 2,
"hostname": "example.com",
"network_anonymization_key": [ ],
"port": 443,
"scheme": "https",
"secure": false
} ]
)",
expiration_time_str.c_str()),
base::JSON_PARSE_CHROMIUM_EXTENSIONS);
ASSERT_TRUE(dict);
HostCache restored_cache(kMaxCacheEntries);
ASSERT_TRUE(dict->is_list());
EXPECT_FALSE(restored_cache.RestoreFromListValue(dict->GetList()));
ASSERT_EQ(0u, restored_cache.size());
}
TEST(HostCacheTest, PersistenceDelegate) {
const base::TimeDelta kTTL = base::Seconds(10);
HostCache cache(kMaxCacheEntries);
MockPersistenceDelegate delegate;
cache.set_persistence_delegate(&delegate);
HostCache::Key key1 = Key("foobar.com");
HostCache::Key key2 = Key("foobar2.com");
HostCache::Entry ok_entry =
HostCache::Entry(OK, {}, {},
HostCache::Entry::SOURCE_UNKNOWN);
std::vector<IPEndPoint> other_endpoints = {
IPEndPoint(IPAddress(1, 1, 1, 1), 300)};
HostCache::Entry other_entry(OK, std::move(other_endpoints), {},
HostCache::Entry::SOURCE_UNKNOWN);
HostCache::Entry error_entry =
HostCache::Entry(ERR_NAME_NOT_RESOLVED, {},
{}, HostCache::Entry::SOURCE_UNKNOWN);
base::TimeTicks now;
EXPECT_EQ(0u, cache.size());
EXPECT_FALSE(cache.Lookup(key1, now));
cache.Set(key1, ok_entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(1u, cache.size());
EXPECT_EQ(1, delegate.num_changes());
EXPECT_FALSE(cache.Lookup(key2, now));
cache.Set(key2, error_entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key2, now));
EXPECT_EQ(2u, cache.size());
EXPECT_EQ(2, delegate.num_changes());
now += base::Seconds(5);
EXPECT_TRUE(cache.Lookup(key1, now));
cache.Set(key1, ok_entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(2u, cache.size());
EXPECT_EQ(2, delegate.num_changes());
EXPECT_TRUE(cache.Lookup(key1, now));
cache.Set(key1, ok_entry, now, kTTL - base::Seconds(5));
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(2u, cache.size());
EXPECT_EQ(2, delegate.num_changes());
EXPECT_TRUE(cache.Lookup(key1, now));
cache.Set(key1, other_entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(2u, cache.size());
EXPECT_EQ(3, delegate.num_changes());
EXPECT_TRUE(cache.Lookup(key1, now));
cache.Set(key2, ok_entry, now, kTTL);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_EQ(2u, cache.size());
EXPECT_EQ(4, delegate.num_changes());
}
TEST(HostCacheTest, MergeEndpointsWithAliases) {
const IPAddress kAddressFront(1, 2, 3, 4);
const IPEndPoint kEndpointFront(kAddressFront, 0);
HostCache::Entry front(OK, {kEndpointFront}, {"alias1", "alias2", "alias3"},
HostCache::Entry::SOURCE_DNS);
front.set_text_records(std::vector<std::string>{"text1"});
const HostPortPair kHostnameFront("host", 1);
front.set_hostnames(std::vector<HostPortPair>{kHostnameFront});
const IPAddress kAddressBack(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0);
const IPEndPoint kEndpointBack(kAddressBack, 0);
HostCache::Entry back(OK, {kEndpointBack}, {"alias2", "alias4", "alias5"},
HostCache::Entry::SOURCE_DNS);
back.set_text_records(std::vector<std::string>{"text2"});
const HostPortPair kHostnameBack("host", 2);
back.set_hostnames(std::vector<HostPortPair>{kHostnameBack});
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_EQ(OK, result.error());
EXPECT_EQ(HostCache::Entry::SOURCE_DNS, result.source());
EXPECT_THAT(result.ip_endpoints(),
ElementsAre(kEndpointFront, kEndpointBack));
EXPECT_THAT(result.text_records(), ElementsAre("text1", "text2"));
EXPECT_THAT(result.hostnames(), ElementsAre(kHostnameFront, kHostnameBack));
EXPECT_THAT(
result.aliases(),
UnorderedElementsAre("alias1", "alias2", "alias3", "alias4", "alias5"));
}
TEST(HostCacheTest, MergeEndpointsKeepEndpointsOrder) {
std::vector<IPEndPoint> front_addresses =
MakeEndpoints({"::1", "0.0.0.2", "0.0.0.4"});
std::vector<IPEndPoint> back_addresses =
MakeEndpoints({"0.0.0.2", "0.0.0.2", "::3", "::3", "0.0.0.4"});
HostCache::Entry front(OK, front_addresses, {"front"},
HostCache::Entry::SOURCE_DNS);
HostCache::Entry back(OK, back_addresses, {"back"},
HostCache::Entry::SOURCE_DNS);
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_THAT(
result.ip_endpoints(),
ElementsAreArray(MakeEndpoints({"::1", "0.0.0.2", "0.0.0.4", "0.0.0.2",
"0.0.0.2", "::3", "::3", "0.0.0.4"})));
EXPECT_THAT(result.aliases(), UnorderedElementsAre("front", "back"));
}
TEST(HostCacheTest, MergeMetadatas) {
ConnectionEndpointMetadata front_metadata;
front_metadata.supported_protocol_alpns = {"h5", "h6", "monster truck rally"};
front_metadata.ech_config_list = {'h', 'i'};
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>
front_metadata_map{{4u, front_metadata}};
HostCache::Entry front(OK, front_metadata_map, HostCache::Entry::SOURCE_DNS);
ConnectionEndpointMetadata back_metadata;
back_metadata.supported_protocol_alpns = {"h5"};
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>
back_metadata_map{{2u, back_metadata}};
HostCache::Entry back(OK, back_metadata_map, HostCache::Entry::SOURCE_DNS);
HostCache::Entry result = HostCache::Entry::MergeEntries(front, back);
EXPECT_THAT(result.GetEndpoints(), IsEmpty());
result = HostCache::Entry::MergeEntries(back, front);
EXPECT_THAT(result.GetEndpoints(), IsEmpty());
}
TEST(HostCacheTest, MergeMetadatasWithIpEndpointsDifferentCanonicalName) {
std::string target_name = "example.com";
std::string other_target_name = "other.example.com";
ConnectionEndpointMetadata metadata;
metadata.supported_protocol_alpns = {"h5", "h6", "monster truck rally"};
metadata.ech_config_list = {'h', 'i'};
metadata.target_name = target_name;
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata> metadata_map{
{4u, metadata}};
HostCache::Entry metadata_entry(OK, metadata_map,
HostCache::Entry::SOURCE_DNS);
EXPECT_THAT(metadata_entry.GetEndpoints(), IsEmpty());
IPEndPoint ip_endpoint(IPAddress(1, 1, 1, 1), 0);
HostCache::Entry with_ip_endpoint(OK, {ip_endpoint}, {},
HostCache::Entry::SOURCE_DNS);
with_ip_endpoint.set_canonical_names(
std::set<std::string>{other_target_name});
HostCache::Entry result =
HostCache::Entry::MergeEntries(metadata_entry, with_ip_endpoint);
EXPECT_THAT(
result.GetEndpoints(),
ElementsAre(ExpectEndpointResult(std::vector<IPEndPoint>{ip_endpoint})));
EXPECT_EQ(result,
HostCache::Entry::MergeEntries(with_ip_endpoint, metadata_entry));
}
TEST(HostCacheTest, MergeMetadatasWithIpEndpointsMatchingCanonicalName) {
std::string target_name = "example.com";
ConnectionEndpointMetadata metadata;
metadata.supported_protocol_alpns = {"h5", "h6", "monster truck rally"};
metadata.ech_config_list = {'h', 'i'};
metadata.target_name = target_name;
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata> metadata_map{
{4u, metadata}};
HostCache::Entry metadata_entry(OK, metadata_map,
HostCache::Entry::SOURCE_DNS);
EXPECT_THAT(metadata_entry.GetEndpoints(), IsEmpty());
IPEndPoint ip_endpoint(IPAddress(1, 1, 1, 1), 0);
HostCache::Entry with_ip_endpoint(OK, {ip_endpoint}, {},
HostCache::Entry::SOURCE_DNS);
with_ip_endpoint.set_canonical_names(std::set<std::string>{target_name});
HostCache::Entry result =
HostCache::Entry::MergeEntries(metadata_entry, with_ip_endpoint);
EXPECT_THAT(
result.GetEndpoints(),
ElementsAre(ExpectEndpointResult(ElementsAre(ip_endpoint), metadata),
ExpectEndpointResult(ElementsAre(ip_endpoint))));
EXPECT_EQ(result,
HostCache::Entry::MergeEntries(with_ip_endpoint, metadata_entry));
}
TEST(HostCacheTest, MergeMultipleMetadatasWithIpEndpoints) {
std::string target_name = "example.com";
ConnectionEndpointMetadata front_metadata;
front_metadata.supported_protocol_alpns = {"h5", "h6", "monster truck rally"};
front_metadata.ech_config_list = {'h', 'i'};
front_metadata.target_name = target_name;
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>
front_metadata_map{{4u, front_metadata}};
HostCache::Entry front(OK, front_metadata_map, HostCache::Entry::SOURCE_DNS);
ConnectionEndpointMetadata back_metadata;
back_metadata.supported_protocol_alpns = {"h5"};
back_metadata.target_name = target_name;
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>
back_metadata_map{{2u, back_metadata}};
HostCache::Entry back(OK, back_metadata_map, HostCache::Entry::SOURCE_DNS);
HostCache::Entry merged_metadatas =
HostCache::Entry::MergeEntries(front, back);
HostCache::Entry reversed_merged_metadatas =
HostCache::Entry::MergeEntries(back, front);
EXPECT_THAT(merged_metadatas.GetEndpoints(), IsEmpty());
EXPECT_THAT(reversed_merged_metadatas.GetEndpoints(), IsEmpty());
IPEndPoint ip_endpoint(IPAddress(1, 1, 1, 1), 0);
HostCache::Entry with_ip_endpoint(OK, {ip_endpoint}, {},
HostCache::Entry::SOURCE_DNS);
with_ip_endpoint.set_canonical_names(std::set<std::string>{target_name});
HostCache::Entry result =
HostCache::Entry::MergeEntries(merged_metadatas, with_ip_endpoint);
EXPECT_THAT(
result.GetEndpoints(),
ElementsAre(
ExpectEndpointResult(ElementsAre(ip_endpoint), back_metadata),
ExpectEndpointResult(ElementsAre(ip_endpoint), front_metadata),
ExpectEndpointResult(ElementsAre(ip_endpoint))));
EXPECT_EQ(result, HostCache::Entry::MergeEntries(reversed_merged_metadatas,
with_ip_endpoint));
EXPECT_EQ(result,
HostCache::Entry::MergeEntries(with_ip_endpoint, merged_metadatas));
EXPECT_EQ(result, HostCache::Entry::MergeEntries(with_ip_endpoint,
reversed_merged_metadatas));
}
TEST(HostCacheTest, MergeAliases) {
HostCache::Entry front(OK, {},
{"foo1.test", "foo2.test", "foo3.test"},
HostCache::Entry::SOURCE_DNS);
HostCache::Entry back(OK, {},
{"foo2.test", "foo4.test"},
HostCache::Entry::SOURCE_DNS);
HostCache::Entry expected(
OK, {},
{"foo1.test", "foo2.test", "foo3.test", "foo4.test"},
HostCache::Entry::SOURCE_DNS);
HostCache::Entry result = HostCache::Entry::MergeEntries(front, back);
EXPECT_EQ(result, expected);
result = HostCache::Entry::MergeEntries(back, front);
EXPECT_EQ(result, expected);
}
TEST(HostCacheTest, MergeEntries_frontEmpty) {
HostCache::Entry front(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS);
const IPAddress kAddressBack(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0);
const IPEndPoint kEndpointBack(kAddressBack, 0);
HostCache::Entry back(OK, {kEndpointBack}, {"alias1", "alias2", "alias3"},
HostCache::Entry::SOURCE_DNS, base::Hours(4));
back.set_text_records(std::vector<std::string>{"text2"});
const HostPortPair kHostnameBack("host", 2);
back.set_hostnames(std::vector<HostPortPair>{kHostnameBack});
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_EQ(OK, result.error());
EXPECT_EQ(HostCache::Entry::SOURCE_DNS, result.source());
EXPECT_THAT(result.ip_endpoints(), ElementsAre(kEndpointBack));
EXPECT_THAT(result.text_records(), ElementsAre("text2"));
EXPECT_THAT(result.hostnames(), ElementsAre(kHostnameBack));
EXPECT_EQ(base::Hours(4), result.ttl());
EXPECT_THAT(result.aliases(),
UnorderedElementsAre("alias1", "alias2", "alias3"));
}
TEST(HostCacheTest, MergeEntries_backEmpty) {
const IPAddress kAddressFront(1, 2, 3, 4);
const IPEndPoint kEndpointFront(kAddressFront, 0);
HostCache::Entry front(OK, {kEndpointFront}, {"alias1", "alias2", "alias3"},
HostCache::Entry::SOURCE_DNS, base::Minutes(5));
front.set_text_records(std::vector<std::string>{"text1"});
const HostPortPair kHostnameFront("host", 1);
front.set_hostnames(std::vector<HostPortPair>{kHostnameFront});
HostCache::Entry back(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS);
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_EQ(OK, result.error());
EXPECT_EQ(HostCache::Entry::SOURCE_DNS, result.source());
EXPECT_THAT(result.ip_endpoints(), ElementsAre(kEndpointFront));
EXPECT_THAT(result.text_records(), ElementsAre("text1"));
EXPECT_THAT(result.hostnames(), ElementsAre(kHostnameFront));
EXPECT_EQ(base::Minutes(5), result.ttl());
EXPECT_THAT(result.aliases(),
UnorderedElementsAre("alias1", "alias2", "alias3"));
}
TEST(HostCacheTest, MergeEntries_bothEmpty) {
HostCache::Entry front(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS);
HostCache::Entry back(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS);
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_EQ(ERR_NAME_NOT_RESOLVED, result.error());
EXPECT_EQ(HostCache::Entry::SOURCE_DNS, result.source());
EXPECT_THAT(result.ip_endpoints(), IsEmpty());
EXPECT_THAT(result.text_records(), IsEmpty());
EXPECT_THAT(result.hostnames(), IsEmpty());
EXPECT_FALSE(result.has_ttl());
}
TEST(HostCacheTest, MergeEntries_frontWithAliasesNoAddressesBackWithBoth) {
HostCache::Entry front(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS);
std::set<std::string> aliases_front({"alias0", "alias1", "alias2"});
front.set_aliases(aliases_front);
const IPAddress kAddressBack(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0);
const IPEndPoint kEndpointBack(kAddressBack, 0);
HostCache::Entry back(OK, {kEndpointBack}, {"alias1", "alias2", "alias3"},
HostCache::Entry::SOURCE_DNS, base::Hours(4));
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_EQ(OK, result.error());
EXPECT_EQ(HostCache::Entry::SOURCE_DNS, result.source());
EXPECT_THAT(result.ip_endpoints(), ElementsAre(kEndpointBack));
EXPECT_EQ(base::Hours(4), result.ttl());
EXPECT_THAT(result.aliases(),
UnorderedElementsAre("alias0", "alias1", "alias2", "alias3"));
}
TEST(HostCacheTest, MergeEntries_backWithAliasesNoAddressesFrontWithBoth) {
HostCache::Entry back(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS);
std::set<std::string> aliases_back({"alias1", "alias2", "alias3"});
back.set_aliases(aliases_back);
const IPAddress kAddressFront(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0);
const IPEndPoint kEndpointFront(kAddressFront, 0);
HostCache::Entry front(OK, {kEndpointFront}, {"alias0", "alias1", "alias2"},
HostCache::Entry::SOURCE_DNS, base::Hours(4));
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_EQ(OK, result.error());
EXPECT_EQ(HostCache::Entry::SOURCE_DNS, result.source());
EXPECT_THAT(result.ip_endpoints(), ElementsAre(kEndpointFront));
EXPECT_EQ(base::Hours(4), result.ttl());
EXPECT_THAT(result.aliases(),
UnorderedElementsAre("alias0", "alias1", "alias2", "alias3"));
}
TEST(HostCacheTest, MergeEntries_frontWithAddressesNoAliasesBackWithBoth) {
const IPAddress kAddressFront(1, 2, 3, 4);
const IPEndPoint kEndpointFront(kAddressFront, 0);
HostCache::Entry front(OK, {kEndpointFront}, {},
HostCache::Entry::SOURCE_DNS, base::Hours(4));
const IPAddress kAddressBack(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0);
const IPEndPoint kEndpointBack(kAddressBack, 0);
HostCache::Entry back(OK, {kEndpointBack}, {"alias1", "alias2", "alias3"},
HostCache::Entry::SOURCE_DNS, base::Hours(4));
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_EQ(OK, result.error());
EXPECT_EQ(HostCache::Entry::SOURCE_DNS, result.source());
EXPECT_THAT(result.ip_endpoints(),
ElementsAre(kEndpointFront, kEndpointBack));
EXPECT_EQ(base::Hours(4), result.ttl());
EXPECT_THAT(result.aliases(),
UnorderedElementsAre("alias1", "alias2", "alias3"));
}
TEST(HostCacheTest, MergeEntries_backWithAddressesNoAliasesFrontWithBoth) {
const IPAddress kAddressFront(1, 2, 3, 4);
const IPEndPoint kEndpointFront(kAddressFront, 0);
HostCache::Entry front(OK, {kEndpointFront}, {"alias1", "alias2", "alias3"},
HostCache::Entry::SOURCE_DNS, base::Hours(4));
const IPAddress kAddressBack(0x20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0);
const IPEndPoint kEndpointBack(kAddressBack, 0);
HostCache::Entry back(OK, {kEndpointBack}, {},
HostCache::Entry::SOURCE_DNS, base::Hours(4));
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_EQ(OK, result.error());
EXPECT_EQ(HostCache::Entry::SOURCE_DNS, result.source());
EXPECT_THAT(result.ip_endpoints(),
ElementsAre(kEndpointFront, kEndpointBack));
EXPECT_EQ(base::Hours(4), result.ttl());
EXPECT_THAT(result.aliases(),
UnorderedElementsAre("alias1", "alias2", "alias3"));
}
TEST(HostCacheTest, MergeEntries_differentTtl) {
HostCache::Entry front(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS,
base::Days(12));
HostCache::Entry back(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS,
base::Seconds(42));
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_EQ(base::Seconds(42), result.ttl());
}
TEST(HostCacheTest, MergeEntries_FrontCannonnamePreserved) {
HostCache::Entry front(OK, {}, {"name1"},
HostCache::Entry::SOURCE_DNS);
HostCache::Entry back(OK, {}, {"name2"},
HostCache::Entry::SOURCE_DNS);
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_THAT(result.aliases(), UnorderedElementsAre("name1", "name2"));
}
TEST(HostCacheTest, MergeEntries_BackCannonnameUsable) {
HostCache::Entry front(OK, {}, {},
HostCache::Entry::SOURCE_DNS);
HostCache::Entry back(OK, {}, {"name2"},
HostCache::Entry::SOURCE_DNS);
HostCache::Entry result =
HostCache::Entry::MergeEntries(std::move(front), std::move(back));
EXPECT_THAT(result.aliases(), UnorderedElementsAre("name2"));
}
TEST(HostCacheTest, ConvertFromInternalAddressResult) {
const std::vector<IPEndPoint> kEndpoints{
IPEndPoint(IPAddress(2, 2, 2, 2), 46)};
constexpr base::TimeDelta kTtl1 = base::Minutes(45);
constexpr base::TimeDelta kTtl2 = base::Minutes(40);
constexpr base::TimeDelta kTtl3 = base::Minutes(55);
std::set<std::unique_ptr<HostResolverInternalResult>> results;
results.insert(std::make_unique<HostResolverInternalDataResult>(
"endpoint.test", DnsQueryType::AAAA, base::TimeTicks() + kTtl1,
base::Time() + kTtl1, HostResolverInternalResult::Source::kDns,
kEndpoints, std::vector<std::string>{}, std::vector<HostPortPair>{}));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::AAAA, base::TimeTicks() + kTtl2,
base::Time() + kTtl2, HostResolverInternalResult::Source::kDns,
"domain2.test"));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain2.test", DnsQueryType::AAAA, base::TimeTicks() + kTtl3,
base::Time() + kTtl3, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
HostCache::Entry converted(std::move(results), base::Time(),
base::TimeTicks());
HostCache::Entry expected(
OK, kEndpoints,
{"domain1.test", "domain2.test", "endpoint.test"},
HostCache::Entry::SOURCE_DNS, kTtl2);
expected.set_canonical_names(std::set<std::string>{"endpoint.test"});
expected.set_text_records(std::vector<std::string>());
expected.set_hostnames(std::vector<HostPortPair>());
EXPECT_EQ(converted, expected);
}
TEST(HostCacheTest, ConvertFromInternalMetadataResult) {
const std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>
kMetadatas{{1, ConnectionEndpointMetadata({"h2", "h3"},
{},
"target.test", {})}};
constexpr base::TimeDelta kTtl1 = base::Minutes(45);
constexpr base::TimeDelta kTtl2 = base::Minutes(40);
constexpr base::TimeDelta kTtl3 = base::Minutes(55);
std::set<std::unique_ptr<HostResolverInternalResult>> results;
results.insert(std::make_unique<HostResolverInternalMetadataResult>(
"endpoint.test", DnsQueryType::HTTPS, base::TimeTicks() + kTtl1,
base::Time() + kTtl1, HostResolverInternalResult::Source::kDns,
kMetadatas));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::HTTPS, base::TimeTicks() + kTtl2,
base::Time() + kTtl2, HostResolverInternalResult::Source::kDns,
"domain2.test"));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain2.test", DnsQueryType::HTTPS, base::TimeTicks() + kTtl3,
base::Time() + kTtl3, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
HostCache::Entry converted(std::move(results), base::Time(),
base::TimeTicks());
HostCache::Entry expected(OK, kMetadatas, HostCache::Entry::SOURCE_DNS,
kTtl2);
EXPECT_EQ(converted, expected);
}
TEST(HostCacheTest, ConvertFromCompatibleOnlyInternalMetadataResult) {
const std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>
kMetadatas;
constexpr base::TimeDelta kTtl1 = base::Minutes(45);
constexpr base::TimeDelta kTtl2 = base::Minutes(40);
constexpr base::TimeDelta kTtl3 = base::Minutes(55);
std::set<std::unique_ptr<HostResolverInternalResult>> results;
results.insert(std::make_unique<HostResolverInternalMetadataResult>(
"endpoint.test", DnsQueryType::HTTPS, base::TimeTicks() + kTtl1,
base::Time() + kTtl1, HostResolverInternalResult::Source::kDns,
kMetadatas));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::HTTPS, base::TimeTicks() + kTtl2,
base::Time() + kTtl2, HostResolverInternalResult::Source::kDns,
"domain2.test"));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain2.test", DnsQueryType::HTTPS, base::TimeTicks() + kTtl3,
base::Time() + kTtl3, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
HostCache::Entry converted(std::move(results), base::Time(),
base::TimeTicks());
HostCache::Entry expected(ERR_NAME_NOT_RESOLVED, kMetadatas,
HostCache::Entry::SOURCE_DNS, kTtl2);
EXPECT_EQ(converted, expected);
}
TEST(HostCacheTest, ConvertFromInternalErrorResult) {
constexpr base::TimeDelta kTtl1 = base::Minutes(45);
constexpr base::TimeDelta kTtl2 = base::Minutes(40);
constexpr base::TimeDelta kTtl3 = base::Minutes(55);
std::set<std::unique_ptr<HostResolverInternalResult>> results;
results.insert(std::make_unique<HostResolverInternalErrorResult>(
"endpoint.test", DnsQueryType::A, base::TimeTicks() + kTtl1,
base::Time() + kTtl1, HostResolverInternalResult::Source::kDns,
ERR_NAME_NOT_RESOLVED));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::A, base::TimeTicks() + kTtl2,
base::Time() + kTtl2, HostResolverInternalResult::Source::kDns,
"domain2.test"));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain2.test", DnsQueryType::A, base::TimeTicks() + kTtl3,
base::Time() + kTtl3, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
HostCache::Entry converted(std::move(results), base::Time(),
base::TimeTicks());
HostCache::Entry expected(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS,
kTtl2);
EXPECT_EQ(converted, expected);
}
TEST(HostCacheTest, ConvertFromNonCachableInternalErrorResult) {
constexpr base::TimeDelta kTtl1 = base::Minutes(45);
constexpr base::TimeDelta kTtl2 = base::Minutes(40);
std::set<std::unique_ptr<HostResolverInternalResult>> results;
results.insert(std::make_unique<HostResolverInternalErrorResult>(
"endpoint.test", DnsQueryType::AAAA, std::nullopt,
std::nullopt,
HostResolverInternalResult::Source::kDns, ERR_NAME_NOT_RESOLVED));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::AAAA, base::TimeTicks() + kTtl1,
base::Time() + kTtl1, HostResolverInternalResult::Source::kDns,
"domain2.test"));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain2.test", DnsQueryType::AAAA, base::TimeTicks() + kTtl2,
base::Time() + kTtl2, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
HostCache::Entry converted(std::move(results), base::Time(),
base::TimeTicks());
HostCache::Entry expected(ERR_NAME_NOT_RESOLVED,
HostCache::Entry::SOURCE_DNS);
EXPECT_EQ(converted, expected);
}
TEST(HostCacheTest, ConvertFromInternalAliasOnlyResult) {
constexpr base::TimeDelta kTtl1 = base::Minutes(45);
constexpr base::TimeDelta kTtl2 = base::Minutes(40);
std::set<std::unique_ptr<HostResolverInternalResult>> results;
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::A, base::TimeTicks() + kTtl1,
base::Time() + kTtl1, HostResolverInternalResult::Source::kDns,
"domain2.test"));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain2.test", DnsQueryType::A, base::TimeTicks() + kTtl2,
base::Time() + kTtl2, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
HostCache::Entry converted(std::move(results), base::Time(),
base::TimeTicks());
HostCache::Entry expected(ERR_NAME_NOT_RESOLVED,
HostCache::Entry::SOURCE_DNS);
EXPECT_EQ(converted, expected);
}
TEST(HostCacheTest, ConvertFromEmptyInternalResult) {
HostCache::Entry converted({}, base::Time(), base::TimeTicks());
HostCache::Entry expected(ERR_NAME_NOT_RESOLVED,
HostCache::Entry::SOURCE_UNKNOWN);
EXPECT_EQ(converted, expected);
}
TEST(HostCacheTest, SerializeTrustAnchorIDs) {
base::TimeTicks now;
base::TimeDelta ttl = base::Seconds(99);
HostCache::Key key(url::SchemeHostPort(url::kHttpsScheme, "example.com", 443),
DnsQueryType::A, 0, HostResolverSource::DNS,
NetworkAnonymizationKey());
std::string ipv6_alias = "ipv6_alias.test";
ConnectionEndpointMetadata metadata;
metadata.supported_protocol_alpns = {"h3", "h2"};
metadata.ech_config_list = {'f', 'o', 'o'};
metadata.target_name = ipv6_alias;
metadata.trust_anchor_ids = {{0x01, 0x02, 0x03}, {0x02, 0x02}};
HostCache::Entry metadata_entry(
OK,
std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>{
{1u, metadata}},
HostCache::Entry::SOURCE_DNS);
HostCache cache(kMaxCacheEntries);
cache.Set(key, metadata_entry, now, ttl);
ASSERT_EQ(1u, cache.size());
base::Value::List serialized_cache;
cache.GetList(serialized_cache, false ,
HostCache::SerializationType::kRestorable);
HostCache restored_cache(kMaxCacheEntries);
ASSERT_TRUE(restored_cache.RestoreFromListValue(serialized_cache));
ASSERT_EQ(1u, restored_cache.size());
HostCache::EntryStaleness stale;
const std::pair<const HostCache::Key, HostCache::Entry>* result =
restored_cache.LookupStale(key, now, &stale);
ASSERT_TRUE(result);
EXPECT_THAT(result->second.GetMetadatas(),
testing::ElementsAre(ExpectConnectionEndpointMetadata(
testing::ElementsAre("h3", "h2"),
testing::ElementsAre('f', 'o', 'o'), ipv6_alias,
testing::ElementsAre(std::vector<uint8_t>({0x01, 0x02, 0x03}),
std::vector<uint8_t>({0x02, 0x02})))));
}
TEST(HostCacheTest, ConvertFromInternalMergedResult) {
const std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>
kMetadatas{{1, ConnectionEndpointMetadata({"h2", "h3"},
{},
"target.test", {})}};
const IPEndPoint kIpv4 =
IPEndPoint(IPAddress::FromIPLiteral("192.168.1.20").value(), 46);
const IPEndPoint kIpv6 =
IPEndPoint(IPAddress::FromIPLiteral("2001:db8:1::").value(), 46);
constexpr base::TimeDelta kMinTtl = base::Minutes(30);
constexpr base::TimeDelta kOtherTtl = base::Minutes(40);
std::set<std::unique_ptr<HostResolverInternalResult>> results;
results.insert(std::make_unique<HostResolverInternalDataResult>(
"endpoint.test", DnsQueryType::AAAA, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
std::vector<IPEndPoint>{kIpv6}, std::vector<std::string>{},
std::vector<HostPortPair>{}));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::AAAA, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
results.insert(std::make_unique<HostResolverInternalDataResult>(
"endpoint.test", DnsQueryType::A, base::TimeTicks() + kMinTtl,
base::Time() + kMinTtl, HostResolverInternalResult::Source::kDns,
std::vector<IPEndPoint>{kIpv4}, std::vector<std::string>{},
std::vector<HostPortPair>{}));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::A, base::TimeTicks() + kMinTtl,
base::Time() + kMinTtl, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
results.insert(std::make_unique<HostResolverInternalMetadataResult>(
"endpoint.test", DnsQueryType::HTTPS, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
kMetadatas));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::HTTPS, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
HostCache::Entry converted(std::move(results), base::Time(),
base::TimeTicks());
HostCache::Entry expected(OK, kMetadatas, HostCache::Entry::SOURCE_DNS,
kMinTtl);
expected.set_ip_endpoints({kIpv6, kIpv4});
expected.set_canonical_names(std::set<std::string>{"endpoint.test"});
expected.set_aliases({"endpoint.test", "domain1.test"});
EXPECT_EQ(converted, expected);
}
TEST(HostCacheTest, ConvertFromInternalMergedResultWithPartialError) {
const std::multimap<HttpsRecordPriority, ConnectionEndpointMetadata>
kMetadatas{{1, ConnectionEndpointMetadata({"h2", "h3"},
{},
"target.test", {})}};
const IPEndPoint kIpv6 =
IPEndPoint(IPAddress::FromIPLiteral("2001:db8:1::").value(), 46);
constexpr base::TimeDelta kMinTtl = base::Minutes(30);
constexpr base::TimeDelta kOtherTtl = base::Minutes(40);
std::set<std::unique_ptr<HostResolverInternalResult>> results;
results.insert(std::make_unique<HostResolverInternalDataResult>(
"endpoint.test", DnsQueryType::AAAA, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
std::vector<IPEndPoint>{kIpv6}, std::vector<std::string>{},
std::vector<HostPortPair>{}));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::AAAA, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
results.insert(std::make_unique<HostResolverInternalErrorResult>(
"endpoint.test", DnsQueryType::A, base::TimeTicks() + kMinTtl,
base::Time() + kMinTtl, HostResolverInternalResult::Source::kDns,
ERR_NAME_NOT_RESOLVED));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::A, base::TimeTicks() + kMinTtl,
base::Time() + kMinTtl, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
results.insert(std::make_unique<HostResolverInternalMetadataResult>(
"endpoint.test", DnsQueryType::HTTPS, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
kMetadatas));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::HTTPS, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
HostCache::Entry converted(std::move(results), base::Time(),
base::TimeTicks());
HostCache::Entry expected(OK, kMetadatas, HostCache::Entry::SOURCE_DNS,
kMinTtl);
expected.set_ip_endpoints({kIpv6});
expected.set_canonical_names(std::set<std::string>{"endpoint.test"});
expected.set_aliases({"endpoint.test", "domain1.test"});
EXPECT_EQ(converted, expected);
}
TEST(HostCacheTest, ConvertFromInternalMergedNodata) {
constexpr base::TimeDelta kMinTtl = base::Minutes(30);
constexpr base::TimeDelta kOtherTtl = base::Minutes(40);
std::set<std::unique_ptr<HostResolverInternalResult>> results;
results.insert(std::make_unique<HostResolverInternalErrorResult>(
"endpoint.test", DnsQueryType::AAAA, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
ERR_NAME_NOT_RESOLVED));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::AAAA, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
results.insert(std::make_unique<HostResolverInternalErrorResult>(
"endpoint.test", DnsQueryType::A, std::nullopt,
std::nullopt,
HostResolverInternalResult::Source::kDns, ERR_NAME_NOT_RESOLVED));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::A, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
results.insert(std::make_unique<HostResolverInternalErrorResult>(
"endpoint.test", DnsQueryType::HTTPS, base::TimeTicks() + kMinTtl,
base::Time() + kMinTtl, HostResolverInternalResult::Source::kDns,
ERR_NAME_NOT_RESOLVED));
results.insert(std::make_unique<HostResolverInternalAliasResult>(
"domain1.test", DnsQueryType::HTTPS, base::TimeTicks() + kOtherTtl,
base::Time() + kOtherTtl, HostResolverInternalResult::Source::kDns,
"endpoint.test"));
HostCache::Entry converted(std::move(results), base::Time(),
base::TimeTicks());
HostCache::Entry expected(ERR_NAME_NOT_RESOLVED, HostCache::Entry::SOURCE_DNS,
kMinTtl);
EXPECT_EQ(converted, expected);
}
}