• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package test.java.net.InetSocketAddress;
24 
25 /*
26  * @test
27  * @bug 8225499 4464064
28  * @library /test/lib
29  * @summary InetSocketAddress::toString not friendly to IPv6 literal addresses
30  * @run testng/othervm ToString
31  * @run testng/othervm -Djava.net.preferIPv4Stack=true ToString
32  * @run testng/othervm -Djava.net.preferIPv6Addresses=true ToString
33  */
34 
35 import java.net.*;
36 
37 import org.testng.annotations.DataProvider;
38 import org.testng.annotations.Test;
39 import static org.testng.Assert.fail;
40 
41 public class ToString {
42 
43     private static final String loopbackAddr;
44     private static final String wildcardAddr;
45     private static final String localAddr;
46 
47     static {
48         try {
49             InetAddress loopback = InetAddress.getLoopbackAddress();
50             String addr = loopback.getHostAddress();
51             if (loopback instanceof Inet6Address) {
52                 addr = "[" + addr + "]";
53             }
54             loopbackAddr = addr;
55 
56             InetSocketAddress isa = new InetSocketAddress((InetAddress) null, 80);
57             addr = isa.getAddress().toString();
58             if (isa.getAddress() instanceof Inet6Address) {
59                 addr = "::/[0:0:0:0:0:0:0:0]";
60             }
61             wildcardAddr = addr;
62 
63             InetAddress ia = InetAddress.getLocalHost();
64             addr = ia.toString();
65             if (ia instanceof Inet6Address) {
66                 addr = ia.getHostName() + "/[" + ia.getHostAddress() + "]";
67             }
68             localAddr = addr;
69 
70         } catch (UnknownHostException uhe) {
71             throw new RuntimeException(uhe);
72         }
73     }
74 
75     @Test
76     // InetSocketAddress.toString() throws NPE with unresolved address
NPETest()77     public static void NPETest() {
78         new InetSocketAddress("unresolved", 12345);
79     }
80 
81     @DataProvider(name = "unresolved")
createArgs3()82     public Object[][] createArgs3() {
83         return new Object[][]{
84                 // hostname, port number, expected string
85                 {"::1", 80, "::1/<unresolved>:80"},
86                 {"fedc:ba98:7654:3210:fedc:ba98:7654:3210", 80, "fedc:ba98:7654:3210:fedc:ba98:7654:3210/<unresolved>:80"},
87                 {"::192.9.5.5", 80, "::192.9.5.5/<unresolved>:80"},
88                 {"127.0.0.1", 80, "127.0.0.1/<unresolved>:80"},
89                 {"::ffff:192.0.2.128", 80, "::ffff:192.0.2.128/<unresolved>:80"},
90                 {"0", 80, "0/<unresolved>:80"},
91                 {"foo", 80, "foo/<unresolved>:80"},
92                 {":", 80, ":/<unresolved>:80"},
93                 {":1", 80, ":1/<unresolved>:80"}
94         };
95     }
96 
97     @Test(dataProvider = "unresolved")
testCreateUnresolved(String host, int port, String string)98     public static void testCreateUnresolved(String host, int port, String string) {
99         String received = InetSocketAddress.createUnresolved(host, port).toString();
100 
101         if (!string.equals(received)) {
102             fail("Expected: " + string + " Received: " + received);
103         }
104     }
105 }