LookupTest.java revision 15491:6f390eafc676
1/* 2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/** 25 * This is a simple smoke test of the HttpURLPermission mechanism, which 26 * checks for either IOException (due to unknown host) or SecurityException 27 * due to lack of permission to connect 28 */ 29 30import java.net.*; 31import java.io.*; 32import jdk.testlibrary.Utils; 33 34public class LookupTest { 35 36 static void test( 37 String url, boolean throwsSecException, boolean throwsIOException) 38 { 39 try { 40 URL u = new URL(url); 41 System.err.println ("Connecting to " + u); 42 URLConnection urlc = u.openConnection(); 43 InputStream is = urlc.getInputStream(); 44 } catch (SecurityException e) { 45 if (!throwsSecException) { 46 throw new RuntimeException ("(1) was not expecting ", e); 47 } 48 return; 49 } catch (IOException ioe) { 50 if (!throwsIOException) { 51 throw new RuntimeException ("(2) was not expecting ", ioe); 52 } 53 return; 54 } 55 if (throwsSecException || throwsIOException) { 56 System.err.printf ("was expecting a %s\n", throwsSecException ? 57 "security exception" : "IOException"); 58 throw new RuntimeException("was expecting an exception"); 59 } 60 } 61 62 static int port; 63 static ServerSocket serverSocket; 64 65 public static void main(String args[]) throws Exception { 66 67 68 String cmd = args[0]; 69 if (cmd.equals("-getport")) { 70 port = Utils.getFreePort(); 71 System.out.print(port); 72 } else if (cmd.equals("-runtest")) { 73 port = Integer.parseInt(args[1]); 74 String hostsFileName = System.getProperty("test.src", ".") + "/LookupTestHosts"; 75 System.setProperty("jdk.net.hosts.file", hostsFileName); 76 addMappingToHostsFile("allowedAndFound.com", "127.0.0.1", hostsFileName, false); 77 addMappingToHostsFile("notAllowedButFound.com", "99.99.99.99", hostsFileName, true); 78 // name "notAllowedAndNotFound.com" is not in map 79 // name "allowedButNotfound.com" is not in map 80 try { 81 startServer(); 82 83 System.setSecurityManager(new SecurityManager()); 84 85 test("http://allowedAndFound.com:" + port + "/foo", false, false); 86 87 test("http://notAllowedButFound.com:" + port + "/foo", true, false); 88 89 test("http://allowedButNotfound.com:" + port + "/foo", false, true); 90 91 test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false); 92 } finally { 93 serverSocket.close(); 94 } 95 } else { 96 throw new RuntimeException("Bad invocation: " + cmd); 97 } 98 } 99 100 static Thread server; 101 102 static class Server extends Thread { 103 public void run() { 104 byte[] buf = new byte[1000]; 105 try { 106 while (true) { 107 Socket s = serverSocket.accept(); 108 InputStream i = s.getInputStream(); 109 i.read(buf); 110 OutputStream o = s.getOutputStream(); 111 String rsp = "HTTP/1.1 200 Ok\r\n" + 112 "Connection: close\r\nContent-length: 0\r\n\r\n"; 113 o.write(rsp.getBytes()); 114 o.close(); 115 } 116 } catch (IOException e) { 117 return; 118 } 119 } 120 } 121 122 static void startServer() { 123 try { 124 serverSocket = new ServerSocket(port); 125 server = new Server(); 126 server.start(); 127 } catch (Exception e) { 128 throw new RuntimeException ("Test failed to initialize", e); 129 } 130 } 131 132 private static void addMappingToHostsFile (String host, 133 String addr, 134 String hostsFileName, 135 boolean append) 136 throws Exception { 137 String mapping = addr + " " + host; 138 try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter( 139 new FileWriter(hostsFileName, append)))) { 140 hfPWriter.println(mapping); 141} 142 } 143 144} 145