8155928: Remove hardcoded port numbers from httpclient/Security.java test

Reviewed-by: chegar, rriggs
This commit is contained in:
Michael McMahon 2016-05-05 11:37:18 +01:00
parent bd9c092ecc
commit 0cf5c57454
18 changed files with 215 additions and 63 deletions

View File

@ -146,7 +146,7 @@ final class Utils {
} else {
sb.append(uri.getScheme())
.append("://")
.append(uri.getHost())
.append(uri.getAuthority())
.append(uri.getPath());
urlstring = sb.toString();

View File

@ -2,8 +2,7 @@
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";

View File

@ -1,8 +1,7 @@
// Policy 1
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";
@ -13,7 +12,7 @@ grant {
// permissions specific to this test
permission java.net.URLPermission "http://127.0.0.1:*/files/foo.txt", "GET";
permission java.net.URLPermission "http://127.0.0.1:${port.number}/files/foo.txt", "GET";
};
// For proxy only. Not being tested

View File

@ -1,8 +1,7 @@
// Policy 10
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";
@ -12,7 +11,7 @@ grant {
permission java.lang.RuntimePermission "createClassLoader";
// permissions specific to this test
permission java.net.URLPermission "http://127.0.0.1:*/files/foo.txt", "GET:*";
permission java.net.URLPermission "http://127.0.0.1:${port.number}/files/foo.txt", "GET:*";
};
// For proxy only. Not being tested

View File

@ -1,8 +1,7 @@
// Policy 11
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";
@ -12,8 +11,8 @@ grant {
permission java.lang.RuntimePermission "createClassLoader";
// permissions specific to this test
permission java.net.URLPermission "http://127.0.0.1:*/files/foo.txt", "GET:*";
permission java.net.URLPermission "socket://127.0.0.1:27301", "CONNECT";
permission java.net.URLPermission "http://127.0.0.1:${port.number}/files/foo.txt", "GET:*";
permission java.net.URLPermission "socket://127.0.0.1:${port.number1}", "CONNECT";
};

View File

@ -1,8 +1,7 @@
// Policy 11
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";
@ -12,8 +11,8 @@ grant {
permission java.lang.RuntimePermission "createClassLoader";
// permissions specific to this test
permission java.net.URLPermission "http://127.0.0.1:*/files/foo.txt", "GET:*";
permission java.net.URLPermission "socket://127.0.0.1:27301", "CONNECT";
permission java.net.URLPermission "http://127.0.0.1:${port.number}/files/foo.txt", "GET:*";
permission java.net.URLPermission "socket://127.0.0.1:${port.number1}", "CONNECT";
};

View File

@ -0,0 +1,23 @@
// Policy 14
grant {
// permissions common to all tests
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";
permission java.util.logging.LoggingPermission "control", "";
permission java.net.SocketPermission "localhost:1024-", "accept,listen";
permission java.io.FilePermission "${test.src}${/}docs${/}-", "read";
permission java.lang.RuntimePermission "createClassLoader";
// permissions specific to this test
permission java.net.URLPermission "http://127.0.0.1:*/files/foo.txt", "GET";
};
// For proxy only. Not being tested
grant codebase "file:${test.classes}/proxydir/-" {
permission java.net.SocketPermission "localhost:1024-", "accept,listen,connect";
permission java.net.SocketPermission "127.0.0.1:1024-", "connect,resolve";
};

View File

@ -1,8 +1,7 @@
// Policy 11
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";
@ -13,7 +12,6 @@ grant {
// permissions specific to this test
permission java.net.URLPermission "http://127.0.0.1:*/files/foo.txt", "GET:*";
permission java.net.URLPermission "socket://127.0.0.1:27301", "CONNECT";
// Test checks for this explicitly
permission java.net.RuntimePermission "foobar";

View File

@ -1,8 +1,7 @@
// Policy 2
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";

View File

@ -1,8 +1,7 @@
// Policy 3
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";

View File

@ -1,8 +1,7 @@
// Policy 4
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";

View File

@ -1,8 +1,7 @@
// Policy 5
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";

View File

@ -1,8 +1,7 @@
// Policy 6
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";

View File

@ -1,8 +1,7 @@
// Policy 7
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";

View File

@ -1,8 +1,7 @@
// Policy 8
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";

View File

@ -1,8 +1,7 @@
// Policy 9
grant {
// permissions common to all tests
permission java.util.PropertyPermission "test.src", "read";
permission java.util.PropertyPermission "test.classes", "read";
permission java.util.PropertyPermission "*", "read";
permission java.io.FilePermission "${test.classes}${/}-", "read,write,delete";
permission java.net.NetPermission "getDefaultHttpClient";
permission java.lang.RuntimePermission "modifyThread";

View File

@ -0,0 +1,139 @@
/*
* Copyright (c) 2015, 2016, 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. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* 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
*/
/**
* @test
* @bug 8087112
* @library /lib/testlibrary/
* @build jdk.testlibrary.SimpleSSLContext jdk.testlibrary.Utils
* @compile ../../../../com/sun/net/httpserver/LogFilter.java
* @compile ../../../../com/sun/net/httpserver/FileServerHandler.java
* @compile ../ProxyServer.java
* @build Security
*
* @run driver/timeout=60 Driver
*/
/**
* driver required for allocating free portnumbers and putting this number
* into security policy file used in some tests.
*
* The tests are in Security.java and port number supplied in -Dport.number
* and -Dport.number1 for tests that require a second free port
*/
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.io.*;
import java.net.*;
import jdk.testlibrary.OutputAnalyzer;
import jdk.testlibrary.Utils;
/**
* Driver for tests
*/
public class Driver {
public static void main(String[] args) throws Throwable {
System.out.println("Starting Driver");
runtest("1.policy", "1");
runtest("10.policy", "10");
runtest("11.policy", "11");
runtest("12.policy", "12");
System.out.println("DONE");
}
static class Logger extends Thread {
private final OutputStream ps;
private final InputStream stdout;
Logger(String cmdLine, Process p, String dir) throws IOException {
super();
setDaemon(true);
cmdLine = "Command line = [" + cmdLine + "]";
stdout = p.getInputStream();
File f = File.createTempFile("debug", ".txt", new File(dir));
ps = new FileOutputStream(f);
ps.write(cmdLine.getBytes());
ps.flush();
}
public void run() {
try {
byte[] buf = new byte[128];
int c;
while ((c = stdout.read(buf)) != -1) {
ps.write(buf, 0, c);
ps.flush();
}
ps.close();
} catch (Throwable e) {
e.printStackTrace();
}
}
}
public static void runtest(String policy, String testnum) throws Throwable {
String testJdk = System.getProperty("test.jdk", "?");
String testSrc = System.getProperty("test.src", "?");
String testClassPath = System.getProperty("test.class.path", "?");
String testClasses = System.getProperty("test.classes", "?");
String sep = System.getProperty("file.separator", "?");
String javaCmd = testJdk + sep + "bin" + sep + "java";
int retval = 10; // 10 is special exit code denoting a bind error
// in which case, we retry
while (retval == 10) {
List<String> cmd = new ArrayList<>();
cmd.add(javaCmd);
cmd.add("-Dtest.jdk=" + testJdk);
cmd.add("-Dtest.src=" + testSrc);
cmd.add("-Dtest.classes=" + testClasses);
cmd.add("-Djava.security.manager");
cmd.add("-Djava.security.policy=" + testSrc + sep + policy);
cmd.add("-Dport.number=" + Integer.toString(Utils.getFreePort()));
cmd.add("-Dport.number1=" + Integer.toString(Utils.getFreePort()));
cmd.add("-cp");
cmd.add(testClassPath);
cmd.add("Security");
cmd.add(testnum);
ProcessBuilder processBuilder = new ProcessBuilder(cmd)
.redirectOutput(ProcessBuilder.Redirect.PIPE)
.redirectErrorStream(true);
String cmdLine = cmd.stream().collect(Collectors.joining(" "));
Process child = processBuilder.start();
Logger log = new Logger(cmdLine, child, testClasses);
log.start();
retval = child.waitFor();
System.out.println("retval = " + retval);
}
if (retval != 0) {
Thread.sleep(2000);
throw new RuntimeException("Non zero return value");
}
}
}

View File

@ -32,7 +32,6 @@
* @compile ../ProxyServer.java
*
* @run main/othervm/secure=java.lang.SecurityManager/policy=0.policy Security 0
* @run main/othervm/secure=java.lang.SecurityManager/policy=1.policy Security 1
* @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
@ -41,14 +40,13 @@
* @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=10.policy Security 10
* @run main/othervm/secure=java.lang.SecurityManager/policy=11.policy Security 11
* @run main/othervm/secure=java.lang.SecurityManager/policy=12.policy Security 12
* @run main/othervm/secure=java.lang.SecurityManager/policy=0.policy Security 13
* @run main/othervm/secure=java.lang.SecurityManager/policy=1.policy Security 14
* @run main/othervm/secure=java.lang.SecurityManager/policy=14.policy Security 14
* @run main/othervm/secure=java.lang.SecurityManager/policy=15.policy Security 15
*/
// Tests 1, 10, 11 and 12 executed from Driver
import com.sun.net.httpserver.*;
import java.io.IOException;
import java.io.InputStream;
@ -78,7 +76,7 @@ public class Security {
static HttpServer s1 = null;
static ExecutorService executor=null;
static int port;
static int port, proxyPort;
static HttpClient client;
static String httproot, fileuri, fileroot, redirectroot;
static List<HttpClient> clients = new LinkedList<>();
@ -136,6 +134,9 @@ public class Security {
if (!dest.toFile().exists()) {
System.out.printf("moving %s to %s\n", src.toString(), dest.toString());
Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING);
} else if (src.toFile().exists()) {
System.out.printf("%s exists, deleting %s\n", dest.toString(), src.toString());
Files.delete(src);
} else {
System.out.printf("NOT moving %s to %s\n", src.toString(), dest.toString());
}
@ -225,15 +226,15 @@ public class Security {
}),
// (10) policy has permission for destination URL but not for proxy
test(false, () -> { //Policy 10
directProxyTest(27208, true);
directProxyTest(proxyPort, true);
}),
// (11) policy has permission for both destination URL and proxy
test(true, () -> { //Policy 11
directProxyTest(27301, true);
directProxyTest(proxyPort, true);
}),
// (12) policy has permission for both destination URL and proxy
test(false, () -> { //Policy 11
directProxyTest(28301, false);
directProxyTest(proxyPort, false);
}),
// (13) async version of test 0
test(false, () -> { // Policy 0
@ -350,6 +351,8 @@ public class Security {
throw new RuntimeException("Failed");
}
System.out.println (policy + " succeeded as expected");
} catch (BindException e) {
System.exit(10);
} catch (SecurityException e) {
if (succeeds) {
System.out.println("FAILED");
@ -362,8 +365,12 @@ public class Security {
}
public static void main(String[] args) throws Exception {
initServer();
setupProxy();
try {
initServer();
setupProxy();
} catch (BindException e) {
System.exit(10);
}
fileroot = System.getProperty ("test.src")+ "/docs";
int testnum = Integer.parseInt(args[0]);
String policy = args[0];
@ -382,33 +389,25 @@ public class Security {
runtest(tr.test, policy, tr.result);
} finally {
s1.stop(0);
//executor.shutdownNow();
executor.shutdownNow();
for (HttpClient client : clients)
client.executorService().shutdownNow();
}
}
// create Http Server on port range below. So, we can
HttpServer createServer() {
HttpServer server;
for (int i=25800; i<26800; i++) {
InetSocketAddress a = new InetSocketAddress(i);
try {
server = HttpServer.create(a, 0);
return server;
} catch (IOException e) {}
}
return null;
}
public static void initServer() throws Exception {
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;
Logger logger = Logger.getLogger("com.sun.net.httpserver");
ConsoleHandler ch = new ConsoleHandler();
logger.setLevel(Level.ALL);
ch.setLevel(Level.ALL);
logger.addHandler(ch);
String root = System.getProperty ("test.src")+ "/docs";
InetSocketAddress addr = new InetSocketAddress (0);
InetSocketAddress addr = new InetSocketAddress (port);
s1 = HttpServer.create (addr, 0);
if (s1 instanceof HttpsServer) {
throw new RuntimeException ("should not be httpsserver");
@ -423,7 +422,13 @@ public class Security {
s1.setExecutor (executor);
s1.start();
port = s1.getAddress().getPort();
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");
}
System.out.println("HTTP server port = " + port);
httproot = "http://127.0.0.1:" + port + "/files/";
redirectroot = "http://127.0.0.1:" + port + "/redirect/";