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/dns/dns_protocol.h" | |
| 11 #include "net/socket/socket_test_util.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 namespace net { | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 class TestSocketFactory; | |
| 19 | |
| 20 class TestUDPClientSocket : public MockUDPClientSocket { | |
| 21 public: | |
| 22 TestUDPClientSocket(TestSocketFactory* factory, | |
| 23 SocketDataProvider* data, | |
| 24 net::NetLog* net_log) | |
| 25 : MockUDPClientSocket(data, net_log), factory_(factory) {} | |
| 26 virtual ~TestUDPClientSocket(); | |
| 27 private: | |
| 28 TestSocketFactory* factory_; | |
| 29 }; | |
| 30 | |
| 31 // TODO(ttuttle): Does it make sense to subclass MockClientSocketFactory? | |
| 32 // We're basically entirely overriding the functionality it | |
| 33 // provides. | |
| 34 class TestSocketFactory : public MockClientSocketFactory { | |
| 35 public: | |
| 36 TestSocketFactory() : pending_created_(0), pending_destroyed_(0) {} | |
| 37 virtual ~TestSocketFactory() {} | |
| 38 | |
| 39 virtual DatagramClientSocket* CreateDatagramClientSocket( | |
| 40 DatagramSocket::BindType bind_type, | |
| 41 const RandIntCallback& rand_int_cb, | |
| 42 net::NetLog* net_log, | |
| 43 const net::NetLog::Source& source) OVERRIDE { | |
| 44 // We're not actually expecting to send or receive any data, so use the | |
| 45 // simplest SocketDataProvider with no data supplied. | |
| 46 SocketDataProvider* data_provider = new StaticSocketDataProvider(); | |
| 47 TestUDPClientSocket* socket = new TestUDPClientSocket(this, | |
| 48 data_provider, | |
| 49 net_log); | |
| 50 data_provider->set_socket(socket); | |
| 51 pending_created_++; | |
| 52 return socket; | |
| 53 } | |
| 54 | |
| 55 void OnSocketDestroyed() { | |
| 56 ++pending_destroyed_; | |
| 57 } | |
| 58 | |
| 59 void ExpectSocketsCreated(int created) { | |
| 60 EXPECT_EQ(created, pending_created_); | |
| 61 pending_created_ = 0; | |
| 62 } | |
| 63 | |
| 64 void ExpectSocketsDestroyed(int destroyed) { | |
| 65 EXPECT_EQ(destroyed, pending_destroyed_); | |
| 66 pending_destroyed_ = 0; | |
| 67 } | |
| 68 | |
| 69 protected: | |
| 70 int pending_created_; | |
| 71 int pending_destroyed_; | |
| 72 }; | |
| 73 | |
| 74 TestUDPClientSocket::~TestUDPClientSocket() { | |
| 75 factory_->OnSocketDestroyed(); | |
| 76 } | |
| 77 | |
| 78 typedef DnsSession::SocketLease SocketLease; | |
| 79 static const size_t kBufferSize = 4; | |
| 80 | |
| 81 class DnsSessionTest : public testing::Test { | |
| 82 public: | |
| 83 void Initialize() { | |
| 84 InitializeFactory(); | |
| 85 InitializeConfig(1); | |
| 86 InitializeSession(); | |
| 87 } | |
| 88 | |
| 89 protected: | |
| 90 DnsConfig config_; | |
| 91 scoped_ptr<TestSocketFactory> socket_factory_; | |
| 92 scoped_refptr<DnsSession> session_; | |
| 93 | |
| 94 void InitializeFactory() { | |
| 95 socket_factory_.reset(new TestSocketFactory()); | |
| 96 } | |
| 97 | |
| 98 // TODO(ttuttle): Refactor this and the one in DnsTransactionTest somewhere. | |
| 99 void InitializeConfig(unsigned num_servers) { | |
| 100 CHECK_LE(num_servers, 255u); | |
| 101 config_.nameservers.clear(); | |
| 102 IPAddressNumber dns_ip; | |
| 103 bool rv = ParseIPLiteralToNumber("192.168.1.0", &dns_ip); | |
| 104 EXPECT_TRUE(rv); | |
| 105 for (unsigned i = 0; i < num_servers; ++i) { | |
| 106 dns_ip[3] = i; | |
| 107 IPEndPoint dns_endpoint(dns_ip, dns_protocol::kDefaultPort); | |
| 108 config_.nameservers.push_back(dns_endpoint); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void InitializeSession() { | |
| 113 session_ = new DnsSession(config_, | |
| 114 socket_factory_.get(), | |
| 115 base::Bind(&base::RandInt), | |
| 116 NULL /* NetLog */); | |
| 117 } | |
| 118 | |
| 119 void ExpectCreated(int num) { | |
| 120 socket_factory_->ExpectSocketsCreated(num); | |
| 
 
szym
2012/09/10 19:31:00
Wrapping EXPECT_* in methods makes it more difficu
 
Deprecated (see juliatuttle)
2012/09/14 15:28:18
Done.
 
 | |
| 121 } | |
| 122 | |
| 123 void ExpectDestroyed(int num) { | |
| 124 socket_factory_->ExpectSocketsDestroyed(num); | |
| 125 } | |
| 126 }; | |
| 127 | |
| 128 /* | |
| 129 * TODO(ttuttle): | |
| 130 * Mock out SocketPools and make sure the pools get the right calls. | |
| 131 * Test the default SocketPool implementation. | |
| 132 * ...while in DnsSession or separately? | |
| 133 */ | |
| 134 | |
| 135 } | |
| 136 | |
| 137 } | |
| OLD | NEW |