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