1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.net;
18 
19 import com.google.common.testing.NullPointerTester;
20 
21 import junit.framework.TestCase;
22 
23 import java.net.Inet4Address;
24 import java.net.Inet6Address;
25 import java.net.InetAddress;
26 import java.net.UnknownHostException;
27 
28 /**
29  * Tests for {@link InetAddresses}.
30  *
31  * @author Erik Kline
32  */
33 public class InetAddressesTest extends TestCase {
34 
testNulls()35   public void testNulls() {
36     NullPointerTester tester = new NullPointerTester();
37 
38     tester.testAllPublicStaticMethods(InetAddresses.class);
39   }
40 
testForStringBogusInput()41   public void testForStringBogusInput() {
42     String[] bogusInputs = {
43         "",
44         "016.016.016.016",
45         "016.016.016",
46         "016.016",
47         "016",
48         "000.000.000.000",
49         "000",
50         "0x0a.0x0a.0x0a.0x0a",
51         "0x0a.0x0a.0x0a",
52         "0x0a.0x0a",
53         "0x0a",
54         "42.42.42.42.42",
55         "42.42.42",
56         "42.42",
57         "42",
58         "42..42.42",
59         "42..42.42.42",
60         "42.42.42.42.",
61         "42.42.42.42...",
62         ".42.42.42.42",
63         "...42.42.42.42",
64         "42.42.42.-0",
65         "42.42.42.+0",
66         ".",
67         "...",
68         "bogus",
69         "bogus.com",
70         "192.168.0.1.com",
71         "12345.67899.-54321.-98765",
72         "257.0.0.0",
73         "42.42.42.-42",
74         "3ffe::1.net",
75         "3ffe::1::1",
76         "1::2::3::4:5",
77         "::7:6:5:4:3:2:",  // should end with ":0"
78         ":6:5:4:3:2:1::",  // should begin with "0:"
79         "2001::db:::1",
80         "FEDC:9878",
81         "+1.+2.+3.4",
82         "1.2.3.4e0",
83         "::7:6:5:4:3:2:1:0",  // too many parts
84         "7:6:5:4:3:2:1:0::",  // too many parts
85         "9:8:7:6:5:4:3::2:1",  // too many parts
86         "0:1:2:3::4:5:6:7",  // :: must remove at least one 0.
87         "3ffe:0:0:0:0:0:0:0:1",  // too many parts (9 instead of 8)
88         "3ffe::10000",  // hextet exceeds 16 bits
89         "3ffe::goog",
90         "3ffe::-0",
91         "3ffe::+0",
92         "3ffe::-1",
93         ":",
94         ":::",
95         "::1.2.3",
96         "::1.2.3.4.5",
97         "::1.2.3.4:",
98         "1.2.3.4::",
99         "2001:db8::1:",
100         ":2001:db8::1",
101         ":1:2:3:4:5:6:7",
102         "1:2:3:4:5:6:7:",
103         ":1:2:3:4:5:6:"
104     };
105 
106     for (int i = 0; i < bogusInputs.length; i++) {
107       try {
108         InetAddresses.forString(bogusInputs[i]);
109         fail("IllegalArgumentException expected for '" + bogusInputs[i] + "'");
110       } catch (IllegalArgumentException expected) {
111         // expected behavior
112       }
113       assertFalse(InetAddresses.isInetAddress(bogusInputs[i]));
114     }
115   }
116 
test3ff31()117   public void test3ff31() {
118     try {
119       InetAddresses.forString("3ffe:::1");
120       fail("IllegalArgumentException expected");
121     } catch (IllegalArgumentException expected) {
122       // expected behavior
123     }
124     assertFalse(InetAddresses.isInetAddress("016.016.016.016"));
125   }
126 
testForStringIPv4Input()127   public void testForStringIPv4Input() throws UnknownHostException {
128     String ipStr = "192.168.0.1";
129     InetAddress ipv4Addr = null;
130     // Shouldn't hit DNS, because it's an IP string literal.
131     ipv4Addr = InetAddress.getByName(ipStr);
132     assertEquals(ipv4Addr, InetAddresses.forString(ipStr));
133     assertTrue(InetAddresses.isInetAddress(ipStr));
134   }
135 
testForStringIPv6Input()136   public void testForStringIPv6Input() throws UnknownHostException {
137     String ipStr = "3ffe::1";
138     InetAddress ipv6Addr = null;
139     // Shouldn't hit DNS, because it's an IP string literal.
140     ipv6Addr = InetAddress.getByName(ipStr);
141     assertEquals(ipv6Addr, InetAddresses.forString(ipStr));
142     assertTrue(InetAddresses.isInetAddress(ipStr));
143   }
144 
testForStringIPv6EightColons()145   public void testForStringIPv6EightColons() throws UnknownHostException {
146     String[] eightColons = {
147       "::7:6:5:4:3:2:1",
148       "::7:6:5:4:3:2:0",
149       "7:6:5:4:3:2:1::",
150       "0:6:5:4:3:2:1::",
151     };
152 
153     for (int i = 0; i < eightColons.length; i++) {
154       InetAddress ipv6Addr = null;
155       // Shouldn't hit DNS, because it's an IP string literal.
156       ipv6Addr = InetAddress.getByName(eightColons[i]);
157       assertEquals(ipv6Addr, InetAddresses.forString(eightColons[i]));
158       assertTrue(InetAddresses.isInetAddress(eightColons[i]));
159     }
160   }
161 
testConvertDottedQuadToHex()162   public void testConvertDottedQuadToHex() throws UnknownHostException {
163     String[] ipStrings = {"7::0.128.0.127", "7::0.128.0.128",
164         "7::128.128.0.127", "7::0.128.128.127"};
165 
166     for (String ipString : ipStrings) {
167       // Shouldn't hit DNS, because it's an IP string literal.
168       InetAddress ipv6Addr = InetAddress.getByName(ipString);
169       assertEquals(ipv6Addr, InetAddresses.forString(ipString));
170       assertTrue(InetAddresses.isInetAddress(ipString));
171     }
172   }
173 
testToAddrStringIPv4()174   public void testToAddrStringIPv4() {
175     // Don't need to test IPv4 much; it just calls getHostAddress().
176     assertEquals("1.2.3.4",
177                  InetAddresses.toAddrString(
178                      InetAddresses.forString("1.2.3.4")));
179   }
180 
testToAddrStringIPv6()181   public void testToAddrStringIPv6() {
182     assertEquals("1:2:3:4:5:6:7:8",
183                  InetAddresses.toAddrString(
184                      InetAddresses.forString("1:2:3:4:5:6:7:8")));
185     assertEquals("2001:0:0:4::8",
186                  InetAddresses.toAddrString(
187                      InetAddresses.forString("2001:0:0:4:0:0:0:8")));
188     assertEquals("2001::4:5:6:7:8",
189                  InetAddresses.toAddrString(
190                      InetAddresses.forString("2001:0:0:4:5:6:7:8")));
191     assertEquals("2001:0:3:4:5:6:7:8",
192                  InetAddresses.toAddrString(
193                      InetAddresses.forString("2001:0:3:4:5:6:7:8")));
194     assertEquals("0:0:3::ffff",
195                  InetAddresses.toAddrString(
196                      InetAddresses.forString("0:0:3:0:0:0:0:ffff")));
197     assertEquals("::4:0:0:0:ffff",
198                  InetAddresses.toAddrString(
199                      InetAddresses.forString("0:0:0:4:0:0:0:ffff")));
200     assertEquals("::5:0:0:ffff",
201                  InetAddresses.toAddrString(
202                      InetAddresses.forString("0:0:0:0:5:0:0:ffff")));
203     assertEquals("1::4:0:0:7:8",
204                  InetAddresses.toAddrString(
205                      InetAddresses.forString("1:0:0:4:0:0:7:8")));
206     assertEquals("::",
207                  InetAddresses.toAddrString(
208                      InetAddresses.forString("0:0:0:0:0:0:0:0")));
209     assertEquals("::1",
210                  InetAddresses.toAddrString(
211                      InetAddresses.forString("0:0:0:0:0:0:0:1")));
212     assertEquals("2001:658:22a:cafe::",
213                  InetAddresses.toAddrString(
214                      InetAddresses.forString("2001:0658:022a:cafe::")));
215     assertEquals("::102:304",
216                  InetAddresses.toAddrString(
217                      InetAddresses.forString("::1.2.3.4")));
218   }
219 
testToUriStringIPv4()220   public void testToUriStringIPv4() {
221     String ipStr = "1.2.3.4";
222     InetAddress ip = InetAddresses.forString(ipStr);
223     assertEquals("1.2.3.4", InetAddresses.toUriString(ip));
224   }
225 
testToUriStringIPv6()226   public void testToUriStringIPv6() {
227     // Unfortunately the InetAddress.toString() method for IPv6 addresses
228     // does not collapse contiguous shorts of zeroes with the :: abbreviation.
229     String ipStr = "3ffe::1";
230     InetAddress ip = InetAddresses.forString(ipStr);
231     assertEquals("[3ffe::1]", InetAddresses.toUriString(ip));
232   }
233 
testForUriStringIPv4()234   public void testForUriStringIPv4() {
235     Inet4Address expected = (Inet4Address) InetAddresses.forString("192.168.1.1");
236     assertEquals(expected, InetAddresses.forUriString("192.168.1.1"));
237   }
238 
testForUriStringIPv6()239   public void testForUriStringIPv6() {
240     Inet6Address expected = (Inet6Address) InetAddresses.forString("3ffe:0:0:0:0:0:0:1");
241     assertEquals(expected, InetAddresses.forUriString("[3ffe:0:0:0:0:0:0:1]"));
242   }
243 
testForUriStringIPv4Mapped()244   public void testForUriStringIPv4Mapped() {
245     Inet4Address expected = (Inet4Address) InetAddresses.forString("192.0.2.1");
246     assertEquals(expected, InetAddresses.forUriString("[::ffff:192.0.2.1]"));
247   }
248 
testIsUriInetAddress()249   public void testIsUriInetAddress() {
250     assertTrue(InetAddresses.isUriInetAddress("192.168.1.1"));
251     assertTrue(InetAddresses.isUriInetAddress("[3ffe:0:0:0:0:0:0:1]"));
252     assertTrue(InetAddresses.isUriInetAddress("[::ffff:192.0.2.1]"));
253 
254     assertFalse(InetAddresses.isUriInetAddress("[192.168.1.1"));
255     assertFalse(InetAddresses.isUriInetAddress("192.168.1.1]"));
256     assertFalse(InetAddresses.isUriInetAddress(""));
257     assertFalse(InetAddresses.isUriInetAddress("192.168.999.888"));
258     assertFalse(InetAddresses.isUriInetAddress("www.google.com"));
259     assertFalse(InetAddresses.isUriInetAddress("1:2e"));
260     assertFalse(InetAddresses.isUriInetAddress("[3ffe:0:0:0:0:0:0:1"));
261     assertFalse(InetAddresses.isUriInetAddress("3ffe:0:0:0:0:0:0:1]"));
262     assertFalse(InetAddresses.isUriInetAddress("3ffe:0:0:0:0:0:0:1"));
263     assertFalse(InetAddresses.isUriInetAddress("::ffff:192.0.2.1"));
264   }
265 
testForUriStringBad()266   public void testForUriStringBad() {
267     try {
268       InetAddresses.forUriString("");
269       fail("expected IllegalArgumentException");  // COV_NF_LINE
270     } catch (IllegalArgumentException e) {
271       // expected
272     }
273 
274     try {
275       InetAddresses.forUriString("192.168.999.888");
276       fail("expected IllegalArgumentException");  // COV_NF_LINE
277     } catch (IllegalArgumentException e) {
278       // expected
279     }
280 
281     try {
282       InetAddresses.forUriString("www.google.com");
283       fail("expected IllegalArgumentException");  // COV_NF_LINE
284     } catch (IllegalArgumentException e) {
285       // expected
286     }
287 
288     try {
289       InetAddresses.forUriString("[1:2e]");
290       fail("expected IllegalArgumentException");  // COV_NF_LINE
291     } catch (IllegalArgumentException e) {
292       // expected
293     }
294 
295     try {
296       InetAddresses.forUriString("[192.168.1.1]");
297       fail("expected IllegalArgumentException");  // COV_NF_LINE
298     } catch (IllegalArgumentException e) {
299       // expected
300     }
301 
302     try {
303       InetAddresses.forUriString("192.168.1.1]");
304       fail("expected IllegalArgumentException");  // COV_NF_LINE
305     } catch (IllegalArgumentException e) {
306       // expected
307     }
308 
309     try {
310       InetAddresses.forUriString("[192.168.1.1");
311       fail("expected IllegalArgumentException");  // COV_NF_LINE
312     } catch (IllegalArgumentException e) {
313       // expected
314     }
315 
316     try {
317       InetAddresses.forUriString("[3ffe:0:0:0:0:0:0:1");
318       fail("expected IllegalArgumentException");  // COV_NF_LINE
319     } catch (IllegalArgumentException e) {
320       // expected
321     }
322 
323     try {
324       InetAddresses.forUriString("3ffe:0:0:0:0:0:0:1]");
325       fail("expected IllegalArgumentException");  // COV_NF_LINE
326     } catch (IllegalArgumentException e) {
327       // expected
328     }
329 
330     try {
331       InetAddresses.forUriString("3ffe:0:0:0:0:0:0:1");
332       fail("expected IllegalArgumentException");  // COV_NF_LINE
333     } catch (IllegalArgumentException e) {
334       // expected
335     }
336 
337     try {
338       InetAddresses.forUriString("::ffff:192.0.2.1");
339       fail("expected IllegalArgumentException");  // COV_NF_LINE
340     } catch (IllegalArgumentException e) {
341       // expected
342     }
343   }
344 
testCompatIPv4Addresses()345   public void testCompatIPv4Addresses() {
346     String[] nonCompatAddresses = {
347         "3ffe::1",
348         "::",
349         "::1",
350     };
351 
352     for (int i = 0; i < nonCompatAddresses.length; i++) {
353       InetAddress ip = InetAddresses.forString(nonCompatAddresses[i]);
354       assertFalse(InetAddresses.isCompatIPv4Address((Inet6Address) ip));
355       try {
356         InetAddresses.getCompatIPv4Address((Inet6Address) ip);
357         fail("IllegalArgumentException expected for '" +
358              nonCompatAddresses[i] + "'");
359       } catch (IllegalArgumentException expected) {
360         // expected behavior
361       }
362     }
363 
364     String[] validCompatAddresses = {
365         "::1.2.3.4",
366         "::102:304",
367     };
368     String compatStr = "1.2.3.4";
369     InetAddress compat = InetAddresses.forString(compatStr);
370 
371     for (int i = 0; i < validCompatAddresses.length; i++) {
372       InetAddress ip = InetAddresses.forString(validCompatAddresses[i]);
373       assertTrue("checking '" + validCompatAddresses[i] + "'", ip instanceof Inet6Address);
374       assertTrue("checking '" + validCompatAddresses[i] + "'",
375           InetAddresses.isCompatIPv4Address((Inet6Address) ip));
376       assertEquals("checking '" + validCompatAddresses[i] + "'", compat,
377                    InetAddresses.getCompatIPv4Address((Inet6Address) ip));
378     }
379   }
380 
testMappedIPv4Addresses()381   public void testMappedIPv4Addresses() throws UnknownHostException {
382     /*
383      * Verify that it is not possible to instantiate an Inet6Address
384      * from an "IPv4 mapped" IPv6 address.  Our String-based method can
385      * at least identify them, however.
386      */
387     String mappedStr = "::ffff:192.168.0.1";
388     assertTrue(InetAddresses.isMappedIPv4Address(mappedStr));
389     InetAddress mapped = InetAddresses.forString(mappedStr);
390     assertFalse(mapped instanceof Inet6Address);
391     assertEquals(InetAddress.getByName("192.168.0.1"), mapped);
392 
393     // check upper case
394     mappedStr = "::FFFF:192.168.0.1";
395     assertTrue(InetAddresses.isMappedIPv4Address(mappedStr));
396     mapped = InetAddresses.forString(mappedStr);
397     assertFalse(mapped instanceof Inet6Address);
398     assertEquals(InetAddress.getByName("192.168.0.1"), mapped);
399 
400     mappedStr = "0:00:000:0000:0:ffff:1.2.3.4";
401     assertTrue(InetAddresses.isMappedIPv4Address(mappedStr));
402     mapped = InetAddresses.forString(mappedStr);
403     assertFalse(mapped instanceof Inet6Address);
404     assertEquals(InetAddress.getByName("1.2.3.4"), mapped);
405 
406     mappedStr = "::ffff:0102:0304";
407     assertTrue(InetAddresses.isMappedIPv4Address(mappedStr));
408     mapped = InetAddresses.forString(mappedStr);
409     assertFalse(mapped instanceof Inet6Address);
410     assertEquals(InetAddress.getByName("1.2.3.4"), mapped);
411 
412     assertFalse(InetAddresses.isMappedIPv4Address("::"));
413     assertFalse(InetAddresses.isMappedIPv4Address("::ffff"));
414     assertFalse(InetAddresses.isMappedIPv4Address("::ffff:0"));
415     assertFalse(InetAddresses.isMappedIPv4Address("::fffe:0:0"));
416     assertFalse(InetAddresses.isMappedIPv4Address("::1:ffff:0:0"));
417     assertFalse(InetAddresses.isMappedIPv4Address("foo"));
418     assertFalse(InetAddresses.isMappedIPv4Address("192.0.2.1"));
419   }
420 
test6to4Addresses()421   public void test6to4Addresses() {
422     String[] non6to4Addresses = {
423         "::1.2.3.4",
424         "3ffe::1",
425         "::",
426         "::1",
427     };
428 
429     for (int i = 0; i < non6to4Addresses.length; i++) {
430       InetAddress ip = InetAddresses.forString(non6to4Addresses[i]);
431       assertFalse(InetAddresses.is6to4Address((Inet6Address) ip));
432       try {
433         InetAddresses.get6to4IPv4Address(
434             (Inet6Address) ip);
435         fail("IllegalArgumentException expected for '" +
436              non6to4Addresses[i] + "'");
437       } catch (IllegalArgumentException expected) {
438         // expected behavior
439       }
440     }
441 
442     String valid6to4Address = "2002:0102:0304::1";
443     String ipv4Str = "1.2.3.4";
444 
445     InetAddress ipv4 = InetAddresses.forString(ipv4Str);
446     InetAddress ip = InetAddresses.forString(valid6to4Address);
447     assertTrue(InetAddresses.is6to4Address((Inet6Address) ip));
448     assertEquals(ipv4, InetAddresses.get6to4IPv4Address((Inet6Address) ip));
449   }
450 
testTeredoAddresses()451   public void testTeredoAddresses() {
452     String[] nonTeredoAddresses = {
453         "::1.2.3.4",
454         "3ffe::1",
455         "::",
456         "::1",
457     };
458 
459     for (int i = 0; i < nonTeredoAddresses.length; i++) {
460       InetAddress ip = InetAddresses.forString(nonTeredoAddresses[i]);
461       assertFalse(InetAddresses.isTeredoAddress((Inet6Address) ip));
462       try {
463         InetAddresses.getTeredoInfo((Inet6Address) ip);
464         fail("IllegalArgumentException expected for '" +
465              nonTeredoAddresses[i] + "'");
466       } catch (IllegalArgumentException expected) {
467         // expected behavior
468       }
469     }
470 
471     String validTeredoAddress = "2001:0000:4136:e378:8000:63bf:3fff:fdd2";
472     String serverStr = "65.54.227.120";
473     String clientStr = "192.0.2.45";
474     int port = 40000;
475     int flags = 0x8000;
476 
477     InetAddress ip = InetAddresses.forString(validTeredoAddress);
478     assertTrue(InetAddresses.isTeredoAddress((Inet6Address) ip));
479     InetAddresses.TeredoInfo teredo =
480         InetAddresses.getTeredoInfo((Inet6Address) ip);
481 
482     InetAddress server = InetAddresses.forString(serverStr);
483     assertEquals(server, teredo.getServer());
484 
485     InetAddress client = InetAddresses.forString(clientStr);
486     assertEquals(client, teredo.getClient());
487 
488     assertEquals(port, teredo.getPort());
489     assertEquals(flags, teredo.getFlags());
490   }
491 
testTeredoAddress_nullServer()492   public void testTeredoAddress_nullServer() {
493     InetAddresses.TeredoInfo info = new InetAddresses.TeredoInfo(null, null, 80, 1000);
494     assertEquals(InetAddresses.forString("0.0.0.0"), info.getServer());
495     assertEquals(InetAddresses.forString("0.0.0.0"), info.getClient());
496     assertEquals(80, info.getPort());
497     assertEquals(1000, info.getFlags());
498   }
499 
testIsatapAddresses()500   public void testIsatapAddresses() {
501     InetAddress ipv4 = InetAddresses.forString("1.2.3.4");
502     String[] validIsatapAddresses = {
503         "2001:db8::5efe:102:304",
504         "2001:db8::100:5efe:102:304",  // Private Multicast? Not likely.
505         "2001:db8::200:5efe:102:304",
506         "2001:db8::300:5efe:102:304"   // Public Multicast? Also unlikely.
507     };
508     String[] nonIsatapAddresses = {
509         "::1.2.3.4",
510         "3ffe::1",
511         "::",
512         "::1",
513         "2001:db8::0040:5efe:102:304",
514         "2001:db8::5ffe:102:304",
515         "2001:db8::5eff:102:304",
516         "2001:0:102:203:200:5efe:506:708",  // Teredo address; not ISATAP
517     };
518 
519     for (int i = 0; i < validIsatapAddresses.length; i++) {
520       InetAddress ip = InetAddresses.forString(validIsatapAddresses[i]);
521       assertTrue(InetAddresses.isIsatapAddress((Inet6Address) ip));
522       assertEquals("checking '" + validIsatapAddresses[i] + "'", ipv4,
523                    InetAddresses.getIsatapIPv4Address((Inet6Address) ip));
524     }
525     for (int i = 0; i < nonIsatapAddresses.length; i++) {
526       InetAddress ip = InetAddresses.forString(nonIsatapAddresses[i]);
527       assertFalse(InetAddresses.isIsatapAddress((Inet6Address) ip));
528       try {
529         InetAddresses.getIsatapIPv4Address((Inet6Address) ip);
530         fail("IllegalArgumentException expected for '" +
531              nonIsatapAddresses[i] + "'");
532       } catch (IllegalArgumentException expected) {
533         // expected behavior
534       }
535     }
536   }
537 
testGetEmbeddedIPv4ClientAddress()538   public void testGetEmbeddedIPv4ClientAddress() {
539     Inet6Address testIp;
540 
541     // Test regular global unicast address.
542     testIp = (Inet6Address) InetAddresses.forString("2001:db8::1");
543     assertFalse(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp));
544 
545     // Test ISATAP address.
546     testIp = (Inet6Address) InetAddresses.forString("2001:db8::5efe:102:304");
547     assertFalse(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp));
548 
549     // Test compat address.
550     testIp = (Inet6Address) InetAddresses.forString("::1.2.3.4");
551     assertTrue(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp));
552     InetAddress ipv4 = InetAddresses.forString("1.2.3.4");
553     assertEquals(ipv4, InetAddresses.getEmbeddedIPv4ClientAddress(testIp));
554 
555     // Test 6to4 address.
556     testIp = (Inet6Address) InetAddresses.forString("2002:0102:0304::1");
557     assertTrue(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp));
558     ipv4 = InetAddresses.forString("1.2.3.4");
559     assertEquals(ipv4, InetAddresses.getEmbeddedIPv4ClientAddress(testIp));
560 
561     // Test Teredo address.
562     testIp = (Inet6Address) InetAddresses.forString(
563         "2001:0000:4136:e378:8000:63bf:3fff:fdd2");
564     assertTrue(InetAddresses.hasEmbeddedIPv4ClientAddress(testIp));
565     ipv4 = InetAddresses.forString("192.0.2.45");
566     assertEquals(ipv4, InetAddresses.getEmbeddedIPv4ClientAddress(testIp));
567   }
568 
testGetCoercedIPv4Address()569   public void testGetCoercedIPv4Address() {
570     // Check that a coerced IPv4 address is unaltered.
571     InetAddress localHost4 = InetAddresses.forString("127.0.0.1");
572     assertEquals(localHost4,
573                  InetAddresses.getCoercedIPv4Address(localHost4));
574 
575     // ::1 special case
576     assertEquals(localHost4,
577                  InetAddresses.getCoercedIPv4Address(
578                      InetAddresses.forString("::1")));
579 
580     // :: special case
581     assertEquals(InetAddresses.forString("0.0.0.0"),
582                  InetAddresses.getCoercedIPv4Address(
583                      InetAddresses.forString("::")));
584 
585     // test compat address (should be hashed)
586     assertTrue(InetAddresses.forString("1.2.3.4") !=
587                InetAddresses.getCoercedIPv4Address(
588                    InetAddresses.forString("::1.2.3.4")));
589 
590     // test 6to4 address (should be hashed)
591     assertTrue(InetAddresses.forString("1.2.3.4") !=
592                InetAddresses.getCoercedIPv4Address(
593                    InetAddresses.forString("2002:0102:0304::1")));
594 
595     // 2 6to4 addresses differing in the embedded IPv4 address should
596     // hash to the different values.
597     assertTrue(InetAddresses.getCoercedIPv4Address(
598                    InetAddresses.forString("2002:0102:0304::1")) !=
599                InetAddresses.getCoercedIPv4Address(
600                    InetAddresses.forString("2002:0506:0708::1")));
601 
602     // 2 6to4 addresses NOT differing in the embedded IPv4 address should
603     // hash to the same value.
604     assertTrue(InetAddresses.getCoercedIPv4Address(
605                    InetAddresses.forString("2002:0102:0304::1")) !=
606                InetAddresses.getCoercedIPv4Address(
607                    InetAddresses.forString("2002:0102:0304::2")));
608 
609     // test Teredo address (should be hashed)
610     assertTrue(InetAddresses.forString("192.0.2.45") !=
611                InetAddresses.getCoercedIPv4Address(
612                    InetAddresses.forString(
613                        "2001:0000:4136:e378:8000:63bf:3fff:fdd2")));
614 
615     // 2 Teredo addresses differing in the embedded IPv4 address should
616     // hash to the different values.
617     assertTrue(InetAddresses.getCoercedIPv4Address(
618                    InetAddresses.forString(
619                        "2001:0000:4136:e378:8000:63bf:3fff:fdd2")) !=
620                InetAddresses.getCoercedIPv4Address(
621                    InetAddresses.forString(
622                        "2001:0000:4136:e379:8000:63bf:3fff:fdd2")));
623 
624     // 2 Teredo addresses NOT differing in the embedded IPv4 address should
625     // hash to the same value.
626     assertEquals(InetAddresses.getCoercedIPv4Address(
627                      InetAddresses.forString(
628                          "2001:0000:4136:e378:8000:63bf:3fff:fdd2")),
629                  InetAddresses.getCoercedIPv4Address(
630                      InetAddresses.forString(
631                          "2001:0000:4136:e378:9000:63bf:3fff:fdd2")));
632 
633     // Test that an address hashes in to the 224.0.0.0/3 number-space.
634     InetAddress coerced = InetAddresses.getCoercedIPv4Address(
635                               InetAddresses.forString("2001:4860::1"));
636     assertTrue(0xe0000000 <= InetAddresses.coerceToInteger(coerced));
637     assertTrue(InetAddresses.coerceToInteger(coerced) <= 0xfffffffe);
638   }
639 
testToInteger()640   public void testToInteger() {
641     InetAddress ipv4Addr = InetAddresses.forString("127.0.0.1");
642     assertEquals(0x7f000001, InetAddresses.coerceToInteger(ipv4Addr));
643   }
644 
testFromInteger()645   public void testFromInteger() {
646     assertEquals(InetAddresses.fromInteger(0x7f000001),
647                  InetAddresses.forString("127.0.0.1"));
648   }
649 
testFromLittleEndianByteArray()650   public void testFromLittleEndianByteArray() throws UnknownHostException {
651     assertEquals(
652         InetAddresses.fromLittleEndianByteArray(new byte[] {1, 2, 3, 4}),
653         InetAddress.getByAddress(new byte[] {4, 3, 2, 1}));
654 
655     assertEquals(
656         InetAddresses.fromLittleEndianByteArray(
657             new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}),
658         InetAddress.getByAddress(
659             new byte[] {16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}));
660 
661     try {
662       InetAddresses.fromLittleEndianByteArray(new byte[3]);
663       fail("expected exception");
664     } catch (UnknownHostException expected) {
665       // success
666     }
667   }
668 
testIsMaximum()669   public void testIsMaximum() throws UnknownHostException {
670     InetAddress address = InetAddress.getByName("255.255.255.254");
671     assertFalse(InetAddresses.isMaximum(address));
672 
673     address = InetAddress.getByName("255.255.255.255");
674     assertTrue(InetAddresses.isMaximum(address));
675 
676     address = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:fffe");
677     assertFalse(InetAddresses.isMaximum(address));
678 
679     address = InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
680     assertTrue(InetAddresses.isMaximum(address));
681   }
682 
testIncrementIPv4()683   public void testIncrementIPv4() throws UnknownHostException {
684     InetAddress address_66_0 = InetAddress.getByName("172.24.66.0");
685     InetAddress address_66_255 = InetAddress.getByName("172.24.66.255");
686     InetAddress address_67_0 = InetAddress.getByName("172.24.67.0");
687 
688     InetAddress address = address_66_0;
689     for (int i = 0; i < 255; i++) {
690       address = InetAddresses.increment(address);
691     }
692     assertEquals(address_66_255, address);
693 
694     address = InetAddresses.increment(address);
695     assertEquals(address_67_0, address);
696 
697     InetAddress address_ffffff = InetAddress.getByName("255.255.255.255");
698     address = address_ffffff;
699     try {
700       address = InetAddresses.increment(address);
701       fail();
702     } catch (IllegalArgumentException expected) {}
703   }
704 
testIncrementIPv6()705   public void testIncrementIPv6() throws UnknownHostException {
706     InetAddress addressV6_66_0 = InetAddress.getByName("2001:db8::6600");
707     InetAddress addressV6_66_ff = InetAddress.getByName("2001:db8::66ff");
708     InetAddress addressV6_67_0 = InetAddress.getByName("2001:db8::6700");
709 
710     InetAddress address = addressV6_66_0;
711     for (int i = 0; i < 255; i++) {
712       address = InetAddresses.increment(address);
713     }
714     assertEquals(addressV6_66_ff, address);
715 
716     address = InetAddresses.increment(address);
717     assertEquals(addressV6_67_0, address);
718 
719     InetAddress addressV6_ffffff =
720         InetAddress.getByName("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff");
721     address = addressV6_ffffff;
722     try {
723       address = InetAddresses.increment(address);
724       fail();
725     } catch (IllegalArgumentException expected) {}
726   }
727 
testDecrementIPv4()728   public void testDecrementIPv4() throws UnknownHostException {
729     InetAddress address660 = InetAddress.getByName("172.24.66.0");
730     InetAddress address66255 = InetAddress.getByName("172.24.66.255");
731     InetAddress address670 = InetAddress.getByName("172.24.67.0");
732 
733     InetAddress address = address670;
734     address = InetAddresses.decrement(address);
735 
736     assertEquals(address66255, address);
737 
738     for (int i = 0; i < 255; i++) {
739       address = InetAddresses.decrement(address);
740     }
741     assertEquals(address660, address);
742 
743     InetAddress address0000 = InetAddress.getByName("0.0.0.0");
744     address = address0000;
745     try {
746       address = InetAddresses.decrement(address);
747       fail();
748     } catch (IllegalArgumentException expected) {}
749   }
750 
testDecrementIPv6()751   public void testDecrementIPv6() throws UnknownHostException {
752     InetAddress addressV6660 = InetAddress.getByName("2001:db8::6600");
753     InetAddress addressV666ff = InetAddress.getByName("2001:db8::66ff");
754     InetAddress addressV6670 = InetAddress.getByName("2001:db8::6700");
755 
756     InetAddress address = addressV6670;
757     address = InetAddresses.decrement(address);
758 
759     assertEquals(addressV666ff, address);
760 
761     for (int i = 0; i < 255; i++) {
762       address = InetAddresses.decrement(address);
763     }
764     assertEquals(addressV6660, address);
765 
766     InetAddress addressV6000000 =
767         InetAddress.getByName("0:0:0:0:0:0:0:0");
768     address = addressV6000000;
769     try {
770       address = InetAddresses.decrement(address);
771       fail();
772     } catch (IllegalArgumentException expected) {}
773   }
774 }
775