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 30 import java.net.*; 31 import java.io.*; 32 import jdk.testlibrary.Utils; 33 34 public class LookupTest { 35 36 static void test( 37 String url, boolean throwsSecException, boolean throwsIOException) 38 { 39 try { 40 ProxySelector.setDefault(null); 41 URL u = new URL(url); 42 System.err.println ("Connecting to " + u); 43 URLConnection urlc = u.openConnection(); 44 InputStream is = urlc.getInputStream(); 45 } catch (SecurityException e) { 46 if (!throwsSecException) { 47 throw new RuntimeException ("(1) was not expecting ", e); 48 } 49 return; 50 } catch (IOException ioe) { 51 if (!throwsIOException) { 52 throw new RuntimeException ("(2) was not expecting ", ioe); 53 } 54 return; 55 } 56 if (throwsSecException || throwsIOException) { 57 System.err.printf ("was expecting a %s\n", throwsSecException ? 58 "security exception" : "IOException"); 59 throw new RuntimeException("was expecting an exception"); 60 } 61 } 62 63 static int port; 64 static ServerSocket serverSocket; 65 66 public static void main(String args[]) throws Exception { 67 68 69 String cmd = args[0]; 70 if (cmd.equals("-getport")) { 71 port = Utils.getFreePort(); 72 System.out.print(port); 73 } else if (cmd.equals("-runtest")) { 74 port = Integer.parseInt(args[1]); 75 String hostsFileName = System.getProperty("user.dir", ".") + "/LookupTestHosts"; 76 System.setProperty("jdk.net.hosts.file", hostsFileName); 77 addMappingToHostsFile("allowedAndFound.com", "127.0.0.1", hostsFileName, false); 78 addMappingToHostsFile("notAllowedButFound.com", "99.99.99.99", hostsFileName, true); 79 // name "notAllowedAndNotFound.com" is not in map 80 // name "allowedButNotfound.com" is not in map 81 try { 82 startServer(); 83 84 System.setSecurityManager(new SecurityManager()); 85 86 test("http://allowedAndFound.com:" + port + "/foo", false, false); 87 88 test("http://notAllowedButFound.com:" + port + "/foo", true, false); 89 90 test("http://allowedButNotfound.com:" + port + "/foo", false, true); 91 92 test("http://notAllowedAndNotFound.com:" + port + "/foo", true, false); 93 } finally { 94 serverSocket.close(); 95 } 96 } else { 97 throw new RuntimeException("Bad invocation: " + cmd); 98 } 99 } 100 101 static Thread server; 102 103 static class Server extends Thread { 104 public void run() { 105 byte[] buf = new byte[1000]; 106 try { 107 while (true) { 108 Socket s = serverSocket.accept(); 109 InputStream i = s.getInputStream(); 110 i.read(buf); 111 OutputStream o = s.getOutputStream(); 112 String rsp = "HTTP/1.1 200 Ok\r\n" + 113 "Connection: close\r\nContent-length: 0\r\n\r\n"; 114 o.write(rsp.getBytes()); 115 o.close(); 116 } 117 } catch (IOException e) { 118 return; 119 } 120 } 121 } 122 123 static void startServer() { 124 try { 125 serverSocket = new ServerSocket(port); 126 server = new Server(); 127 server.start(); 128 } catch (Exception e) { 129 throw new RuntimeException ("Test failed to initialize", e); 130 } 131 } 132 133 private static void addMappingToHostsFile (String host, 134 String addr, 135 String hostsFileName, 136 boolean append) 137 throws Exception { 138 String mapping = addr + " " + host; 139 try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter( 140 new FileWriter(hostsFileName, append)))) { 141 hfPWriter.println(mapping); 142 } 143 } 144 145 }