0x00, 0x01, // CLASS is IN.
};
+const uint8 kQueryPacketPrivetA[] = {
+ // Header
+ 0x00, 0x00, // ID is zeroed out
+ 0x00, 0x00, // No flags.
+ 0x00, 0x01, // One question.
+ 0x00, 0x00, // 0 RRs (answers)
+ 0x00, 0x00, // 0 authority RRs
+ 0x00, 0x00, // 0 additional RRs
+
+ // Question
+ // This part is echoed back from the respective query.
+ 0x07, '_', 'p', 'r', 'i', 'v', 'e', 't',
+ 0x04, '_', 't', 'c', 'p',
+ 0x05, 'l', 'o', 'c', 'a', 'l',
+ 0x00,
+ 0x00, 0x01, // TYPE is A.
+ 0x00, 0x01, // CLASS is IN.
+};
+
const uint8 kSamplePacketAdditionalOnly[] = {
// Header
0x00, 0x00, // ID is zeroed out
0x00,
0x00, 0x01, // TYPE is A.
0x00, 0x01, // CLASS is IN.
- 0x00, 0x01, // TTL (4 bytes) is 20 hours, 47 minutes, 48 seconds.
- 0x24, 0x74,
+ 0x00, 0x00, // TTL (4 bytes) is 5 seconds
+ 0x00, 0x05,
0x00, 0x04, // RDLENGTH is 4 bytes.
0xc0, 0x0c,
0x00, 0x02,
class MDnsTest : public ::testing::Test {
public:
- MDnsTest();
- virtual ~MDnsTest();
virtual void SetUp() OVERRIDE;
- virtual void TearDown() OVERRIDE;
void DeleteTransaction();
void DeleteBothListeners();
void RunFor(base::TimeDelta time_period);
void ExpectPacket(const uint8* packet, unsigned size);
void SimulatePacketReceive(const uint8* packet, unsigned size);
- scoped_ptr<MDnsClientImpl> test_client_;
+ MDnsClientImpl test_client_;
IPEndPoint mdns_ipv4_endpoint_;
- StrictMock<MockMDnsSocketFactory>* socket_factory_;
+ StrictMock<MockMDnsSocketFactory> socket_factory_;
// Transactions and listeners that can be deleted by class methods for
// reentrancy tests.
MOCK_METHOD0(OnCachePurged, void());
};
-MDnsTest::MDnsTest() {
- socket_factory_ = new StrictMock<MockMDnsSocketFactory>();
- test_client_.reset(new MDnsClientImpl(
- scoped_ptr<MDnsConnection::SocketFactory>(socket_factory_)));
-}
-
-MDnsTest::~MDnsTest() {
-}
-
void MDnsTest::SetUp() {
- test_client_->StartListening();
-}
-
-void MDnsTest::TearDown() {
+ test_client_.StartListening(&socket_factory_);
}
void MDnsTest::SimulatePacketReceive(const uint8* packet, unsigned size) {
- socket_factory_->SimulateReceive(packet, size);
+ socket_factory_.SimulateReceive(packet, size);
}
void MDnsTest::ExpectPacket(const uint8* packet, unsigned size) {
- EXPECT_CALL(*socket_factory_, OnSendTo(MakeString(packet, size)))
+ EXPECT_CALL(socket_factory_, OnSendTo(MakeString(packet, size)))
.Times(2);
}
PtrRecordCopyContainer record_privet;
PtrRecordCopyContainer record_printer;
- scoped_ptr<MDnsListener> listener_privet = test_client_->CreateListener(
- dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet);
- scoped_ptr<MDnsListener> listener_printer = test_client_->CreateListener(
- dns_protocol::kTypePTR, "_printer._tcp.local", &delegate_printer);
+ scoped_ptr<MDnsListener> listener_privet =
+ test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local",
+ &delegate_privet);
+ scoped_ptr<MDnsListener> listener_printer =
+ test_client_.CreateListener(dns_protocol::kTypePTR, "_printer._tcp.local",
+ &delegate_printer);
ASSERT_TRUE(listener_privet->Start());
ASSERT_TRUE(listener_printer->Start());
PtrRecordCopyContainer record_privet;
PtrRecordCopyContainer record_privet2;
- scoped_ptr<MDnsListener> listener_privet = test_client_->CreateListener(
- dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet);
+ scoped_ptr<MDnsListener> listener_privet =
+ test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local",
+ &delegate_privet);
ASSERT_TRUE(listener_privet->Start());
PtrRecordCopyContainer record_printer;
- scoped_ptr<MDnsListener> listener_printer = test_client_->CreateListener(
- dns_protocol::kTypePTR, "_printer._tcp.local", &delegate_printer);
+ scoped_ptr<MDnsListener> listener_printer =
+ test_client_.CreateListener(dns_protocol::kTypePTR, "_printer._tcp.local",
+ &delegate_printer);
ASSERT_TRUE(listener_printer->Start());
ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet));
scoped_ptr<MDnsTransaction> transaction_privet =
- test_client_->CreateTransaction(
+ test_client_.CreateTransaction(
dns_protocol::kTypePTR, "_privet._tcp.local",
MDnsTransaction::QUERY_NETWORK |
MDnsTransaction::QUERY_CACHE |
TEST_F(MDnsTest, TransactionCacheOnlyNoResult) {
scoped_ptr<MDnsTransaction> transaction_privet =
- test_client_->CreateTransaction(
+ test_client_.CreateTransaction(
dns_protocol::kTypePTR, "_privet._tcp.local",
MDnsTransaction::QUERY_CACHE |
MDnsTransaction::SINGLE_RESULT,
TEST_F(MDnsTest, TransactionWithCache) {
// Listener to force the client to listen
StrictMock<MockListenerDelegate> delegate_irrelevant;
- scoped_ptr<MDnsListener> listener_irrelevant = test_client_->CreateListener(
- dns_protocol::kTypeA, "codereview.chromium.local",
- &delegate_irrelevant);
+ scoped_ptr<MDnsListener> listener_irrelevant =
+ test_client_.CreateListener(dns_protocol::kTypeA,
+ "codereview.chromium.local",
+ &delegate_irrelevant);
ASSERT_TRUE(listener_irrelevant->Start());
&PtrRecordCopyContainer::SaveWithDummyArg));
scoped_ptr<MDnsTransaction> transaction_privet =
- test_client_->CreateTransaction(
+ test_client_.CreateTransaction(
dns_protocol::kTypePTR, "_privet._tcp.local",
MDnsTransaction::QUERY_NETWORK |
MDnsTransaction::QUERY_CACHE |
PtrRecordCopyContainer record_privet;
- scoped_ptr<MDnsListener> listener_privet = test_client_->CreateListener(
- dns_protocol::kTypePTR, "_privet._tcp.local",
- &delegate_privet);
+ scoped_ptr<MDnsListener> listener_privet =
+ test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local",
+ &delegate_privet);
ASSERT_TRUE(listener_privet->Start());
ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet));
scoped_ptr<MDnsTransaction> transaction_privet =
- test_client_->CreateTransaction(
+ test_client_.CreateTransaction(
dns_protocol::kTypePTR, "_privet._tcp.local",
MDnsTransaction::QUERY_NETWORK |
MDnsTransaction::QUERY_CACHE |
ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet));
scoped_ptr<MDnsTransaction> transaction_privet =
- test_client_->CreateTransaction(
+ test_client_.CreateTransaction(
dns_protocol::kTypePTR, "_privet._tcp.local",
MDnsTransaction::QUERY_NETWORK |
MDnsTransaction::QUERY_CACHE ,
TEST_F(MDnsTest, TransactionReentrantDelete) {
ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet));
- transaction_ = test_client_->CreateTransaction(
+ transaction_ = test_client_.CreateTransaction(
dns_protocol::kTypePTR, "_privet._tcp.local",
MDnsTransaction::QUERY_NETWORK |
MDnsTransaction::QUERY_CACHE |
TEST_F(MDnsTest, TransactionReentrantDeleteFromCache) {
StrictMock<MockListenerDelegate> delegate_irrelevant;
- scoped_ptr<MDnsListener> listener_irrelevant = test_client_->CreateListener(
+ scoped_ptr<MDnsListener> listener_irrelevant = test_client_.CreateListener(
dns_protocol::kTypeA, "codereview.chromium.local",
&delegate_irrelevant);
ASSERT_TRUE(listener_irrelevant->Start());
SimulatePacketReceive(kSamplePacket1, sizeof(kSamplePacket1));
- transaction_ = test_client_->CreateTransaction(
+ transaction_ = test_client_.CreateTransaction(
dns_protocol::kTypePTR, "_privet._tcp.local",
MDnsTransaction::QUERY_NETWORK |
MDnsTransaction::QUERY_CACHE,
TEST_F(MDnsTest, TransactionReentrantCacheLookupStart) {
ExpectPacket(kQueryPacketPrivet, sizeof(kQueryPacketPrivet));
- scoped_ptr<MDnsTransaction> transaction1 = test_client_->CreateTransaction(
- dns_protocol::kTypePTR, "_privet._tcp.local",
- MDnsTransaction::QUERY_NETWORK |
- MDnsTransaction::QUERY_CACHE |
- MDnsTransaction::SINGLE_RESULT,
- base::Bind(&MDnsTest::MockableRecordCallback,
- base::Unretained(this)));
+ scoped_ptr<MDnsTransaction> transaction1 =
+ test_client_.CreateTransaction(
+ dns_protocol::kTypePTR, "_privet._tcp.local",
+ MDnsTransaction::QUERY_NETWORK |
+ MDnsTransaction::QUERY_CACHE |
+ MDnsTransaction::SINGLE_RESULT,
+ base::Bind(&MDnsTest::MockableRecordCallback,
+ base::Unretained(this)));
- scoped_ptr<MDnsTransaction> transaction2 = test_client_->CreateTransaction(
- dns_protocol::kTypePTR, "_printer._tcp.local",
- MDnsTransaction::QUERY_CACHE |
- MDnsTransaction::SINGLE_RESULT,
- base::Bind(&MDnsTest::MockableRecordCallback2,
- base::Unretained(this)));
+ scoped_ptr<MDnsTransaction> transaction2 =
+ test_client_.CreateTransaction(
+ dns_protocol::kTypePTR, "_printer._tcp.local",
+ MDnsTransaction::QUERY_CACHE |
+ MDnsTransaction::SINGLE_RESULT,
+ base::Bind(&MDnsTest::MockableRecordCallback2,
+ base::Unretained(this)));
EXPECT_CALL(*this, MockableRecordCallback2(MDnsTransaction::RESULT_RECORD,
_))
TEST_F(MDnsTest, GoodbyePacketNotification) {
StrictMock<MockListenerDelegate> delegate_privet;
- scoped_ptr<MDnsListener> listener_privet = test_client_->CreateListener(
+ scoped_ptr<MDnsListener> listener_privet = test_client_.CreateListener(
dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet);
ASSERT_TRUE(listener_privet->Start());
TEST_F(MDnsTest, GoodbyePacketRemoval) {
StrictMock<MockListenerDelegate> delegate_privet;
- scoped_ptr<MDnsListener> listener_privet = test_client_->CreateListener(
- dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet);
+ scoped_ptr<MDnsListener> listener_privet =
+ test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local",
+ &delegate_privet);
ASSERT_TRUE(listener_privet->Start());
EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _))
TEST_F(MDnsTest, ListenerReentrantDelete) {
StrictMock<MockListenerDelegate> delegate_privet;
- listener1_ = test_client_->CreateListener(
- dns_protocol::kTypePTR, "_privet._tcp.local",
- &delegate_privet);
+ listener1_ = test_client_.CreateListener(dns_protocol::kTypePTR,
+ "_privet._tcp.local",
+ &delegate_privet);
- listener2_ = test_client_->CreateListener(
- dns_protocol::kTypePTR, "_privet._tcp.local",
- &delegate_privet);
+ listener2_ = test_client_.CreateListener(dns_protocol::kTypePTR,
+ "_privet._tcp.local",
+ &delegate_privet);
ASSERT_TRUE(listener1_->Start());
IPAddressNumber address;
StrictMock<MockListenerDelegate> delegate_privet;
- scoped_ptr<MDnsListener> listener_privet = test_client_->CreateListener(
- dns_protocol::kTypeA, "privet.local", &delegate_privet);
+ scoped_ptr<MDnsListener> listener_privet =
+ test_client_.CreateListener(dns_protocol::kTypeA, "privet.local",
+ &delegate_privet);
ASSERT_TRUE(listener_privet->Start());
TEST_F(MDnsTest, NsecWithListener) {
StrictMock<MockListenerDelegate> delegate_privet;
- scoped_ptr<MDnsListener> listener_privet = test_client_->CreateListener(
- dns_protocol::kTypeA, "_privet._tcp.local", &delegate_privet);
+ scoped_ptr<MDnsListener> listener_privet =
+ test_client_.CreateListener(dns_protocol::kTypeA, "_privet._tcp.local",
+ &delegate_privet);
// Test to make sure nsec callback is NOT called for PTR
// (which is marked as existing).
StrictMock<MockListenerDelegate> delegate_privet2;
- scoped_ptr<MDnsListener> listener_privet2 = test_client_->CreateListener(
- dns_protocol::kTypePTR, "_privet._tcp.local", &delegate_privet2);
+ scoped_ptr<MDnsListener> listener_privet2 =
+ test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local",
+ &delegate_privet2);
ASSERT_TRUE(listener_privet->Start());
TEST_F(MDnsTest, NsecWithTransactionFromNetwork) {
scoped_ptr<MDnsTransaction> transaction_privet =
- test_client_->CreateTransaction(
+ test_client_.CreateTransaction(
dns_protocol::kTypeA, "_privet._tcp.local",
MDnsTransaction::QUERY_NETWORK |
MDnsTransaction::QUERY_CACHE |
base::Bind(&MDnsTest::MockableRecordCallback,
base::Unretained(this)));
- EXPECT_CALL(*socket_factory_, OnSendTo(_))
- .Times(2);
+ EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(2);
ASSERT_TRUE(transaction_privet->Start());
// Force mDNS to listen.
StrictMock<MockListenerDelegate> delegate_irrelevant;
scoped_ptr<MDnsListener> listener_irrelevant =
- test_client_->CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local",
- &delegate_irrelevant);
+ test_client_.CreateListener(dns_protocol::kTypePTR, "_privet._tcp.local",
+ &delegate_irrelevant);
listener_irrelevant->Start();
SimulatePacketReceive(kSamplePacketNsec,
MockableRecordCallback(MDnsTransaction::RESULT_NSEC, NULL));
scoped_ptr<MDnsTransaction> transaction_privet_a =
- test_client_->CreateTransaction(
+ test_client_.CreateTransaction(
dns_protocol::kTypeA, "_privet._tcp.local",
MDnsTransaction::QUERY_NETWORK |
MDnsTransaction::QUERY_CACHE |
// valid answer to the query
scoped_ptr<MDnsTransaction> transaction_privet_ptr =
- test_client_->CreateTransaction(
+ test_client_.CreateTransaction(
dns_protocol::kTypePTR, "_privet._tcp.local",
MDnsTransaction::QUERY_NETWORK |
MDnsTransaction::QUERY_CACHE |
base::Bind(&MDnsTest::MockableRecordCallback,
base::Unretained(this)));
- EXPECT_CALL(*socket_factory_, OnSendTo(_))
- .Times(2);
+ EXPECT_CALL(socket_factory_, OnSendTo(_)).Times(2);
ASSERT_TRUE(transaction_privet_ptr->Start());
}
TEST_F(MDnsTest, NsecConflictRemoval) {
StrictMock<MockListenerDelegate> delegate_privet;
- scoped_ptr<MDnsListener> listener_privet = test_client_->CreateListener(
- dns_protocol::kTypeA, "_privet._tcp.local", &delegate_privet);
+ scoped_ptr<MDnsListener> listener_privet =
+ test_client_.CreateListener(dns_protocol::kTypeA, "_privet._tcp.local",
+ &delegate_privet);
ASSERT_TRUE(listener_privet->Start());
}
+TEST_F(MDnsTest, RefreshQuery) {
+ StrictMock<MockListenerDelegate> delegate_privet;
+ scoped_ptr<MDnsListener> listener_privet =
+ test_client_.CreateListener(dns_protocol::kTypeA, "_privet._tcp.local",
+ &delegate_privet);
+
+ listener_privet->SetActiveRefresh(true);
+ ASSERT_TRUE(listener_privet->Start());
+
+ EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_ADDED, _));
+
+ SimulatePacketReceive(kSamplePacketAPrivet,
+ sizeof(kSamplePacketAPrivet));
+
+ // Expecting 2 calls (one for ipv4 and one for ipv6) for each of the 2
+ // scheduled refresh queries.
+ EXPECT_CALL(socket_factory_, OnSendTo(
+ MakeString(kQueryPacketPrivetA, sizeof(kQueryPacketPrivetA))))
+ .Times(4);
+
+ EXPECT_CALL(delegate_privet, OnRecordUpdate(MDnsListener::RECORD_REMOVED, _));
+
+ RunFor(base::TimeDelta::FromSeconds(6));
+}
+
// Note: These tests assume that the ipv4 socket will always be created first.
// This is a simplifying assumption based on the way the code works now.
-
-class SimpleMockSocketFactory
- : public MDnsConnection::SocketFactory {
+class SimpleMockSocketFactory : public MDnsSocketFactory {
public:
- SimpleMockSocketFactory() {
- }
- virtual ~SimpleMockSocketFactory() {
- }
-
- virtual scoped_ptr<DatagramServerSocket> CreateSocket() OVERRIDE {
- MockMDnsDatagramServerSocket* socket = sockets_.back();
- sockets_.weak_erase(sockets_.end() - 1);
- return scoped_ptr<DatagramServerSocket>(socket);
+ virtual void CreateSockets(
+ ScopedVector<DatagramServerSocket>* sockets) OVERRIDE {
+ sockets->clear();
+ sockets->swap(sockets_);
}
- void PushSocket(MockMDnsDatagramServerSocket* socket) {
+ void PushSocket(DatagramServerSocket* socket) {
sockets_.push_back(socket);
}
private:
- ScopedVector<MockMDnsDatagramServerSocket> sockets_;
+ ScopedVector<DatagramServerSocket> sockets_;
};
class MockMDnsConnectionDelegate : public MDnsConnection::Delegate {
protected:
// Follow successful connection initialization.
virtual void SetUp() OVERRIDE {
- socket_ipv4_ = new MockMDnsDatagramServerSocket;
- socket_ipv6_ = new MockMDnsDatagramServerSocket;
+ socket_ipv4_ = new MockMDnsDatagramServerSocket(ADDRESS_FAMILY_IPV4);
+ socket_ipv6_ = new MockMDnsDatagramServerSocket(ADDRESS_FAMILY_IPV6);
factory_.PushSocket(socket_ipv6_);
factory_.PushSocket(socket_ipv4_);
}
bool InitConnection() {
- EXPECT_CALL(*socket_ipv4_, AllowAddressReuse());
- EXPECT_CALL(*socket_ipv6_, AllowAddressReuse());
-
- EXPECT_CALL(*socket_ipv4_, SetMulticastLoopbackMode(false));
- EXPECT_CALL(*socket_ipv6_, SetMulticastLoopbackMode(false));
-
- EXPECT_CALL(*socket_ipv4_, ListenInternal("0.0.0.0:5353"))
- .WillOnce(Return(OK));
- EXPECT_CALL(*socket_ipv6_, ListenInternal("[::]:5353"))
- .WillOnce(Return(OK));
-
- EXPECT_CALL(*socket_ipv4_, JoinGroupInternal("224.0.0.251"))
- .WillOnce(Return(OK));
- EXPECT_CALL(*socket_ipv6_, JoinGroupInternal("ff02::fb"))
- .WillOnce(Return(OK));
-
return connection_.Init(&factory_);
}