Path: blob/master/test/jdk/java/net/InetSocketAddress/ToString.java
41149 views
/*1* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8225499 446406426* @library /test/lib27* @summary InetSocketAddress::toString not friendly to IPv6 literal addresses28* @run testng/othervm ToString29* @run testng/othervm -Djava.net.preferIPv4Stack=true ToString30* @run testng/othervm -Djava.net.preferIPv6Addresses=true ToString31*/3233import java.net.*;3435import jdk.test.lib.net.IPSupport;36import org.testng.annotations.BeforeTest;37import org.testng.annotations.DataProvider;38import org.testng.annotations.Test;3940public class ToString {4142private static final String loopbackAddr;43private static final String wildcardAddr;44private static final String localAddr;4546static {47try {48InetAddress loopback = InetAddress.getLoopbackAddress();49String addr = loopback.getHostAddress();50if (loopback instanceof Inet6Address) {51addr = "[" + addr + "]";52}53loopbackAddr = addr;5455InetSocketAddress isa = new InetSocketAddress((InetAddress) null, 80);56addr = isa.getAddress().toString();57if (isa.getAddress() instanceof Inet6Address) {58addr = "::/[0:0:0:0:0:0:0:0]";59}60wildcardAddr = addr;6162InetAddress ia = InetAddress.getLocalHost();63addr = ia.toString();64if (ia instanceof Inet6Address) {65addr = ia.getHostName() + "/[" + ia.getHostAddress() + "]";66}67localAddr = addr;6869} catch (UnknownHostException uhe) {70throw new RuntimeException(uhe);71}72}7374@BeforeTest75public void setup() {76IPSupport.throwSkippedExceptionIfNonOperational();77}7879@Test80// InetSocketAddress.toString() throws NPE with unresolved address81public static void NPETest() {82System.out.println(new InetSocketAddress("unresolved", 12345));83}8485@DataProvider(name = "hostPortArgs")86public Object[][] createArgs1() {87return new Object[][]{88// hostname, port number, expected string in format89// <hostname>/<IP literal>:<port> or90// <hostname>/<unresolved>:<port> if address is unresolved91{"::1", 80, "/[0:0:0:0:0:0:0:1]:80"},92{"fedc:ba98:7654:3210:fedc:ba98:7654:3210", 80, "/[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:80"},93{"::192.9.5.5", 80, "/[0:0:0:0:0:0:c009:505]:80"},94{"127.0.0.1", 80, "/127.0.0.1:80"},95{"::ffff:192.0.2.128", 80, "/192.0.2.128:80"},96{"0", 80, "/0.0.0.0:80"},97{":", 80, ":/<unresolved>:80"},98{":1", 80, ":1/<unresolved>:80"}99};100}101102@Test(dataProvider = "hostPortArgs")103public static void testConstructor(String host, int port, String string) {104String received = new InetSocketAddress(host, port).toString();105106if (!string.equals(received)) {107throw new RuntimeException("Expected: " + string + " Received: " + received);108}109}110111@DataProvider(name = "addrPortArgs")112public Object[][] createArgs2() {113InetAddress nullAddr = null;114try {115return new Object[][]{116// InetAddress, port number, expected string117{InetAddress.getLoopbackAddress(), 80, "localhost/" + loopbackAddr + ":80"},118{InetAddress.getLocalHost(), 80, localAddr + ":80"},119{InetAddress.getByAddress(new byte[]{1, 1, 1, 1}), 80, "/1.1.1.1:80"},120{InetAddress.getByAddress(new byte[]{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}), 80, "/[101:101:101:101:101:101:101:101]:80"},121{InetAddress.getByName("225.225.225.0"), 80, "/225.225.225.0:80"},122{nullAddr, 80, wildcardAddr + ":80"}123};124} catch (UnknownHostException uhe) {125throw new RuntimeException("Data provider creation failed: " + uhe, uhe);126}127}128129@Test(dataProvider = "addrPortArgs")130public static void testConstructor(InetAddress addr, int port, String string) {131String received = new InetSocketAddress(addr, port).toString();132133if (!string.equals(received)) {134throw new RuntimeException("Expected: " + string + " Received: " + received);135}136}137138@DataProvider(name = "unresolved")139public Object[][] createArgs3() {140return new Object[][]{141// hostname, port number, expected string142{"::1", 80, "::1/<unresolved>:80"},143{"fedc:ba98:7654:3210:fedc:ba98:7654:3210", 80, "fedc:ba98:7654:3210:fedc:ba98:7654:3210/<unresolved>:80"},144{"::192.9.5.5", 80, "::192.9.5.5/<unresolved>:80"},145{"127.0.0.1", 80, "127.0.0.1/<unresolved>:80"},146{"::ffff:192.0.2.128", 80, "::ffff:192.0.2.128/<unresolved>:80"},147{"0", 80, "0/<unresolved>:80"},148{"foo", 80, "foo/<unresolved>:80"},149{":", 80, ":/<unresolved>:80"},150{":1", 80, ":1/<unresolved>:80"}151};152}153154@Test(dataProvider = "unresolved")155public static void testCreateUnresolved(String host, int port, String string) {156String received = InetSocketAddress.createUnresolved(host, port).toString();157158if (!string.equals(received)) {159throw new RuntimeException("Expected: " + string + " Received: " + received);160}161}162}163164165