0N/A/*
2362N/A * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 5100561
0N/A * @bug 6273812
0N/A * @summary Can not explicitly create a java.security.Policy object from a file
0N/A * @build GetInstancePolicySpi GetInstanceProvider
0N/A * @run main/othervm/policy=GetInstance.policy GetInstance
0N/A */
0N/A
0N/Aimport java.security.*;
0N/A
0N/Aimport java.io.File;
0N/Aimport java.net.URI;
0N/Aimport sun.net.www.ParseUtil;
0N/A
0N/Apublic class GetInstance {
0N/A
0N/A private static final String JAVA_POLICY = "JavaPolicy";
0N/A
0N/A private static class BadParam implements Policy.Parameters { }
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A
0N/A int testnum = 1;
0N/A GetInstance gi = new GetInstance();
0N/A
0N/A testnum = gi.testDefault(testnum);
0N/A testnum = gi.testStringProvider(testnum);
0N/A testnum = gi.testProvider(testnum);
0N/A testnum = gi.testCustomImpl(testnum);
0N/A testnum = gi.testBadParam(testnum);
0N/A
0N/A // make this go last because we don't want to leave its policy set
0N/A // for other tests
0N/A testnum = gi.testURIParam(testnum);
0N/A }
0N/A
0N/A private int testDefault(int testnum) throws Exception {
0N/A // get an instance of the default PolicySpiFile
0N/A Policy p = Policy.getInstance(JAVA_POLICY, null);
0N/A doTest(p, testnum++);
0N/A Policy.setPolicy(p);
0N/A doTestSM(testnum++);
0N/A
0N/A // get an instance of FooPolicy
0N/A try {
0N/A p = Policy.getInstance("FooPolicy", null);
0N/A throw new SecurityException("test " + testnum++ + " failed");
0N/A } catch (NoSuchAlgorithmException nsae) {
0N/A // good
0N/A System.out.println("test " + testnum++ + " passed");
0N/A }
0N/A
0N/A return testnum;
0N/A }
0N/A
0N/A private int testStringProvider(int testnum) throws Exception {
0N/A // get an instance of JavaPolicy from SUN
0N/A Policy p = Policy.getInstance(JAVA_POLICY, null, "SUN");
0N/A doTest(p, testnum++);
0N/A Policy.setPolicy(p);
0N/A doTestSM(testnum++);
0N/A
0N/A // get an instance of JavaPolicy from SunRsaSign
0N/A try {
0N/A p = Policy.getInstance(JAVA_POLICY, null, "SunRsaSign");
0N/A throw new SecurityException("test " + testnum++ + " failed");
0N/A } catch (NoSuchAlgorithmException nsae) {
0N/A // good
0N/A System.out.println("test " + testnum++ + " passed");
0N/A }
0N/A
0N/A // get an instance of JavaPolicy from FOO
0N/A try {
0N/A p = Policy.getInstance(JAVA_POLICY, null, "FOO");
0N/A throw new SecurityException("test " + testnum++ + " failed");
0N/A } catch (NoSuchProviderException nspe) {
0N/A // good
0N/A System.out.println("test " + testnum++ + " passed");
0N/A }
0N/A
0N/A return testnum;
0N/A }
0N/A
0N/A private int testProvider(int testnum) throws Exception {
0N/A // get an instance of JavaPolicy from SUN
0N/A Policy p = Policy.getInstance(JAVA_POLICY,
0N/A null,
0N/A Security.getProvider("SUN"));
0N/A doTest(p, testnum++);
0N/A Policy.setPolicy(p);
0N/A doTestSM(testnum++);
0N/A
0N/A // get an instance of JavaPolicy from SunRsaSign
0N/A try {
0N/A p = Policy.getInstance(JAVA_POLICY,
0N/A null,
0N/A Security.getProvider("SunRsaSign"));
0N/A throw new SecurityException("test " + testnum++ + " failed");
0N/A } catch (NoSuchAlgorithmException nsae) {
0N/A // good
0N/A System.out.println("test " + testnum++ + " passed");
0N/A }
0N/A
0N/A return testnum;
0N/A }
0N/A
0N/A private int testBadParam(int testnum) throws Exception {
0N/A
0N/A // pass bad param
0N/A
0N/A try {
0N/A Policy p = Policy.getInstance(JAVA_POLICY,
0N/A new BadParam());
0N/A throw new SecurityException("test " + testnum++ + " failed");
0N/A } catch (IllegalArgumentException iae) {
0N/A // good
0N/A System.out.println("test " + testnum++ + " passed");
0N/A }
0N/A
0N/A try {
0N/A Policy p = Policy.getInstance(JAVA_POLICY,
0N/A new BadParam(),
0N/A "SUN");
0N/A throw new SecurityException("test " + testnum++ + " failed");
0N/A } catch (IllegalArgumentException iae) {
0N/A // good
0N/A System.out.println("test " + testnum++ + " passed");
0N/A }
0N/A
0N/A try {
0N/A Policy p = Policy.getInstance(JAVA_POLICY,
0N/A new BadParam(),
0N/A Security.getProvider("SUN"));
0N/A throw new SecurityException("test " + testnum++ + " failed");
0N/A } catch (IllegalArgumentException iae) {
0N/A // good
0N/A System.out.println("test " + testnum++ + " passed");
0N/A }
0N/A
0N/A return testnum;
0N/A }
0N/A
0N/A private int testURIParam(int testnum) throws Exception {
0N/A // get an instance of JavaPolicy from SUN and have it read from the URL
0N/A
0N/A File file = new File(System.getProperty("test.src", "."),
0N/A "GetInstance.policyURL");
0N/A URI uri = file.toURI();
0N/A Policy p = Policy.getInstance(JAVA_POLICY, new URIParameter(uri));
0N/A
0N/A doTest(p, testnum++);
0N/A Policy.setPolicy(p);
0N/A doTestSM(testnum++);
0N/A
0N/A return testnum;
0N/A }
0N/A
0N/A private int testCustomImpl(int testnum) throws Exception {
0N/A Provider customProvider = new GetInstanceProvider();
0N/A Policy p = Policy.getInstance("GetInstancePolicySpi",
0N/A null,
0N/A customProvider);
0N/A
0N/A // doTest has a case that will not work with custom policies,
0N/A // so do not call it
0N/A //
0N/A // doTest(p, testnum++);
0N/A
0N/A Policy.setPolicy(p);
0N/A doTestSM(testnum++);
0N/A
0N/A return testnum;
0N/A }
0N/A
0N/A private void doTest(Policy p, int testnum) throws Exception {
0N/A
0N/A // check granted perm
0N/A if (p.implies(this.getClass().getProtectionDomain(),
0N/A new SecurityPermission("GetInstanceTest"))) {
0N/A System.out.println("test " + testnum + ".1 passed");
0N/A } else {
0N/A throw new SecurityException("test " + testnum + ".1 failed");
0N/A }
0N/A
0N/A // check perm not granted
0N/A if (p.implies(this.getClass().getProtectionDomain(),
0N/A new SecurityPermission("NotGranted"))) {
0N/A throw new SecurityException("test " + testnum + ".2 failed");
0N/A } else {
0N/A System.out.println("test " + testnum + ".2 passed");
0N/A }
0N/A
0N/A // test getProvider
0N/A if ("SUN".equals(p.getProvider().getName())) {
0N/A System.out.println("test " + testnum + ".3 passed");
0N/A } else {
0N/A throw new SecurityException("test " + testnum + ".3 failed");
0N/A }
0N/A
0N/A // test getType
0N/A if (JAVA_POLICY.equals(p.getType())) {
0N/A System.out.println("test " + testnum + ".4 passed");
0N/A } else {
0N/A throw new SecurityException("test " + testnum + ".4 failed");
0N/A }
0N/A }
0N/A
0N/A private void doTestSM(int testnum) throws Exception {
0N/A
0N/A // check granted perm
0N/A System.getSecurityManager().checkPermission
0N/A (new SecurityPermission("GetInstanceTest"));
0N/A System.out.println("test " + testnum + ".1 passed");
0N/A
0N/A // check perm not granted
0N/A try {
0N/A System.getSecurityManager().checkPermission
0N/A (new SecurityPermission("NotGranted"));
0N/A throw new RuntimeException("test " + testnum + ".2 failed");
0N/A } catch (SecurityException se) {
0N/A System.out.println("test " + testnum + ".2 passed");
0N/A }
0N/A }
0N/A}