OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 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_client.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/memory/scoped_ptr.h" | |
9 #include "net/base/net_log.h" | |
10 #include "net/base/sys_addrinfo.h" | |
11 #include "net/dns/dns_response.h" | |
12 #include "net/dns/dns_session.h" | |
13 #include "net/dns/dns_test_util.h" | |
14 #include "net/socket/socket_test_util.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 // TODO(szym): test failures (NXDOMAIN, etc.) | |
18 // TODO(szym): test DnsClient::Request::Start with synchronous failure | |
19 // TODO(szym): test suffix search once implemented | |
20 | |
21 namespace net { | |
22 | |
23 namespace { | |
mmenke
2011/12/02 00:53:55
If you don't need to make the test the friend of t
szym
2011/12/05 23:06:28
Done.
| |
24 const int kTransactionIds[] = {0, 1, 2, 3}; | |
25 const int kNumTransactions = arraysize(kTransactionIds); | |
26 } | |
27 | |
28 class DnsClientTest : public testing::Test { | |
29 public: | |
30 struct TestRequest { | |
mmenke
2011/12/02 00:53:55
This is sufficiently big and complicated that I th
mmenke
2011/12/02 00:53:55
"TestRequest" seems like a misnomer, since it's ac
szym
2011/12/05 23:06:28
Done.
| |
31 void Init(const char* name, | |
32 uint16 type, | |
33 const MockWrite& write, | |
34 const MockRead& read, | |
35 int answer_count) { | |
36 // Must include the terminating \x00. | |
37 qname = std::string(name, strlen(name) + 1); | |
38 qtype = type; | |
39 expected_answer_count = answer_count; | |
40 completed = false; | |
41 writes.push_back(write); | |
42 reads.push_back(read); | |
43 data.reset(new StaticSocketDataProvider(&reads[0], reads.size(), | |
44 &writes[0], writes.size())); | |
45 } | |
46 | |
47 void MakeRequest(DnsClient* client) { | |
48 DCHECK(request.get() == NULL); | |
49 request.reset(client->CreateRequest( | |
50 qname, | |
51 qtype, | |
52 base::Bind(&TestRequest::OnRequestComplete, | |
53 base::Unretained(this)), | |
54 BoundNetLog())); | |
55 EXPECT_EQ(qname, request->qname()); | |
56 EXPECT_EQ(qtype, request->qtype()); | |
57 EXPECT_EQ(ERR_IO_PENDING, request->Start()); | |
58 } | |
59 | |
60 void Cancel() { | |
61 DCHECK(request.get() != NULL); | |
mmenke
2011/12/02 00:53:55
ASSERT_TRUE(request.get()) is preferred in unit te
szym
2011/12/05 23:06:28
Done, although ASSERT_* does not work in functions
| |
62 request.reset(NULL); | |
63 } | |
64 | |
65 void OnRequestComplete(DnsClient::Request* req, | |
66 int rv, | |
67 const DnsResponse* response) { | |
68 EXPECT_FALSE(completed); | |
69 EXPECT_EQ(request.get(), req); | |
70 | |
71 EXPECT_EQ(OK, rv); | |
72 EXPECT_EQ(expected_answer_count, response->answer_count()); | |
73 | |
74 DnsRecordParser parser = response->Parser(); | |
75 DnsResourceRecord record; | |
76 for (int i = 0; i < expected_answer_count; ++i) { | |
77 EXPECT_TRUE(parser.ParseRecord(&record)); | |
78 } | |
79 EXPECT_TRUE(parser.AtEnd()); | |
80 | |
81 completed = true; | |
82 } | |
83 | |
84 void OnRequestCompleteCancel(DnsClient::Request* req, | |
mmenke
2011/12/02 00:53:55
I find this function name a little vague. It coul
szym
2011/12/05 23:06:28
Done.
| |
85 int rv, | |
86 const DnsResponse* response) { | |
87 EXPECT_FALSE(completed); | |
88 Cancel(); | |
89 } | |
90 | |
91 std::string qname; | |
92 uint16 qtype; | |
93 std::vector<MockWrite> writes; | |
94 std::vector<MockRead> reads; | |
95 scoped_ptr<StaticSocketDataProvider> data; | |
96 scoped_ptr<DnsClient::Request> request; | |
97 int expected_answer_count; | |
98 | |
99 bool completed; | |
100 }; | |
101 | |
102 DnsClientTest() | |
103 : transaction_ids_(kTransactionIds, | |
104 kTransactionIds + arraysize(kTransactionIds)) { | |
105 requests_[0].Init( | |
106 kT0DnsName, | |
107 kT0Qtype, | |
108 MockWrite(true, reinterpret_cast<const char*>(kT0QueryDatagram), | |
109 arraysize(kT0QueryDatagram)), | |
110 MockRead(true, reinterpret_cast<const char*>(kT0ResponseDatagram), | |
111 arraysize(kT0ResponseDatagram)), | |
112 arraysize(kT0IpAddresses) + 1); // +1 for CNAME RR | |
113 | |
114 requests_[1].Init( | |
115 kT1DnsName, | |
116 kT1Qtype, | |
117 MockWrite(true, reinterpret_cast<const char*>(kT1QueryDatagram), | |
118 arraysize(kT1QueryDatagram)), | |
119 MockRead(true, reinterpret_cast<const char*>(kT1ResponseDatagram), | |
120 arraysize(kT1ResponseDatagram)), | |
121 arraysize(kT1IpAddresses) + 1); // +1 for CNAME RR | |
122 | |
123 requests_[2].Init( | |
124 kT2DnsName, | |
125 kT2Qtype, | |
126 MockWrite(true, reinterpret_cast<const char*>(kT2QueryDatagram), | |
127 arraysize(kT2QueryDatagram)), | |
128 MockRead(true, reinterpret_cast<const char*>(kT2ResponseDatagram), | |
129 arraysize(kT2ResponseDatagram)), | |
130 arraysize(kT2IpAddresses) + 1); // +1 for CNAME RR | |
131 | |
132 requests_[3].Init( | |
133 kT3DnsName, | |
134 kT3Qtype, | |
135 MockWrite(true, reinterpret_cast<const char*>(kT3QueryDatagram), | |
136 arraysize(kT3QueryDatagram)), | |
137 MockRead(true, reinterpret_cast<const char*>(kT3ResponseDatagram), | |
138 arraysize(kT3ResponseDatagram)), | |
139 arraysize(kT3IpAddresses) + 2); // +2 for CNAME RR | |
140 | |
141 MockClientSocketFactory* factory = new MockClientSocketFactory(); | |
142 | |
143 for (int i = 0; i < kNumTransactions; ++i) { | |
144 factory->AddSocketDataProvider(requests_[i].data.get()); | |
145 } | |
146 | |
147 DnsConfig config; | |
148 | |
149 IPEndPoint dns_server; | |
150 { | |
151 bool rv = CreateDnsAddress(kDnsIp, kDnsPort, &dns_server); | |
152 DCHECK(rv); | |
mmenke
2011/12/02 00:53:55
EXPECT_EQ(OK, rv);
szym
2011/12/05 23:06:28
Done.
| |
153 } | |
154 config.nameservers.push_back(dns_server); | |
155 | |
156 DnsSession* session = new DnsSession( | |
157 config, | |
158 factory, | |
159 base::Bind(&DnsClientTest::GetNextId, base::Unretained(this)), | |
160 NULL /* NetLog */); | |
161 | |
162 client_.reset(DnsClient::CreateClient(session)); | |
163 } | |
164 | |
165 int GetNextId(int min, int max) { | |
166 DCHECK(!transaction_ids_.empty()); | |
mmenke
2011/12/02 00:53:55
EXPECT_FALSE(transaction_ids_.empty());
szym
2011/12/05 23:06:28
Done.
| |
167 int id = transaction_ids_.front(); | |
168 transaction_ids_.pop_front(); | |
169 DCHECK(id >= min && id <= max); | |
mmenke
2011/12/02 00:53:55
EXPECT_GE(id, min);
EXPECT_LE(id, max);
szym
2011/12/05 23:06:28
Done.
| |
170 return id; | |
171 } | |
172 | |
173 protected: | |
174 TestRequest requests_[kNumTransactions]; | |
175 std::deque<int> transaction_ids_; | |
176 scoped_ptr<DnsClient> client_; | |
177 }; | |
178 | |
179 TEST_F(DnsClientTest, Lookup) { | |
180 requests_[0].MakeRequest(client_.get()); | |
181 | |
182 // wait until result | |
mmenke
2011/12/02 00:53:55
nit: Wait until result.
szym
2011/12/05 23:06:28
Done.
| |
183 MessageLoop::current()->RunAllPending(); | |
184 | |
185 EXPECT_TRUE(requests_[0].completed); | |
186 } | |
187 | |
188 TEST_F(DnsClientTest, ConcurrentLookup) { | |
189 for (int i = 0; i < kNumTransactions; ++i) { | |
190 requests_[i].MakeRequest(client_.get()); | |
191 } | |
192 | |
193 MessageLoop::current()->RunAllPending(); | |
194 | |
195 for (int i = 0; i < kNumTransactions; ++i) { | |
196 EXPECT_TRUE(requests_[i].completed); | |
197 } | |
198 } | |
199 | |
200 TEST_F(DnsClientTest, CancelLookup) { | |
201 for (int i = 0; i < kNumTransactions; ++i) { | |
202 requests_[i].MakeRequest(client_.get()); | |
203 } | |
204 | |
205 requests_[0].Cancel(); | |
206 requests_[2].Cancel(); | |
207 | |
208 MessageLoop::current()->RunAllPending(); | |
209 | |
210 EXPECT_FALSE(requests_[0].completed); | |
211 EXPECT_TRUE(requests_[1].completed); | |
212 EXPECT_FALSE(requests_[2].completed); | |
213 EXPECT_TRUE(requests_[3].completed); | |
214 } | |
215 | |
216 TEST_F(DnsClientTest, DestroyClient) { | |
217 for (int i = 0; i < kNumTransactions; ++i) { | |
218 requests_[i].MakeRequest(client_.get()); | |
219 } | |
220 | |
221 // Destroying the client does not affect running requests. | |
222 client_.reset(NULL); | |
223 | |
224 MessageLoop::current()->RunAllPending(); | |
225 | |
226 for (int i = 0; i < kNumTransactions; ++i) { | |
227 EXPECT_TRUE(requests_[i].completed); | |
228 } | |
229 } | |
230 | |
231 TEST_F(DnsClientTest, DestroyRequestFromCallback) { | |
232 // Custom callback to cancel the completing request. | |
233 requests_[0].request.reset(client_->CreateRequest( | |
234 requests_[0].qname, | |
235 requests_[0].qtype, | |
236 base::Bind(&TestRequest::OnRequestCompleteCancel, | |
237 base::Unretained(&requests_[0])), | |
238 BoundNetLog())); | |
239 requests_[0].request->Start(); | |
240 | |
241 for (int i = 1; i < kNumTransactions; ++i) { | |
242 requests_[i].MakeRequest(client_.get()); | |
mmenke
2011/12/02 00:53:55
nit: Too much indentation.
szym
2011/12/05 23:06:28
Done.
| |
243 } | |
244 | |
245 MessageLoop::current()->RunAllPending(); | |
246 | |
247 EXPECT_FALSE(requests_[0].completed); | |
248 for (int i = 1; i < kNumTransactions; ++i) { | |
249 EXPECT_TRUE(requests_[i].completed); | |
250 } | |
251 } | |
252 | |
mmenke
2011/12/02 00:53:55
Think it might be nice to have a test for a simula
szym
2011/12/05 23:06:28
Done.
| |
253 } // namespace net | |
254 | |
OLD | NEW |