Home | History | Annotate | Download | only in net
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      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 libcore.java.net;
     18 
     19 import java.net.Inet4Address;
     20 import java.net.Inet6Address;
     21 import java.net.InetAddress;
     22 import java.net.NetworkInterface;
     23 import java.net.UnknownHostException;
     24 import java.util.Collections;
     25 import libcore.util.SerializationTester;
     26 
     27 public class InetAddressTest extends junit.framework.TestCase {
     28     private static final byte[] LOOPBACK6_BYTES = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
     29 
     30     private static final String[] INVALID_IPv4_NUMERIC_ADDRESSES = new String[] {
     31         // IPv4 addresses may not be surrounded by square brackets.
     32         "[127.0.0.1]",
     33 
     34         // Trailing dots are not allowed.
     35         "1.2.3.4.",
     36         // Nor is any kind of trailing junk.
     37         "1.2.3.4hello",
     38 
     39         // Out of range.
     40         "256.2.3.4",
     41         "1.256.3.4",
     42         "1.2.256.4",
     43         "1.2.3.256",
     44 
     45         // Deprecated.
     46         "1.2.3",
     47         "1.2",
     48         "1",
     49         "1234",
     50         "0", // Single out the deprecated form of the ANY address.
     51 
     52         // Hex.
     53         "0x1.0x2.0x3.0x4",
     54         "0x7f.0x00.0x00.0x01",
     55         "7f.0.0.1",
     56 
     57         // Octal.
     58         "0177.00.00.01", // Historically, this would have been interpreted as 127.0.0.1.
     59 
     60         // Negative numbers.
     61         "-1.0.0.1",
     62         "1.-1.0.1",
     63         "1.0.-1.1",
     64         "1.0.0.-1",
     65     };
     66 
     67     private static Inet6Address loopback6() throws Exception {
     68         return (Inet6Address) InetAddress.getByAddress(LOOPBACK6_BYTES);
     69     }
     70 
     71     private static Inet6Address localhost6() throws Exception {
     72         return (Inet6Address) InetAddress.getByAddress("localhost", LOOPBACK6_BYTES);
     73     }
     74 
     75     public void test_parseNumericAddress() throws Exception {
     76         // Regular IPv4.
     77         assertEquals("/1.2.3.4", InetAddress.parseNumericAddress("1.2.3.4").toString());
     78         // Regular IPv6.
     79         assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("2001:4860:800d::68").toString());
     80         // Mapped IPv4
     81         assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("::ffff:127.0.0.1").toString());
     82         // Optional square brackets around IPv6 addresses, including mapped IPv4.
     83         assertEquals("/2001:4860:800d::68", InetAddress.parseNumericAddress("[2001:4860:800d::68]").toString());
     84         assertEquals("/127.0.0.1", InetAddress.parseNumericAddress("[::ffff:127.0.0.1]").toString());
     85 
     86         try {
     87             InetAddress.parseNumericAddress("example.com"); // Not numeric.
     88             fail();
     89         } catch (IllegalArgumentException expected) {
     90         }
     91 
     92         for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) {
     93             try {
     94                 InetAddress.parseNumericAddress(invalid);
     95                 fail(invalid);
     96             } catch (IllegalArgumentException expected) {
     97             }
     98         }
     99 
    100         // Strange special cases, for compatibility with InetAddress.getByName.
    101         assertTrue(InetAddress.parseNumericAddress(null).isLoopbackAddress());
    102         assertTrue(InetAddress.parseNumericAddress("").isLoopbackAddress());
    103     }
    104 
    105     public void test_isNumeric() throws Exception {
    106         // IPv4
    107         assertTrue(InetAddress.isNumeric("1.2.3.4"));
    108         assertTrue(InetAddress.isNumeric("127.0.0.1"));
    109 
    110         // IPv6
    111         assertTrue(InetAddress.isNumeric("::1"));
    112         assertTrue(InetAddress.isNumeric("2001:4860:800d::68"));
    113 
    114         // Mapped IPv4
    115         assertTrue(InetAddress.isNumeric("::ffff:127.0.0.1"));
    116 
    117         // Optional square brackets around IPv6 addresses, including mapped IPv4.
    118         assertTrue(InetAddress.isNumeric("[2001:4860:800d::68]"));
    119         assertTrue(InetAddress.isNumeric("[::ffff:127.0.0.1]"));
    120 
    121         // Negative test
    122         assertFalse(InetAddress.isNumeric("example.com"));
    123 
    124         for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) {
    125             assertFalse(invalid, InetAddress.isNumeric(invalid));
    126         }
    127     }
    128 
    129     public void test_isLinkLocalAddress() throws Exception {
    130         assertFalse(InetAddress.getByName("127.0.0.1").isLinkLocalAddress());
    131         assertFalse(InetAddress.getByName("::ffff:127.0.0.1").isLinkLocalAddress());
    132         assertTrue(InetAddress.getByName("169.254.1.2").isLinkLocalAddress());
    133 
    134         assertFalse(InetAddress.getByName("fec0::").isLinkLocalAddress());
    135         assertTrue(InetAddress.getByName("fe80::").isLinkLocalAddress());
    136     }
    137 
    138     public void test_isMCSiteLocalAddress() throws Exception {
    139         assertFalse(InetAddress.getByName("239.254.255.255").isMCSiteLocal());
    140         assertTrue(InetAddress.getByName("239.255.0.0").isMCSiteLocal());
    141         assertTrue(InetAddress.getByName("239.255.255.255").isMCSiteLocal());
    142         assertFalse(InetAddress.getByName("240.0.0.0").isMCSiteLocal());
    143 
    144         assertFalse(InetAddress.getByName("ff06::").isMCSiteLocal());
    145         assertTrue(InetAddress.getByName("ff05::").isMCSiteLocal());
    146         assertTrue(InetAddress.getByName("ff15::").isMCSiteLocal());
    147     }
    148 
    149     public void test_isReachable() throws Exception {
    150         // http://code.google.com/p/android/issues/detail?id=20203
    151         String s = "aced0005737200146a6176612e6e65742e496e6574416464726573732d9b57af"
    152                 + "9fe3ebdb0200034900076164647265737349000666616d696c794c0008686f737"
    153                 + "44e616d657400124c6a6176612f6c616e672f537472696e673b78704a7d9d6300"
    154                 + "00000274000e7777772e676f6f676c652e636f6d";
    155         InetAddress inetAddress = InetAddress.getByName("www.google.com");
    156         new SerializationTester<InetAddress>(inetAddress, s) {
    157             @Override protected void verify(InetAddress deserialized) throws Exception {
    158                 deserialized.isReachable(500);
    159                 for (NetworkInterface nif
    160                         : Collections.list(NetworkInterface.getNetworkInterfaces())) {
    161                     deserialized.isReachable(nif, 20, 500);
    162                 }
    163             }
    164             @Override protected boolean equals(InetAddress a, InetAddress b) {
    165                 return a.getHostName().equals(b.getHostName());
    166             }
    167         }.test();
    168     }
    169 
    170     public void test_isSiteLocalAddress() throws Exception {
    171         assertFalse(InetAddress.getByName("144.32.32.1").isSiteLocalAddress());
    172         assertTrue(InetAddress.getByName("10.0.0.1").isSiteLocalAddress());
    173         assertTrue(InetAddress.getByName("172.16.0.1").isSiteLocalAddress());
    174         assertFalse(InetAddress.getByName("172.32.0.1").isSiteLocalAddress());
    175         assertTrue(InetAddress.getByName("192.168.0.1").isSiteLocalAddress());
    176 
    177         assertFalse(InetAddress.getByName("fc00::").isSiteLocalAddress());
    178         assertTrue(InetAddress.getByName("fec0::").isSiteLocalAddress());
    179     }
    180 
    181     public void test_getByName() throws Exception {
    182         for (String invalid : INVALID_IPv4_NUMERIC_ADDRESSES) {
    183             try {
    184                 InetAddress.getByName(invalid);
    185                 fail(invalid);
    186             } catch (UnknownHostException expected) {
    187             }
    188         }
    189     }
    190 
    191     public void test_getLoopbackAddress() throws Exception {
    192         assertTrue(InetAddress.getLoopbackAddress().isLoopbackAddress());
    193     }
    194 
    195     public void test_equals() throws Exception {
    196         InetAddress addr = InetAddress.getByName("239.191.255.255");
    197         assertTrue(addr.equals(addr));
    198         assertTrue(loopback6().equals(localhost6()));
    199         assertFalse(addr.equals(loopback6()));
    200 
    201         assertTrue(Inet4Address.LOOPBACK.equals(Inet4Address.LOOPBACK));
    202 
    203         // http://b/4328294 - the scope id isn't included when comparing Inet6Address instances.
    204         byte[] bs = new byte[16];
    205         assertEquals(Inet6Address.getByAddress("1", bs, 1), Inet6Address.getByAddress("2", bs, 2));
    206     }
    207 
    208     public void test_getHostAddress() throws Exception {
    209         assertEquals("::1", localhost6().getHostAddress());
    210         assertEquals("::1", InetAddress.getByName("::1").getHostAddress());
    211 
    212         assertEquals("127.0.0.1", Inet4Address.LOOPBACK.getHostAddress());
    213 
    214         // IPv4 mapped address
    215         assertEquals("127.0.0.1", InetAddress.getByName("::ffff:127.0.0.1").getHostAddress());
    216 
    217         InetAddress aAddr = InetAddress.getByName("224.0.0.0");
    218         assertEquals("224.0.0.0", aAddr.getHostAddress());
    219 
    220 
    221         try {
    222             InetAddress.getByName("1");
    223             fail();
    224         } catch (UnknownHostException expected) {
    225         }
    226 
    227         byte[] bAddr = {
    228             (byte) 0xFE, (byte) 0x80, (byte) 0x00, (byte) 0x00,
    229             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
    230             (byte) 0x02, (byte) 0x11, (byte) 0x25, (byte) 0xFF,
    231             (byte) 0xFE, (byte) 0xF8, (byte) 0x7C, (byte) 0xB2
    232         };
    233         aAddr = Inet6Address.getByAddress(bAddr);
    234         String aString = aAddr.getHostAddress();
    235         assertTrue(aString.equals("fe80:0:0:0:211:25ff:fef8:7cb2") || aString.equals("fe80::211:25ff:fef8:7cb2"));
    236 
    237         byte[] cAddr = {
    238             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
    239             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
    240             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF,
    241             (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF
    242         };
    243         aAddr = Inet6Address.getByAddress(cAddr);
    244         assertEquals("ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", aAddr.getHostAddress());
    245 
    246         byte[] dAddr = {
    247             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
    248             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
    249             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
    250             (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
    251         };
    252         aAddr = Inet6Address.getByAddress(dAddr);
    253         aString = aAddr.getHostAddress();
    254         assertTrue(aString.equals("0:0:0:0:0:0:0:0") || aString.equals("::"));
    255 
    256         byte[] eAddr = {
    257             (byte) 0x00, (byte) 0x01, (byte) 0x02, (byte) 0x03,
    258             (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07,
    259             (byte) 0x08, (byte) 0x09, (byte) 0x0a, (byte) 0x0b,
    260             (byte) 0x0c, (byte) 0x0d, (byte) 0x0e, (byte) 0x0f
    261         };
    262         aAddr = Inet6Address.getByAddress(eAddr);
    263         assertEquals("1:203:405:607:809:a0b:c0d:e0f", aAddr.getHostAddress());
    264 
    265         byte[] fAddr = {
    266             (byte) 0x00, (byte) 0x10, (byte) 0x20, (byte) 0x30,
    267             (byte) 0x40, (byte) 0x50, (byte) 0x60, (byte) 0x70,
    268             (byte) 0x80, (byte) 0x90, (byte) 0xa0, (byte) 0xb0,
    269             (byte) 0xc0, (byte) 0xd0, (byte) 0xe0, (byte) 0xf0
    270         };
    271         aAddr = Inet6Address.getByAddress(fAddr);
    272         assertEquals("10:2030:4050:6070:8090:a0b0:c0d0:e0f0", aAddr.getHostAddress());
    273     }
    274 
    275     public void test_hashCode() throws Exception {
    276         InetAddress addr1 = InetAddress.getByName("1.0.0.1");
    277         InetAddress addr2 = InetAddress.getByName("1.0.0.1");
    278         assertTrue(addr1.hashCode() == addr2.hashCode());
    279 
    280         assertTrue(loopback6().hashCode() == localhost6().hashCode());
    281     }
    282 
    283     public void test_toString() throws Exception {
    284         String validIPAddresses[] = {
    285             "::1.2.3.4", "::", "::", "1::0", "1::", "::1",
    286             "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
    287             "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
    288             "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0"
    289         };
    290 
    291         String [] resultStrings = {
    292             "/::1.2.3.4", "/::", "/::", "/1::", "/1::", "/::1",
    293             "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
    294             "/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff", "/::",
    295             "/::"
    296         };
    297 
    298         for(int i = 0; i < validIPAddresses.length; i++) {
    299             InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
    300             String result = ia.toString();
    301             assertNotNull(result);
    302             assertEquals(resultStrings[i], result);
    303         }
    304     }
    305 }
    306