Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socks/SocksV4Test.java
41149 views
1
/*
2
* Copyright (c) 2002, 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 4727547
27
* @summary SocksSocketImpl throws NullPointerException
28
* @build SocksServer
29
* @run main/othervm SocksV4Test
30
*/
31
32
import java.io.IOException;
33
import java.net.Authenticator;
34
import java.net.InetAddress;
35
import java.net.InetSocketAddress;
36
import java.net.PasswordAuthentication;
37
import java.net.Proxy;
38
import java.net.ServerSocket;
39
import java.net.Socket;
40
import java.net.UnknownHostException;
41
42
public class SocksV4Test {
43
44
// An unresolvable host
45
static final String HOSTNAME = "doesnot.exist.invalid";
46
static final String USER = "johndoe";
47
static final String PASSWORD = "helloworld";
48
49
public static void main(String[] args) throws Exception {
50
Authenticator.setDefault(new Auth());
51
UHETest();
52
getLocalPortTest();
53
}
54
55
static class Auth extends Authenticator {
56
protected PasswordAuthentication getPasswordAuthentication() {
57
return new PasswordAuthentication(USER, PASSWORD.toCharArray());
58
}
59
}
60
61
public static void getLocalPortTest() throws Exception {
62
// We actually use V5 for this test because that is the default
63
// protocol version used by the client and it doesn't really handle
64
// down grading very well.
65
InetAddress lba = InetAddress.getLoopbackAddress();
66
try (SocksServer srvr = new SocksServer(lba, 0, false);
67
ServerSocket ss = new ServerSocket(0, 0, lba)) {
68
69
srvr.addUser(USER, PASSWORD);
70
int serverPort = ss.getLocalPort();
71
srvr.start();
72
int proxyPort = srvr.getPort();
73
System.out.printf("Server port %d, Proxy port %d\n", serverPort, proxyPort);
74
Proxy sp = new Proxy(Proxy.Type.SOCKS,
75
new InetSocketAddress(lba, proxyPort));
76
// Let's create an unresolved address
77
InetSocketAddress ad = new InetSocketAddress(lba.getHostAddress(), serverPort);
78
try (Socket s = new Socket(sp)) {
79
s.connect(ad, 10000);
80
int pp = s.getLocalPort();
81
System.out.println("Local port = " + pp);
82
if (pp == serverPort || pp == proxyPort)
83
throw new RuntimeException("wrong port returned");
84
} catch (UnknownHostException ex) {
85
throw new RuntimeException(ex);
86
} catch (IOException ioe) {
87
throw new RuntimeException(ioe);
88
}
89
}
90
}
91
92
public static void UHETest() throws Exception {
93
// sanity before running the test
94
assertUnresolvableHost(HOSTNAME);
95
96
InetAddress lba = InetAddress.getLoopbackAddress();
97
// Create a SOCKS V4 proxy
98
try (SocksServer srvr = new SocksServer(lba, 0, true)) {
99
srvr.start();
100
Proxy sp = new Proxy(Proxy.Type.SOCKS,
101
new InetSocketAddress(lba, srvr.getPort()));
102
// Let's create an unresolved address
103
InetSocketAddress ad = new InetSocketAddress(HOSTNAME, 1234);
104
try (Socket s = new Socket(sp)) {
105
s.connect(ad, 10000);
106
} catch (UnknownHostException ex) {
107
// OK, that's what we expected
108
} catch (NullPointerException npe) {
109
// Not OK, this used to be the bug
110
throw new RuntimeException("Got a NUllPointerException");
111
}
112
}
113
}
114
115
static void assertUnresolvableHost(String host) {
116
InetAddress addr = null;
117
try {
118
addr = InetAddress.getByName(host);
119
} catch (UnknownHostException x) {
120
// OK, expected
121
}
122
if (addr != null)
123
throw new RuntimeException("Test cannot run. resolvable address:" + addr);
124
}
125
}
126
127