Path: blob/master/test/jdk/java/net/URLPermission/URLTest.java
41149 views
/*1* Copyright (c) 2013, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.net.URLPermission;24/*25* @test26* @bug 801046427* @modules jdk.httpserver28* @library /test/lib29* @build jdk.test.lib.net.SimpleSSLContext30* @run main/othervm -Djava.security.manager=allow URLTest31* @run main/othervm -Djava.security.manager=allow -Djava.net.preferIPv6Addresses=true URLTest32* @summary check URLPermission with Http(s)URLConnection33*/3435import java.net.*;36import java.io.*;37import java.security.*;38import java.util.concurrent.*;39import com.sun.net.httpserver.*;40import javax.net.ssl.*;41import jdk.test.lib.net.SimpleSSLContext;4243public class URLTest {4445static boolean failed;4647public static void main (String[] args) throws Exception {48createServers();4950try {51// Verify without a Security Manager52test1();53test2();54test3();5556// Set the security manager. Each test will set its own policy.57Policy.setPolicy(new CustomPolicy());58System.setSecurityManager(new SecurityManager());59System.out.println("\n Security Manager has been set.");6061test1();62test2();63test3();6465if (failed)66throw new RuntimeException("Test failed");67} finally {68shutdown();69}70}7172static void test1() throws IOException {73System.out.println("\n--- Test 1 ---");7475boolean expectException = false;76SecurityManager sm = System.getSecurityManager();77if (sm != null) {78expectException = true;79Policy.setPolicy(new CustomPolicy(80new URLPermission("http://" + httpAuth + "/foo.html", "GET:X-Foo,Z-Bar"),81new URLPermission("https://" + httpsAuth + "/foo.html", "POST:X-Fob,T-Bar")));82}8384String url1 = "http://" + httpAuth + "/foo.html";85String url2 = "https://" + httpsAuth + "/foo.html";86String url3 = "http://" + httpAuth + "/bar.html";87String url4 = "https://" + httpsAuth + "/bar.html";8889// simple positive test. Should succeed90test(url1, "GET", "X-Foo");91test(url1, "GET", "Z-Bar", "X-Foo");92test(url1, "GET", "X-Foo", "Z-Bar");93test(url1, "GET", "Z-Bar");94test(url2, "POST", "X-Fob");9596// reverse the methods, should fail97test(url1, "POST", "X-Foo", expectException);98test(url2, "GET", "X-Fob", expectException);99100// different URLs, should fail101test(url3, "GET", "X-Foo", expectException);102test(url4, "POST", "X-Fob", expectException);103}104105static void test2() throws IOException {106System.out.println("\n--- Test 2 ---");107108SecurityManager sm = System.getSecurityManager();109if (sm != null) {110Policy.setPolicy(new CustomPolicy(111new URLPermission("http://" + httpAuth + "/*", "GET:X-Foo"),112new URLPermission("https://" + httpsAuth + "/*", "POST:X-Fob")));113}114115String url1 = "http://" + httpAuth + "/foo.html";116String url2 = "https://" + httpsAuth + "/foo.html";117String url3 = "http://" + httpAuth + "/bar.html";118String url4 = "https://" + httpsAuth + "/bar.html";119120// simple positive test. Should succeed121test(url1, "GET", "X-Foo");122test(url2, "POST", "X-Fob");123test(url3, "GET", "X-Foo");124test(url4, "POST", "X-Fob");125}126127static void test3() throws IOException {128System.out.println("\n--- Test 3 ---");129130boolean expectException = false;131SecurityManager sm = System.getSecurityManager();132if (sm != null) {133expectException = true;134Policy.setPolicy(new CustomPolicy(135new URLPermission("http://" + httpAuth + "/a/b/-", "DELETE,GET:X-Foo,Y-Foo"),136new URLPermission("https://" + httpsAuth + "/a/c/-", "POST:*")));137}138139String url1 = "http://" + httpAuth + "/foo.html";140String url2 = "https://" + httpsAuth + "/a/c/d/e/foo.html";141String url3 = "http://" + httpAuth + "/a/b/c";142String url4 = "https://" + httpsAuth + "/a/b/c";143144test(url1, "GET", "X-Foo", expectException);145test(url2, "POST", "X-Zxc");146test(url3, "DELETE", "Y-Foo");147test(url4, "POST", "Y-Foo", expectException);148}149150static String authority(InetSocketAddress address) {151String hostaddr = address.getAddress().getHostAddress();152int port = address.getPort();153if (hostaddr.indexOf(':') > -1) {154return "[" + hostaddr + "]:" + port;155} else {156return hostaddr + ":" + port;157}158}159160// Convenience methods to simplify previous explicit test scenarios.161static void test(String u, String method, String header) throws IOException {162test(u, method, header, null, false);163}164165static void test(String u, String method, String header, boolean expectException)166throws IOException167{168test(u, method, header, null, expectException);169}170171static void test(String u, String method, String header1, String header2)172throws IOException173{174test(u, method, header1, header2, false);175}176177static void test(String u,178String method,179String header1,180String header2,181boolean expectException)182throws IOException183{184URL url = new URL(u);185System.out.println("url=" + u + " method=" + method +186" header1=" + header1 + " header2=" + header2 +187" expectException=" + expectException);188HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY);189if (urlc instanceof HttpsURLConnection) {190HttpsURLConnection ssl = (HttpsURLConnection)urlc;191ssl.setHostnameVerifier((host, sess) -> true);192ssl.setSSLSocketFactory(ctx.getSocketFactory());193}194urlc.setRequestMethod(method);195if (header1 != null)196urlc.addRequestProperty(header1, "foo");197if (header2 != null)198urlc.addRequestProperty(header2, "bar");199200try {201int code = urlc.getResponseCode();202if (expectException) {203failed = true;204System.out.println("FAIL");205return;206}207if (code != 200)208throw new RuntimeException("Unexpected response " + code);209210InputStream is = urlc.getInputStream();211is.readAllBytes();212is.close();213} catch (RuntimeException e) {214if (!expectException || !(e.getCause() instanceof SecurityException)) {215System.out.println ("FAIL. Unexpected: " + e.getMessage());216e.printStackTrace();217failed = true;218return;219} else {220System.out.println("Got expected exception: " + e.getMessage());221}222}223System.out.println ("PASS");224}225226static HttpServer httpServer;227static HttpsServer httpsServer;228static HttpContext c, cs;229static ExecutorService e, es;230static SSLContext ctx;231static int httpPort;232static int httpsPort;233static String httpAuth;234static String httpsAuth;235236static void createServers() throws Exception {237InetAddress loopback = InetAddress.getLoopbackAddress();238InetSocketAddress address = new InetSocketAddress(loopback, 0);239httpServer = HttpServer.create(address, 0);240httpsServer = HttpsServer.create(address, 0);241242OkHandler h = new OkHandler();243244c = httpServer.createContext("/", h);245cs = httpsServer.createContext("/", h);246e = Executors.newCachedThreadPool();247es = Executors.newCachedThreadPool();248httpServer.setExecutor(e);249httpsServer.setExecutor(es);250251ctx = new SimpleSSLContext().get();252httpsServer.setHttpsConfigurator(new HttpsConfigurator (ctx));253254httpServer.start();255httpsServer.start();256257httpPort = httpServer.getAddress().getPort();258httpsPort = httpsServer.getAddress().getPort();259httpAuth = authority(httpServer.getAddress());260httpsAuth = authority(httpsServer.getAddress());261}262263static void shutdown() {264httpServer.stop(1);265httpsServer.stop(1);266e.shutdown();267es.shutdown();268}269270static class OkHandler implements HttpHandler {271public void handle(HttpExchange x) throws IOException {272x.sendResponseHeaders(200, -1);273x.close();274}275}276277static class CustomPolicy extends Policy {278static final Policy DEFAULT_POLICY = Policy.getPolicy();279final PermissionCollection perms = new Permissions();280281CustomPolicy(Permission... permissions) {282java.util.Arrays.stream(permissions).forEach(perms::add);283284// needed for the HTTP(S) server285InetAddress loopback = InetAddress.getLoopbackAddress();286InetSocketAddress serverBound = new InetSocketAddress(loopback,1024);287perms.add(new SocketPermission(authority(serverBound) + "-", "listen,resolve,accept"));288// needed by the test to reset the policy, per testX method289perms.add(new SecurityPermission("setPolicy"));290// needed to shutdown the ThreadPoolExecutor ( used by the servers )291perms.add(new RuntimePermission("modifyThread"));292// needed by the client code forHttpsURLConnection.setSSLSocketFactory293perms.add(new RuntimePermission("setFactory"));294}295296public PermissionCollection getPermissions(ProtectionDomain domain) {297return perms;298}299300public PermissionCollection getPermissions(CodeSource codesource) {301return perms;302}303304public boolean implies(ProtectionDomain domain, Permission perm) {305return perms.implies(perm) || DEFAULT_POLICY.implies(domain, perm);306}307}308}309310311