8156504: java/net/URLPermission/nstest/lookup.sh fails intermittently

Reviewed-by: chegar, dfuchs
This commit is contained in:
Felix Yang 2016-11-02 18:44:59 -07:00
parent 1bf1087e7f
commit 447e8a5232
2 changed files with 152 additions and 144 deletions

View File

@ -22,124 +22,195 @@
*/ */
/** /**
* This is a simple smoke test of the HttpURLPermission mechanism, which * @test
* checks for either IOException (due to unknown host) or SecurityException * @summary A simple smoke test of the HttpURLPermission mechanism, which checks
* due to lack of permission to connect * for either IOException (due to unknown host) or SecurityException
* due to lack of permission to connect
* @run main/othervm LookupTest
*/ */
import java.net.*; import java.io.BufferedWriter;
import java.io.*; import java.io.FilePermission;
import jdk.testlibrary.Utils; import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.NetPermission;
import java.net.ProxySelector;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketPermission;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLPermission;
import java.security.CodeSource;
import java.security.Permission;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.Policy;
import java.security.ProtectionDomain;
import static java.nio.charset.StandardCharsets.US_ASCII;
public class LookupTest { public class LookupTest {
static void test( static int port;
String url, boolean throwsSecException, boolean throwsIOException) static volatile ServerSocket serverSocket;
{
static void test(String url,
boolean throwsSecException,
boolean throwsIOException) {
ProxySelector.setDefault(null);
URL u;
InputStream is = null;
try { try {
ProxySelector.setDefault(null); u = new URL(url);
URL u = new URL(url); System.err.println("Connecting to " + u);
System.err.println ("Connecting to " + u);
URLConnection urlc = u.openConnection(); URLConnection urlc = u.openConnection();
InputStream is = urlc.getInputStream(); is = urlc.getInputStream();
} catch (SecurityException e) { } catch (SecurityException e) {
if (!throwsSecException) { if (!throwsSecException) {
throw new RuntimeException ("(1) was not expecting ", e); throw new RuntimeException("Unexpected SecurityException:", e);
} }
return; return;
} catch (IOException ioe) { } catch (IOException e) {
if (!throwsIOException) { if (!throwsIOException) {
throw new RuntimeException ("(2) was not expecting ", ioe); System.err.println("Unexpected IOException:" + e.getMessage());
throw new RuntimeException(e);
} }
return; return;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
System.err.println("Unexpected IOException:" + e.getMessage());
throw new RuntimeException(e);
}
}
} }
if (throwsSecException || throwsIOException) { if (throwsSecException || throwsIOException) {
System.err.printf ("was expecting a %s\n", throwsSecException ? System.err.printf("was expecting a %s\n", throwsSecException
"security exception" : "IOException"); ? "security exception" : "IOException");
throw new RuntimeException("was expecting an exception"); throw new RuntimeException("was expecting an exception");
} }
} }
static int port; static final String CWD = System.getProperty("user.dir", ".");
static ServerSocket serverSocket;
public static void main(String args[]) throws Exception { public static void main(String args[]) throws Exception {
String hostsFileName = CWD + "/LookupTestHosts";
System.setProperty("jdk.net.hosts.file", hostsFileName);
String cmd = args[0]; addMappingToHostsFile("allowedAndFound.com",
if (cmd.equals("-getport")) { "127.0.0.1",
port = Utils.getFreePort(); hostsFileName,
System.out.print(port); false);
} else if (cmd.equals("-runtest")) { addMappingToHostsFile("notAllowedButFound.com",
port = Integer.parseInt(args[1]); "99.99.99.99",
String hostsFileName = System.getProperty("user.dir", ".") + "/LookupTestHosts"; hostsFileName,
System.setProperty("jdk.net.hosts.file", hostsFileName); true);
addMappingToHostsFile("allowedAndFound.com", "127.0.0.1", hostsFileName, false); // name "notAllowedAndNotFound.com" is not in map
addMappingToHostsFile("notAllowedButFound.com", "99.99.99.99", hostsFileName, true); // name "allowedButNotfound.com" is not in map
// name "notAllowedAndNotFound.com" is not in map Server server = new Server();
// name "allowedButNotfound.com" is not in map try {
try { Policy.setPolicy(new LookupTestPolicy());
startServer(); System.setSecurityManager(new SecurityManager());
server.start();
System.setSecurityManager(new SecurityManager()); test("http://allowedAndFound.com:" + port + "/foo", false, false);
test("http://notAllowedButFound.com:" + port + "/foo", true, false);
test("http://allowedAndFound.com:" + port + "/foo", false, false); test("http://allowedButNotfound.com:" + port + "/foo", false, true);
test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
test("http://notAllowedButFound.com:" + port + "/foo", true, false); } finally {
server.terminate();
test("http://allowedButNotfound.com:" + port + "/foo", false, true);
test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);
} finally {
serverSocket.close();
}
} else {
throw new RuntimeException("Bad invocation: " + cmd);
} }
} }
static Thread server;
static class Server extends Thread { static class Server extends Thread {
private volatile boolean done;
public Server() throws IOException {
serverSocket = new ServerSocket(0);
port = serverSocket.getLocalPort();
}
public void run() { public void run() {
byte[] buf = new byte[1000];
try { try {
while (true) { while (!done) {
Socket s = serverSocket.accept(); try (Socket s = serverSocket.accept()) {
InputStream i = s.getInputStream(); readOneRequest(s.getInputStream());
i.read(buf); OutputStream o = s.getOutputStream();
OutputStream o = s.getOutputStream(); String rsp = "HTTP/1.1 200 Ok\r\n" +
String rsp = "HTTP/1.1 200 Ok\r\n" + "Connection: close\r\n" +
"Connection: close\r\nContent-length: 0\r\n\r\n"; "Content-length: 0\r\n\r\n";
o.write(rsp.getBytes()); o.write(rsp.getBytes(US_ASCII));
o.close(); }
} }
} catch (IOException e) { } catch (IOException e) {
return; if (!done)
e.printStackTrace();
} }
} }
}
static void startServer() { void terminate() {
try { done = true;
serverSocket = new ServerSocket(port); try { serverSocket.close(); }
server = new Server(); catch (IOException unexpected) { unexpected.printStackTrace(); }
server.start(); }
} catch (Exception e) {
throw new RuntimeException ("Test failed to initialize", e); static final byte[] requestEnd = new byte[] {'\r', '\n', '\r', '\n' };
// Read until the end of a HTTP request
void readOneRequest(InputStream is) throws IOException {
int requestEndCount = 0, r;
while ((r = is.read()) != -1) {
if (r == requestEnd[requestEndCount]) {
requestEndCount++;
if (requestEndCount == 4) {
break;
}
} else {
requestEndCount = 0;
}
}
} }
} }
private static void addMappingToHostsFile (String host, private static void addMappingToHostsFile(String host,
String addr, String addr,
String hostsFileName, String hostsFileName,
boolean append) boolean append)
throws Exception { throws IOException
{
String mapping = addr + " " + host; String mapping = addr + " " + host;
try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter( try (FileWriter fr = new FileWriter(hostsFileName, append);
new FileWriter(hostsFileName, append)))) { PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(fr))) {
hfPWriter.println(mapping); hfPWriter.println(mapping);
} }
} }
static class LookupTestPolicy extends Policy {
final PermissionCollection perms = new Permissions();
LookupTestPolicy() throws Exception {
perms.add(new NetPermission("setProxySelector"));
perms.add(new SocketPermission("localhost:1024-", "resolve,accept"));
perms.add(new URLPermission("http://allowedAndFound.com:" + port + "/-", "*:*"));
perms.add(new URLPermission("http://allowedButNotfound.com:" + port + "/-", "*:*"));
perms.add(new FilePermission("<<ALL FILES>>", "read,write,delete"));
//perms.add(new PropertyPermission("java.io.tmpdir", "read"));
}
public PermissionCollection getPermissions(ProtectionDomain domain) {
return perms;
}
public PermissionCollection getPermissions(CodeSource codesource) {
return perms;
}
public boolean implies(ProtectionDomain domain, Permission perm) {
return perms.implies(perm);
}
}
} }

View File

@ -1,63 +0,0 @@
#!/bin/sh
#
# Copyright (c) 2013, 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.
#
# 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
# @library /lib/testlibrary
# @build jdk.testlibrary.*
# @compile -XDignore.symbol.file=true LookupTest.java
# @run shell/timeout=50 lookup.sh
# @key intermittent
#
OS=`uname -s`
case ${OS} in
Windows_* | CYGWIN*)
PS=";"
FS="\\"
;;
*)
PS=":"
FS="/"
;;
esac
port=`${TESTJAVA}/bin/java -cp ${TESTCLASSPATH} LookupTest -getport`
cat << POLICY > policy
grant {
permission java.net.URLPermission "http://allowedAndFound.com:${port}/-", "*:*";
permission java.net.URLPermission "http://allowedButNotfound.com:${port}/-", "*:*";
permission java.net.NetPermission "setProxySelector";
permission java.io.FilePermission "<<ALL FILES>>", "read,write,delete";
permission java.util.PropertyPermission "java.io.tmpdir", "read";
// needed for HttpServer
permission "java.net.SocketPermission" "localhost:1024-", "resolve,accept";
};
POLICY
${TESTJAVA}/bin/java ${TESTVMOPTS} \
-Djava.security.policy=file:./policy \
-Dtest.src=${TESTSRC} \
-cp ${TESTCLASSPATH}${PS}${TESTSRC} LookupTest -runtest ${port}