Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(239)

Side by Side Diff: net/dns/address_sorter_posix_unittest.cc

Issue 10442098: [net/dns] Resolve AF_UNSPEC on dual-stacked systems. Sort addresses according to RFC3484. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Replace WaitableEvent with TestCompletionCallback. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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/address_sorter_posix.h"
6
7 #include "base/bind.h"
8 #include "base/logging.h"
9 #include "net/base/net_errors.h"
10 #include "net/base/net_util.h"
11 #include "net/base/test_completion_callback.h"
12 #include "net/socket/client_socket_factory.h"
13 #include "net/udp/datagram_client_socket.h"
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 namespace net {
17 namespace {
18
19 // Used to map destination address to source address.
20 typedef std::map<IPAddressNumber, IPAddressNumber> AddressMapping;
21
22 IPAddressNumber ParseIP(const std::string& str) {
23 IPAddressNumber addr;
24 CHECK(ParseIPLiteralToNumber(str, &addr));
25 return addr;
26 }
27
28 // A mock socket which binds to source address according to AddressMapping.
29 class TestUDPClientSocket : public DatagramClientSocket {
30 public:
31 explicit TestUDPClientSocket(const AddressMapping* mapping)
32 : mapping_(mapping), connected_(false) {}
33
34 virtual ~TestUDPClientSocket() {}
35
36 virtual int Read(IOBuffer*, int, const CompletionCallback&) OVERRIDE {
37 NOTIMPLEMENTED();
38 return OK;
39 }
40 virtual int Write(IOBuffer*, int, const CompletionCallback&) OVERRIDE {
41 NOTIMPLEMENTED();
42 return OK;
43 }
44 virtual bool SetReceiveBufferSize(int32) OVERRIDE {
45 return true;
46 }
47 virtual bool SetSendBufferSize(int32) OVERRIDE {
48 return true;
49 }
50
51 virtual void Close() OVERRIDE {}
52 virtual int GetPeerAddress(IPEndPoint* address) const {
mmenke 2012/08/09 19:51:11 nit: OVERRIDE
mmenke 2012/08/09 19:51:11 Do we even need this function?
53 if (!connected_)
54 return ERR_UNEXPECTED;
55 *address = remote_endpoint_;
56 return OK;
57 }
58 virtual int GetLocalAddress(IPEndPoint* address) const {
mmenke 2012/08/09 19:51:11 nit: OVERRIDE
59 if (!connected_)
60 return ERR_UNEXPECTED;
61 *address = local_endpoint_;
62 return OK;
63 }
64
65 virtual int Connect(const IPEndPoint& remote) OVERRIDE {
66 if (connected_)
67 return ERR_UNEXPECTED;
68 AddressMapping::const_iterator it = mapping_->find(remote.address());
69 if (it == mapping_->end())
70 return ERR_FAILED;
71 connected_ = true;
72 remote_endpoint_ = remote;
73 local_endpoint_ = IPEndPoint(it->second, 39874 /* arbitrary port */);
74 return OK;
75 }
76
77 virtual const BoundNetLog& NetLog() const {
mmenke 2012/08/09 19:51:11 nit: OVERRIDE
78 return net_log_;
79 }
80
81 private:
82 BoundNetLog net_log_;
83 const AddressMapping* mapping_;
84 bool connected_;
85 IPEndPoint remote_endpoint_;
86 IPEndPoint local_endpoint_;
mmenke 2012/08/09 19:51:11 nit: Last two can be const.
szym 2012/08/11 22:04:20 They are set in Connect not in constructor.
87
88 DISALLOW_COPY_AND_ASSIGN(TestUDPClientSocket);
89 };
90
91 // Creates TestUDPClientSockets and maintains an AddressMapping.
92 class TestSocketFactory : public ClientSocketFactory {
93 public:
94 TestSocketFactory() {}
95 virtual ~TestSocketFactory() {}
96
97 virtual DatagramClientSocket* CreateDatagramClientSocket(
98 DatagramSocket::BindType,
99 const RandIntCallback&,
100 NetLog*,
101 const NetLog::Source&) OVERRIDE {
102 return new TestUDPClientSocket(&mapping_);
103 }
104 virtual StreamSocket* CreateTransportClientSocket(
105 const AddressList&,
106 NetLog*,
107 const NetLog::Source&) OVERRIDE {
108 NOTIMPLEMENTED();
109 return NULL;
110 }
111 virtual SSLClientSocket* CreateSSLClientSocket(
112 ClientSocketHandle*,
113 const HostPortPair&,
114 const SSLConfig&,
115 const SSLClientSocketContext&) OVERRIDE {
116 NOTIMPLEMENTED();
117 return NULL;
118 }
119 virtual void ClearSSLSessionCache() OVERRIDE {
120 NOTIMPLEMENTED();
121 }
122
123 void AddMapping(const IPAddressNumber& dst, const IPAddressNumber& src) {
124 mapping_[dst] = src;
125 }
126
127 private:
128 AddressMapping mapping_;
129
130 DISALLOW_COPY_AND_ASSIGN(TestSocketFactory);
131 };
132
133 void OnSortComplete(AddressList* result_buf,
134 const CompletionCallback& callback,
135 bool success,
136 const AddressList& result) {
137 EXPECT_TRUE(success);
138 if (success)
139 *result_buf = result;
140 callback.Run(OK);
141 }
142
143 } // namespace
144
145 class AddressSorterPosixTest : public testing::Test {
146 protected:
147 AddressSorterPosixTest() : sorter_(&socket_factory_) {}
148
149 void AddMapping(const std::string& dst, const std::string& src) {
150 socket_factory_.AddMapping(ParseIP(dst), ParseIP(src));
151 }
152
153 SourceAddressInfo* GetSourceInfo(const std::string& addr) {
154 IPAddressNumber address = ParseIP(addr);
155 SourceAddressInfo* info = &sorter_.source_map_[address];
156 if (info->scope == SCOPE_UNDEFINED)
157 sorter_.FillPolicy(address, info);
158 return info;
159 }
160
161 // Verify that NULL-terminated |addresses| matches (-1)-terminated |order|
162 // after sorting.
163 void Verify(const char* addresses[], const int order[]) {
164 AddressList list;
165 for (const char** addr = addresses; *addr != NULL; ++addr)
166 list.push_back(IPEndPoint(ParseIP(*addr), 80));
167 for (size_t i = 0; order[i] >= 0; ++i)
168 CHECK_LT(order[i], static_cast<int>(list.size()));
169
170 AddressList result;
171 TestCompletionCallback callback;
172 sorter_.Sort(list, base::Bind(&OnSortComplete, &result,
173 callback.callback()));
174 callback.WaitForResult();
175
176 for (size_t i = 0; (i < result.size()) || (order[i] >= 0); ++i) {
177 IPEndPoint expected = order[i] >= 0 ? list[order[i]] : IPEndPoint();
178 IPEndPoint actual = i < result.size() ? result[i] : IPEndPoint();
179 EXPECT_TRUE(expected.address() == actual.address()) <<
180 "Address out of order at position " << i << "\n" <<
181 " Actual: " << actual.ToStringWithoutPort() << "\n" <<
182 "Expected: " << expected.ToStringWithoutPort();
183 }
184 }
185
186 TestSocketFactory socket_factory_;
187 AddressSorterPosix sorter_;
188 };
189
190 // Rule 1: Avoid unusable destinations.
191 TEST_F(AddressSorterPosixTest, Rule1) {
192 AddMapping("10.0.0.231", "10.0.0.1");
193 const char* addresses[] = { "1.2.3.4", "10.0.0.231", "127.0.0.1", NULL };
mmenke 2012/08/09 19:51:11 Just for completeness, might want an IPv6 ip that'
194 const int order[] = { 1, -1 };
195 Verify(addresses, order);
196 }
197
198 // Rule 2: Prefer matching scope.
199 TEST_F(AddressSorterPosixTest, Rule2) {
200 AddMapping("3002::1", "4000::10"); // matching global
201 AddMapping("ff32::1", "fe81::10"); // matching link-local
202 AddMapping("fec1::1", "fec1::10"); // matching node-local
203 AddMapping("8.0.0.1", "169.254.0.10"); // global vs. link-local
204 AddMapping("3002::2", "::1"); // global vs. link-local
205 AddMapping("fec1::2", "fe81::10"); // site-local vs. link-local
mmenke 2012/08/09 19:51:11 Suggest you put the bottom 3 in the same order tha
206 // In all three cases, matching scope is preferred.
207 const int order[] = { 1, 0, -1 };
208 {
209 const char* addresses[] = { "3002::2", "3002::1", NULL };
210 Verify(addresses, order);
mmenke 2012/08/09 19:51:11 Suggest just using different names (addresses1, ad
211 }
212 {
213 const char* addresses[] = { "fec1::2", "ff32::1", NULL };
214 Verify(addresses, order);
215 }
216 {
217 const char* addresses[] = { "8.0.0.1", "fec1::1", NULL };
218 Verify(addresses, order);
219 }
220 }
221
222 // Rule 3: Avoid deprecated addresses.
223 TEST_F(AddressSorterPosixTest, Rule3) {
224 // Matching scope.
225 AddMapping("3002::1", "4000::10");
226 GetSourceInfo("4000::10")->deprecated = true;
227 AddMapping("3002::2", "4000::20");
228 const char* addresses[] = { "3002::1", "3002::2", NULL };
229 const int order[] = { 1, 0, -1 };
230 Verify(addresses, order);
231 }
232
233 // Rule 4: Prefer home addresses.
234 TEST_F(AddressSorterPosixTest, Rule4) {
235 AddMapping("3002::1", "4000::10");
236 AddMapping("3002::2", "4000::20");
237 GetSourceInfo("4000::20")->home = true;
238 const char* addresses[] = { "3002::1", "3002::2", NULL };
239 const int order[] = { 1, 0, -1 };
240 Verify(addresses, order);
241 }
242
243 // Rule 5: Prefer matching label.
244 TEST_F(AddressSorterPosixTest, Rule5) {
245 AddMapping("::1", "::1"); // matching loopback
246 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // matching IPv4-mapped
247 AddMapping("2001::1", "::ffff:1234:10"); // Teredo vs. IPv4-mapped
248 AddMapping("2002::1", "2001::10"); // 6to4 vs. Teredo
249 const int order[] = { 1, 0, -1 };
250 {
251 const char* addresses[] = { "2001::1", "::1", NULL };
252 Verify(addresses, order);
253 }
254 {
255 const char* addresses[] = { "2002::1", "::ffff:1234:1", NULL };
256 Verify(addresses, order);
257 }
258 }
259
260 // Rule 6: Prefer higher precedence.
261 TEST_F(AddressSorterPosixTest, Rule6) {
262 AddMapping("::1", "::1"); // loopback
263 AddMapping("ff32::1", "fe81::10"); // multicast
264 AddMapping("::ffff:1234:1", "::ffff:1234:10"); // IPv4-mapped
265 AddMapping("2001::1", "2001::10"); // Teredo
266 const char* addresses[] = { "2001::1", "::ffff:1234:1", "ff32::1", "::1",
267 NULL };
268 const int order[] = { 3, 2, 1, 0, -1 };
269 Verify(addresses, order);
270 }
271
272 // Rule 7: Prefer native transport.
273 TEST_F(AddressSorterPosixTest, Rule7) {
274 AddMapping("3002::1", "4000::10");
275 AddMapping("3002::2", "4000::20");
276 GetSourceInfo("4000::20")->native = true;
277 const char* addresses[] = { "3002::1", "3002::2", NULL };
278 const int order[] = { 1, 0, -1 };
279 Verify(addresses, order);
280 }
281
282 // Rule 8: Prefer smaller scope.
283 TEST_F(AddressSorterPosixTest, Rule8) {
284 AddMapping("3000::1", "4000::10"); // matching global
285 AddMapping("fe81::1", "fe81::10"); // matching link-local
mmenke 2012/08/09 19:51:11 nit: You're generally ordering mappings by sort o
mmenke 2012/08/09 19:51:11 By matching, you mean matching everything from pre
szym 2012/08/10 05:56:01 I meant matching scope. Within both groups Rule 8
mmenke 2012/08/10 14:20:11 Right, but for both sets, earlier rules must not d
szym 2012/08/11 22:04:20 All addresses in this test have the same precedenc
286 AddMapping("ff32::1", "4000::10"); // link-local
287 AddMapping("ff35::1", "4000::10"); // site-local
288 AddMapping("ff38::1", "4000::10"); // org-local
289 const char* addresses[] = { "ff38::1",
290 "3000::1",
291 "ff35::1",
292 "ff32::1",
293 "fe81::1",
294 NULL };
mmenke 2012/08/10 14:20:11 nit: You put each entry on its own line here, but
295 const int order[] = { 4, 1, 3, 2, 0, -1 };
296 Verify(addresses, order);
297 }
298
299 // Rule 9: Use longest matching prefix.
300 TEST_F(AddressSorterPosixTest, Rule9) {
mmenke 2012/08/09 19:51:11 I suggest adding one more entry here: AddMapping("
301 GetSourceInfo("4000::10")->prefix_length = 15;
302 GetSourceInfo("3000:ffff::10")->prefix_length = 16;
mmenke 2012/08/09 19:51:11 In other tests you put GetSourceInfo just after th
303 AddMapping("4002::1", "4000::10"); // 14 bit match
304 AddMapping("4000::1", "4000::10"); // 120 bit match, limited to 15
mmenke 2012/08/09 19:51:11 Isn't this a 123-bit match?
szym 2012/08/10 05:56:01 Right.
305 AddMapping("3000::1", "3000:ffff::10"); // 16 bit match
306 const char* addresses[] = { "4002::1", "4000::1", "3000::1", NULL };
307 const int order[] = { 2, 1, 0, -1 };
308 Verify(addresses, order);
309 }
310
311 // Rule 10: Leave the order unchanged.
312 TEST_F(AddressSorterPosixTest, Rule10) {
313 AddMapping("4000::1", "4000::10");
314 AddMapping("4000::2", "4000::10");
315 AddMapping("4000::3", "4000::10");
316 const char* addresses[] = { "4000::1", "4000::2", "4000::3", NULL };
317 const int order[] = { 0, 1, 2, -1 };
318 Verify(addresses, order);
319 }
320
321 TEST_F(AddressSorterPosixTest, MultipleRules) {
322 AddMapping("::1", "::1"); // loopback
323 AddMapping("ff32::1", "fe81::10"); // link-local multicast
324 GetSourceInfo("fe81::20")->deprecated = true;
mmenke 2012/08/09 19:51:11 nit: Move down a line, for consistency.
325 AddMapping("ff32::2", "fe81::20"); // deprecated link-local multicast
326 AddMapping("ff3e::1", "4000::10"); // global multicast, mismatched label
mmenke 2012/08/09 19:51:11 This isn't actually mismatched label, right? Othe
szym 2012/08/10 05:56:01 That's right. I keep forgetting that multicast add
327 AddMapping("4000::1", "4000::10"); // global unicast
mmenke 2012/08/09 19:51:11 Suggest you sort these in order of precedence like
szym 2012/08/10 05:56:01 Do you mean make |order| 4, 3, 2, 1, 0, -1?
mmenke 2012/08/10 14:20:11 No, I mean the order the AddMapping calls are in,
328 const char* addresses[] = { "ff3e::1", "ff32::2", "4000::1", "ff32::1", "::1",
329 "8.0.0.1", NULL };
330 const int order[] = { 4, 3, 0, 2, 1, -1 };
331 Verify(addresses, order);
332 }
333
334 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698