This source file includes following definitions.
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
- TEST
#include "net/dns/host_cache.h"
#include "base/format_macros.h"
#include "base/stl_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "net/base/net_errors.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
const int kMaxCacheEntries = 10;
HostCache::Key Key(const std::string& hostname) {
return HostCache::Key(hostname, ADDRESS_FAMILY_UNSPECIFIED, 0);
}
}
TEST(HostCacheTest, Basic) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(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, AddressList());
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)->error == entry.error);
EXPECT_EQ(1U, cache.size());
now += base::TimeDelta::FromSeconds(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::TimeDelta::FromSeconds(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::TimeDelta::FromSeconds(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::TimeDelta::FromSeconds(10);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
}
TEST(HostCacheTest, NoCacheZeroTTL) {
const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10);
const base::TimeDelta kFailureEntryTTL = base::TimeDelta::FromSeconds(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, AddressList());
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::TimeDelta::FromSeconds(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, AddressList());
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::TimeDelta::FromSeconds(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::TimeDelta::FromSeconds(4);
EXPECT_TRUE(cache.Lookup(key1, now));
EXPECT_TRUE(cache.Lookup(key2, now));
now += base::TimeDelta::FromSeconds(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::TimeDelta::FromSeconds(10);
EXPECT_FALSE(cache.Lookup(key1, now));
EXPECT_FALSE(cache.Lookup(key2, now));
}
TEST(HostCacheTest, AddressFamilyIsPartOfKey) {
const base::TimeDelta kSuccessEntryTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key1("foobar.com", ADDRESS_FAMILY_UNSPECIFIED, 0);
HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4, 0);
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
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 base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Key key1("foobar.com", ADDRESS_FAMILY_IPV4, 0);
HostCache::Key key2("foobar.com", ADDRESS_FAMILY_IPV4,
HOST_RESOLVER_CANONNAME);
HostCache::Key key3("foobar.com", ADDRESS_FAMILY_IPV4,
HOST_RESOLVER_LOOPBACK_ONLY);
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
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, NoCache) {
const base::TimeDelta kTTL = base::TimeDelta::FromSeconds(10);
HostCache cache(0);
EXPECT_TRUE(cache.caching_is_disabled());
base::TimeTicks now;
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
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::TimeDelta::FromSeconds(10);
HostCache cache(kMaxCacheEntries);
base::TimeTicks now;
HostCache::Entry entry = HostCache::Entry(OK, AddressList());
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, KeyComparators) {
struct {
HostCache::Key key1;
HostCache::Key key2;
int expected_comparison;
} tests[] = {
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
0
},
{
HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0),
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0),
-1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0),
-1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_IPV4, 0),
HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED, 0),
1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host2", ADDRESS_FAMILY_IPV4, 0),
-1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED,
HOST_RESOLVER_CANONNAME),
-1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED,
HOST_RESOLVER_CANONNAME),
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED, 0),
1
},
{
HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED,
HOST_RESOLVER_CANONNAME),
HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED,
HOST_RESOLVER_CANONNAME),
-1
},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(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";
}
}
}
}