Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLPermission/URLTest.java
47217 views
/*1* Copyright (c) 2013, 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* Run the tests once without security manager and once with26*27* @test28* @bug 801046429* @compile ../../../com/sun/net/httpserver/SimpleSSLContext.java30* @run main/othervm/policy=policy.1 URLTest one31* @run main/othervm URLTest one32* @run main/othervm/policy=policy.2 URLTest two33* @run main/othervm URLTest two34* @run main/othervm/policy=policy.3 URLTest three35* @run main/othervm URLTest three36*/3738import java.net.*;39import java.io.*;40import java.util.*;41import java.util.concurrent.*;42import java.util.logging.*;43import com.sun.net.httpserver.*;44import javax.net.ssl.*;4546public class URLTest {47static boolean failed = false;4849public static void main (String[] args) throws Exception {50boolean no = false, yes = true;5152if (System.getSecurityManager() == null) {53yes = false;54}55createServers();56InetSocketAddress addr1 = httpServer.getAddress();57int port1 = addr1.getPort();58InetSocketAddress addr2 = httpsServer.getAddress();59int port2 = addr2.getPort();6061// each of the following cases is run with a different policy file6263switch (args[0]) {64case "one":65String url1 = "http://127.0.0.1:"+ port1 + "/foo.html";66String url2 = "https://127.0.0.1:"+ port2 + "/foo.html";67String url3 = "http://127.0.0.1:"+ port1 + "/bar.html";68String url4 = "https://127.0.0.1:"+ port2 + "/bar.html";6970// simple positive test. Should succceed71test(url1, "GET", "X-Foo", no);72test(url1, "GET", "Z-Bar", "X-Foo", no);73test(url1, "GET", "X-Foo", "Z-Bar", no);74test(url1, "GET", "Z-Bar", no);75test(url2, "POST", "X-Fob", no);7677// reverse the methods, should fail78test(url1, "POST", "X-Foo", yes);79test(url2, "GET", "X-Fob", yes);8081// different URLs, should fail82test(url3, "GET", "X-Foo", yes);83test(url4, "POST", "X-Fob", yes);84break;8586case "two":87url1 = "http://127.0.0.1:"+ port1 + "/foo.html";88url2 = "https://127.0.0.1:"+ port2 + "/foo.html";89url3 = "http://127.0.0.1:"+ port1 + "/bar.html";90url4 = "https://127.0.0.1:"+ port2 + "/bar.html";9192// simple positive test. Should succceed93test(url1, "GET", "X-Foo", no);94test(url2, "POST", "X-Fob", no);95test(url3, "GET", "X-Foo", no);96test(url4, "POST", "X-Fob", no);97break;9899case "three":100url1 = "http://127.0.0.1:"+ port1 + "/foo.html";101url2 = "https://127.0.0.1:"+ port2 + "/a/c/d/e/foo.html";102url3 = "http://127.0.0.1:"+ port1 + "/a/b/c";103url4 = "https://127.0.0.1:"+ port2 + "/a/b/c";104105test(url1, "GET", "X-Foo", yes);106test(url2, "POST", "X-Zxc", no);107test(url3, "DELETE", "Y-Foo", no);108test(url4, "POST", "Y-Foo", yes);109break;110}111shutdown();112if (failed) {113throw new RuntimeException("Test failed");114}115}116117public static void test (118String u, String method,119String header, boolean exceptionExpected120)121throws Exception122{123test(u, method, header, null, exceptionExpected);124}125126public static void test (127String u, String method,128String header1, String header2, boolean exceptionExpected129)130throws Exception131{132URL url = new URL(u);133System.out.println ("url=" + u + " method="+method + " header1="+header1134+" header2 = " + header2135+" exceptionExpected="+exceptionExpected);136HttpURLConnection urlc = (HttpURLConnection)url.openConnection();137if (urlc instanceof HttpsURLConnection) {138HttpsURLConnection ssl = (HttpsURLConnection)urlc;139ssl.setHostnameVerifier(new HostnameVerifier() {140public boolean verify(String host, SSLSession sess) {141return true;142}143});144ssl.setSSLSocketFactory (ctx.getSocketFactory());145}146urlc.setRequestMethod(method);147if (header1 != null) {148urlc.addRequestProperty(header1, "foo");149}150if (header2 != null) {151urlc.addRequestProperty(header2, "bar");152}153try {154int g = urlc.getResponseCode();155if (exceptionExpected) {156failed = true;157System.out.println ("FAIL");158return;159}160if (g != 200) {161String s = Integer.toString(g);162throw new RuntimeException("unexpected response "+ s);163}164InputStream is = urlc.getInputStream();165int c,count=0;166byte[] buf = new byte[1024];167while ((c=is.read(buf)) != -1) {168count += c;169}170is.close();171} catch (RuntimeException e) {172if (! (e instanceof SecurityException) &&173!(e.getCause() instanceof SecurityException) ||174!exceptionExpected)175{176System.out.println ("FAIL");177//e.printStackTrace();178failed = true;179}180}181System.out.println ("OK");182}183184static HttpServer httpServer;185static HttpsServer httpsServer;186static HttpContext c, cs;187static ExecutorService e, es;188static SSLContext ctx;189190// These ports need to be hard-coded until we support port number191// ranges in the permission class192193static final int PORT1 = 12567;194static final int PORT2 = 12568;195196static void createServers() throws Exception {197InetSocketAddress addr1 = new InetSocketAddress (PORT1);198InetSocketAddress addr2 = new InetSocketAddress (PORT2);199httpServer = HttpServer.create (addr1, 0);200httpsServer = HttpsServer.create (addr2, 0);201202MyHandler h = new MyHandler();203204c = httpServer.createContext ("/", h);205cs = httpsServer.createContext ("/", h);206e = Executors.newCachedThreadPool();207es = Executors.newCachedThreadPool();208httpServer.setExecutor (e);209httpsServer.setExecutor (es);210211// take the keystore from elsewhere in test hierarchy212String keysdir = System.getProperty("test.src")213+ "/../../../com/sun/net/httpserver/";214ctx = new SimpleSSLContext(keysdir).get();215httpsServer.setHttpsConfigurator(new HttpsConfigurator (ctx));216217httpServer.start();218httpsServer.start();219}220221static void shutdown() {222httpServer.stop(1);223httpsServer.stop(1);224e.shutdown();225es.shutdown();226}227228static class MyHandler implements HttpHandler {229230MyHandler() {231}232233public void handle(HttpExchange x) throws IOException {234x.sendResponseHeaders(200, -1);235x.close();236}237}238239}240241242