< prev index next >

test/jdk/java/net/URLPermission/nstest/LookupTest.java

Print this page
rev 47439 : 8189291: Test policy should extend the default system policy
Reviewed-by:
   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  */


  37 import java.io.OutputStream;
  38 import java.io.PrintWriter;
  39 import java.net.NetPermission;
  40 import java.net.ProxySelector;
  41 import java.net.ServerSocket;
  42 import java.net.Socket;
  43 import java.net.SocketPermission;
  44 import java.net.URL;
  45 import java.net.URLConnection;
  46 import java.net.URLPermission;
  47 import java.security.CodeSource;
  48 import java.security.Permission;
  49 import java.security.PermissionCollection;
  50 import java.security.Permissions;
  51 import java.security.Policy;
  52 import java.security.ProtectionDomain;
  53 import static java.nio.charset.StandardCharsets.US_ASCII;
  54 
  55 public class LookupTest {
  56 

  57     static int port;
  58     static volatile ServerSocket serverSocket;
  59 
  60     static void test(String url,
  61                      boolean throwsSecException,
  62                      boolean throwsIOException) {
  63         ProxySelector.setDefault(null);
  64         URL u;
  65         InputStream is = null;
  66         try {
  67             u = new URL(url);
  68             System.err.println("Connecting to " + u);
  69             URLConnection urlc = u.openConnection();
  70             is = urlc.getInputStream();
  71         } catch (SecurityException e) {
  72             if (!throwsSecException) {
  73                 throw new RuntimeException("Unexpected SecurityException:", e);
  74             }
  75             return;
  76         } catch (IOException e) {


 193         final PermissionCollection perms = new Permissions();
 194 
 195         LookupTestPolicy() throws Exception {
 196             perms.add(new NetPermission("setProxySelector"));
 197             perms.add(new SocketPermission("localhost:1024-", "resolve,accept"));
 198             perms.add(new URLPermission("http://allowedAndFound.com:" + port + "/-", "*:*"));
 199             perms.add(new URLPermission("http://allowedButNotfound.com:" + port + "/-", "*:*"));
 200             perms.add(new FilePermission("<<ALL FILES>>", "read,write,delete"));
 201             //perms.add(new PropertyPermission("java.io.tmpdir", "read"));
 202         }
 203 
 204         public PermissionCollection getPermissions(ProtectionDomain domain) {
 205             return perms;
 206         }
 207 
 208         public PermissionCollection getPermissions(CodeSource codesource) {
 209             return perms;
 210         }
 211 
 212         public boolean implies(ProtectionDomain domain, Permission perm) {
 213             return perms.implies(perm);
 214         }
 215     }
 216 }
   1 /*
   2  * Copyright (c) 2013, 2017, 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  */


  37 import java.io.OutputStream;
  38 import java.io.PrintWriter;
  39 import java.net.NetPermission;
  40 import java.net.ProxySelector;
  41 import java.net.ServerSocket;
  42 import java.net.Socket;
  43 import java.net.SocketPermission;
  44 import java.net.URL;
  45 import java.net.URLConnection;
  46 import java.net.URLPermission;
  47 import java.security.CodeSource;
  48 import java.security.Permission;
  49 import java.security.PermissionCollection;
  50 import java.security.Permissions;
  51 import java.security.Policy;
  52 import java.security.ProtectionDomain;
  53 import static java.nio.charset.StandardCharsets.US_ASCII;
  54 
  55 public class LookupTest {
  56 
  57     static final Policy DEFAULT_POLICY = Policy.getPolicy();
  58     static int port;
  59     static volatile ServerSocket serverSocket;
  60 
  61     static void test(String url,
  62                      boolean throwsSecException,
  63                      boolean throwsIOException) {
  64         ProxySelector.setDefault(null);
  65         URL u;
  66         InputStream is = null;
  67         try {
  68             u = new URL(url);
  69             System.err.println("Connecting to " + u);
  70             URLConnection urlc = u.openConnection();
  71             is = urlc.getInputStream();
  72         } catch (SecurityException e) {
  73             if (!throwsSecException) {
  74                 throw new RuntimeException("Unexpected SecurityException:", e);
  75             }
  76             return;
  77         } catch (IOException e) {


 194         final PermissionCollection perms = new Permissions();
 195 
 196         LookupTestPolicy() throws Exception {
 197             perms.add(new NetPermission("setProxySelector"));
 198             perms.add(new SocketPermission("localhost:1024-", "resolve,accept"));
 199             perms.add(new URLPermission("http://allowedAndFound.com:" + port + "/-", "*:*"));
 200             perms.add(new URLPermission("http://allowedButNotfound.com:" + port + "/-", "*:*"));
 201             perms.add(new FilePermission("<<ALL FILES>>", "read,write,delete"));
 202             //perms.add(new PropertyPermission("java.io.tmpdir", "read"));
 203         }
 204 
 205         public PermissionCollection getPermissions(ProtectionDomain domain) {
 206             return perms;
 207         }
 208 
 209         public PermissionCollection getPermissions(CodeSource codesource) {
 210             return perms;
 211         }
 212 
 213         public boolean implies(ProtectionDomain domain, Permission perm) {
 214             return perms.implies(perm) || DEFAULT_POLICY.implies(domain, perm);
 215         }
 216     }
 217 }
< prev index next >