1 /*
   2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 8225499 4464064
  27  * @library /test/lib
  28  * @summary InetSocketAddress::toString not friendly to IPv6 literal addresses
  29  * @run testng/othervm ToString
  30  * @run testng/othervm -Djava.net.preferIPv4Stack=true ToString
  31  * @run testng/othervm -Djava.net.preferIPv6Addresses=true ToString
  32  */
  33 
  34 import java.net.*;

  35 
  36 import jdk.test.lib.net.IPSupport;
  37 import org.testng.annotations.BeforeTest;
  38 import org.testng.annotations.DataProvider;
  39 import org.testng.annotations.Test;
  40 
  41 public class ToString {
  42 
  43     private static final String loopbackAddr;
  44 
  45     static {
  46         InetAddress loopback = InetAddress.getLoopbackAddress();
  47         String addr = loopback.getHostAddress();
  48         if (loopback instanceof Inet6Address) {
  49             addr = "[" + addr + "]";
  50         }
  51         loopbackAddr = addr;
  52     }
  53 
  54     @BeforeTest
  55     public void setup() {
  56         IPSupport.throwSkippedExceptionIfNonOperational();
  57     }
  58 
  59     @Test
  60     // InetSocketAddress.toString() throws NPE with unresolved address
  61     public static void NPETest (){
  62         System.out.println(new InetSocketAddress("unresolved", 12345));
  63     }
  64 
  65     @DataProvider(name = "hostPortArgs")
  66     public Object[][] createArgs1() {
  67         return new Object[][]{
  68                 // hostname, port number, expected string in format
  69                 // <hostname>/<IP literal>:<port> or
  70                 // <hostname>/<unresolved>:<port> if address is unresolved
  71                 {"::1", 80, "/[0:0:0:0:0:0:0:1]:80"},
  72                 {"fedc:ba98:7654:3210:fedc:ba98:7654:3210", 80, "/[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:80"},
  73                 {"::192.9.5.5", 80, "/[0:0:0:0:0:0:c009:505]:80"},
  74                 {"127.0.0.1", 80, "/127.0.0.1:80"},
  75                 {"::ffff:192.0.2.128", 80, "/192.0.2.128:80"},
  76                 {"0", 80, "/0.0.0.0:80"},
  77                 {":", 80, ":/<unresolved>:80"},
  78                 {":1", 80, ":1/<unresolved>:80"},
  79         };
  80     }
  81 
  82     @Test(dataProvider = "hostPortArgs")
  83     public static void testConstructor(String host, int port, String string) {
  84         String received = new InetSocketAddress(host, port).toString();
  85 
  86         if (!string.equals(received)) {
  87             throw new RuntimeException("Expected: " + string + " Received: " + received);
  88         }
  89     }
  90 
  91     @DataProvider(name = "addrPortArgs")
  92     public Object[][] createArgs2() {
  93         try {
  94             return new Object[][]{
  95                     // InetAddress, port number, expected string
  96                     {InetAddress.getLoopbackAddress(), 80, "localhost/" + loopbackAddr + ":80"},
  97                     {InetAddress.getLocalHost(), 80, InetAddress.getLocalHost().toString() + ":80"},
  98                     {InetAddress.getByAddress(new byte[]{1, 1, 1, 1}), 80, "/1.1.1.1:80"},
  99                     {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"},
 100                     {InetAddress.getByName("225.225.225.0"), 80, "/225.225.225.0:80"},
 101             };
 102         } catch (UnknownHostException uhe) {
 103             throw new RuntimeException("Data provider creation failed: " + uhe, uhe);
 104         }
 105     }
 106 
 107     @Test(dataProvider = "addrPortArgs")
 108     public static void testConstructor(InetAddress addr, int port, String string) {
 109         String received = new InetSocketAddress(addr, port).toString();
 110 
 111         if (!string.equals(received)) {
 112             throw new RuntimeException("Expected: " + string + " Received: " + received);
 113         }
 114     }
 115 
 116     @DataProvider(name = "unresolved")
 117     public Object[][] createArgs3() {
 118         return new Object[][]{
 119                 // hostname, port number, expected string
 120                 {"::1", 80, "::1/<unresolved>:80"},
 121                 {"fedc:ba98:7654:3210:fedc:ba98:7654:3210", 80, "fedc:ba98:7654:3210:fedc:ba98:7654:3210/<unresolved>:80"},
 122                 {"::192.9.5.5", 80, "::192.9.5.5/<unresolved>:80"},
 123                 {"127.0.0.1", 80, "127.0.0.1/<unresolved>:80"},
 124                 {"::ffff:192.0.2.128", 80, "::ffff:192.0.2.128/<unresolved>:80"},
 125                 {"0", 80, "0/<unresolved>:80"},
 126                 {"foo", 80, "foo/<unresolved>:80"},
 127                 {":", 80, ":/<unresolved>:80"},
 128                 {":1", 80, ":1/<unresolved>:80"},
 129         };
 130     }
 131 
 132     @Test(dataProvider = "unresolved")
 133     public static void testCreateUnresolved(String host, int port, String string) {
 134         String received = InetSocketAddress.createUnresolved(host, port).toString();
 135 
 136         if (!string.equals(received)) {
 137             throw new RuntimeException("Expected: " + string + " Received: " + received);
 138         }
 139     }
 140 }
--- EOF ---