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