1 /*
2  * Copyright (C) 2018 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 package android.net.cts;
17 
18 import android.net.InetAddresses;
19 import java.net.InetAddress;
20 import junitparams.JUnitParamsRunner;
21 import junitparams.Parameters;
22 import org.junit.Test;
23 import org.junit.runner.RunWith;
24 
25 import static com.google.common.truth.Truth.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30 
31 @RunWith(JUnitParamsRunner.class)
32 public class InetAddressesTest {
33 
validNumericAddressesAndStringRepresentation()34     public static String[][] validNumericAddressesAndStringRepresentation() {
35         return new String[][] {
36             // Regular IPv4.
37             { "1.2.3.4", "1.2.3.4" },
38 
39             // Regular IPv6.
40             { "2001:4860:800d::68", "2001:4860:800d::68" },
41             { "1234:5678::9ABC:DEF0", "1234:5678::9abc:def0" },
42             { "2001:cdba:9abc:5678::", "2001:cdba:9abc:5678::" },
43             { "::2001:cdba:9abc:5678", "::2001:cdba:9abc:5678" },
44             { "64:ff9b::1.2.3.4", "64:ff9b::102:304" },
45 
46             { "::9abc:5678", "::154.188.86.120" },
47 
48             // Mapped IPv4
49             { "::ffff:127.0.0.1", "127.0.0.1" },
50 
51             // Android does not recognize Octal (leading 0) cases: they are treated as decimal.
52             { "0177.00.00.01", "177.0.0.1" },
53 
54             // Verify that examples from JavaDoc work correctly.
55             { "192.0.2.1", "192.0.2.1" },
56             { "2001:db8::1:2", "2001:db8::1:2" },
57         };
58     }
59 
invalidNumericAddresses()60     public static String[] invalidNumericAddresses() {
61         return new String[] {
62             "",
63             " ",
64             "\t",
65             "\n",
66             "1.2.3.4.",
67             "1.2.3",
68             "1.2",
69             "1",
70             "1234",
71             "0",
72             "0x1.0x2.0x3.0x4",
73             "0x7f.0x00.0x00.0x01",
74             "0256.00.00.01",
75             "fred",
76             "www.google.com",
77             // IPv6 encoded for use in URL as defined in RFC 2732
78             "[fe80::6:2222]",
79         };
80     }
81 
82     @Parameters(method = "validNumericAddressesAndStringRepresentation")
83     @Test
parseNumericAddress(String address, String expectedString)84     public void parseNumericAddress(String address, String expectedString) {
85         InetAddress inetAddress = InetAddresses.parseNumericAddress(address);
86         assertEquals(expectedString, inetAddress.getHostAddress());
87     }
88 
89     @Parameters(method = "invalidNumericAddresses")
90     @Test
test_parseNonNumericAddress(String address)91     public void test_parseNonNumericAddress(String address) {
92         try {
93             InetAddress inetAddress = InetAddresses.parseNumericAddress(address);
94             fail(String.format(
95                 "Address %s is not numeric but was parsed as %s", address, inetAddress));
96         } catch (IllegalArgumentException e) {
97             assertThat(e.getMessage()).contains(address);
98         }
99     }
100 
101     @Test
test_parseNumericAddress_null()102     public void test_parseNumericAddress_null() {
103         try {
104             InetAddress inetAddress = InetAddresses.parseNumericAddress(null);
105             fail(String.format("null is not numeric but was parsed as %s", inetAddress));
106         } catch (NullPointerException e) {
107             // expected
108         }
109     }
110 
111     @Parameters(method = "validNumericAddressesAndStringRepresentation")
112     @Test
test_isNumericAddress(String address, String unused)113     public void test_isNumericAddress(String address, String unused) {
114         assertTrue("expected '" + address + "' to be treated as numeric",
115             InetAddresses.isNumericAddress(address));
116     }
117 
118     @Parameters(method = "invalidNumericAddresses")
119     @Test
test_isNotNumericAddress(String address)120     public void test_isNotNumericAddress(String address) {
121         assertFalse("expected '" + address + "' to be treated as non-numeric",
122             InetAddresses.isNumericAddress(address));
123     }
124 
125     @Test
test_isNumericAddress_null()126     public void test_isNumericAddress_null() {
127         try {
128             InetAddresses.isNumericAddress(null);
129             fail("expected null to throw a NullPointerException");
130         } catch (NullPointerException e) {
131             // expected
132         }
133     }
134 }
135