1 /*
2 * Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "webrtc/base/gunit.h"
12 #include "webrtc/base/ipaddress.h"
13
14 namespace rtc {
15
16 static const unsigned int kIPv4AddrSize = 4;
17 static const unsigned int kIPv6AddrSize = 16;
18 static const unsigned int kIPv4RFC1918Addr = 0xC0A80701;
19 static const unsigned int kIPv4PublicAddr = 0x01020304;
20 static const in6_addr kIPv6LinkLocalAddr = {{{0xfe, 0x80, 0x00, 0x00,
21 0x00, 0x00, 0x00, 0x00,
22 0xbe, 0x30, 0x5b, 0xff,
23 0xfe, 0xe5, 0x00, 0xc3}}};
24 static const in6_addr kIPv6PublicAddr = {{{0x24, 0x01, 0xfa, 0x00,
25 0x00, 0x04, 0x10, 0x00,
26 0xbe, 0x30, 0x5b, 0xff,
27 0xfe, 0xe5, 0x00, 0xc3}}};
28 static const in6_addr kIPv6PublicAddr2 = {{{0x24, 0x01, 0x00, 0x00,
29 0x00, 0x00, 0x10, 0x00,
30 0xbe, 0x30, 0x5b, 0xff,
31 0xfe, 0xe5, 0x00, 0xc3}}};
32 static const in6_addr kIPv4MappedAnyAddr = {{{0x00, 0x00, 0x00, 0x00,
33 0x00, 0x00, 0x00, 0x00,
34 0x00, 0x00, 0xff, 0xff,
35 0x00, 0x00, 0x00, 0x00}}};
36 static const in6_addr kIPv4MappedRFC1918Addr = {{{0x00, 0x00, 0x00, 0x00,
37 0x00, 0x00, 0x00, 0x00,
38 0x00, 0x00, 0xff, 0xff,
39 0xc0, 0xa8, 0x07, 0x01}}};
40 static const in6_addr kIPv4MappedPublicAddr = {{{0x00, 0x00, 0x00, 0x00,
41 0x00, 0x00, 0x00, 0x00,
42 0x00, 0x00, 0xff, 0xff,
43 0x01, 0x02, 0x03, 0x04}}};
44
45 static const std::string kIPv4AnyAddrString = "0.0.0.0";
46 static const std::string kIPv4LoopbackAddrString = "127.0.0.1";
47 static const std::string kIPv4RFC1918AddrString = "192.168.7.1";
48 static const std::string kIPv4PublicAddrString = "1.2.3.4";
49 static const std::string kIPv4PublicAddrAnonymizedString = "1.2.3.x";
50 static const std::string kIPv6AnyAddrString = "::";
51 static const std::string kIPv6LoopbackAddrString = "::1";
52 static const std::string kIPv6LinkLocalAddrString = "fe80::be30:5bff:fee5:c3";
53 static const std::string kIPv6EuiAddrString =
54 "2620:0:1008:1201:a248:1cff:fe98:360";
55 static const std::string kIPv6TemporaryAddrString =
56 "2620:0:1008:1201:2089:6dda:385e:80c0";
57 static const std::string kIPv6PublicAddrString =
58 "2401:fa00:4:1000:be30:5bff:fee5:c3";
59 static const std::string kIPv6PublicAddr2String =
60 "2401::1000:be30:5bff:fee5:c3";
61 static const std::string kIPv6PublicAddrAnonymizedString =
62 "2401:fa00:4:x:x:x:x:x";
63 static const std::string kIPv6PublicAddr2AnonymizedString =
64 "2401:0:0:x:x:x:x:x";
65 static const std::string kIPv4MappedAnyAddrString = "::ffff:0:0";
66 static const std::string kIPv4MappedRFC1918AddrString = "::ffff:c0a8:701";
67 static const std::string kIPv4MappedLoopbackAddrString = "::ffff:7f00:1";
68 static const std::string kIPv4MappedPublicAddrString = "::ffff:102:0304";
69 static const std::string kIPv4MappedV4StyleAddrString = "::ffff:192.168.7.1";
70
71 static const std::string kIPv4BrokenString1 = "192.168.7.";
72 static const std::string kIPv4BrokenString2 = "192.168.7.1.1";
73 static const std::string kIPv4BrokenString3 = "192.168.7.1:80";
74 static const std::string kIPv4BrokenString4 = "192.168.7.ONE";
75 static const std::string kIPv4BrokenString5 = "-192.168.7.1";
76 static const std::string kIPv4BrokenString6 = "256.168.7.1";
77 static const std::string kIPv6BrokenString1 = "2401:fa00:4:1000:be30";
78 static const std::string kIPv6BrokenString2 =
79 "2401:fa00:4:1000:be30:5bff:fee5:c3:1";
80 static const std::string kIPv6BrokenString3 =
81 "[2401:fa00:4:1000:be30:5bff:fee5:c3]:1";
82 static const std::string kIPv6BrokenString4 =
83 "2401::4::be30";
84 static const std::string kIPv6BrokenString5 =
85 "2401:::4:fee5:be30";
86 static const std::string kIPv6BrokenString6 =
87 "2401f:fa00:4:1000:be30:5bff:fee5:c3";
88 static const std::string kIPv6BrokenString7 =
89 "2401:ga00:4:1000:be30:5bff:fee5:c3";
90 static const std::string kIPv6BrokenString8 =
91 "2401:fa000:4:1000:be30:5bff:fee5:c3";
92 static const std::string kIPv6BrokenString9 =
93 "2401:fal0:4:1000:be30:5bff:fee5:c3";
94 static const std::string kIPv6BrokenString10 =
95 "::ffff:192.168.7.";
96 static const std::string kIPv6BrokenString11 =
97 "::ffff:192.168.7.1.1.1";
98 static const std::string kIPv6BrokenString12 =
99 "::fffe:192.168.7.1";
100 static const std::string kIPv6BrokenString13 =
101 "::ffff:192.168.7.ff";
102 static const std::string kIPv6BrokenString14 =
103 "0x2401:fa00:4:1000:be30:5bff:fee5:c3";
104
AreEqual(const IPAddress & addr,const IPAddress & addr2)105 bool AreEqual(const IPAddress& addr,
106 const IPAddress& addr2) {
107 if ((IPIsAny(addr) != IPIsAny(addr2)) ||
108 (IPIsLoopback(addr) != IPIsLoopback(addr2)) ||
109 (IPIsPrivate(addr) != IPIsPrivate(addr2)) ||
110 (HashIP(addr) != HashIP(addr2)) ||
111 (addr.Size() != addr2.Size()) ||
112 (addr.family() != addr2.family()) ||
113 (addr.ToString() != addr2.ToString())) {
114 return false;
115 }
116 in_addr v4addr, v4addr2;
117 v4addr = addr.ipv4_address();
118 v4addr2 = addr2.ipv4_address();
119 if (0 != memcmp(&v4addr, &v4addr2, sizeof(v4addr))) {
120 return false;
121 }
122 in6_addr v6addr, v6addr2;
123 v6addr = addr.ipv6_address();
124 v6addr2 = addr2.ipv6_address();
125 if (0 != memcmp(&v6addr, &v6addr2, sizeof(v6addr))) {
126 return false;
127 }
128 return true;
129 }
130
BrokenIPStringFails(const std::string & broken)131 bool BrokenIPStringFails(const std::string& broken) {
132 IPAddress addr(0); // Intentionally make it v4.
133 if (IPFromString(kIPv4BrokenString1, &addr)) {
134 return false;
135 }
136 return addr.family() == AF_UNSPEC;
137 }
138
CheckMaskCount(const std::string & mask,int expected_length)139 bool CheckMaskCount(const std::string& mask, int expected_length) {
140 IPAddress addr;
141 return IPFromString(mask, &addr) &&
142 (expected_length == CountIPMaskBits(addr));
143 }
144
TryInvalidMaskCount(const std::string & mask)145 bool TryInvalidMaskCount(const std::string& mask) {
146 // We don't care about the result at all, but we do want to know if
147 // CountIPMaskBits is going to crash or infinite loop or something.
148 IPAddress addr;
149 if (!IPFromString(mask, &addr)) {
150 return false;
151 }
152 CountIPMaskBits(addr);
153 return true;
154 }
155
CheckTruncateIP(const std::string & initial,int truncate_length,const std::string & expected_result)156 bool CheckTruncateIP(const std::string& initial, int truncate_length,
157 const std::string& expected_result) {
158 IPAddress addr, expected;
159 IPFromString(initial, &addr);
160 IPFromString(expected_result, &expected);
161 IPAddress truncated = TruncateIP(addr, truncate_length);
162 return truncated == expected;
163 }
164
TEST(IPAddressTest,TestDefaultCtor)165 TEST(IPAddressTest, TestDefaultCtor) {
166 IPAddress addr;
167 EXPECT_FALSE(IPIsAny(addr));
168 EXPECT_FALSE(IPIsLoopback(addr));
169 EXPECT_FALSE(IPIsPrivate(addr));
170
171 EXPECT_EQ(0U, addr.Size());
172 EXPECT_EQ(AF_UNSPEC, addr.family());
173 EXPECT_EQ("", addr.ToString());
174 }
175
TEST(IPAddressTest,TestInAddrCtor)176 TEST(IPAddressTest, TestInAddrCtor) {
177 in_addr v4addr;
178
179 // Test V4 Any address.
180 v4addr.s_addr = INADDR_ANY;
181 IPAddress addr(v4addr);
182 EXPECT_TRUE(IPIsAny(addr));
183 EXPECT_FALSE(IPIsLoopback(addr));
184 EXPECT_FALSE(IPIsPrivate(addr));
185 EXPECT_EQ(kIPv4AddrSize, addr.Size());
186 EXPECT_EQ(kIPv4AnyAddrString, addr.ToString());
187
188 // Test a V4 loopback address.
189 v4addr.s_addr = htonl(INADDR_LOOPBACK);
190 addr = IPAddress(v4addr);
191 EXPECT_FALSE(IPIsAny(addr));
192 EXPECT_TRUE(IPIsLoopback(addr));
193 EXPECT_TRUE(IPIsPrivate(addr));
194 EXPECT_EQ(kIPv4AddrSize, addr.Size());
195 EXPECT_EQ(kIPv4LoopbackAddrString, addr.ToString());
196
197 // Test an RFC1918 address.
198 v4addr.s_addr = htonl(kIPv4RFC1918Addr);
199 addr = IPAddress(v4addr);
200 EXPECT_FALSE(IPIsAny(addr));
201 EXPECT_FALSE(IPIsLoopback(addr));
202 EXPECT_TRUE(IPIsPrivate(addr));
203 EXPECT_EQ(kIPv4AddrSize, addr.Size());
204 EXPECT_EQ(kIPv4RFC1918AddrString, addr.ToString());
205
206 // Test a 'normal' v4 address.
207 v4addr.s_addr = htonl(kIPv4PublicAddr);
208 addr = IPAddress(v4addr);
209 EXPECT_FALSE(IPIsAny(addr));
210 EXPECT_FALSE(IPIsLoopback(addr));
211 EXPECT_FALSE(IPIsPrivate(addr));
212 EXPECT_EQ(kIPv4AddrSize, addr.Size());
213 EXPECT_EQ(kIPv4PublicAddrString, addr.ToString());
214 }
215
TEST(IPAddressTest,TestInAddr6Ctor)216 TEST(IPAddressTest, TestInAddr6Ctor) {
217 // Test v6 empty.
218 IPAddress addr(in6addr_any);
219 EXPECT_TRUE(IPIsAny(addr));
220 EXPECT_FALSE(IPIsLoopback(addr));
221 EXPECT_FALSE(IPIsPrivate(addr));
222 EXPECT_EQ(kIPv6AddrSize, addr.Size());
223 EXPECT_EQ(kIPv6AnyAddrString, addr.ToString());
224
225 // Test v6 loopback.
226 addr = IPAddress(in6addr_loopback);
227 EXPECT_FALSE(IPIsAny(addr));
228 EXPECT_TRUE(IPIsLoopback(addr));
229 EXPECT_TRUE(IPIsPrivate(addr));
230 EXPECT_EQ(kIPv6AddrSize, addr.Size());
231 EXPECT_EQ(kIPv6LoopbackAddrString, addr.ToString());
232
233 // Test v6 link-local.
234 addr = IPAddress(kIPv6LinkLocalAddr);
235 EXPECT_FALSE(IPIsAny(addr));
236 EXPECT_FALSE(IPIsLoopback(addr));
237 EXPECT_TRUE(IPIsPrivate(addr));
238 EXPECT_EQ(kIPv6AddrSize, addr.Size());
239 EXPECT_EQ(kIPv6LinkLocalAddrString, addr.ToString());
240
241 // Test v6 global address.
242 addr = IPAddress(kIPv6PublicAddr);
243 EXPECT_FALSE(IPIsAny(addr));
244 EXPECT_FALSE(IPIsLoopback(addr));
245 EXPECT_FALSE(IPIsPrivate(addr));
246 EXPECT_EQ(kIPv6AddrSize, addr.Size());
247 EXPECT_EQ(kIPv6PublicAddrString, addr.ToString());
248 }
249
TEST(IPAddressTest,TestUint32Ctor)250 TEST(IPAddressTest, TestUint32Ctor) {
251 // Test V4 Any address.
252 IPAddress addr(0);
253 EXPECT_TRUE(IPIsAny(addr));
254 EXPECT_FALSE(IPIsLoopback(addr));
255 EXPECT_FALSE(IPIsPrivate(addr));
256 EXPECT_EQ(kIPv4AddrSize, addr.Size());
257 EXPECT_EQ(kIPv4AnyAddrString, addr.ToString());
258
259 // Test a V4 loopback address.
260 addr = IPAddress(INADDR_LOOPBACK);
261 EXPECT_FALSE(IPIsAny(addr));
262 EXPECT_TRUE(IPIsLoopback(addr));
263 EXPECT_TRUE(IPIsPrivate(addr));
264 EXPECT_EQ(kIPv4AddrSize, addr.Size());
265 EXPECT_EQ(kIPv4LoopbackAddrString, addr.ToString());
266
267 // Test an RFC1918 address.
268 addr = IPAddress(kIPv4RFC1918Addr);
269 EXPECT_FALSE(IPIsAny(addr));
270 EXPECT_FALSE(IPIsLoopback(addr));
271 EXPECT_TRUE(IPIsPrivate(addr));
272 EXPECT_EQ(kIPv4AddrSize, addr.Size());
273 EXPECT_EQ(kIPv4RFC1918AddrString, addr.ToString());
274
275 // Test a 'normal' v4 address.
276 addr = IPAddress(kIPv4PublicAddr);
277 EXPECT_FALSE(IPIsAny(addr));
278 EXPECT_FALSE(IPIsLoopback(addr));
279 EXPECT_FALSE(IPIsPrivate(addr));
280 EXPECT_EQ(kIPv4AddrSize, addr.Size());
281 EXPECT_EQ(kIPv4PublicAddrString, addr.ToString());
282 }
283
TEST(IPAddressTest,TestCopyCtor)284 TEST(IPAddressTest, TestCopyCtor) {
285 in_addr v4addr;
286 v4addr.s_addr = htonl(kIPv4PublicAddr);
287 IPAddress addr(v4addr);
288 IPAddress addr2(addr);
289
290 EXPECT_PRED2(AreEqual, addr, addr2);
291
292 addr = IPAddress(INADDR_ANY);
293 addr2 = IPAddress(addr);
294 EXPECT_PRED2(AreEqual, addr, addr2);
295
296 addr = IPAddress(INADDR_LOOPBACK);
297 addr2 = IPAddress(addr);
298 EXPECT_PRED2(AreEqual, addr, addr2);
299
300 addr = IPAddress(kIPv4PublicAddr);
301 addr2 = IPAddress(addr);
302 EXPECT_PRED2(AreEqual, addr, addr2);
303
304 addr = IPAddress(kIPv4RFC1918Addr);
305 addr2 = IPAddress(addr);
306 EXPECT_PRED2(AreEqual, addr, addr2);
307
308 addr = IPAddress(in6addr_any);
309 addr2 = IPAddress(addr);
310 EXPECT_PRED2(AreEqual, addr, addr2);
311
312 addr = IPAddress(in6addr_loopback);
313 addr2 = IPAddress(addr);
314 EXPECT_PRED2(AreEqual, addr, addr2);
315
316 addr = IPAddress(kIPv6LinkLocalAddr);
317 addr2 = IPAddress(addr);
318 EXPECT_PRED2(AreEqual, addr, addr2);
319
320 addr = IPAddress(kIPv6PublicAddr);
321 addr2 = IPAddress(addr);
322 EXPECT_PRED2(AreEqual, addr, addr2);
323 }
324
TEST(IPAddressTest,TestEquality)325 TEST(IPAddressTest, TestEquality) {
326 // Check v4 equality
327 in_addr v4addr, v4addr2;
328 v4addr.s_addr = htonl(kIPv4PublicAddr);
329 v4addr2.s_addr = htonl(kIPv4PublicAddr + 1);
330 IPAddress addr(v4addr);
331 IPAddress addr2(v4addr2);
332 IPAddress addr3(v4addr);
333
334 EXPECT_TRUE(addr == addr);
335 EXPECT_TRUE(addr2 == addr2);
336 EXPECT_TRUE(addr3 == addr3);
337 EXPECT_TRUE(addr == addr3);
338 EXPECT_TRUE(addr3 == addr);
339 EXPECT_FALSE(addr2 == addr);
340 EXPECT_FALSE(addr2 == addr3);
341 EXPECT_FALSE(addr == addr2);
342 EXPECT_FALSE(addr3 == addr2);
343
344 // Check v6 equality
345 IPAddress addr4(kIPv6PublicAddr);
346 IPAddress addr5(kIPv6LinkLocalAddr);
347 IPAddress addr6(kIPv6PublicAddr);
348
349 EXPECT_TRUE(addr4 == addr4);
350 EXPECT_TRUE(addr5 == addr5);
351 EXPECT_TRUE(addr4 == addr6);
352 EXPECT_TRUE(addr6 == addr4);
353 EXPECT_FALSE(addr4 == addr5);
354 EXPECT_FALSE(addr5 == addr4);
355 EXPECT_FALSE(addr6 == addr5);
356 EXPECT_FALSE(addr5 == addr6);
357
358 // Check v4/v6 cross-equality
359 EXPECT_FALSE(addr == addr4);
360 EXPECT_FALSE(addr == addr5);
361 EXPECT_FALSE(addr == addr6);
362 EXPECT_FALSE(addr4 == addr);
363 EXPECT_FALSE(addr5 == addr);
364 EXPECT_FALSE(addr6 == addr);
365 EXPECT_FALSE(addr2 == addr4);
366 EXPECT_FALSE(addr2 == addr5);
367 EXPECT_FALSE(addr2 == addr6);
368 EXPECT_FALSE(addr4 == addr2);
369 EXPECT_FALSE(addr5 == addr2);
370 EXPECT_FALSE(addr6 == addr2);
371 EXPECT_FALSE(addr3 == addr4);
372 EXPECT_FALSE(addr3 == addr5);
373 EXPECT_FALSE(addr3 == addr6);
374 EXPECT_FALSE(addr4 == addr3);
375 EXPECT_FALSE(addr5 == addr3);
376 EXPECT_FALSE(addr6 == addr3);
377
378 // Special cases: loopback and any.
379 // They're special but they're still not equal.
380 IPAddress v4loopback(htonl(INADDR_LOOPBACK));
381 IPAddress v6loopback(in6addr_loopback);
382 EXPECT_FALSE(v4loopback == v6loopback);
383
384 IPAddress v4any(0);
385 IPAddress v6any(in6addr_any);
386 EXPECT_FALSE(v4any == v6any);
387 }
388
TEST(IPAddressTest,TestComparison)389 TEST(IPAddressTest, TestComparison) {
390 // Defined in 'ascending' order.
391 // v6 > v4, and intra-family sorting is purely numerical
392 IPAddress addr0; // AF_UNSPEC
393 IPAddress addr1(INADDR_ANY); // 0.0.0.0
394 IPAddress addr2(kIPv4PublicAddr); // 1.2.3.4
395 IPAddress addr3(INADDR_LOOPBACK); // 127.0.0.1
396 IPAddress addr4(kIPv4RFC1918Addr); // 192.168.7.1.
397 IPAddress addr5(in6addr_any); // ::
398 IPAddress addr6(in6addr_loopback); // ::1
399 IPAddress addr7(kIPv6PublicAddr); // 2401....
400 IPAddress addr8(kIPv6LinkLocalAddr); // fe80....
401
402 EXPECT_TRUE(addr0 < addr1);
403 EXPECT_TRUE(addr1 < addr2);
404 EXPECT_TRUE(addr2 < addr3);
405 EXPECT_TRUE(addr3 < addr4);
406 EXPECT_TRUE(addr4 < addr5);
407 EXPECT_TRUE(addr5 < addr6);
408 EXPECT_TRUE(addr6 < addr7);
409 EXPECT_TRUE(addr7 < addr8);
410
411 EXPECT_FALSE(addr0 > addr1);
412 EXPECT_FALSE(addr1 > addr2);
413 EXPECT_FALSE(addr2 > addr3);
414 EXPECT_FALSE(addr3 > addr4);
415 EXPECT_FALSE(addr4 > addr5);
416 EXPECT_FALSE(addr5 > addr6);
417 EXPECT_FALSE(addr6 > addr7);
418 EXPECT_FALSE(addr7 > addr8);
419
420 EXPECT_FALSE(addr0 > addr0);
421 EXPECT_FALSE(addr1 > addr1);
422 EXPECT_FALSE(addr2 > addr2);
423 EXPECT_FALSE(addr3 > addr3);
424 EXPECT_FALSE(addr4 > addr4);
425 EXPECT_FALSE(addr5 > addr5);
426 EXPECT_FALSE(addr6 > addr6);
427 EXPECT_FALSE(addr7 > addr7);
428 EXPECT_FALSE(addr8 > addr8);
429
430 EXPECT_FALSE(addr0 < addr0);
431 EXPECT_FALSE(addr1 < addr1);
432 EXPECT_FALSE(addr2 < addr2);
433 EXPECT_FALSE(addr3 < addr3);
434 EXPECT_FALSE(addr4 < addr4);
435 EXPECT_FALSE(addr5 < addr5);
436 EXPECT_FALSE(addr6 < addr6);
437 EXPECT_FALSE(addr7 < addr7);
438 EXPECT_FALSE(addr8 < addr8);
439 }
440
TEST(IPAddressTest,TestFromString)441 TEST(IPAddressTest, TestFromString) {
442 IPAddress addr;
443 IPAddress addr2;
444 addr2 = IPAddress(INADDR_ANY);
445
446 EXPECT_TRUE(IPFromString(kIPv4AnyAddrString, &addr));
447 EXPECT_EQ(addr.ToString(), kIPv4AnyAddrString);
448 EXPECT_PRED2(AreEqual, addr, addr2);
449
450 addr2 = IPAddress(INADDR_LOOPBACK);
451 EXPECT_TRUE(IPFromString(kIPv4LoopbackAddrString, &addr));
452 EXPECT_EQ(addr.ToString(), kIPv4LoopbackAddrString);
453 EXPECT_PRED2(AreEqual, addr, addr2);
454
455 addr2 = IPAddress(kIPv4RFC1918Addr);
456 EXPECT_TRUE(IPFromString(kIPv4RFC1918AddrString, &addr));
457 EXPECT_EQ(addr.ToString(), kIPv4RFC1918AddrString);
458 EXPECT_PRED2(AreEqual, addr, addr2);
459
460 addr2 = IPAddress(kIPv4PublicAddr);
461 EXPECT_TRUE(IPFromString(kIPv4PublicAddrString, &addr));
462 EXPECT_EQ(addr.ToString(), kIPv4PublicAddrString);
463 EXPECT_PRED2(AreEqual, addr, addr2);
464
465 addr2 = IPAddress(in6addr_any);
466 EXPECT_TRUE(IPFromString(kIPv6AnyAddrString, &addr));
467 EXPECT_EQ(addr.ToString(), kIPv6AnyAddrString);
468 EXPECT_PRED2(AreEqual, addr, addr2);
469
470 addr2 = IPAddress(in6addr_loopback);
471 EXPECT_TRUE(IPFromString(kIPv6LoopbackAddrString, &addr));
472 EXPECT_EQ(addr.ToString(), kIPv6LoopbackAddrString);
473 EXPECT_PRED2(AreEqual, addr, addr2);
474
475 addr2 = IPAddress(kIPv6LinkLocalAddr);
476 EXPECT_TRUE(IPFromString(kIPv6LinkLocalAddrString, &addr));
477 EXPECT_EQ(addr.ToString(), kIPv6LinkLocalAddrString);
478 EXPECT_PRED2(AreEqual, addr, addr2);
479
480 addr2 = IPAddress(kIPv6PublicAddr);
481 EXPECT_TRUE(IPFromString(kIPv6PublicAddrString, &addr));
482 EXPECT_EQ(addr.ToString(), kIPv6PublicAddrString);
483 EXPECT_PRED2(AreEqual, addr, addr2);
484
485 addr2 = IPAddress(kIPv4MappedRFC1918Addr);
486 EXPECT_TRUE(IPFromString(kIPv4MappedV4StyleAddrString, &addr));
487 EXPECT_PRED2(AreEqual, addr, addr2);
488
489 // Broken cases, should set addr to AF_UNSPEC.
490 EXPECT_PRED1(BrokenIPStringFails, kIPv4BrokenString1);
491 EXPECT_PRED1(BrokenIPStringFails, kIPv4BrokenString2);
492 EXPECT_PRED1(BrokenIPStringFails, kIPv4BrokenString3);
493 EXPECT_PRED1(BrokenIPStringFails, kIPv4BrokenString4);
494 EXPECT_PRED1(BrokenIPStringFails, kIPv4BrokenString5);
495 EXPECT_PRED1(BrokenIPStringFails, kIPv4BrokenString6);
496 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString1);
497 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString2);
498 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString3);
499 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString4);
500 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString5);
501 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString6);
502 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString7);
503 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString8);
504 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString9);
505 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString10);
506 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString11);
507 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString12);
508 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString13);
509 EXPECT_PRED1(BrokenIPStringFails, kIPv6BrokenString14);
510 }
511
TEST(IPAddressTest,TestIPFromAddrInfo)512 TEST(IPAddressTest, TestIPFromAddrInfo) {
513 struct sockaddr_in expected4;
514 struct sockaddr_in6 expected6;
515 struct addrinfo test_info;
516 struct addrinfo next_info;
517 memset(&next_info, 'A', sizeof(next_info));
518 test_info.ai_next = &next_info;
519 // Check that we can get an IPv4 address out.
520 test_info.ai_addr = reinterpret_cast<struct sockaddr*>(&expected4);
521 expected4.sin_addr.s_addr = HostToNetwork32(kIPv4PublicAddr);
522 expected4.sin_family = AF_INET;
523 IPAddress expected(kIPv4PublicAddr);
524 IPAddress addr;
525 EXPECT_TRUE(IPFromAddrInfo(&test_info, &addr));
526 EXPECT_EQ(expected, addr);
527 // Check that we can get an IPv6 address out.
528 expected6.sin6_addr = kIPv6PublicAddr;
529 expected6.sin6_family = AF_INET6;
530 expected = IPAddress(kIPv6PublicAddr);
531 test_info.ai_addr = reinterpret_cast<struct sockaddr*>(&expected6);
532 EXPECT_TRUE(IPFromAddrInfo(&test_info, &addr));
533 EXPECT_EQ(expected, addr);
534 // Check that unspec fails.
535 expected6.sin6_family = AF_UNSPEC;
536 EXPECT_FALSE(IPFromAddrInfo(&test_info, &addr));
537 // Check a zeroed out addrinfo doesn't crash us.
538 memset(&next_info, 0, sizeof(next_info));
539 EXPECT_FALSE(IPFromAddrInfo(&next_info, &addr));
540 }
541
TEST(IPAddressTest,TestIsPrivate)542 TEST(IPAddressTest, TestIsPrivate) {
543 EXPECT_FALSE(IPIsPrivate(IPAddress(INADDR_ANY)));
544 EXPECT_FALSE(IPIsPrivate(IPAddress(kIPv4PublicAddr)));
545 EXPECT_FALSE(IPIsPrivate(IPAddress(in6addr_any)));
546 EXPECT_FALSE(IPIsPrivate(IPAddress(kIPv6PublicAddr)));
547 EXPECT_FALSE(IPIsPrivate(IPAddress(kIPv4MappedAnyAddr)));
548 EXPECT_FALSE(IPIsPrivate(IPAddress(kIPv4MappedPublicAddr)));
549
550 EXPECT_TRUE(IPIsPrivate(IPAddress(kIPv4RFC1918Addr)));
551 EXPECT_TRUE(IPIsPrivate(IPAddress(INADDR_LOOPBACK)));
552 EXPECT_TRUE(IPIsPrivate(IPAddress(in6addr_loopback)));
553 EXPECT_TRUE(IPIsPrivate(IPAddress(kIPv6LinkLocalAddr)));
554 }
555
TEST(IPAddressTest,TestIsNil)556 TEST(IPAddressTest, TestIsNil) {
557 IPAddress addr;
558 EXPECT_TRUE(IPAddress().IsNil());
559
560 EXPECT_TRUE(IPFromString(kIPv6AnyAddrString, &addr));
561 EXPECT_FALSE(addr.IsNil());
562
563 EXPECT_TRUE(IPFromString(kIPv4AnyAddrString, &addr));
564 EXPECT_FALSE(addr.IsNil());
565
566 EXPECT_FALSE(IPAddress(kIPv4PublicAddr).IsNil());
567 }
568
TEST(IPAddressTest,TestIsLoopback)569 TEST(IPAddressTest, TestIsLoopback) {
570 EXPECT_FALSE(IPIsLoopback(IPAddress(INADDR_ANY)));
571 EXPECT_FALSE(IPIsLoopback(IPAddress(kIPv4PublicAddr)));
572 EXPECT_FALSE(IPIsLoopback(IPAddress(in6addr_any)));
573 EXPECT_FALSE(IPIsLoopback(IPAddress(kIPv6PublicAddr)));
574 EXPECT_FALSE(IPIsLoopback(IPAddress(kIPv4MappedAnyAddr)));
575 EXPECT_FALSE(IPIsLoopback(IPAddress(kIPv4MappedPublicAddr)));
576
577 EXPECT_TRUE(IPIsLoopback(IPAddress(INADDR_LOOPBACK)));
578 EXPECT_TRUE(IPIsLoopback(IPAddress(in6addr_loopback)));
579 }
580
581 // Verify that IPIsAny catches all cases of "any" address.
TEST(IPAddressTest,TestIsAny)582 TEST(IPAddressTest, TestIsAny) {
583 IPAddress addr;
584
585 EXPECT_TRUE(IPFromString(kIPv6AnyAddrString, &addr));
586 EXPECT_TRUE(IPIsAny(addr));
587
588 EXPECT_TRUE(IPFromString(kIPv4AnyAddrString, &addr));
589 EXPECT_TRUE(IPIsAny(addr));
590
591 EXPECT_TRUE(IPIsAny(IPAddress(kIPv4MappedAnyAddr)));
592 }
593
TEST(IPAddressTest,TestIsEui64)594 TEST(IPAddressTest, TestIsEui64) {
595 IPAddress addr;
596 EXPECT_TRUE(IPFromString(kIPv6EuiAddrString, &addr));
597 EXPECT_TRUE(IPIsMacBased(addr));
598
599 EXPECT_TRUE(IPFromString(kIPv6TemporaryAddrString, &addr));
600 EXPECT_FALSE(IPIsMacBased(addr));
601
602 EXPECT_TRUE(IPFromString(kIPv6LinkLocalAddrString, &addr));
603 EXPECT_TRUE(IPIsMacBased(addr));
604
605 EXPECT_TRUE(IPFromString(kIPv6AnyAddrString, &addr));
606 EXPECT_FALSE(IPIsMacBased(addr));
607
608 EXPECT_TRUE(IPFromString(kIPv6LoopbackAddrString, &addr));
609 EXPECT_FALSE(IPIsMacBased(addr));
610 }
611
TEST(IPAddressTest,TestNormalized)612 TEST(IPAddressTest, TestNormalized) {
613 // Check normalizing a ::ffff:a.b.c.d address.
614 IPAddress addr;
615 EXPECT_TRUE(IPFromString(kIPv4MappedV4StyleAddrString, &addr));
616 IPAddress addr2(kIPv4RFC1918Addr);
617 addr = addr.Normalized();
618 EXPECT_EQ(addr2, addr);
619
620 // Check normalizing a ::ffff:aabb:ccdd address.
621 addr = IPAddress(kIPv4MappedPublicAddr);
622 addr2 = IPAddress(kIPv4PublicAddr);
623 addr = addr.Normalized();
624 EXPECT_EQ(addr, addr2);
625
626 // Check that a non-mapped v6 addresses isn't altered.
627 addr = IPAddress(kIPv6PublicAddr);
628 addr2 = IPAddress(kIPv6PublicAddr);
629 addr = addr.Normalized();
630 EXPECT_EQ(addr, addr2);
631
632 // Check that addresses that look a bit like mapped addresses aren't altered
633 EXPECT_TRUE(IPFromString("fe80::ffff:0102:0304", &addr));
634 addr2 = addr;
635 addr = addr.Normalized();
636 EXPECT_EQ(addr, addr2);
637 EXPECT_TRUE(IPFromString("::0102:0304", &addr));
638 addr2 = addr;
639 addr = addr.Normalized();
640 EXPECT_EQ(addr, addr2);
641 // This string should 'work' as an IP address but is not a mapped address,
642 // so it shouldn't change on normalization.
643 EXPECT_TRUE(IPFromString("::192.168.7.1", &addr));
644 addr2 = addr;
645 addr = addr.Normalized();
646 EXPECT_EQ(addr, addr2);
647
648 // Check that v4 addresses aren't altered.
649 addr = IPAddress(htonl(kIPv4PublicAddr));
650 addr2 = IPAddress(htonl(kIPv4PublicAddr));
651 addr = addr.Normalized();
652 EXPECT_EQ(addr, addr2);
653 }
654
TEST(IPAddressTest,TestAsIPv6Address)655 TEST(IPAddressTest, TestAsIPv6Address) {
656 IPAddress addr(kIPv4PublicAddr);
657 IPAddress addr2(kIPv4MappedPublicAddr);
658 addr = addr.AsIPv6Address();
659 EXPECT_EQ(addr, addr2);
660
661 addr = IPAddress(kIPv4MappedPublicAddr);
662 addr2 = IPAddress(kIPv4MappedPublicAddr);
663 addr = addr.AsIPv6Address();
664 EXPECT_EQ(addr, addr2);
665
666 addr = IPAddress(kIPv6PublicAddr);
667 addr2 = IPAddress(kIPv6PublicAddr);
668 addr = addr.AsIPv6Address();
669 EXPECT_EQ(addr, addr2);
670 }
671
TEST(IPAddressTest,TestCountIPMaskBits)672 TEST(IPAddressTest, TestCountIPMaskBits) {
673 IPAddress mask;
674 // IPv4 on byte boundaries
675 EXPECT_PRED2(CheckMaskCount, "255.255.255.255", 32);
676 EXPECT_PRED2(CheckMaskCount, "255.255.255.0", 24);
677 EXPECT_PRED2(CheckMaskCount, "255.255.0.0", 16);
678 EXPECT_PRED2(CheckMaskCount, "255.0.0.0", 8);
679 EXPECT_PRED2(CheckMaskCount, "0.0.0.0", 0);
680
681 // IPv4 not on byte boundaries
682 EXPECT_PRED2(CheckMaskCount, "128.0.0.0", 1);
683 EXPECT_PRED2(CheckMaskCount, "224.0.0.0", 3);
684 EXPECT_PRED2(CheckMaskCount, "255.248.0.0", 13);
685 EXPECT_PRED2(CheckMaskCount, "255.255.224.0", 19);
686 EXPECT_PRED2(CheckMaskCount, "255.255.255.252", 30);
687
688 // V6 on byte boundaries
689 EXPECT_PRED2(CheckMaskCount, "::", 0);
690 EXPECT_PRED2(CheckMaskCount, "ff00::", 8);
691 EXPECT_PRED2(CheckMaskCount, "ffff::", 16);
692 EXPECT_PRED2(CheckMaskCount, "ffff:ff00::", 24);
693 EXPECT_PRED2(CheckMaskCount, "ffff:ffff::", 32);
694 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ff00::", 40);
695 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff::", 48);
696 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ff00::", 56);
697 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff::", 64);
698 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ff00::", 72);
699 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff::", 80);
700 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ff00::", 88);
701 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff::", 96);
702 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:ff00:0000", 104);
703 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:0000", 112);
704 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00", 120);
705 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", 128);
706
707 // V6 not on byte boundaries.
708 EXPECT_PRED2(CheckMaskCount, "8000::", 1);
709 EXPECT_PRED2(CheckMaskCount, "ff80::", 9);
710 EXPECT_PRED2(CheckMaskCount, "ffff:fe00::", 23);
711 EXPECT_PRED2(CheckMaskCount, "ffff:fffe::", 31);
712 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:e000::", 35);
713 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffe0::", 43);
714 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:f800::", 53);
715 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:fff8::", 61);
716 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:fc00::", 70);
717 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:fffc::", 78);
718 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:8000::", 81);
719 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ff80::", 89);
720 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:fe00::", 103);
721 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:fffe:0000", 111);
722 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fc00", 118);
723 EXPECT_PRED2(CheckMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffc", 126);
724
725 // Non-contiguous ranges. These are invalid but lets test them
726 // to make sure they don't crash anything or infinite loop or something.
727 EXPECT_PRED1(TryInvalidMaskCount, "217.0.0.0");
728 EXPECT_PRED1(TryInvalidMaskCount, "255.185.0.0");
729 EXPECT_PRED1(TryInvalidMaskCount, "255.255.251.0");
730 EXPECT_PRED1(TryInvalidMaskCount, "255.255.251.255");
731 EXPECT_PRED1(TryInvalidMaskCount, "255.255.254.201");
732 EXPECT_PRED1(TryInvalidMaskCount, "::1");
733 EXPECT_PRED1(TryInvalidMaskCount, "fe80::1");
734 EXPECT_PRED1(TryInvalidMaskCount, "ff80::1");
735 EXPECT_PRED1(TryInvalidMaskCount, "ffff::1");
736 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ff00:1::1");
737 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff::ffff:1");
738 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ff00:1::");
739 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff::ff00");
740 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff:ff00:1234::");
741 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff:ffff:0012::ffff");
742 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff:ffff:ff01::");
743 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff:ffff:ffff:7f00::");
744 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff:ffff:ffff:ff7a::");
745 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:7f00:0000");
746 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:ff70:0000");
747 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:0211");
748 EXPECT_PRED1(TryInvalidMaskCount, "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff7f");
749 }
750
TEST(IPAddressTest,TestTruncateIP)751 TEST(IPAddressTest, TestTruncateIP) {
752 EXPECT_PRED3(CheckTruncateIP, "255.255.255.255", 24, "255.255.255.0");
753 EXPECT_PRED3(CheckTruncateIP, "255.255.255.255", 16, "255.255.0.0");
754 EXPECT_PRED3(CheckTruncateIP, "255.255.255.255", 8, "255.0.0.0");
755 EXPECT_PRED3(CheckTruncateIP, "202.67.7.255", 24, "202.67.7.0");
756 EXPECT_PRED3(CheckTruncateIP, "202.129.65.205", 16, "202.129.0.0");
757 EXPECT_PRED3(CheckTruncateIP, "55.25.2.77", 8, "55.0.0.0");
758 EXPECT_PRED3(CheckTruncateIP, "74.128.99.254", 1, "0.0.0.0");
759 EXPECT_PRED3(CheckTruncateIP, "106.55.99.254", 3, "96.0.0.0");
760 EXPECT_PRED3(CheckTruncateIP, "172.167.53.222", 13, "172.160.0.0");
761 EXPECT_PRED3(CheckTruncateIP, "255.255.224.0", 18, "255.255.192.0");
762 EXPECT_PRED3(CheckTruncateIP, "255.255.255.252", 28, "255.255.255.240");
763
764 EXPECT_PRED3(CheckTruncateIP, "fe80:1111:2222:3333:4444:5555:6666:7777", 1,
765 "8000::");
766 EXPECT_PRED3(CheckTruncateIP, "fff0:1111:2222:3333:4444:5555:6666:7777", 9,
767 "ff80::");
768 EXPECT_PRED3(CheckTruncateIP, "ffff:ff80:1111:2222:3333:4444:5555:6666", 23,
769 "ffff:fe00::");
770 EXPECT_PRED3(CheckTruncateIP, "ffff:ff80:1111:2222:3333:4444:5555:6666", 32,
771 "ffff:ff80::");
772 EXPECT_PRED3(CheckTruncateIP, "2400:f9af:e456:1111:2222:3333:4444:5555", 35,
773 "2400:f9af:e000::");
774 EXPECT_PRED3(CheckTruncateIP, "9999:1111:2233:4444:5555:6666:7777:8888", 53,
775 "9999:1111:2233:4000::");
776 EXPECT_PRED3(CheckTruncateIP, "9999:1111:2233:4567:5555:6666:7777:8888", 64,
777 "9999:1111:2233:4567::");
778 EXPECT_PRED3(CheckTruncateIP, "1111:2222:3333:4444:5555:6666:7777:8888", 68,
779 "1111:2222:3333:4444:5000::");
780 EXPECT_PRED3(CheckTruncateIP, "1111:2222:3333:4444:5555:6666:7777:8888", 92,
781 "1111:2222:3333:4444:5555:6660::");
782 EXPECT_PRED3(CheckTruncateIP, "1111:2222:3333:4444:5555:6666:7777:8888", 96,
783 "1111:2222:3333:4444:5555:6666::");
784 EXPECT_PRED3(CheckTruncateIP, "1111:2222:3333:4444:5555:6666:7777:8888", 105,
785 "1111:2222:3333:4444:5555:6666:7700::");
786 EXPECT_PRED3(CheckTruncateIP, "1111:2222:3333:4444:5555:6666:7777:8888", 124,
787 "1111:2222:3333:4444:5555:6666:7777:8880");
788
789 // Slightly degenerate cases
790 EXPECT_PRED3(CheckTruncateIP, "202.165.33.127", 32, "202.165.33.127");
791 EXPECT_PRED3(CheckTruncateIP, "235.105.77.12", 0, "0.0.0.0");
792 EXPECT_PRED3(CheckTruncateIP, "1111:2222:3333:4444:5555:6666:7777:8888", 128,
793 "1111:2222:3333:4444:5555:6666:7777:8888");
794 EXPECT_PRED3(CheckTruncateIP, "1111:2222:3333:4444:5555:6666:7777:8888", 0,
795 "::");
796 }
797
TEST(IPAddressTest,TestCategorizeIPv6)798 TEST(IPAddressTest, TestCategorizeIPv6) {
799 // Test determining if an IPAddress is 6Bone/6To4/Teredo/etc.
800 // IPv4 address, should be none of these (not even v4compat/v4mapped).
801 IPAddress v4_addr(kIPv4PublicAddr);
802 EXPECT_FALSE(IPIs6Bone(v4_addr));
803 EXPECT_FALSE(IPIs6To4(v4_addr));
804 EXPECT_FALSE(IPIsSiteLocal(v4_addr));
805 EXPECT_FALSE(IPIsTeredo(v4_addr));
806 EXPECT_FALSE(IPIsULA(v4_addr));
807 EXPECT_FALSE(IPIsV4Compatibility(v4_addr));
808 EXPECT_FALSE(IPIsV4Mapped(v4_addr));
809 // Linklocal (fe80::/16) adddress; should be none of these.
810 IPAddress linklocal_addr(kIPv6LinkLocalAddr);
811 EXPECT_FALSE(IPIs6Bone(linklocal_addr));
812 EXPECT_FALSE(IPIs6To4(linklocal_addr));
813 EXPECT_FALSE(IPIsSiteLocal(linklocal_addr));
814 EXPECT_FALSE(IPIsTeredo(linklocal_addr));
815 EXPECT_FALSE(IPIsULA(linklocal_addr));
816 EXPECT_FALSE(IPIsV4Compatibility(linklocal_addr));
817 EXPECT_FALSE(IPIsV4Mapped(linklocal_addr));
818 // 'Normal' IPv6 address, should also be none of these.
819 IPAddress normal_addr(kIPv6PublicAddr);
820 EXPECT_FALSE(IPIs6Bone(normal_addr));
821 EXPECT_FALSE(IPIs6To4(normal_addr));
822 EXPECT_FALSE(IPIsSiteLocal(normal_addr));
823 EXPECT_FALSE(IPIsTeredo(normal_addr));
824 EXPECT_FALSE(IPIsULA(normal_addr));
825 EXPECT_FALSE(IPIsV4Compatibility(normal_addr));
826 EXPECT_FALSE(IPIsV4Mapped(normal_addr));
827 // IPv4 mapped address (::ffff:123.123.123.123)
828 IPAddress v4mapped_addr(kIPv4MappedPublicAddr);
829 EXPECT_TRUE(IPIsV4Mapped(v4mapped_addr));
830 EXPECT_FALSE(IPIsV4Compatibility(v4mapped_addr));
831 EXPECT_FALSE(IPIs6Bone(v4mapped_addr));
832 EXPECT_FALSE(IPIs6To4(v4mapped_addr));
833 EXPECT_FALSE(IPIsSiteLocal(v4mapped_addr));
834 EXPECT_FALSE(IPIsTeredo(v4mapped_addr));
835 EXPECT_FALSE(IPIsULA(v4mapped_addr));
836 // IPv4 compatibility address (::123.123.123.123)
837 IPAddress v4compat_addr;
838 IPFromString("::192.168.7.1", &v4compat_addr);
839 EXPECT_TRUE(IPIsV4Compatibility(v4compat_addr));
840 EXPECT_FALSE(IPIs6Bone(v4compat_addr));
841 EXPECT_FALSE(IPIs6To4(v4compat_addr));
842 EXPECT_FALSE(IPIsSiteLocal(v4compat_addr));
843 EXPECT_FALSE(IPIsTeredo(v4compat_addr));
844 EXPECT_FALSE(IPIsULA(v4compat_addr));
845 EXPECT_FALSE(IPIsV4Mapped(v4compat_addr));
846 // 6Bone address (3FFE::/16)
847 IPAddress sixbone_addr;
848 IPFromString("3FFE:123:456::789:123", &sixbone_addr);
849 EXPECT_TRUE(IPIs6Bone(sixbone_addr));
850 EXPECT_FALSE(IPIs6To4(sixbone_addr));
851 EXPECT_FALSE(IPIsSiteLocal(sixbone_addr));
852 EXPECT_FALSE(IPIsTeredo(sixbone_addr));
853 EXPECT_FALSE(IPIsULA(sixbone_addr));
854 EXPECT_FALSE(IPIsV4Mapped(sixbone_addr));
855 EXPECT_FALSE(IPIsV4Compatibility(sixbone_addr));
856 // Unique Local Address (FC::/7)
857 IPAddress ula_addr;
858 IPFromString("FC00:123:456::789:123", &ula_addr);
859 EXPECT_TRUE(IPIsULA(ula_addr));
860 EXPECT_FALSE(IPIs6Bone(ula_addr));
861 EXPECT_FALSE(IPIs6To4(ula_addr));
862 EXPECT_FALSE(IPIsSiteLocal(ula_addr));
863 EXPECT_FALSE(IPIsTeredo(ula_addr));
864 EXPECT_FALSE(IPIsV4Mapped(ula_addr));
865 EXPECT_FALSE(IPIsV4Compatibility(ula_addr));
866 // 6To4 Address (2002::/16)
867 IPAddress sixtofour_addr;
868 IPFromString("2002:123:456::789:123", &sixtofour_addr);
869 EXPECT_TRUE(IPIs6To4(sixtofour_addr));
870 EXPECT_FALSE(IPIs6Bone(sixtofour_addr));
871 EXPECT_FALSE(IPIsSiteLocal(sixtofour_addr));
872 EXPECT_FALSE(IPIsTeredo(sixtofour_addr));
873 EXPECT_FALSE(IPIsULA(sixtofour_addr));
874 EXPECT_FALSE(IPIsV4Compatibility(sixtofour_addr));
875 EXPECT_FALSE(IPIsV4Mapped(sixtofour_addr));
876 // Site Local address (FEC0::/10)
877 IPAddress sitelocal_addr;
878 IPFromString("FEC0:123:456::789:123", &sitelocal_addr);
879 EXPECT_TRUE(IPIsSiteLocal(sitelocal_addr));
880 EXPECT_FALSE(IPIs6Bone(sitelocal_addr));
881 EXPECT_FALSE(IPIs6To4(sitelocal_addr));
882 EXPECT_FALSE(IPIsTeredo(sitelocal_addr));
883 EXPECT_FALSE(IPIsULA(sitelocal_addr));
884 EXPECT_FALSE(IPIsV4Compatibility(sitelocal_addr));
885 EXPECT_FALSE(IPIsV4Mapped(sitelocal_addr));
886 // Teredo Address (2001:0000::/32)
887 IPAddress teredo_addr;
888 IPFromString("2001:0000:123:456::789:123", &teredo_addr);
889 EXPECT_TRUE(IPIsTeredo(teredo_addr));
890 EXPECT_FALSE(IPIsSiteLocal(teredo_addr));
891 EXPECT_FALSE(IPIs6Bone(teredo_addr));
892 EXPECT_FALSE(IPIs6To4(teredo_addr));
893 EXPECT_FALSE(IPIsULA(teredo_addr));
894 EXPECT_FALSE(IPIsV4Compatibility(teredo_addr));
895 EXPECT_FALSE(IPIsV4Mapped(teredo_addr));
896 }
897
TEST(IPAddressTest,TestToSensitiveString)898 TEST(IPAddressTest, TestToSensitiveString) {
899 IPAddress addr_v4 = IPAddress(kIPv4PublicAddr);
900 IPAddress addr_v6 = IPAddress(kIPv6PublicAddr);
901 IPAddress addr_v6_2 = IPAddress(kIPv6PublicAddr2);
902 EXPECT_EQ(kIPv4PublicAddrString, addr_v4.ToString());
903 EXPECT_EQ(kIPv6PublicAddrString, addr_v6.ToString());
904 EXPECT_EQ(kIPv6PublicAddr2String, addr_v6_2.ToString());
905 #if defined(NDEBUG)
906 EXPECT_EQ(kIPv4PublicAddrAnonymizedString, addr_v4.ToSensitiveString());
907 EXPECT_EQ(kIPv6PublicAddrAnonymizedString, addr_v6.ToSensitiveString());
908 EXPECT_EQ(kIPv6PublicAddr2AnonymizedString, addr_v6_2.ToSensitiveString());
909 #else
910 EXPECT_EQ(kIPv4PublicAddrString, addr_v4.ToSensitiveString());
911 EXPECT_EQ(kIPv6PublicAddrString, addr_v6.ToSensitiveString());
912 EXPECT_EQ(kIPv6PublicAddr2String, addr_v6_2.ToSensitiveString());
913 #endif // defined(NDEBUG)
914 }
915
TEST(IPAddressTest,TestInterfaceAddress)916 TEST(IPAddressTest, TestInterfaceAddress) {
917 in6_addr addr;
918 InterfaceAddress addr1(kIPv6PublicAddr,
919 IPV6_ADDRESS_FLAG_TEMPORARY);
920 EXPECT_EQ(addr1.ipv6_flags(), IPV6_ADDRESS_FLAG_TEMPORARY);
921 EXPECT_EQ(addr1.family(), AF_INET6);
922
923 addr = addr1.ipv6_address();
924 EXPECT_TRUE(IN6_ARE_ADDR_EQUAL(&addr, &kIPv6PublicAddr));
925
926 InterfaceAddress addr2 = addr1;
927 EXPECT_EQ(addr1, addr2);
928 EXPECT_EQ(addr2.ipv6_flags(), IPV6_ADDRESS_FLAG_TEMPORARY);
929 addr = addr2.ipv6_address();
930 EXPECT_TRUE(IN6_ARE_ADDR_EQUAL(&addr, &kIPv6PublicAddr));
931
932 InterfaceAddress addr3(addr1);
933 EXPECT_EQ(addr1, addr3);
934 EXPECT_EQ(addr3.ipv6_flags(), IPV6_ADDRESS_FLAG_TEMPORARY);
935 addr = addr3.ipv6_address();
936 EXPECT_TRUE(IN6_ARE_ADDR_EQUAL(&addr, &kIPv6PublicAddr));
937
938 InterfaceAddress addr4(kIPv6PublicAddr,
939 IPV6_ADDRESS_FLAG_DEPRECATED);
940 EXPECT_NE(addr1, addr4);
941
942 // When you compare them as IPAddress, since operator==
943 // is not virtual, it'll be equal.
944 IPAddress *paddr1 = &addr1;
945 IPAddress *paddr4 = &addr4;
946 EXPECT_EQ(*paddr1, *paddr4);
947
948 InterfaceAddress addr5(kIPv6LinkLocalAddr,
949 IPV6_ADDRESS_FLAG_TEMPORARY);
950 EXPECT_NE(addr1, addr5);
951 }
952
953 } // namespace rtc
954