summaryrefslogtreecommitdiffstats
path: root/luni/src/test/java/libcore/java/net/InetSocketAddressTest.java
blob: 1cbcd1a2bf11508329c04bbafdd9ee8f2b4b7749 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package libcore.java.net;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import junit.framework.TestCase;

public class InetSocketAddressTest extends TestCase {
    public void test_ConstructorLjava_lang_StringI() throws Exception {
        try {
            new InetSocketAddress("127.0.0.1", -1);
            fail();
        } catch (IllegalArgumentException expected) {
        }
        try {
            new InetSocketAddress("127.0.0.1", 65536);
            fail();
        } catch (IllegalArgumentException expected) {
        }
    }

    public void test_ConstructorLInetAddressI() throws Exception {
        String[] validIPAddresses = {
            "::1.2.3.4",
            "::", "::",
            "1::0", "1::", "::1",
            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0",
            "127.0.0.1", "localhost", "42.42.42.42", "0.0.0.0"
        };

        String[] results = {
            "0:0:0:0:0:0:102:304",
            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:0",
            "1:0:0:0:0:0:0:0", "1:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:1",
            "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
            "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff",
            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0:0",
            "localhost", "localhost", "42.42.42.42", "0.0.0.0"
        };

        for (int i = 0; i < validIPAddresses.length; i++) {
            InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
            InetSocketAddress isa = new InetSocketAddress(ia, 80);
            assertEquals(80,isa.getPort());
            //assertEquals(results[i], isa.getHostName());
        }

        InetSocketAddress isa = new InetSocketAddress((InetAddress)null, 80);
        assertEquals("0.0.0.0", isa.getHostName());

        try {
            new InetSocketAddress(InetAddress.getByName("localhost"), 65536);
            fail();
        } catch(IllegalArgumentException expected) {
        }

        try {
            new InetSocketAddress(InetAddress.getByName("localhost"), -1);
            fail();
        } catch (IllegalArgumentException expected) {
        }
    }

    public void test_ConstructorI() {
        InetSocketAddress isa = new  InetSocketAddress(65535);
        assertEquals("0.0.0.0", isa.getHostName());
        assertEquals(65535, isa.getPort());

        try {
            new  InetSocketAddress(-1);
            fail();
        } catch (IllegalArgumentException  expected) {
        }

        try {
            new  InetSocketAddress(65536);
            fail();
        } catch (IllegalArgumentException  expected) {
        }
    }

    public void test_equals() throws Exception {
        InetSocketAddress isa1 = new InetSocketAddress(1);
        InetSocketAddress isa2 = new InetSocketAddress(2);
        assertFalse(isa1.equals(isa2));
        InetSocketAddress isa3 = new InetSocketAddress(1);
        assertTrue(isa1.equals(isa3));

        InetAddress localhost = InetAddress.getByName("localhost");
        isa1 = new InetSocketAddress(localhost.getHostName(), 80);
        isa2 = new InetSocketAddress(localhost.getHostAddress(), 80);
        assertTrue(isa1.equals(isa2));
    }

    public void test_getAddress() throws Exception {
        String[] validIPAddresses = {
            "::1.2.3.4", "::", "::", "1::0", "1::", "::1",
            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:FFFF",
            "FFFF:FFFF:FFFF:FFFF:FFFF:FFFF:255.255.255.255",
            "0:0:0:0:0:0:0:0", "0:0:0:0:0:0:0.0.0.0",
            "127.0.0.1", "localhost", "42.42.42.42", "0.0.0.0"
        };
        for (int i = 0; i < validIPAddresses.length; i++) {
            InetAddress ia = InetAddress.getByName(validIPAddresses[i]);
            InetSocketAddress isa = new InetSocketAddress(ia, 0);
            assertEquals(ia, isa.getAddress());
        }
        InetSocketAddress isa = new InetSocketAddress((InetAddress) null, 0);
        assertNotNull(isa.getAddress());
    }

    public void test_hashCode() throws Exception {
        InetAddress localhost = InetAddress.getByName("localhost");
        InetSocketAddress isa1 = new InetSocketAddress(localhost.getHostName(), 8080);
        InetSocketAddress isa2 = new InetSocketAddress(localhost.getHostAddress(), 8080);
        assertTrue(isa1.hashCode() == isa2.hashCode());

        InetSocketAddress isa3 = new InetSocketAddress("0.0.0.0", 8080);
        assertFalse(isa1.hashCode() == isa3.hashCode());
    }

    public void test_isUnresolved() {
        InetSocketAddress isa1 = new InetSocketAddress("localhost", 80);
        assertFalse(isa1.isUnresolved());

        InetSocketAddress sockAddr = new InetSocketAddress("unknown.host", 1000);
        assertTrue(sockAddr.isUnresolved());
    }

    public void test_getHostString() throws Exception {
        // When we have a hostname, we'll get it back because that doesn't cost a DNS lookup...
        InetSocketAddress hasHostname = InetSocketAddress.createUnresolved("some host", 1234);
        assertEquals("some host", hasHostname.getHostString());
        assertEquals("some host", hasHostname.getHostName());
        // When we don't have a hostname, whether or not we do the reverse lookup is the difference
        // between getHostString and getHostName...
        InetAddress address = InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 });
        InetSocketAddress noHostname = new InetSocketAddress(address, 1234);
        assertEquals("127.0.0.1", noHostname.getHostString());
        assertEquals("localhost", noHostname.getHostName());
    }
}