Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/URLPermission/nstest/LookupTest.java
47490 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*/2223/**24* This is a simple smoke test of the HttpURLPermission mechanism, which25* checks for either IOException (due to unknown host) or SecurityException26* due to lack of permission to connect27*/2829import java.net.*;30import java.io.*;31import jdk.testlibrary.Utils;3233public class LookupTest {3435static void test(36String url, boolean throwsSecException, boolean throwsIOException)37{38try {39URL u = new URL(url);40System.err.println ("Connecting to " + u);41URLConnection urlc = u.openConnection();42InputStream is = urlc.getInputStream();43} catch (SecurityException e) {44if (!throwsSecException) {45throw new RuntimeException ("(1) was not expecting ", e);46}47return;48} catch (IOException ioe) {49if (!throwsIOException) {50throw new RuntimeException ("(2) was not expecting ", ioe);51}52return;53}54if (throwsSecException || throwsIOException) {55System.err.printf ("was expecting a %s\n", throwsSecException ?56"security exception" : "IOException");57throw new RuntimeException("was expecting an exception");58}59}6061static int port;62static ServerSocket serverSocket;6364public static void main(String args[]) throws Exception {65String cmd = args[0];66if (cmd.equals("-getport")) {67port = Utils.getFreePort();68System.out.print(port);69} else if (cmd.equals("-runtest")) {70port = Integer.parseInt(args[1]);71SimpleNameService.put("allowedAndFound.com", "127.0.0.1");72SimpleNameService.put("notAllowedButFound.com", "99.99.99.99");73// name "notAllowedAndNotFound.com" is not in map74// name "allowedButNotfound.com" is not in map75try {76startServer();7778System.setSecurityManager(new SecurityManager());7980test("http://allowedAndFound.com:" + port + "/foo", false, false);8182test("http://notAllowedButFound.com:" + port + "/foo", true, false);8384test("http://allowedButNotfound.com:" + port + "/foo", false, true);8586test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false);87} finally {88serverSocket.close();89}90} else {91throw new RuntimeException("Bad invocation: " + cmd);92}93}9495static Thread server;9697static class Server extends Thread {98public void run() {99byte[] buf = new byte[1000];100try {101while (true) {102Socket s = serverSocket.accept();103InputStream i = s.getInputStream();104i.read(buf);105OutputStream o = s.getOutputStream();106String rsp = "HTTP/1.1 200 Ok\r\n" +107"Connection: close\r\nContent-length: 0\r\n\r\n";108o.write(rsp.getBytes());109o.close();110}111} catch (IOException e) {112return;113}114}115}116117static void startServer() {118try {119serverSocket = new ServerSocket(port);120server = new Server();121server.start();122} catch (Exception e) {123throw new RuntimeException ("Test failed to initialize", e);124}125}126}127128129