Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "net/dns/dns_session.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/memory/scoped_ptr.h" | |
| 9 #include "base/rand_util.h" | |
| 10 #include "net/base/net_log.h" | |
| 11 #include "net/dns/dns_protocol.h" | |
| 12 #include "net/dns/dns_socket_pool.h" | |
| 13 #include "net/socket/socket_test_util.h" | |
| 14 #include "testing/gtest/include/gtest/gtest.h" | |
| 15 | |
| 16 namespace net { | |
| 17 | |
| 18 namespace { | |
| 19 | |
| 20 class TestClientSocketFactory : public MockClientSocketFactory { | |
|
szym
2012/09/14 20:49:27
Why are you deriving from MockClientSocketFactory?
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 21 public: | |
| 22 virtual DatagramClientSocket* CreateDatagramClientSocket( | |
|
szym
2012/09/14 20:49:27
You will need to add a virtual destructor for this
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 23 DatagramSocket::BindType bind_type, | |
| 24 const RandIntCallback& rand_int_cb, | |
| 25 net::NetLog* net_log, | |
| 26 const net::NetLog::Source& source) OVERRIDE; | |
| 27 }; | |
| 28 | |
| 29 struct PoolEvent { | |
| 30 enum { ALLOCATE, FREE } action_; | |
|
szym
2012/09/14 20:49:27
nit: public fields should have no "_" suffix.
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 31 unsigned server_index_; | |
| 32 }; | |
| 33 | |
| 34 class DnsSessionTest : public testing::Test { | |
| 35 public: | |
| 36 void OnSocketAllocated(unsigned server_index); | |
| 37 void OnSocketFreed(unsigned server_index); | |
| 38 | |
| 39 protected: | |
| 40 void Initialize(unsigned char num_servers); | |
|
szym
2012/09/14 20:49:27
unsigned char? make it unsigned (int). If you want
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 41 scoped_ptr<DnsSession::SocketLease> Allocate(unsigned server_index); | |
| 42 bool Allocated(unsigned server_index); | |
|
szym
2012/09/14 20:49:27
I'm ok with Allocated, but perhaps "DidAllocate" i
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 43 bool Freed(unsigned server_index); | |
| 44 bool Ready(); | |
| 45 | |
| 46 DnsConfig config_; | |
| 47 scoped_ptr<TestClientSocketFactory> test_client_socket_factory_; | |
| 48 scoped_refptr<DnsSession> session_; | |
| 49 NetLog::Source source_; | |
| 50 | |
| 51 private: | |
| 52 bool ExpectEvent(const PoolEvent& event); | |
|
szym
2012/09/14 20:49:27
suggest: "ReceivedEvent" or "EventHappened" etc.
Deprecated (see juliatuttle)
2012/09/18 20:28:00
The problem with those is that either could be use
| |
| 53 std::list<PoolEvent> events_; | |
| 54 }; | |
| 55 | |
| 56 class NullDnsSocketPool : public DnsSocketPool { | |
| 57 public: | |
| 58 NullDnsSocketPool(DnsSessionTest* test) : test_(test) { } | |
|
szym
2012/09/14 20:49:27
need virtual destructor
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 59 | |
| 60 virtual scoped_ptr<DatagramClientSocket> AllocateSocket( | |
| 61 unsigned server_index) OVERRIDE { | |
| 62 test_->OnSocketAllocated(server_index); | |
| 63 return CreateConnectedSocket(server_index).Pass(); | |
| 64 } | |
| 65 | |
| 66 virtual void FreeSocket( | |
| 67 unsigned server_index, | |
| 68 scoped_ptr<DatagramClientSocket> socket) OVERRIDE { | |
| 69 test_->OnSocketFreed(server_index); | |
| 70 } | |
| 71 | |
| 72 private: | |
| 73 DnsSessionTest* test_; | |
| 74 }; | |
| 75 | |
| 76 void DnsSessionTest::Initialize(unsigned char num_servers) { | |
| 77 config_.nameservers.clear(); | |
| 78 IPAddressNumber dns_ip; | |
| 79 bool rv = ParseIPLiteralToNumber("192.168.1.0", &dns_ip); | |
| 80 EXPECT_TRUE(rv); | |
| 81 for (unsigned char i = 0; i < num_servers; i++) { | |
|
szym
2012/09/14 20:49:27
nit: ++i
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 82 dns_ip[3] = i; | |
| 83 IPEndPoint dns_endpoint(dns_ip, dns_protocol::kDefaultPort); | |
| 84 config_.nameservers.push_back(dns_endpoint); | |
| 85 } | |
| 86 | |
| 87 test_client_socket_factory_.reset(new TestClientSocketFactory()); | |
| 88 ClientSocketFactory* client_socket_factory = | |
| 89 static_cast<ClientSocketFactory*>(test_client_socket_factory_.get()); | |
| 90 | |
| 91 DnsSocketPool* dns_socket_pool = | |
| 92 static_cast<DnsSocketPool*>(new NullDnsSocketPool(this)); | |
|
szym
2012/09/14 20:49:27
No need for static_cast to base class.
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 93 | |
| 94 session_ = new DnsSession(config_, | |
| 95 client_socket_factory, | |
| 96 scoped_ptr<DnsSocketPool>(dns_socket_pool), | |
| 97 base::Bind(&base::RandInt), | |
| 98 NULL /* NetLog */); | |
| 99 | |
| 100 events_.clear(); | |
| 101 } | |
| 102 | |
| 103 scoped_ptr<DnsSession::SocketLease> DnsSessionTest::Allocate( | |
| 104 unsigned server_index) { | |
| 105 return session_->AllocateSocket(server_index, source_); | |
| 106 } | |
| 107 | |
| 108 bool DnsSessionTest::Allocated(unsigned server_index) { | |
| 109 PoolEvent expected_event = { PoolEvent::ALLOCATE, server_index }; | |
| 110 return ExpectEvent(expected_event); | |
| 111 } | |
| 112 | |
| 113 bool DnsSessionTest::Freed(unsigned server_index) { | |
| 114 PoolEvent expected_event = { PoolEvent::FREE, server_index }; | |
| 115 return ExpectEvent(expected_event); | |
| 116 } | |
| 117 | |
| 118 bool DnsSessionTest::Ready() { | |
|
szym
2012/09/14 20:49:27
suggest: "NoMoreEvents"
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 119 return events_.empty(); | |
| 120 } | |
| 121 | |
| 122 void DnsSessionTest::OnSocketAllocated(unsigned server_index) { | |
| 123 PoolEvent event = { PoolEvent::ALLOCATE, server_index }; | |
| 124 events_.push_back(event); | |
| 125 } | |
| 126 | |
| 127 void DnsSessionTest::OnSocketFreed(unsigned server_index) { | |
| 128 PoolEvent event = { PoolEvent::FREE, server_index }; | |
| 129 events_.push_back(event); | |
| 130 } | |
| 131 | |
| 132 bool DnsSessionTest::ExpectEvent(const PoolEvent& expected) { | |
| 133 if (events_.empty()) { | |
| 134 return false; | |
| 135 } | |
| 136 | |
| 137 const PoolEvent actual = events_.front(); | |
| 138 if ((expected.action_ != actual.action_) | |
| 139 || (expected.server_index_ != actual.server_index_)) { | |
| 140 return false; | |
| 141 } | |
| 142 events_.pop_front(); | |
| 143 | |
| 144 return true; | |
| 145 } | |
| 146 | |
| 147 DatagramClientSocket* TestClientSocketFactory::CreateDatagramClientSocket( | |
| 148 DatagramSocket::BindType bind_type, | |
| 149 const RandIntCallback& rand_int_cb, | |
| 150 net::NetLog* net_log, | |
| 151 const net::NetLog::Source& source) { | |
| 152 // We're not actually expecting to send or receive any data, so use the | |
| 153 // simplest SocketDataProvider with no data supplied. | |
| 154 SocketDataProvider* data_provider = new StaticSocketDataProvider(); | |
|
szym
2012/09/14 20:49:27
You are leaking |data_provider|. MockUDPClientSock
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 155 MockUDPClientSocket* socket = new MockUDPClientSocket(data_provider, net_log); | |
| 156 data_provider->set_socket(socket); | |
| 157 return socket; | |
| 158 } | |
| 159 | |
| 160 /* | |
| 161 * TODO(ttuttle): | |
| 162 * Mock out SocketPools and make sure the pools get the right calls. | |
| 163 * Test the default SocketPool implementation. | |
| 164 * ...while in DnsSession or separately? | |
|
szym
2012/09/14 20:49:27
Since you moved DnsSocketPool implementation to dn
| |
| 165 */ | |
| 166 | |
| 167 TEST_F(DnsSessionTest, AllocateFree) { | |
| 168 scoped_ptr<DnsSession::SocketLease> lease1, lease2; | |
| 169 | |
| 170 Initialize(2); | |
| 171 EXPECT_TRUE(Ready()); | |
| 172 | |
| 173 lease1 = Allocate(0); | |
| 174 EXPECT_TRUE(Allocated(0)); | |
| 175 EXPECT_TRUE(Ready()); | |
| 176 | |
| 177 lease2 = Allocate(1); | |
| 178 EXPECT_TRUE(Allocated(1)); | |
| 179 EXPECT_TRUE(Ready()); | |
| 180 | |
| 181 lease1.reset(); | |
| 182 EXPECT_TRUE(Freed(0)); | |
| 183 EXPECT_TRUE(Ready()); | |
| 184 | |
| 185 lease2.reset(); | |
| 186 EXPECT_TRUE(Freed(1)); | |
| 187 EXPECT_TRUE(Ready()); | |
| 188 } | |
| 189 | |
| 190 } | |
|
szym
2012/09/14 20:49:27
nit: // namespace
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| 191 | |
| 192 } | |
|
szym
2012/09/14 20:49:27
nit: // namespace net
Deprecated (see juliatuttle)
2012/09/18 20:28:00
Done.
| |
| OLD | NEW |