21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 /* |
24 /* |
25 * @test |
25 * @test |
26 * @bug 4464064 |
26 * @bug 8225499 4464064 |
27 * @summary InetSocketAddress.toString() throws NPE with unresolved address |
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 |
28 */ |
32 */ |
29 |
33 |
30 import java.net.*; |
34 import java.net.*; |
31 import java.io.*; |
|
32 |
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; |
33 |
40 |
34 public class ToString { |
41 public class ToString { |
35 |
42 |
36 public static void main (String args[]){ |
43 private static final String loopbackAddr; |
|
44 private static final String wildcardAddr; |
|
45 private static final String localAddr; |
37 |
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 @BeforeTest |
|
76 public void setup() { |
|
77 IPSupport.throwSkippedExceptionIfNonOperational(); |
|
78 } |
|
79 |
|
80 @Test |
|
81 // InetSocketAddress.toString() throws NPE with unresolved address |
|
82 public static void NPETest() { |
38 System.out.println(new InetSocketAddress("unresolved", 12345)); |
83 System.out.println(new InetSocketAddress("unresolved", 12345)); |
|
84 } |
39 |
85 |
|
86 @DataProvider(name = "hostPortArgs") |
|
87 public Object[][] createArgs1() { |
|
88 return new Object[][]{ |
|
89 // hostname, port number, expected string in format |
|
90 // <hostname>/<IP literal>:<port> or |
|
91 // <hostname>/<unresolved>:<port> if address is unresolved |
|
92 {"::1", 80, "/[0:0:0:0:0:0:0:1]:80"}, |
|
93 {"fedc:ba98:7654:3210:fedc:ba98:7654:3210", 80, "/[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:80"}, |
|
94 {"::192.9.5.5", 80, "/[0:0:0:0:0:0:c009:505]:80"}, |
|
95 {"127.0.0.1", 80, "/127.0.0.1:80"}, |
|
96 {"::ffff:192.0.2.128", 80, "/192.0.2.128:80"}, |
|
97 {"0", 80, "/0.0.0.0:80"}, |
|
98 {":", 80, ":/<unresolved>:80"}, |
|
99 {":1", 80, ":1/<unresolved>:80"} |
|
100 }; |
|
101 } |
|
102 |
|
103 @Test(dataProvider = "hostPortArgs") |
|
104 public static void testConstructor(String host, int port, String string) { |
|
105 String received = new InetSocketAddress(host, port).toString(); |
|
106 |
|
107 if (!string.equals(received)) { |
|
108 throw new RuntimeException("Expected: " + string + " Received: " + received); |
|
109 } |
|
110 } |
|
111 |
|
112 @DataProvider(name = "addrPortArgs") |
|
113 public Object[][] createArgs2() { |
|
114 InetAddress nullAddr = null; |
|
115 try { |
|
116 return new Object[][]{ |
|
117 // InetAddress, port number, expected string |
|
118 {InetAddress.getLoopbackAddress(), 80, "localhost/" + loopbackAddr + ":80"}, |
|
119 {InetAddress.getLocalHost(), 80, localAddr + ":80"}, |
|
120 {InetAddress.getByAddress(new byte[]{1, 1, 1, 1}), 80, "/1.1.1.1:80"}, |
|
121 {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"}, |
|
122 {InetAddress.getByName("225.225.225.0"), 80, "/225.225.225.0:80"}, |
|
123 {nullAddr, 80, wildcardAddr + ":80"} |
|
124 }; |
|
125 } catch (UnknownHostException uhe) { |
|
126 throw new RuntimeException("Data provider creation failed: " + uhe, uhe); |
|
127 } |
|
128 } |
|
129 |
|
130 @Test(dataProvider = "addrPortArgs") |
|
131 public static void testConstructor(InetAddress addr, int port, String string) { |
|
132 String received = new InetSocketAddress(addr, port).toString(); |
|
133 |
|
134 if (!string.equals(received)) { |
|
135 throw new RuntimeException("Expected: " + string + " Received: " + received); |
|
136 } |
|
137 } |
|
138 |
|
139 @DataProvider(name = "unresolved") |
|
140 public Object[][] createArgs3() { |
|
141 return new Object[][]{ |
|
142 // hostname, port number, expected string |
|
143 {"::1", 80, "::1/<unresolved>:80"}, |
|
144 {"fedc:ba98:7654:3210:fedc:ba98:7654:3210", 80, "fedc:ba98:7654:3210:fedc:ba98:7654:3210/<unresolved>:80"}, |
|
145 {"::192.9.5.5", 80, "::192.9.5.5/<unresolved>:80"}, |
|
146 {"127.0.0.1", 80, "127.0.0.1/<unresolved>:80"}, |
|
147 {"::ffff:192.0.2.128", 80, "::ffff:192.0.2.128/<unresolved>:80"}, |
|
148 {"0", 80, "0/<unresolved>:80"}, |
|
149 {"foo", 80, "foo/<unresolved>:80"}, |
|
150 {":", 80, ":/<unresolved>:80"}, |
|
151 {":1", 80, ":1/<unresolved>:80"} |
|
152 }; |
|
153 } |
|
154 |
|
155 @Test(dataProvider = "unresolved") |
|
156 public static void testCreateUnresolved(String host, int port, String string) { |
|
157 String received = InetSocketAddress.createUnresolved(host, port).toString(); |
|
158 |
|
159 if (!string.equals(received)) { |
|
160 throw new RuntimeException("Expected: " + string + " Received: " + received); |
|
161 } |
40 } |
162 } |
41 } |
163 } |