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