Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/InetSocketAddress/ToString.java
41149 views
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
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
@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() {
83
System.out.println(new InetSocketAddress("unresolved", 12345));
84
}
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
}
162
}
163
}
164
165