undefect. CWE-407 — 63 sites patched across 27 ecosystems
Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com Patches, unit tests, benchmarks, whitepaper, and outreach briefs. Public domain — no copyright claimed. Use freely.
This commit is contained in:
commit
0a580b313d
70422 changed files with 17213626 additions and 0 deletions
44
test/jdk/java/net/ProxySelector/B6563259.java
Normal file
44
test/jdk/java/net/ProxySelector/B6563259.java
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
/*
|
||||
* @test
|
||||
* @bug 6563259
|
||||
* @summary incorrect handling when including uppercase letter in hostname
|
||||
*/
|
||||
|
||||
import java.net.ProxySelector;
|
||||
import java.net.Proxy;
|
||||
import java.net.URI;
|
||||
|
||||
public class B6563259 {
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.setProperty("http.proxyHost", "myproxy");
|
||||
System.setProperty("http.proxyPort", "8080");
|
||||
System.setProperty("http.nonProxyHosts", "host1.*");
|
||||
ProxySelector sel = ProxySelector.getDefault();
|
||||
java.util.List<Proxy> l = sel.select(new URI("http://HOST1.sun.com/"));
|
||||
if (l.get(0) != Proxy.NO_PROXY) {
|
||||
throw new RuntimeException("ProxySelector returned the wrong proxy");
|
||||
}
|
||||
}
|
||||
}
|
||||
69
test/jdk/java/net/ProxySelector/B6737819.java
Normal file
69
test/jdk/java/net/ProxySelector/B6737819.java
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
/*
|
||||
* @test
|
||||
* @bug 6737819
|
||||
* @run main/othervm B6737819
|
||||
* @summary sun.misc.net.DefaultProxySelector doesn't use proxy setting to localhost
|
||||
*/
|
||||
|
||||
/* Run in othervm mode since the test sets HTTP proxy system properties that
|
||||
* are read once and cached by the protocol handler. A previous test using the
|
||||
* HTTP handler may run and these system properties may be ignored for this test.
|
||||
*/
|
||||
|
||||
import java.net.ProxySelector;
|
||||
import java.net.Proxy;
|
||||
import java.net.URI;
|
||||
|
||||
public class B6737819 {
|
||||
private static String[] uris = {
|
||||
"http://localhost/index.html",
|
||||
"http://127.0.0.1/index.html",
|
||||
"http://127.2/index.html",
|
||||
"http://[::1]/index.html"
|
||||
};
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.setProperty("http.proxyHost", "myproxy");
|
||||
System.setProperty("http.proxyPort", "8080");
|
||||
ProxySelector sel = ProxySelector.getDefault();
|
||||
java.util.List<Proxy> l;
|
||||
// Default value for http.nonProxyHots should exclude all this uris
|
||||
// from going through the HTTP proxy
|
||||
for (String s : uris) {
|
||||
l = sel.select(new URI(s));
|
||||
if (l.size() == 1 && l.get(0).type() != Proxy.Type.DIRECT) {
|
||||
throw new RuntimeException("ProxySelector returned the wrong proxy for " + s);
|
||||
}
|
||||
}
|
||||
// Let's override the default nonProxyHosts and make sure we now get a
|
||||
// HTTP proxy
|
||||
System.setProperty("http.nonProxyHosts", "");
|
||||
for (String s : uris) {
|
||||
l = sel.select(new URI(s));
|
||||
if (l.size() == 1 && l.get(0).type() != Proxy.Type.HTTP) {
|
||||
throw new RuntimeException("ProxySelector returned the wrong proxy for " + s);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
269
test/jdk/java/net/ProxySelector/B8035158.java
Normal file
269
test/jdk/java/net/ProxySelector/B8035158.java
Normal file
|
|
@ -0,0 +1,269 @@
|
|||
/*
|
||||
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8035158 8145732 8144300 8241138
|
||||
* @run main/othervm B8035158
|
||||
*/
|
||||
|
||||
import java.net.Proxy;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.URI;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
public class B8035158 {
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (TestCase t : emptyNonProxiesHosts()) t.run();
|
||||
for (TestCase t : nonEmptyNonProxiesHosts()) t.run();
|
||||
for (TestCase t : misc()) t.run();
|
||||
}
|
||||
|
||||
// Setting http.nonProxyHosts to an empty string has an effect of
|
||||
// not including default hosts to the list of exceptions
|
||||
// (i.e. if you want everything to be connected directly rather than
|
||||
// through proxy, you should set this property to an empty string)
|
||||
private static Collection<TestCase> emptyNonProxiesHosts() {
|
||||
List<TestCase> tests = new LinkedList<>();
|
||||
String[] loopbacks = {"localhost", "[::1]", "[::0]", "0.0.0.0",
|
||||
"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",
|
||||
"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};
|
||||
Map<String, String> properties = new HashMap<>();
|
||||
properties.put("http.proxyHost", "http://proxy.example.com");
|
||||
properties.put("http.nonProxyHosts", "");
|
||||
for (String s : loopbacks) {
|
||||
tests.add(new TestCase(properties, "http://" + s, true));
|
||||
}
|
||||
return tests;
|
||||
}
|
||||
|
||||
// No matter what is set into the http.nonProxyHosts (as far as it is not
|
||||
// an empty string) loopback address aliases must be always connected
|
||||
// directly
|
||||
private static Collection<TestCase> nonEmptyNonProxiesHosts() {
|
||||
List<TestCase> tests = new LinkedList<>();
|
||||
String[] nonProxyHosts = {
|
||||
"google.com",
|
||||
"localhost", "[::1]", "[::0]", "0.0.0.0",
|
||||
"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",
|
||||
"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};
|
||||
String[] loopbacks = {"localhost", "[::1]", "[::0]", "0.0.0.0",
|
||||
"127.0.0.0", "127.0.0.1", "127.0.1.0", "127.0.1.1",
|
||||
"127.1.0.0", "127.1.0.1", "127.1.1.0", "127.1.1.1"};
|
||||
for (String h : nonProxyHosts) {
|
||||
for (String s : loopbacks) {
|
||||
Map<String, String> properties = new HashMap<>();
|
||||
properties.put("http.proxyHost", "http://proxy.example.com");
|
||||
properties.put("http.nonProxyHosts", h);
|
||||
tests.add(new TestCase(properties, "http://" + s, false));
|
||||
}
|
||||
}
|
||||
return tests;
|
||||
}
|
||||
|
||||
// unsorted tests
|
||||
private static Collection<TestCase> misc() {
|
||||
List<TestCase> t = new LinkedList<>();
|
||||
t.add(new TestCase("oracle.com", "http://137.254.16.101", true));
|
||||
t.add(new TestCase("google.com", "http://74.125.200.101", true));
|
||||
|
||||
t.add(new TestCase("google.com|google.ie", "http://google.co.uk",
|
||||
true));
|
||||
t.add(new TestCase("google.com|google.ie", "http://google.com",
|
||||
false));
|
||||
t.add(new TestCase("google.com|google.ie", "http://google.ie",
|
||||
false));
|
||||
t.add(new TestCase("google.com|google.com|google.ie",
|
||||
"http://google.ie", false));
|
||||
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com",
|
||||
"http://127.0.0.1", false));
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com",
|
||||
"http://google.com", false));
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com",
|
||||
"http://bing.com", false));
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com",
|
||||
"http://yahoo.com", false));
|
||||
|
||||
t.add(new TestCase("google.com|bing.com", "http://google.com", false));
|
||||
t.add(new TestCase("google.com|bing.com", "http://bing.com", false));
|
||||
t.add(new TestCase("google.com|bing.com", "http://yahoo.com",
|
||||
true));
|
||||
t.add(new TestCase("google.com|bing.co*", "http://google.com", false));
|
||||
t.add(new TestCase("google.com|bing.co*", "http://bing.com", false));
|
||||
t.add(new TestCase("google.com|bing.co*", "http://yahoo.com",
|
||||
true));
|
||||
t.add(new TestCase("google.com|*ing.com", "http://google.com", false));
|
||||
t.add(new TestCase("google.com|*ing.com", "http://bing.com", false));
|
||||
t.add(new TestCase("google.com|*ing.com", "http://yahoo.com",
|
||||
true));
|
||||
t.add(new TestCase("google.co*|bing.com", "http://google.com", false));
|
||||
t.add(new TestCase("google.co*|bing.com", "http://bing.com", false));
|
||||
t.add(new TestCase("google.co*|bing.com", "http://yahoo.com",
|
||||
true));
|
||||
t.add(new TestCase("google.co*|bing.co*", "http://google.com", false));
|
||||
t.add(new TestCase("google.co*|bing.co*", "http://bing.com", false));
|
||||
t.add(new TestCase("google.co*|bing.co*", "http://yahoo.com",
|
||||
true));
|
||||
t.add(new TestCase("google.co*|*ing.com", "http://google.com", false));
|
||||
t.add(new TestCase("google.co*|*ing.com", "http://bing.com", false));
|
||||
t.add(new TestCase("google.co*|*ing.com", "http://yahoo.com",
|
||||
true));
|
||||
t.add(new TestCase("*oogle.com|bing.com", "http://google.com", false));
|
||||
t.add(new TestCase("*oogle.com|bing.com", "http://bing.com", false));
|
||||
t.add(new TestCase("*oogle.com|bing.com", "http://yahoo.com",
|
||||
true));
|
||||
t.add(new TestCase("*oogle.com|bing.co*", "http://google.com", false));
|
||||
t.add(new TestCase("*oogle.com|bing.co*", "http://bing.com", false));
|
||||
t.add(new TestCase("*oogle.com|bing.co*", "http://yahoo.com",
|
||||
true));
|
||||
t.add(new TestCase("*oogle.com|*ing.com", "http://google.com", false));
|
||||
t.add(new TestCase("*oogle.com|*ing.com", "http://bing.com", false));
|
||||
t.add(new TestCase("*oogle.com|*ing.com", "http://yahoo.com",
|
||||
true));
|
||||
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://google.com", false));
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://bing.com", false));
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://yahoo.com", false));
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com",
|
||||
"http://duckduckgo.com", true));
|
||||
|
||||
t.add(new TestCase("p-proxy.com", "http://p-proxy.com", false));
|
||||
t.add(new TestCase("google.co*|google.ie", "http://google.co.uk",
|
||||
false));
|
||||
t.add(new TestCase("*google.*", "http://google.co.uk",
|
||||
false));
|
||||
t.add(new TestCase("*", "http://google.co.uk",false));
|
||||
t.add(new TestCase("localhost|*", "http://google.co.uk",false));
|
||||
t.add(new TestCase("*|oracle.com", "http://google.co.uk",false));
|
||||
t.add(new TestCase("*|oracle.com|*", "http://google.co.uk",false));
|
||||
t.add(new TestCase("*|*", "http://google.co.uk",false));
|
||||
|
||||
|
||||
t.add(new TestCase("*oracle.com", "http://my.oracle.com", false));
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://127.0.0.1", false));
|
||||
t.add(new TestCase("google.com|bing.com|yahoo.com", "http://yahoo.com", false));
|
||||
|
||||
t.add(new TestCase("localhost|host.example.com", "http://localhost",
|
||||
false));
|
||||
t.add(new TestCase("localhost|host.example.com",
|
||||
"http://host.example.com", false));
|
||||
t.add(new TestCase("localhost|host.example.com",
|
||||
"http://google.com", true));
|
||||
return t;
|
||||
}
|
||||
|
||||
|
||||
private static <T> T withSystemPropertiesSet(
|
||||
Map<String, String> localProperties,
|
||||
Callable<? extends T> code) {
|
||||
Map<String, String> backup = new HashMap<>();
|
||||
try {
|
||||
backupAndSetProperties(localProperties, backup);
|
||||
return code.call();
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
restoreProperties(backup);
|
||||
}
|
||||
}
|
||||
|
||||
private static void backupAndSetProperties(
|
||||
Map<String, String> localProperties,
|
||||
Map<String, String> oldProperties) {
|
||||
for (Map.Entry<String, String> e : localProperties.entrySet()) {
|
||||
String oldValue = System.setProperty(e.getKey(), e.getValue());
|
||||
oldProperties.put(e.getKey(), oldValue);
|
||||
}
|
||||
}
|
||||
|
||||
private static void restoreProperties(Map<String, String> oldProperties) {
|
||||
for (Map.Entry<String, String> e : oldProperties.entrySet()) {
|
||||
String oldValue = e.getValue();
|
||||
String key = e.getKey();
|
||||
if (oldValue == null)
|
||||
System.getProperties().remove(key);
|
||||
else
|
||||
System.setProperty(key, oldValue);
|
||||
}
|
||||
}
|
||||
|
||||
private static class TestCase {
|
||||
|
||||
final Map<String, String> localProperties;
|
||||
final String urlhost;
|
||||
final boolean expectedProxying;
|
||||
|
||||
TestCase(String nonProxyHosts, String urlhost,
|
||||
boolean expectedProxying) {
|
||||
this(nonProxyHosts, "proxy.example.com", urlhost,
|
||||
expectedProxying);
|
||||
}
|
||||
|
||||
TestCase(String nonProxyHosts, String proxyHost, String urlhost,
|
||||
boolean expectedProxying) {
|
||||
this(new HashMap<String, String>() {
|
||||
{
|
||||
put("http.nonProxyHosts", nonProxyHosts);
|
||||
put("http.proxyHost", proxyHost);
|
||||
}
|
||||
}, urlhost, expectedProxying);
|
||||
}
|
||||
|
||||
TestCase(Map<String, String> localProperties, String urlhost,
|
||||
boolean expectedProxying) {
|
||||
this.localProperties = localProperties;
|
||||
this.urlhost = urlhost;
|
||||
this.expectedProxying = expectedProxying;
|
||||
}
|
||||
|
||||
void run() {
|
||||
System.out.printf("urlhost=%s properties=%s: proxied? %s%n",
|
||||
urlhost, localProperties, expectedProxying);
|
||||
|
||||
List<Proxy> proxies = withSystemPropertiesSet(localProperties,
|
||||
() -> ProxySelector.getDefault().select(
|
||||
URI.create(urlhost))
|
||||
);
|
||||
|
||||
verify(proxies);
|
||||
}
|
||||
|
||||
void verify(List<? extends Proxy> proxies) {
|
||||
|
||||
boolean actualProxying = !(proxies.size() == 1 &&
|
||||
proxies.get(0).type() == Proxy.Type.DIRECT);
|
||||
|
||||
if (actualProxying != expectedProxying)
|
||||
throw new AssertionError(String.format(
|
||||
"Expected %s connection for %s (given " +
|
||||
"properties=%s). Here's the list of proxies " +
|
||||
"returned: %s",
|
||||
expectedProxying ? "proxied" : "direct", urlhost,
|
||||
localProperties, proxies
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
171
test/jdk/java/net/ProxySelector/LoopbackAddresses.java
Normal file
171
test/jdk/java/net/ProxySelector/LoopbackAddresses.java
Normal file
|
|
@ -0,0 +1,171 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4924226
|
||||
* @key intermittent
|
||||
* @summary PIT: Can no launch jnlp application via 127.0.0.1 address on the web server.
|
||||
* This test might fail intermittently as it needs a server that
|
||||
* binds to the wildcard address.
|
||||
* @library /test/lib
|
||||
* @compile LoopbackAddresses.java
|
||||
* @run main/othervm LoopbackAddresses
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
/**
|
||||
* Our default proxy selector should bypass localhost and loopback
|
||||
* addresses when selecting proxies. This is the existing behaviour.
|
||||
*/
|
||||
|
||||
public class LoopbackAddresses {
|
||||
static HttpServer server;
|
||||
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
|
||||
// This server needs to bind to the wildcard address as we want it
|
||||
// to answer both for the loopback and "localhost".
|
||||
// Though "localhost" usually point to the loopback there is no
|
||||
// hard guarantee.
|
||||
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10, "/", new LoopbackAddressesHandler());
|
||||
server.setExecutor(Executors.newSingleThreadExecutor());
|
||||
server.start();
|
||||
ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getAddress().getPort());
|
||||
// start proxy server
|
||||
new Thread(pserver).start();
|
||||
|
||||
System.setProperty("http.proxyHost", loopback.getHostAddress());
|
||||
System.setProperty("http.proxyPort", pserver.getPort()+"");
|
||||
|
||||
URL url = new URL("http://localhost:"+server.getAddress().getPort());
|
||||
|
||||
try {
|
||||
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
|
||||
int respCode = urlc.getResponseCode();
|
||||
urlc.disconnect();
|
||||
} catch (IOException ioex) {
|
||||
throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());
|
||||
}
|
||||
|
||||
try {
|
||||
url = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.host(loopback.getHostAddress())
|
||||
.port(server.getAddress().getPort())
|
||||
.toURL();
|
||||
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
|
||||
int respCode = urlc.getResponseCode();
|
||||
urlc.disconnect();
|
||||
} catch (IOException ioex) {
|
||||
throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (server != null) {
|
||||
server.stop(1);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private static class ProxyServer extends Thread {
|
||||
private static ServerSocket ss = null;
|
||||
|
||||
// client requesting for a tunnel
|
||||
private Socket clientSocket = null;
|
||||
|
||||
/*
|
||||
* Origin server's address and port that the client
|
||||
* wants to establish the tunnel for communication. */
|
||||
private InetAddress serverInetAddr;
|
||||
private int serverPort;
|
||||
|
||||
public ProxyServer(InetAddress server, int port) throws IOException {
|
||||
serverInetAddr = server;
|
||||
serverPort = port;
|
||||
ss = new ServerSocket();
|
||||
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
clientSocket = ss.accept();
|
||||
throw new RuntimeException("loopback addresses shouldn't go through the proxy "+clientSocket);
|
||||
|
||||
} catch (IOException e) {
|
||||
System.out.println("Proxy Failed: " + e);
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
ss.close();
|
||||
}
|
||||
catch (IOException excep) {
|
||||
System.out.println("ProxyServer close error: " + excep);
|
||||
excep.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
***************************************************************
|
||||
* helper methods follow
|
||||
***************************************************************
|
||||
*/
|
||||
public int getPort() {
|
||||
return ss.getLocalPort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class LoopbackAddressesHandler implements HttpHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
try {
|
||||
exchange.sendResponseHeaders(200, 0);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try(PrintWriter pw = new PrintWriter(exchange.getResponseBody(), false, Charset.forName("UTF-8"))) {
|
||||
pw.print("Hello .");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
/*
|
||||
* @test
|
||||
* @bug 7188755
|
||||
* @run main/othervm MultiThreadedSystemProxies
|
||||
* @summary Crash due to missing synchronization on gconf_client in
|
||||
* DefaultProxySelector.c
|
||||
*/
|
||||
import java.net.ProxySelector;
|
||||
import java.net.URI;
|
||||
|
||||
/* Racey test, not guaranteed to fail, but if it does we have a problem. */
|
||||
|
||||
public class MultiThreadedSystemProxies {
|
||||
static final int NUM_THREADS = 100;
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.setProperty("java.net.useSystemProxies", "true");
|
||||
final ProxySelector ps = ProxySelector.getDefault();
|
||||
final URI uri = new URI("http://ubuntu.com");
|
||||
Thread[] threads = new Thread[NUM_THREADS];
|
||||
|
||||
for (int i = 0; i < NUM_THREADS; i++) {
|
||||
threads[i] = new Thread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
ps.select(uri);
|
||||
} catch (Exception x) {
|
||||
throw new RuntimeException(x);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
for (int i = 0; i < NUM_THREADS; i++) {
|
||||
threads[i].start();
|
||||
}
|
||||
for (int i = 0; i < NUM_THREADS; i++) {
|
||||
threads[i].join();
|
||||
}
|
||||
}
|
||||
}
|
||||
67
test/jdk/java/net/ProxySelector/NullArguments.java
Normal file
67
test/jdk/java/net/ProxySelector/NullArguments.java
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2023, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 4937962 8318150
|
||||
* @summary ProxySelector.connectFailed and .select never throw IllegalArgumentException
|
||||
* @run junit NullArguments
|
||||
*/
|
||||
import java.net.*;
|
||||
import java.io.IOException;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class NullArguments {
|
||||
|
||||
public static Stream<ProxySelector> testProxies() {
|
||||
return Stream.of(
|
||||
ProxySelector.getDefault(),
|
||||
ProxySelector.of(new InetSocketAddress(1234)));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("testProxies")
|
||||
void testNullArguments(ProxySelector ps) throws URISyntaxException {
|
||||
Assumptions.assumeTrue(ps != null, "Skipping null selector");
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> ps.select(null),
|
||||
"Expected IllegalArgumentException!");
|
||||
URI uri = new URI("http://java.sun.com");
|
||||
SocketAddress sa = new InetSocketAddress("localhost", 80);
|
||||
IOException ioe = new IOException("dummy IOE");
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> ps.connectFailed(uri, sa, null),
|
||||
"Expected IllegalArgumentException!");
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> ps.connectFailed(uri, null, ioe),
|
||||
"Expected IllegalArgumentException!");
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> ps.connectFailed(null, sa, ioe),
|
||||
"Expected IllegalArgumentException!");
|
||||
}
|
||||
}
|
||||
53
test/jdk/java/net/ProxySelector/NullSelector.java
Normal file
53
test/jdk/java/net/ProxySelector/NullSelector.java
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
/*
|
||||
* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/* @test
|
||||
* @bug 6215885
|
||||
* @library /test/lib
|
||||
* @summary URLConnection.openConnection NPE if ProxySelector.setDefault is set to null
|
||||
* @run main/othervm NullSelector
|
||||
*/
|
||||
|
||||
import java.net.*;
|
||||
import java.io.*;
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
public class NullSelector {
|
||||
public static void main(String[] args) throws Exception {
|
||||
URL url = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.loopback()
|
||||
.path("/")
|
||||
.toURLUnchecked();
|
||||
System.out.println("URL: " + url);
|
||||
ProxySelector.setDefault(null);
|
||||
URLConnection con = url.openConnection();
|
||||
con.setConnectTimeout(500);
|
||||
try {
|
||||
// Will throw a NullPointerException if bug still there
|
||||
con.connect();
|
||||
} catch (IOException e) {
|
||||
// OK, don't care about timeouts, or refused connections.
|
||||
}
|
||||
}
|
||||
}
|
||||
121
test/jdk/java/net/ProxySelector/ProxyTest.java
Normal file
121
test/jdk/java/net/ProxySelector/ProxyTest.java
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
/*
|
||||
* Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4696512
|
||||
* @summary HTTP client: Improve proxy server configuration and selection
|
||||
* @library /test/lib
|
||||
* @compile ProxyTest.java
|
||||
* @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 ProxyTest
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintWriter;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.SocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.Executors;
|
||||
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpHandler;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import jdk.test.lib.net.URIBuilder;
|
||||
|
||||
public class ProxyTest {
|
||||
static HttpServer server;
|
||||
|
||||
public ProxyTest() {
|
||||
}
|
||||
|
||||
static public class MyProxySelector extends ProxySelector {
|
||||
private static volatile URI lastURI;
|
||||
private final static List<Proxy> NO_PROXY = List.of(Proxy.NO_PROXY);
|
||||
|
||||
public java.util.List<Proxy> select(URI uri) {
|
||||
System.out.println("Selecting no proxy for " + uri);
|
||||
lastURI = uri;
|
||||
return NO_PROXY;
|
||||
}
|
||||
|
||||
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
|
||||
}
|
||||
|
||||
public static URI lastURI() { return lastURI; }
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ProxySelector defSelector = ProxySelector.getDefault();
|
||||
if (defSelector == null)
|
||||
throw new RuntimeException("Default ProxySelector is null");
|
||||
ProxySelector.setDefault(new MyProxySelector());
|
||||
try {
|
||||
InetAddress loopback = InetAddress.getLoopbackAddress();
|
||||
server = HttpServer.create(new InetSocketAddress(loopback, 0), 10, "/", new ProxyTestHandler());
|
||||
server.setExecutor(Executors.newSingleThreadExecutor());
|
||||
server.start();
|
||||
URL url = URIBuilder.newBuilder()
|
||||
.scheme("http")
|
||||
.loopback()
|
||||
.port(server.getAddress().getPort())
|
||||
.toURL();
|
||||
System.out.println("client opening connection to: " + url);
|
||||
HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
|
||||
InputStream is = urlc.getInputStream();
|
||||
is.close();
|
||||
URI lastURI = MyProxySelector.lastURI();
|
||||
if (!String.valueOf(lastURI).equals(url + "/")) {
|
||||
throw new AssertionError("Custom proxy was not used: last URI was " + lastURI);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
if (server != null) {
|
||||
server.stop(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ProxyTestHandler implements HttpHandler {
|
||||
|
||||
@Override
|
||||
public void handle(HttpExchange exchange) throws IOException {
|
||||
try {
|
||||
exchange.sendResponseHeaders(200, 0);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try(PrintWriter pw = new PrintWriter(exchange.getResponseBody(), false, Charset.forName("UTF-8"))) {
|
||||
pw.print("Hello .");
|
||||
}
|
||||
}
|
||||
}
|
||||
74
test/jdk/java/net/ProxySelector/SystemProxies.java
Normal file
74
test/jdk/java/net/ProxySelector/SystemProxies.java
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 6912868 8170868
|
||||
* @summary Basic test to provide some coverage of system proxy code. Will
|
||||
* always pass. Should be run manually for specific systems to inspect output.
|
||||
* @run main/othervm -Djava.net.useSystemProxies=true SystemProxies
|
||||
*/
|
||||
|
||||
import java.net.Proxy;
|
||||
import java.net.ProxySelector;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.List;
|
||||
|
||||
public class SystemProxies {
|
||||
|
||||
static final String[] uriAuthority = { "myMachine/", "local", "localhost",
|
||||
"127.0.0.1", "127.0.0.123",
|
||||
"127.0.2.2", "127.3.3.3",
|
||||
"128.0.0.1" };
|
||||
static final ProxySelector proxySel = ProxySelector.getDefault();
|
||||
|
||||
public static void main(String[] args) {
|
||||
if (! "true".equals(System.getProperty("java.net.useSystemProxies"))) {
|
||||
System.out.println("Usage: java -Djava.net.useSystemProxies=true SystemProxies");
|
||||
return;
|
||||
}
|
||||
|
||||
printProxies("http://");
|
||||
printProxies("https://");
|
||||
printProxies("ftp://");
|
||||
printProxies("none://");
|
||||
printProxies("rtsp://");
|
||||
printProxies("socket://");
|
||||
}
|
||||
|
||||
static void printProxies(String proto) {
|
||||
System.out.println("Protocol:" + proto);
|
||||
for (String uri : uriAuthority) {
|
||||
String uriStr = proto + uri;
|
||||
try {
|
||||
List<Proxy> proxies = proxySel.select(new URI(uriStr));
|
||||
System.out.println("\tProxies returned for " + uriStr);
|
||||
for (Proxy proxy : proxies)
|
||||
System.out.println("\t\t" + proxy);
|
||||
} catch (URISyntaxException e) {
|
||||
System.err.println(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue