1 /*
   2  * Copyright (c) 2005, 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  * @test
  26  * @bug 6268315
  27  * @summary Configuration should be provider-based
  28  * @build GetInstanceConfigSpi GetInstanceProvider
  29  * @run main/othervm/policy=GetInstanceSecurity.policy GetInstanceSecurity
  30  */
  31 
  32 import java.io.File;
  33 import java.net.URI;
  34 import java.security.*;
  35 import javax.security.auth.login.*;
  36 
  37 public class GetInstanceSecurity {
  38 
  39     private static final String JAVA_CONFIG = "JavaLoginConfig";
  40 
  41     public static void main(String[] args) throws Exception {
  42         try {
  43             Configuration c = Configuration.getInstance(JAVA_CONFIG, null);
  44             throw new RuntimeException("did not catch security exception");
  45         } catch (SecurityException se) {
  46             // good
  47         }
  48 
  49         try {
  50             Configuration c = Configuration.getInstance
  51                         (JAVA_CONFIG, null, "SUN");
  52             throw new RuntimeException("did not catch security exception");
  53         } catch (SecurityException se) {
  54             // good
  55         }
  56 
  57         try {
  58             Configuration c = Configuration.getInstance
  59                         (JAVA_CONFIG, null, Security.getProvider("SUN"));
  60             throw new RuntimeException("did not catch security exception");
  61         } catch (SecurityException se) {
  62             // good
  63         }
  64 
  65         // set a new policy that grants the perms, and then re-check perms
  66 
  67         File file = new File(System.getProperty("test.src", "."),
  68                                 "GetInstanceSecurity.grantedPolicy");
  69         URI uri = file.toURI();
  70         URIParameter param = new URIParameter(uri);
  71         Policy p = Policy.getInstance("JavaPolicy", param, "SUN");
  72         Policy.setPolicy(p);
  73 
  74         // retry operations
  75 
  76         file = new File(System.getProperty("test.src", "."),
  77                         "GetInstance.config");
  78         URIParameter uriParam = new URIParameter(file.toURI());
  79 
  80         try {
  81             Configuration c = Configuration.getInstance(JAVA_CONFIG, uriParam);
  82         } catch (SecurityException se) {
  83             throw new RuntimeException("unexpected SecurityException");
  84         }
  85 
  86         try {
  87             Configuration c = Configuration.getInstance
  88                         (JAVA_CONFIG, uriParam, "SUN");
  89             // good
  90         } catch (SecurityException se) {
  91             throw new RuntimeException("unexpected SecurityException");
  92         }
  93 
  94         try {
  95             Configuration c = Configuration.getInstance
  96                         (JAVA_CONFIG, uriParam, Security.getProvider("SUN"));
  97             // good
  98         } catch (SecurityException se) {
  99             throw new RuntimeException("unexpected SecurityException");
 100         }
 101 
 102         System.out.println("test passed");
 103     }
 104 }