1cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath/* Licensed to the Apache Software Foundation (ASF) under one or more 2cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * contributor license agreements. See the NOTICE file distributed with 3cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * this work for additional information regarding copyright ownership. 4cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * The ASF licenses this file to You under the Apache License, Version 2.0 5cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * (the "License"); you may not use this file except in compliance with 6cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * the License. You may obtain a copy of the License at 7cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * 8cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * http://www.apache.org/licenses/LICENSE-2.0 9cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * 10cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * Unless required by applicable law or agreed to in writing, software 11cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * distributed under the License is distributed on an "AS IS" BASIS, 12cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * See the License for the specific language governing permissions and 14cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * limitations under the License. 15cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 16ab762bb740405d0fefcccf4a0899a234f995be13Narayan Kamathpackage org.apache.harmony.tests.java.net; 17cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 18cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.io.Serializable; 19cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport java.net.InetSocketAddress; 20cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 21cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport junit.framework.TestCase; 22cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 23cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport org.apache.harmony.testframework.serialization.SerializationTest; 24cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathimport org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 25cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 26cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamathpublic class InetSocketAddressTest extends TestCase { 27cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 28cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 29cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.net.InetSocketAddress#InetSocketAddress(String, int) 30cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 31cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_ConstructorLjava_lang_StringI() throws Exception { 32cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath // regression test for Harmony-1042 33cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath InetSocketAddress address = new InetSocketAddress("127.0.0.1", 0); 34cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals("/127.0.0.1:0", address.toString()); 35cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath String localhostName = address.getHostName(); 36cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertNotNull(localhostName); 37cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals(localhostName + "/127.0.0.1:0", address.toString()); 38cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 39cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 40cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 41cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.net.InetSocketAddress#createUnresolved(String, int) 42cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 43cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_createUnresolvedLjava_lang_StringI() { 44cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath HostPortPair[] legalHostPortPairs = { new HostPortPair("127.0.0.1", 1234), 45cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath new HostPortPair("192.168.0.1", 10000), new HostPortPair("127.0.0", 0), 46cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath new HostPortPair("127.0.0", 65535), 47cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath new HostPortPair("strange host", 65535) }; 48cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath for (int i = 0; i < legalHostPortPairs.length; i++) { 49cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath InetSocketAddress isa = InetSocketAddress.createUnresolved( 50cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath legalHostPortPairs[i].host, legalHostPortPairs[i].port); 51cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertTrue(isa.isUnresolved()); 52cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertNull(isa.getAddress()); 53cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals(isa.getHostName(), legalHostPortPairs[i].host); 54cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals(isa.getPort(), legalHostPortPairs[i].port); 55cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 56cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 57cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 58cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 59cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * java.net.InetSocketAddress#createUnresolved(String, int) 60cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 61cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void test_createUnresolvedLjava_lang_StringI_IllegalArgumentException() { 62cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath HostPortPair[] illegalHostPortPairs = { new HostPortPair(null, 1), 63cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath new HostPortPair("host", -1), new HostPortPair("host", 65536) }; 64cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath for (int i = 0; i < illegalHostPortPairs.length; i++) { 65cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath try { 66cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath InetSocketAddress.createUnresolved( 67cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath illegalHostPortPairs[i].host, 68cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath illegalHostPortPairs[i].port); 69cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath fail("should throw IllegalArgumentException, host = " 70cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath + illegalHostPortPairs[i].host + ",port = " 71cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath + illegalHostPortPairs[i].port); 72cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } catch (IllegalArgumentException e) { 73cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath // expected 74cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 75cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 76cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 77cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 78cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /* 79cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * inner class for createUnresolved test convenience. 80cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 81cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath class HostPortPair { 82cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath String host; 83cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 84cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath int port; 85cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 86cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public HostPortPair(String host, int port) { 87cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath this.host = host; 88cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath this.port = port; 89cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 90cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 91cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 92cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath ; 93cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 94cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath // comparator for InetSocketAddress objects 95cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath private static final SerializableAssert COMPARATOR = new SerializableAssert() { 96cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void assertDeserialized(Serializable initial, 97cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Serializable deserialized) { 98cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 99cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath InetSocketAddress init = (InetSocketAddress) initial; 100cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath InetSocketAddress desr = (InetSocketAddress) deserialized; 101cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 102cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals("HostName", init.getHostName(), desr.getHostName()); 103cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals("Port", init.getPort(), desr.getPort()); 104cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath assertEquals("Address", init.getAddress(), desr.getAddress()); 105cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 106cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath }; 107cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 108cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 109cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * serialization/deserialization compatibility. 110cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 111cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void testSerializationSelf() throws Exception { 112cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 113cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Object[] testCases = { 114cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath InetSocketAddress.createUnresolved("badhost", 1000), // unresolved 115cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath new InetSocketAddress("Localhost", 1000) }; 116cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 117cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath SerializationTest.verifySelf(testCases, COMPARATOR); 118cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 119cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 120cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath /** 121cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath * serialization/deserialization compatibility with RI. 122cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath */ 123cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath public void testSerializationCompatibility() throws Exception { 124cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 125cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath Object[] testCases = { 126cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath InetSocketAddress.createUnresolved("badhost", 1000), // unresolved 127cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath new InetSocketAddress("Localhost", 1000) }; 128cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath 129cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath SerializationTest.verifyGolden(this, testCases, COMPARATOR); 130cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath } 131cb318c6f4fe5b0e20099fa85f1b95ccb2d24119fNarayan Kamath} 132