2016-02-25 23:14:22 +00:00
|
|
|
/*
|
2018-04-17 15:54:17 +00:00
|
|
|
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
|
2016-02-25 23:14:22 +00:00
|
|
|
* 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
|
2016-12-09 11:35:02 +00:00
|
|
|
* published by the Free Software Foundation.
|
2016-02-25 23:14:22 +00:00
|
|
|
*
|
|
|
|
* 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
|
2016-08-31 16:46:50 +00:00
|
|
|
* questions.
|
2016-02-25 23:14:22 +00:00
|
|
|
*/
|
|
|
|
|
2016-11-03 13:19:29 +00:00
|
|
|
/*
|
2016-02-25 23:14:22 +00:00
|
|
|
* @test
|
|
|
|
* @bug 8087112
|
2018-04-17 15:54:17 +00:00
|
|
|
* @modules java.net.http
|
2016-11-03 13:19:29 +00:00
|
|
|
* java.logging
|
2016-06-13 04:55:20 +00:00
|
|
|
* jdk.httpserver
|
2016-02-25 23:14:22 +00:00
|
|
|
* @library /lib/testlibrary/
|
|
|
|
* @build jdk.testlibrary.SimpleSSLContext
|
|
|
|
* @compile ../../../../com/sun/net/httpserver/LogFilter.java
|
|
|
|
* @compile ../../../../com/sun/net/httpserver/FileServerHandler.java
|
|
|
|
* @compile ../ProxyServer.java
|
|
|
|
*
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=0.policy Security 0
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=2.policy Security 2
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=3.policy Security 3
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=4.policy Security 4
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=5.policy Security 5
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=6.policy Security 6
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=7.policy Security 7
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=8.policy Security 8
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=9.policy Security 9
|
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=0.policy Security 13
|
2016-05-05 10:37:18 +00:00
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=14.policy Security 14
|
2016-12-09 11:35:02 +00:00
|
|
|
* @run main/othervm/secure=java.lang.SecurityManager/policy=15.policy -Djava.security.debug=access:domain,failure Security 15
|
2016-02-25 23:14:22 +00:00
|
|
|
*/
|
|
|
|
|
2016-05-05 10:37:18 +00:00
|
|
|
// Tests 1, 10, 11 and 12 executed from Driver
|
|
|
|
|
2016-11-03 13:19:29 +00:00
|
|
|
import com.sun.net.httpserver.Headers;
|
|
|
|
import com.sun.net.httpserver.HttpExchange;
|
|
|
|
import com.sun.net.httpserver.HttpHandler;
|
|
|
|
import com.sun.net.httpserver.HttpServer;
|
|
|
|
import com.sun.net.httpserver.HttpsServer;
|
2016-02-25 23:14:22 +00:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.lang.reflect.Constructor;
|
2016-11-03 13:19:29 +00:00
|
|
|
import java.net.BindException;
|
2018-04-17 15:54:17 +00:00
|
|
|
import java.net.InetAddress;
|
2016-11-03 13:19:29 +00:00
|
|
|
import java.net.InetSocketAddress;
|
|
|
|
import java.net.ProxySelector;
|
|
|
|
import java.net.URI;
|
|
|
|
import java.net.URLClassLoader;
|
|
|
|
import java.net.URL;
|
2018-04-17 15:54:17 +00:00
|
|
|
import java.net.http.HttpHeaders;
|
|
|
|
import java.net.http.HttpClient;
|
|
|
|
import java.net.http.HttpRequest;
|
|
|
|
import java.net.http.HttpResponse;
|
2016-02-25 23:14:22 +00:00
|
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
import java.nio.file.StandardCopyOption;
|
|
|
|
import java.util.LinkedList;
|
|
|
|
import java.util.List;
|
2016-11-03 13:19:29 +00:00
|
|
|
import java.util.concurrent.CompletableFuture;
|
2016-12-09 11:35:02 +00:00
|
|
|
import java.util.concurrent.CompletionException;
|
|
|
|
import java.util.concurrent.CompletionStage;
|
2016-11-03 13:19:29 +00:00
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
2016-12-09 11:35:02 +00:00
|
|
|
import java.util.concurrent.Flow;
|
2016-02-25 23:14:22 +00:00
|
|
|
import java.util.logging.ConsoleHandler;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2016-05-12 15:06:17 +00:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2018-04-17 15:54:17 +00:00
|
|
|
import static java.net.http.HttpResponse.BodyHandlers.ofString;
|
2016-02-25 23:14:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Security checks test
|
|
|
|
*/
|
|
|
|
public class Security {
|
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
static HttpServer s1;
|
|
|
|
static ExecutorService executor;
|
2016-05-05 10:37:18 +00:00
|
|
|
static int port, proxyPort;
|
2016-02-25 23:14:22 +00:00
|
|
|
static HttpClient client;
|
|
|
|
static String httproot, fileuri, fileroot, redirectroot;
|
|
|
|
static List<HttpClient> clients = new LinkedList<>();
|
|
|
|
static URI uri;
|
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
interface ThrowingRunnable { void run() throws Throwable; }
|
2016-02-25 23:14:22 +00:00
|
|
|
|
|
|
|
static class TestAndResult {
|
2018-04-17 15:54:17 +00:00
|
|
|
private final ThrowingRunnable runnable;
|
|
|
|
private final boolean expectSecurityException;
|
2016-02-25 23:14:22 +00:00
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult(boolean expectSecurityException, ThrowingRunnable runnable) {
|
|
|
|
this.expectSecurityException = expectSecurityException;
|
|
|
|
this.runnable = runnable;
|
|
|
|
}
|
|
|
|
|
|
|
|
static TestAndResult of(boolean expectSecurityException,
|
|
|
|
ThrowingRunnable runnable) {
|
|
|
|
return new TestAndResult(expectSecurityException, runnable);
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
void runWithPolicy(String policy) {
|
|
|
|
System.out.println("Using policy file: " + policy);
|
|
|
|
try {
|
|
|
|
runnable.run();
|
|
|
|
if (expectSecurityException) {
|
|
|
|
String msg = "FAILED: expected security exception not thrown";
|
|
|
|
System.out.println(msg);
|
|
|
|
throw new RuntimeException(msg);
|
|
|
|
}
|
|
|
|
System.out.println (policy + " succeeded as expected");
|
|
|
|
} catch (BindException e) {
|
|
|
|
System.exit(10);
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
if (!expectSecurityException) {
|
|
|
|
System.out.println("UNEXPECTED security Exception: " + e);
|
|
|
|
throw new RuntimeException("UNEXPECTED security Exception", e);
|
|
|
|
}
|
|
|
|
System.out.println(policy + " threw SecurityException as expected: " + e);
|
|
|
|
} catch (Throwable t) {
|
|
|
|
throw new AssertionError(t);
|
|
|
|
}
|
|
|
|
}
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
static TestAndResult[] tests = createTests();
|
2016-02-25 23:14:22 +00:00
|
|
|
static String testclasses;
|
|
|
|
static File subdir;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The ProxyServer class is compiled by jtreg, but we want to
|
|
|
|
* move it so it is not on the application claspath. We want to
|
|
|
|
* load it through a separate classloader so that it has a separate
|
|
|
|
* protection domain and security permissions.
|
|
|
|
*
|
|
|
|
* Its permissions are in the second grant block in each policy file
|
|
|
|
*/
|
|
|
|
static void setupProxy() throws IOException, ClassNotFoundException, NoSuchMethodException {
|
|
|
|
testclasses = System.getProperty("test.classes");
|
|
|
|
subdir = new File (testclasses, "proxydir");
|
|
|
|
subdir.mkdir();
|
|
|
|
|
|
|
|
movefile("ProxyServer.class");
|
|
|
|
movefile("ProxyServer$Connection.class");
|
|
|
|
movefile("ProxyServer$1.class");
|
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
URL url = subdir.toURI().toURL();
|
2016-02-25 23:14:22 +00:00
|
|
|
System.out.println("URL for class loader = " + url);
|
|
|
|
URLClassLoader urlc = new URLClassLoader(new URL[] {url});
|
|
|
|
proxyClass = Class.forName("ProxyServer", true, urlc);
|
|
|
|
proxyConstructor = proxyClass.getConstructor(Integer.class, Boolean.class);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void movefile(String f) throws IOException {
|
|
|
|
Path src = Paths.get(testclasses, f);
|
|
|
|
Path dest = subdir.toPath().resolve(f);
|
|
|
|
if (!dest.toFile().exists()) {
|
|
|
|
System.out.printf("moving %s to %s\n", src.toString(), dest.toString());
|
|
|
|
Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING);
|
2016-05-05 10:37:18 +00:00
|
|
|
} else if (src.toFile().exists()) {
|
|
|
|
System.out.printf("%s exists, deleting %s\n", dest.toString(), src.toString());
|
|
|
|
Files.delete(src);
|
2016-02-25 23:14:22 +00:00
|
|
|
} else {
|
|
|
|
System.out.printf("NOT moving %s to %s\n", src.toString(), dest.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
static Object createProxy(int port, boolean b) throws Throwable {
|
2016-05-12 15:06:17 +00:00
|
|
|
try {
|
|
|
|
return proxyConstructor.newInstance(port, b);
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
throw e.getTargetException();
|
|
|
|
}
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static Class<?> proxyClass;
|
|
|
|
static Constructor<?> proxyConstructor;
|
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
static TestAndResult[] createTests() {
|
|
|
|
return new TestAndResult[] {
|
2016-02-25 23:14:22 +00:00
|
|
|
// (0) policy does not have permission for file. Should fail
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(true, () -> { // Policy 0
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (1) policy has permission for file URL
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(false, () -> { //Policy 1
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (2) policy has permission for all file URLs under /files
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(false, () -> { // Policy 2
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (3) policy has permission for first URL but not redirected URL
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(true, () -> { // Policy 3
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/redirect/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (4) policy has permission for both first URL and redirected URL
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(false, () -> { // Policy 4
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/redirect/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (5) policy has permission for redirected but not first URL
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(true, () -> { // Policy 5
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/redirect/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (6) policy has permission for file URL, but not method
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(true, () -> { //Policy 6
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (7) policy has permission for file URL, method, but not header
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(true, () -> { //Policy 7
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u)
|
|
|
|
.header("X-Foo", "bar")
|
|
|
|
.GET()
|
|
|
|
.build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (8) policy has permission for file URL, method and header
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(false, () -> { //Policy 8
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u)
|
|
|
|
.header("X-Foo", "bar")
|
|
|
|
.GET()
|
|
|
|
.build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (9) policy has permission for file URL, method and header
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(false, () -> { //Policy 9
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u)
|
|
|
|
.headers("X-Foo", "bar", "X-Bar", "foo")
|
|
|
|
.GET()
|
|
|
|
.build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (10) policy has permission for destination URL but not for proxy
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(true, () -> { //Policy 10
|
2016-05-05 10:37:18 +00:00
|
|
|
directProxyTest(proxyPort, true);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (11) policy has permission for both destination URL and proxy
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(false, () -> { //Policy 11
|
2016-05-05 10:37:18 +00:00
|
|
|
directProxyTest(proxyPort, true);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (12) policy has permission for both destination URL and proxy
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(true, () -> { //Policy 12 ( 11 & 12 are the same )
|
2016-05-05 10:37:18 +00:00
|
|
|
directProxyTest(proxyPort, false);
|
2016-02-25 23:14:22 +00:00
|
|
|
}),
|
|
|
|
// (13) async version of test 0
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(true, () -> { // Policy 0
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2016-02-25 23:14:22 +00:00
|
|
|
try {
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.sendAsync(request, ofString()).get();
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
} catch (ExecutionException e) {
|
|
|
|
if (e.getCause() instanceof SecurityException) {
|
|
|
|
throw (SecurityException)e.getCause();
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
// (14) async version of test 1
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(false, () -> { //Policy 1
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2016-02-25 23:14:22 +00:00
|
|
|
try {
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = client.sendAsync(request, ofString()).get();
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
} catch (ExecutionException e) {
|
|
|
|
if (e.getCause() instanceof SecurityException) {
|
|
|
|
throw (SecurityException)e.getCause();
|
|
|
|
} else {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
// (15) check that user provided unprivileged code running on a worker
|
|
|
|
// thread does not gain ungranted privileges.
|
2018-04-17 15:54:17 +00:00
|
|
|
TestAndResult.of(true, () -> { //Policy 12
|
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u).GET().build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse.BodyHandler<String> sth = ofString();
|
2016-02-25 23:14:22 +00:00
|
|
|
|
2016-12-09 11:35:02 +00:00
|
|
|
CompletableFuture<HttpResponse<String>> cf =
|
|
|
|
client.sendAsync(request, new HttpResponse.BodyHandler<String>() {
|
|
|
|
@Override
|
2018-04-17 15:54:17 +00:00
|
|
|
public HttpResponse.BodySubscriber<String> apply(HttpResponse.ResponseInfo rinfo) {
|
|
|
|
final HttpResponse.BodySubscriber<String> stproc = sth.apply(rinfo);
|
2017-12-06 19:11:59 +00:00
|
|
|
return new HttpResponse.BodySubscriber<String>() {
|
2016-12-09 11:35:02 +00:00
|
|
|
@Override
|
|
|
|
public CompletionStage<String> getBody() {
|
|
|
|
return stproc.getBody();
|
|
|
|
}
|
|
|
|
@Override
|
2017-12-06 19:11:59 +00:00
|
|
|
public void onNext(List<ByteBuffer> item) {
|
2016-12-09 11:35:02 +00:00
|
|
|
SecurityManager sm = System.getSecurityManager();
|
|
|
|
// should succeed.
|
|
|
|
sm.checkPermission(new RuntimePermission("foobar"));
|
|
|
|
// do some mischief here
|
|
|
|
System.setSecurityManager(null);
|
|
|
|
System.setSecurityManager(sm);
|
|
|
|
// problem if we get this far
|
|
|
|
stproc.onNext(item);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void onSubscribe(Flow.Subscription subscription) {
|
|
|
|
stproc.onSubscribe(subscription);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void onError(Throwable throwable) {
|
|
|
|
stproc.onError(throwable);
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void onComplete() {
|
|
|
|
stproc.onComplete();
|
|
|
|
}
|
|
|
|
};
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
try {
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<String> response = cf.join();
|
|
|
|
System.out.println("Received response:" + response);
|
2016-12-09 11:35:02 +00:00
|
|
|
} catch (CompletionException e) {
|
|
|
|
Throwable t = e.getCause();
|
|
|
|
if (t instanceof SecurityException)
|
|
|
|
throw (SecurityException)t;
|
2017-12-06 19:11:59 +00:00
|
|
|
else if ((t instanceof IOException)
|
|
|
|
&& (t.getCause() instanceof SecurityException))
|
|
|
|
throw ((SecurityException)t.getCause());
|
2016-12-09 11:35:02 +00:00
|
|
|
else
|
|
|
|
throw new RuntimeException(t);
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:35:02 +00:00
|
|
|
private static void directProxyTest(int proxyPort, boolean samePort)
|
|
|
|
throws IOException, InterruptedException
|
|
|
|
{
|
2018-04-17 15:54:17 +00:00
|
|
|
System.out.println("proxyPort:" + proxyPort + ", samePort:" + samePort);
|
|
|
|
|
|
|
|
int p = proxyPort;
|
|
|
|
if (samePort) {
|
|
|
|
Object proxy;
|
|
|
|
try {
|
|
|
|
proxy = createProxy(p, true);
|
|
|
|
} catch (BindException e) {
|
|
|
|
System.out.println("Bind failed");
|
|
|
|
throw e;
|
|
|
|
} catch (Throwable ee) {
|
|
|
|
throw new RuntimeException(ee);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (p == proxyPort || p == port) {
|
|
|
|
// avoid ports that may be granted permission
|
|
|
|
p++;
|
|
|
|
if (p > 65535) {
|
|
|
|
p = 32000; // overflow
|
|
|
|
}
|
|
|
|
}
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
2018-04-17 15:54:17 +00:00
|
|
|
System.out.println("Proxy port, p:" + p);
|
2016-02-25 23:14:22 +00:00
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(),
|
|
|
|
p);
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpClient cl = HttpClient.newBuilder()
|
|
|
|
.proxy(ProxySelector.of(addr))
|
|
|
|
.build();
|
2016-02-25 23:14:22 +00:00
|
|
|
clients.add(cl);
|
|
|
|
|
2018-04-17 15:54:17 +00:00
|
|
|
URI u = URI.create("http://localhost:" + port + "/files/foo.txt");
|
2016-12-09 11:35:02 +00:00
|
|
|
HttpRequest request = HttpRequest.newBuilder(u)
|
|
|
|
.headers("X-Foo", "bar", "X-Bar", "foo")
|
|
|
|
.build();
|
2018-04-17 15:54:17 +00:00
|
|
|
HttpResponse<?> response = cl.send(request, ofString());
|
|
|
|
System.out.println("Received response:" + response);
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) throws Exception {
|
2016-05-05 10:37:18 +00:00
|
|
|
try {
|
|
|
|
initServer();
|
|
|
|
setupProxy();
|
|
|
|
} catch (BindException e) {
|
|
|
|
System.exit(10);
|
|
|
|
}
|
2018-04-17 15:54:17 +00:00
|
|
|
fileroot = System.getProperty("test.src")+ "/docs";
|
2016-02-25 23:14:22 +00:00
|
|
|
int testnum = Integer.parseInt(args[0]);
|
|
|
|
String policy = args[0];
|
|
|
|
|
2016-12-09 11:35:02 +00:00
|
|
|
client = HttpClient.newBuilder()
|
|
|
|
.followRedirects(HttpClient.Redirect.ALWAYS)
|
|
|
|
.build();
|
2016-02-25 23:14:22 +00:00
|
|
|
|
|
|
|
clients.add(client);
|
|
|
|
|
|
|
|
try {
|
|
|
|
TestAndResult tr = tests[testnum];
|
2018-04-17 15:54:17 +00:00
|
|
|
tr.runWithPolicy(policy);
|
2016-02-25 23:14:22 +00:00
|
|
|
} finally {
|
|
|
|
s1.stop(0);
|
2016-05-05 10:37:18 +00:00
|
|
|
executor.shutdownNow();
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void initServer() throws Exception {
|
2016-05-05 10:37:18 +00:00
|
|
|
String portstring = System.getProperty("port.number");
|
|
|
|
port = portstring != null ? Integer.parseInt(portstring) : 0;
|
|
|
|
portstring = System.getProperty("port.number1");
|
|
|
|
proxyPort = portstring != null ? Integer.parseInt(portstring) : 0;
|
|
|
|
|
2016-02-25 23:14:22 +00:00
|
|
|
Logger logger = Logger.getLogger("com.sun.net.httpserver");
|
|
|
|
ConsoleHandler ch = new ConsoleHandler();
|
|
|
|
logger.setLevel(Level.ALL);
|
|
|
|
ch.setLevel(Level.ALL);
|
|
|
|
logger.addHandler(ch);
|
2018-04-17 15:54:17 +00:00
|
|
|
String root = System.getProperty("test.src")+ "/docs";
|
|
|
|
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), port);
|
2016-02-25 23:14:22 +00:00
|
|
|
s1 = HttpServer.create (addr, 0);
|
|
|
|
if (s1 instanceof HttpsServer) {
|
2018-04-17 15:54:17 +00:00
|
|
|
throw new RuntimeException("should not be httpsserver");
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
2018-04-17 15:54:17 +00:00
|
|
|
s1.createContext("/files", new FileServerHandler(root));
|
|
|
|
s1.createContext("/redirect", new RedirectHandler("/redirect"));
|
2016-02-25 23:14:22 +00:00
|
|
|
|
|
|
|
executor = Executors.newCachedThreadPool();
|
2018-04-17 15:54:17 +00:00
|
|
|
s1.setExecutor(executor);
|
2016-02-25 23:14:22 +00:00
|
|
|
s1.start();
|
|
|
|
|
2016-05-05 10:37:18 +00:00
|
|
|
if (port == 0)
|
|
|
|
port = s1.getAddress().getPort();
|
|
|
|
else {
|
|
|
|
if (s1.getAddress().getPort() != port)
|
|
|
|
throw new RuntimeException("Error wrong port");
|
|
|
|
System.out.println("Port was assigned by Driver");
|
|
|
|
}
|
2016-02-25 23:14:22 +00:00
|
|
|
System.out.println("HTTP server port = " + port);
|
2018-04-17 15:54:17 +00:00
|
|
|
httproot = "http://localhost:" + port + "/files/";
|
|
|
|
redirectroot = "http://localhost:" + port + "/redirect/";
|
2016-02-25 23:14:22 +00:00
|
|
|
uri = new URI(httproot);
|
|
|
|
fileuri = httproot + "foo.txt";
|
|
|
|
}
|
|
|
|
|
|
|
|
static class RedirectHandler implements HttpHandler {
|
|
|
|
|
|
|
|
String root;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
RedirectHandler(String root) {
|
|
|
|
this.root = root;
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized int count() {
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
synchronized void increment() {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-04-17 15:54:17 +00:00
|
|
|
public synchronized void handle(HttpExchange t) throws IOException {
|
2016-02-25 23:14:22 +00:00
|
|
|
System.out.println("Server: " + t.getRequestURI());
|
|
|
|
try (InputStream is = t.getRequestBody()) {
|
2018-04-17 15:54:17 +00:00
|
|
|
is.readAllBytes();
|
2016-02-25 23:14:22 +00:00
|
|
|
}
|
|
|
|
increment();
|
|
|
|
if (count() == 1) {
|
|
|
|
Headers map = t.getResponseHeaders();
|
|
|
|
String redirect = "/redirect/bar.txt";
|
|
|
|
map.add("Location", redirect);
|
|
|
|
t.sendResponseHeaders(301, -1);
|
|
|
|
t.close();
|
|
|
|
} else {
|
|
|
|
String response = "Hello world";
|
|
|
|
t.sendResponseHeaders(200, response.length());
|
|
|
|
OutputStream os = t.getResponseBody();
|
|
|
|
os.write(response.getBytes(StandardCharsets.ISO_8859_1));
|
|
|
|
t.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|