Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socks/SocksSocketImplTest.java
41149 views
1
/*
2
* Copyright (c) 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
import org.testng.Assert;
25
import org.testng.annotations.AfterTest;
26
import org.testng.annotations.BeforeTest;
27
import org.testng.annotations.Test;
28
import sun.net.spi.DefaultProxySelector;
29
30
import java.io.IOException;
31
import java.net.InetAddress;
32
import java.net.Proxy;
33
import java.net.ProxySelector;
34
import java.net.ServerSocket;
35
import java.net.Socket;
36
import java.net.URI;
37
import java.net.URISyntaxException;
38
import java.util.List;
39
40
/**
41
* @test
42
* @bug 8230310
43
* @summary Tests java.net.SocksSocketImpl
44
* @run testng SocksSocketImplTest
45
* @modules java.base/sun.net.spi:+open
46
*/
47
public class SocksSocketImplTest {
48
49
private ProxySelector previousDefault;
50
51
@BeforeTest
52
public void beforeTest() {
53
previousDefault = ProxySelector.getDefault();
54
ProxySelector.setDefault(new SchemeStrippedProxySelector());
55
}
56
57
@AfterTest
58
public void afterTest() {
59
ProxySelector.setDefault(previousDefault);
60
}
61
62
/**
63
* Creates a socket connection, which internally triggers proxy selection for the target
64
* address. The test has been configured to use a {@link SchemeStrippedProxySelector ProxySelector}
65
* which throws a {@link IllegalArgumentException}. This test then verifies that this IAE gets wrapped
66
* by {@code java.net.SocksSocketImpl} into an {@link IOException} before being thrown
67
*
68
* @throws Exception
69
*/
70
@Test
71
public void testIOEOnProxySelection() throws Exception {
72
final int backlog = -1;
73
final int port = 0;
74
try (ServerSocket ss = new ServerSocket(port, backlog, InetAddress.getLoopbackAddress());
75
Socket s1 = new Socket(ss.getInetAddress(), ss.getLocalPort());
76
Socket s2 = ss.accept()) {
77
Assert.fail("IOException was expected to be thrown, but wasn't");
78
} catch (IOException ioe) {
79
// expected
80
// now verify the IOE was thrown for the correct expected reason
81
if (!(ioe.getCause() instanceof IllegalArgumentException)) {
82
// rethrow this so that the test output failure will capture the entire/real
83
// cause in its stacktrace
84
throw ioe;
85
}
86
}
87
}
88
89
/**
90
* A {@link ProxySelector} which strips the "scheme" part of the {@link URI}
91
* before delegating the selection to the the {@link DefaultProxySelector}.
92
* This is to ensure that the {@code DefaultProxySelector} throws an {@link IllegalArgumentException}
93
* during selection of the proxy
94
*/
95
private static final class SchemeStrippedProxySelector extends DefaultProxySelector {
96
97
@Override
98
public List<Proxy> select(final URI uri) {
99
System.out.println("Proxy selection for " + uri);
100
final URI schemeStrippedURI;
101
try {
102
// strip the scheme and pass the rest
103
schemeStrippedURI = new URI(null, uri.getHost(), uri.getPath(), null);
104
} catch (URISyntaxException e) {
105
throw new RuntimeException(e);
106
}
107
System.out.println("Scheme stripped URI " + schemeStrippedURI + " is being used to select a proxy");
108
return super.select(schemeStrippedURI);
109
}
110
}
111
}
112
113