1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements.  See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License.  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
17package libcore.java.net;
18
19import java.net.InetAddress;
20import java.net.InetSocketAddress;
21import junit.framework.TestCase;
22
23public class InetSocketAddressTest extends TestCase {
24    public void test_ConstructorLjava_lang_StringI() throws Exception {
25        try {
26            new InetSocketAddress("127.0.0.1", -1);
27            fail();
28        } catch (IllegalArgumentException expected) {
29        }
30        try {
31            new InetSocketAddress("127.0.0.1", 65536);
32            fail();
33        } catch (IllegalArgumentException expected) {
34        }
35    }
36
37    public void test_ConstructorLInetAddressI() throws Exception {
38        String[] validIPAddresses = {
39            "::1.2.3.4",
40            "::", "::",
41            "1::0", "1::", "::1",
42            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
43            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
44            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0",
45            "127.0.0.1", "localhost", "42.42.42.42", "0.0.0.0"
46        };
47
48        String[] results = {
49            "0:0:0:0:0:0:102:304",
50            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:0",
51            "1:0:0:0:0:0:0:0", "1:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:1",
52            "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
53            "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
54            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:0",
55            "localhost", "localhost", "42.42.42.42", "0.0.0.0"
56        };
57
58        for (int i = 0; i < validIPAddresses.length; i++) {
59            InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
60            InetSocketAddress isa = new InetSocketAddress(ia, 80);
61            assertEquals(80,isa.getPort());
62            //assertEquals(results[i], isa.getHostName());
63        }
64
65        InetSocketAddress isa = new InetSocketAddress((InetAddress)null, 80);
66        assertEquals("0.0.0.0", isa.getHostName());
67
68        try {
69            new InetSocketAddress(InetAddress.getByName("localhost"), 65536);
70            fail();
71        } catch(IllegalArgumentException expected) {
72        }
73
74        try {
75            new InetSocketAddress(InetAddress.getByName("localhost"), -1);
76            fail();
77        } catch (IllegalArgumentException expected) {
78        }
79    }
80
81    public void test_ConstructorI() {
82        InetSocketAddress isa = new  InetSocketAddress(65535);
83        assertEquals("0.0.0.0", isa.getHostName());
84        assertEquals(65535, isa.getPort());
85
86        try {
87            new  InetSocketAddress(-1);
88            fail();
89        } catch (IllegalArgumentException  expected) {
90        }
91
92        try {
93            new  InetSocketAddress(65536);
94            fail();
95        } catch (IllegalArgumentException  expected) {
96        }
97    }
98
99    public void test_equals() throws Exception {
100        InetSocketAddress isa1 = new InetSocketAddress(1);
101        InetSocketAddress isa2 = new InetSocketAddress(2);
102        assertFalse(isa1.equals(isa2));
103        InetSocketAddress isa3 = new InetSocketAddress(1);
104        assertTrue(isa1.equals(isa3));
105
106        InetAddress localhost = InetAddress.getByName("localhost");
107        isa1 = new InetSocketAddress(localhost.getHostName(), 80);
108        isa2 = new InetSocketAddress(localhost.getHostAddress(), 80);
109        assertTrue(isa1.equals(isa2));
110    }
111
112    public void test_getAddress() throws Exception {
113        String[] validIPAddresses = {
114            "::1.2.3.4", "::", "::", "1::0", "1::", "::1",
115            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
116            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
117            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0",
118            "127.0.0.1", "localhost", "42.42.42.42", "0.0.0.0"
119        };
120        for (int i = 0; i < validIPAddresses.length; i++) {
121            InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
122            InetSocketAddress isa = new InetSocketAddress(ia, 0);
123            assertEquals(ia, isa.getAddress());
124        }
125        InetSocketAddress isa = new InetSocketAddress((InetAddress) null, 0);
126        assertNotNull(isa.getAddress());
127    }
128
129    public void test_hashCode() throws Exception {
130        InetAddress localhost = InetAddress.getByName("localhost");
131        InetSocketAddress isa1 = new InetSocketAddress(localhost.getHostName(), 8080);
132        InetSocketAddress isa2 = new InetSocketAddress(localhost.getHostAddress(), 8080);
133        assertTrue(isa1.hashCode() == isa2.hashCode());
134
135        InetSocketAddress isa3 = new InetSocketAddress("0.0.0.0", 8080);
136        assertFalse(isa1.hashCode() == isa3.hashCode());
137    }
138
139    public void test_isUnresolved() {
140        InetSocketAddress isa1 = new InetSocketAddress("localhost", 80);
141        assertFalse(isa1.isUnresolved());
142
143        InetSocketAddress sockAddr = new InetSocketAddress("unknown.host", 1000);
144        assertTrue(sockAddr.isUnresolved());
145    }
146
147    public void test_getHostString() throws Exception {
148        // When we have a hostname, we'll get it back because that doesn't cost a DNS lookup...
149        InetSocketAddress hasHostname = InetSocketAddress.createUnresolved("some host", 1234);
150        assertEquals("some host", hasHostname.getHostString());
151        assertEquals("some host", hasHostname.getHostName());
152        // When we don't have a hostname, whether or not we do the reverse lookup is the difference
153        // between getHostString and getHostName...
154        InetAddress address = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
155        InetSocketAddress noHostname = new InetSocketAddress(address, 1234);
156        assertEquals("127.0.0.1", noHostname.getHostString());
157        assertEquals("localhost", noHostname.getHostName());
158    }
159}
160