Path: blob/master/test/jdk/java/security/Policy/GetInstance/GetInstance.java
41153 views
/*1* Copyright (c) 2005, 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* @test25* @bug 510056126* @bug 627381227* @summary Can not explicitly create a java.security.Policy object from a file28* @modules java.base/sun.security.provider29* @build GetInstancePolicySpi GetInstanceProvider30* @run main/othervm/policy=GetInstance.policy GetInstance31*/3233import java.security.*;3435import java.io.File;36import java.net.URI;3738public class GetInstance {3940private static final String JAVA_POLICY = "JavaPolicy";4142private static class BadParam implements Policy.Parameters { }4344public static void main(String[] args) throws Exception {4546int testnum = 1;47GetInstance gi = new GetInstance();4849testnum = gi.testDefault(testnum);50testnum = gi.testStringProvider(testnum);51testnum = gi.testProvider(testnum);52testnum = gi.testCustomImpl(testnum);53testnum = gi.testBadParam(testnum);5455// make this go last because we don't want to leave its policy set56// for other tests57testnum = gi.testURIParam(testnum);58}5960private int testDefault(int testnum) throws Exception {61// get an instance of the default PolicySpiFile62Policy p = Policy.getInstance(JAVA_POLICY, null);63doTest(p, testnum++);64Policy.setPolicy(p);65doTestSM(testnum++);6667// get an instance of FooPolicy68try {69p = Policy.getInstance("FooPolicy", null);70throw new SecurityException("test " + testnum++ + " failed");71} catch (NoSuchAlgorithmException nsae) {72// good73System.out.println("test " + testnum++ + " passed");74}7576return testnum;77}7879private int testStringProvider(int testnum) throws Exception {80// get an instance of JavaPolicy from SUN81Policy p = Policy.getInstance(JAVA_POLICY, null, "SUN");82doTest(p, testnum++);83Policy.setPolicy(p);84doTestSM(testnum++);8586// get an instance of JavaPolicy from SunRsaSign87try {88p = Policy.getInstance(JAVA_POLICY, null, "SunRsaSign");89throw new SecurityException("test " + testnum++ + " failed");90} catch (NoSuchAlgorithmException nsae) {91// good92System.out.println("test " + testnum++ + " passed");93}9495// get an instance of JavaPolicy from FOO96try {97p = Policy.getInstance(JAVA_POLICY, null, "FOO");98throw new SecurityException("test " + testnum++ + " failed");99} catch (NoSuchProviderException nspe) {100// good101System.out.println("test " + testnum++ + " passed");102}103104return testnum;105}106107private int testProvider(int testnum) throws Exception {108// get an instance of JavaPolicy from SUN109Policy p = Policy.getInstance(JAVA_POLICY,110null,111Security.getProvider("SUN"));112doTest(p, testnum++);113Policy.setPolicy(p);114doTestSM(testnum++);115116// get an instance of JavaPolicy from SunRsaSign117try {118p = Policy.getInstance(JAVA_POLICY,119null,120Security.getProvider("SunRsaSign"));121throw new SecurityException("test " + testnum++ + " failed");122} catch (NoSuchAlgorithmException nsae) {123// good124System.out.println("test " + testnum++ + " passed");125}126127return testnum;128}129130private int testBadParam(int testnum) throws Exception {131132// pass bad param133134try {135Policy p = Policy.getInstance(JAVA_POLICY,136new BadParam());137throw new SecurityException("test " + testnum++ + " failed");138} catch (IllegalArgumentException iae) {139// good140System.out.println("test " + testnum++ + " passed");141}142143try {144Policy p = Policy.getInstance(JAVA_POLICY,145new BadParam(),146"SUN");147throw new SecurityException("test " + testnum++ + " failed");148} catch (IllegalArgumentException iae) {149// good150System.out.println("test " + testnum++ + " passed");151}152153try {154Policy p = Policy.getInstance(JAVA_POLICY,155new BadParam(),156Security.getProvider("SUN"));157throw new SecurityException("test " + testnum++ + " failed");158} catch (IllegalArgumentException iae) {159// good160System.out.println("test " + testnum++ + " passed");161}162163return testnum;164}165166private int testURIParam(int testnum) throws Exception {167// get an instance of JavaPolicy from SUN and have it read from the URL168169File file = new File(System.getProperty("test.src", "."),170"GetInstance.policyURL");171URI uri = file.toURI();172Policy p = Policy.getInstance(JAVA_POLICY, new URIParameter(uri));173174doTest(p, testnum++);175Policy.setPolicy(p);176doTestSM(testnum++);177178return testnum;179}180181private int testCustomImpl(int testnum) throws Exception {182Provider customProvider = new GetInstanceProvider();183Policy p = Policy.getInstance("GetInstancePolicySpi",184null,185customProvider);186187// doTest has a case that will not work with custom policies,188// so do not call it189//190// doTest(p, testnum++);191192Policy.setPolicy(p);193doTestSM(testnum++);194195return testnum;196}197198private void doTest(Policy p, int testnum) throws Exception {199200// check granted perm201if (p.implies(this.getClass().getProtectionDomain(),202new SecurityPermission("GetInstanceTest"))) {203System.out.println("test " + testnum + ".1 passed");204} else {205throw new SecurityException("test " + testnum + ".1 failed");206}207208// check perm not granted209if (p.implies(this.getClass().getProtectionDomain(),210new SecurityPermission("NotGranted"))) {211throw new SecurityException("test " + testnum + ".2 failed");212} else {213System.out.println("test " + testnum + ".2 passed");214}215216// test getProvider217if ("SUN".equals(p.getProvider().getName())) {218System.out.println("test " + testnum + ".3 passed");219} else {220throw new SecurityException("test " + testnum + ".3 failed");221}222223// test getType224if (JAVA_POLICY.equals(p.getType())) {225System.out.println("test " + testnum + ".4 passed");226} else {227throw new SecurityException("test " + testnum + ".4 failed");228}229}230231private void doTestSM(int testnum) throws Exception {232233// check granted perm234System.getSecurityManager().checkPermission235(new SecurityPermission("GetInstanceTest"));236System.out.println("test " + testnum + ".1 passed");237238// check perm not granted239try {240System.getSecurityManager().checkPermission241(new SecurityPermission("NotGranted"));242throw new RuntimeException("test " + testnum + ".2 failed");243} catch (SecurityException se) {244System.out.println("test " + testnum + ".2 passed");245}246}247}248249250