This source file includes following definitions.
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <algorithm>
#include "base/bind.h"
#include "net/dns/dns_response.h"
#include "net/dns/dns_test_util.h"
#include "net/dns/mdns_cache.h"
#include "net/dns/record_parsed.h"
#include "net/dns/record_rdata.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using ::testing::Return;
using ::testing::StrictMock;
namespace net {
static const uint8 kTestResponsesDifferentAnswers[] = {
3, 'g', 'h', 's',
1, 'l',
6, 'g', 'o', 'o', 'g', 'l', 'e',
3, 'c', 'o', 'm',
0x00,
0x00, 0x01,
0x00, 0x01,
0, 0, 0, 53,
0, 4,
74, 125, 95, 121,
0xc0, 0x00,
0x00, 0x01,
0x00, 0x01,
0, 0, 0, 53,
0, 4,
74, 125, 95, 122,
};
static const uint8 kTestResponsesSameAnswers[] = {
3, 'g', 'h', 's',
1, 'l',
6, 'g', 'o', 'o', 'g', 'l', 'e',
3, 'c', 'o', 'm',
0x00,
0x00, 0x01,
0x00, 0x01,
0, 0, 0, 53,
0, 4,
74, 125, 95, 121,
0xc0, 0x00,
0x00, 0x01,
0x00, 0x01,
0, 0, 0, 112,
0, 4,
74, 125, 95, 121,
};
static const uint8 kTestResponseTwoRecords[] = {
3, 'g', 'h', 's',
1, 'l',
6, 'g', 'o', 'o', 'g', 'l', 'e',
3, 'c', 'o', 'm',
0x00,
0x00, 0x01,
0x00, 0x01,
0, 0, 0, 53,
0, 4,
74, 125, 95, 121,
3, 'g', 'h', 's',
1, 'l',
6, 'g', 'o', 'o', 'g', 'l', 'e',
3, 'c', 'o', 'm',
0x00,
0x00, 0x1c,
0x00, 0x01,
0, 0, 0, 53,
0, 16,
0x4a, 0x7d, 0x4a, 0x7d,
0x5f, 0x79, 0x5f, 0x79,
0x5f, 0x79, 0x5f, 0x79,
0x5f, 0x79, 0x5f, 0x79,
};
static const uint8 kTestResponsesGoodbyePacket[] = {
3, 'g', 'h', 's',
1, 'l',
6, 'g', 'o', 'o', 'g', 'l', 'e',
3, 'c', 'o', 'm',
0x00,
0x00, 0x01,
0x00, 0x01,
0, 0, 0, 0,
0, 4,
74, 125, 95, 121,
3, 'g', 'h', 's',
1, 'l',
6, 'g', 'o', 'o', 'g', 'l', 'e',
3, 'c', 'o', 'm',
0x00,
0x00, 0x01,
0x00, 0x01,
0, 0, 0, 53,
0, 4,
74, 125, 95, 121,
};
class RecordRemovalMock {
public:
MOCK_METHOD1(OnRecordRemoved, void(const RecordParsed*));
};
class MDnsCacheTest : public ::testing::Test {
public:
MDnsCacheTest()
: default_time_(base::Time::FromDoubleT(1234.0)) {}
virtual ~MDnsCacheTest() {}
protected:
base::Time default_time_;
StrictMock<RecordRemovalMock> record_removal_;
MDnsCache cache_;
};
TEST_F(MDnsCacheTest, InsertLookupSingle) {
DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
sizeof(dns_protocol::Header));
parser.SkipQuestion();
scoped_ptr<const RecordParsed> record1;
scoped_ptr<const RecordParsed> record2;
std::vector<const RecordParsed*> results;
record1 = RecordParsed::CreateFrom(&parser, default_time_);
record2 = RecordParsed::CreateFrom(&parser, default_time_);
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));
cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
default_time_);
EXPECT_EQ(1u, results.size());
EXPECT_EQ(default_time_, results.front()->time_created());
EXPECT_EQ("ghs.l.google.com", results.front()->name());
results.clear();
cache_.FindDnsRecords(PtrRecordRdata::kType, "ghs.l.google.com", &results,
default_time_);
EXPECT_EQ(0u, results.size());
}
TEST_F(MDnsCacheTest, Expiration) {
DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
sizeof(dns_protocol::Header));
parser.SkipQuestion();
scoped_ptr<const RecordParsed> record1;
scoped_ptr<const RecordParsed> record2;
std::vector<const RecordParsed*> results;
const RecordParsed* record_to_be_deleted;
record1 = RecordParsed::CreateFrom(&parser, default_time_);
base::TimeDelta ttl1 = base::TimeDelta::FromSeconds(record1->ttl());
record2 = RecordParsed::CreateFrom(&parser, default_time_);
base::TimeDelta ttl2 = base::TimeDelta::FromSeconds(record2->ttl());
record_to_be_deleted = record2.get();
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));
cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
default_time_);
EXPECT_EQ(1u, results.size());
EXPECT_EQ(default_time_ + ttl2, cache_.next_expiration());
cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
default_time_ + ttl2);
EXPECT_EQ(0u, results.size());
EXPECT_CALL(record_removal_, OnRecordRemoved(record_to_be_deleted));
cache_.CleanupRecords(default_time_ + ttl2, base::Bind(
&RecordRemovalMock::OnRecordRemoved, base::Unretained(&record_removal_)));
EXPECT_EQ(default_time_ + ttl1, cache_.next_expiration());
cache_.FindDnsRecords(ARecordRdata::kType, "ghs.l.google.com", &results,
default_time_ + ttl2);
EXPECT_EQ(0u, results.size());
}
TEST_F(MDnsCacheTest, RecordChange) {
DnsRecordParser parser(kTestResponsesDifferentAnswers,
sizeof(kTestResponsesDifferentAnswers),
0);
scoped_ptr<const RecordParsed> record1;
scoped_ptr<const RecordParsed> record2;
std::vector<const RecordParsed*> results;
record1 = RecordParsed::CreateFrom(&parser, default_time_);
record2 = RecordParsed::CreateFrom(&parser, default_time_);
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
EXPECT_EQ(MDnsCache::RecordChanged,
cache_.UpdateDnsRecord(record2.Pass()));
}
TEST_F(MDnsCacheTest, RecordNoChange) {
DnsRecordParser parser(kTestResponsesSameAnswers,
sizeof(kTestResponsesSameAnswers),
0);
scoped_ptr<const RecordParsed> record1;
scoped_ptr<const RecordParsed> record2;
std::vector<const RecordParsed*> results;
record1 = RecordParsed::CreateFrom(&parser, default_time_);
record2 = RecordParsed::CreateFrom(&parser, default_time_ +
base::TimeDelta::FromSeconds(1));
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
EXPECT_EQ(MDnsCache::NoChange, cache_.UpdateDnsRecord(record2.Pass()));
}
TEST_F(MDnsCacheTest, RecordPreemptExpirationTime) {
DnsRecordParser parser(kTestResponsesSameAnswers,
sizeof(kTestResponsesSameAnswers),
0);
scoped_ptr<const RecordParsed> record1;
scoped_ptr<const RecordParsed> record2;
std::vector<const RecordParsed*> results;
record1 = RecordParsed::CreateFrom(&parser, default_time_);
record2 = RecordParsed::CreateFrom(&parser, default_time_);
base::TimeDelta ttl1 = base::TimeDelta::FromSeconds(record1->ttl());
base::TimeDelta ttl2 = base::TimeDelta::FromSeconds(record2->ttl());
EXPECT_EQ(base::Time(), cache_.next_expiration());
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));
EXPECT_EQ(default_time_ + ttl2, cache_.next_expiration());
EXPECT_EQ(MDnsCache::NoChange, cache_.UpdateDnsRecord(record1.Pass()));
EXPECT_EQ(default_time_ + ttl1, cache_.next_expiration());
}
TEST_F(MDnsCacheTest, GoodbyePacket) {
DnsRecordParser parser(kTestResponsesGoodbyePacket,
sizeof(kTestResponsesGoodbyePacket),
0);
scoped_ptr<const RecordParsed> record_goodbye;
scoped_ptr<const RecordParsed> record_hello;
scoped_ptr<const RecordParsed> record_goodbye2;
std::vector<const RecordParsed*> results;
record_goodbye = RecordParsed::CreateFrom(&parser, default_time_);
record_hello = RecordParsed::CreateFrom(&parser, default_time_);
parser = DnsRecordParser(kTestResponsesGoodbyePacket,
sizeof(kTestResponsesGoodbyePacket),
0);
record_goodbye2 = RecordParsed::CreateFrom(&parser, default_time_);
base::TimeDelta ttl = base::TimeDelta::FromSeconds(record_hello->ttl());
EXPECT_EQ(base::Time(), cache_.next_expiration());
EXPECT_EQ(MDnsCache::NoChange, cache_.UpdateDnsRecord(record_goodbye.Pass()));
EXPECT_EQ(base::Time(), cache_.next_expiration());
EXPECT_EQ(MDnsCache::RecordAdded,
cache_.UpdateDnsRecord(record_hello.Pass()));
EXPECT_EQ(default_time_ + ttl, cache_.next_expiration());
EXPECT_EQ(MDnsCache::NoChange,
cache_.UpdateDnsRecord(record_goodbye2.Pass()));
EXPECT_EQ(default_time_ + base::TimeDelta::FromSeconds(1),
cache_.next_expiration());
}
TEST_F(MDnsCacheTest, AnyRRType) {
DnsRecordParser parser(kTestResponseTwoRecords,
sizeof(kTestResponseTwoRecords),
0);
scoped_ptr<const RecordParsed> record1;
scoped_ptr<const RecordParsed> record2;
std::vector<const RecordParsed*> results;
record1 = RecordParsed::CreateFrom(&parser, default_time_);
record2 = RecordParsed::CreateFrom(&parser, default_time_);
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record2.Pass()));
cache_.FindDnsRecords(0, "ghs.l.google.com", &results, default_time_);
EXPECT_EQ(2u, results.size());
EXPECT_EQ(default_time_, results.front()->time_created());
EXPECT_EQ("ghs.l.google.com", results[0]->name());
EXPECT_EQ("ghs.l.google.com", results[1]->name());
EXPECT_EQ(dns_protocol::kTypeA,
std::min(results[0]->type(), results[1]->type()));
EXPECT_EQ(dns_protocol::kTypeAAAA,
std::max(results[0]->type(), results[1]->type()));
}
TEST_F(MDnsCacheTest, RemoveRecord) {
DnsRecordParser parser(kT1ResponseDatagram, sizeof(kT1ResponseDatagram),
sizeof(dns_protocol::Header));
parser.SkipQuestion();
scoped_ptr<const RecordParsed> record1;
std::vector<const RecordParsed*> results;
record1 = RecordParsed::CreateFrom(&parser, default_time_);
EXPECT_EQ(MDnsCache::RecordAdded, cache_.UpdateDnsRecord(record1.Pass()));
cache_.FindDnsRecords(dns_protocol::kTypeCNAME, "codereview.chromium.org",
&results, default_time_);
EXPECT_EQ(1u, results.size());
scoped_ptr<const RecordParsed> record_out =
cache_.RemoveRecord(results.front());
EXPECT_EQ(record_out.get(), results.front());
cache_.FindDnsRecords(dns_protocol::kTypeCNAME, "codereview.chromium.org",
&results, default_time_);
EXPECT_EQ(0u, results.size());
}
}