Path: blob/master/test/jdk/sun/security/provider/PolicyFile/Comparator.java
41153 views
/*1* Copyright (c) 2004, 2017, 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 503700426* @summary Frivolous ClassCastExceptions thrown by SubjectCodeSource.implies27* @modules java.base/sun.security.provider28* @run main/othervm Comparator29*30* Note: if you want to see the java.security.debug output,31* you can not simply set the system property.32* you must run this test by hand and pass -Djava.security.debug=...33*/3435import java.io.*;36import java.security.*;37import java.util.PropertyPermission;38import javax.security.auth.Subject;39import javax.security.auth.x500.X500Principal;4041import sun.security.provider.PolicyFile;42import com.sun.security.auth.UnixPrincipal;43import com.sun.security.auth.NTUserPrincipal;4445public class Comparator {4647private static final PropertyPermission FOO =48new PropertyPermission("foo", "read");49private static final PropertyPermission BAR =50new PropertyPermission("bar", "read");51private static final PropertyPermission FOOBAR =52new PropertyPermission("foobar", "read");53private static final PropertyPermission HELLO =54new PropertyPermission("hello", "read");55private static final PropertyPermission WORLD =56new PropertyPermission("world", "read");5758private static final CodeSource cs =59new CodeSource(null, (java.security.cert.Certificate[])null);6061private static final Principal[] p1 = new Principal[] {62new UnixPrincipal("1") };6364private static final Principal[] p2 = new Principal[] {65new X500Principal("cn=2"),66new NTUserPrincipal("2") };6768private static final Principal[] p3 = new Principal[] {69new UnixPrincipal("1"),70new X500Principal("cn=2"),71new NTUserPrincipal("2") };7273private static final Principal[] p4 = new Principal[] {74new UnixPrincipal("1"),75new NTUserPrincipal("4") };7677private static final Principal[] p5 = new Principal[] {78new UnixPrincipal("1"),79new X500Principal("cn=2"),80new NTUserPrincipal("2"),81new X500Principal("cn=x500") };8283private static final Principal[] p6 = new Principal[] {84new UnixPrincipal("1"),85new NTUserPrincipal("4"),86new X500Principal("cn=x500") };8788private static final Principal[] badP = new Principal[] {89new UnixPrincipal("bad") };9091public static class PCompare1 implements Principal {9293private String name;9495public PCompare1(String name) {96this.name = name;97}9899@Override100public String getName() {101return name;102}103104@Override105public boolean implies (Subject subject) {106if (subject.getPrincipals().contains(p1[0])) {107return true;108}109return false;110}111}112113public static class PCompare2 implements Principal {114private String name;115116public PCompare2(String name) {117this.name = name;118}119120@Override121public String getName() {122return name;123}124125@Override126public boolean implies (Subject subject) {127if (subject.getPrincipals().contains(p2[0]) &&128subject.getPrincipals().contains(p2[1])) {129return true;130}131return false;132}133}134135public static class PCompare3 implements Principal {136private String name;137138public PCompare3(String name) {139this.name = name;140}141142@Override143public String getName() {144return name;145}146147@Override148public boolean implies (Subject subject) {149return false;150}151}152153public static void main(String[] args) throws Exception {154155int testnum = 1;156157// in case we run standalone158String policyDir = System.getProperty("test.src");159if (policyDir == null) {160policyDir = ".";161}162163// do principal-only tests164System.setProperty("java.security.policy",165"=" +166policyDir +167File.separatorChar +168"Comparator.Principal.Policy");169PolicyFile policy = new PolicyFile();170testnum = doPrincipalTest(policy, testnum);171System.out.println("============ Principal Test Passed ============");172173// do comparator-only tests174System.setProperty("java.security.policy",175"=" +176policyDir +177File.separatorChar +178"Comparator.Comparator.Policy");179policy = new PolicyFile();180testnum = doComparatorTest(policy, testnum);181System.out.println("============ Comparator Test Passed ============");182183// combined principal/comparator tests184System.setProperty("java.security.policy",185"=" +186policyDir +187File.separatorChar +188"Comparator.Combined.Policy");189policy = new PolicyFile();190testnum = doCombinedTest(policy, testnum);191System.out.println("============ Combined Test Passed ============");192}193194private static int doBadTest(PolicyFile policy, int testnum) {195196// this principal is not in policy - should not match any policy grants197ProtectionDomain pd = new ProtectionDomain(cs, null, null, badP);198if (policy.implies(pd, FOO)) {199throw new SecurityException("test." + testnum + " failed");200}201testnum++;202203// this principal is not in policy - should not match any policy grants204if (policy.implies(pd, BAR)) {205throw new SecurityException("test." + testnum + " failed");206}207testnum++;208209// this principal is not in policy - should not match any policy grants210if (policy.implies(pd, FOOBAR)) {211throw new SecurityException("test." + testnum + " failed");212}213testnum++;214215return testnum;216}217218private static int doPrincipalTest(PolicyFile policy, int testnum) {219220// security check against one principal should pass221ProtectionDomain pd = new ProtectionDomain(cs, null, null, p1);222if (!policy.implies(pd, FOO)) {223throw new SecurityException("test." + testnum + " failed");224}225testnum++;226227// should not match BAR grant entry in policy228pd = new ProtectionDomain(cs, null, null, p1);229if (policy.implies(pd, BAR)) {230throw new SecurityException("test." + testnum + " failed");231}232testnum++;233234// security check against two principals should pass235pd = new ProtectionDomain(cs, null, null, p2);236if (!policy.implies(pd, BAR)) {237throw new SecurityException("test." + testnum + " failed");238}239testnum++;240241// should not match FOOBAR grant entry in policy242pd = new ProtectionDomain(cs, null, null, p1);243if (policy.implies(pd, FOOBAR)) {244throw new SecurityException("test." + testnum + " failed");245}246testnum++;247248// should not match FOOBAR grant entry in policy249pd = new ProtectionDomain(cs, null, null, p2);250if (policy.implies(pd, FOOBAR)) {251throw new SecurityException("test." + testnum + " failed");252}253testnum++;254255testnum = doBadTest(policy, testnum);256257return testnum;258}259260private static int doComparatorTest(PolicyFile policy, int testnum) {261262// security check against one comparator should pass263ProtectionDomain pd = new ProtectionDomain(cs, null, null, p1);264if (!policy.implies(pd, FOO)) {265throw new SecurityException("test." + testnum + " failed");266}267testnum++;268269// should not match BAR grant entry in policy270pd = new ProtectionDomain(cs, null, null, p1);271if (policy.implies(pd, BAR)) {272throw new SecurityException("test." + testnum + " failed");273}274testnum++;275276// security check against two comparators should pass for FOO277pd = new ProtectionDomain(cs, null, null, p3);278if (!policy.implies(pd, FOO)) {279throw new SecurityException("test." + testnum + " failed");280}281testnum++;282283// security check against two comparators should pass for BAR284pd = new ProtectionDomain(cs, null, null, p3);285if (!policy.implies(pd, BAR)) {286throw new SecurityException("test." + testnum + " failed");287}288testnum++;289290// security check should fail against FOOBAR291pd = new ProtectionDomain(cs, null, null, p3);292if (policy.implies(pd, FOOBAR)) {293throw new SecurityException("test." + testnum + " failed");294}295testnum++;296297testnum = doBadTest(policy, testnum);298299return testnum;300}301302private static int doCombinedTest(PolicyFile policy, int testnum) {303304// security check against principal followed by comparator should pass305ProtectionDomain pd = new ProtectionDomain(cs, null, null, p3);306if (!policy.implies(pd, FOO)) {307throw new SecurityException("test." + testnum + " failed");308}309testnum++;310311// should not match BAR grant entry in policy312pd = new ProtectionDomain(cs, null, null, p3);313if (policy.implies(pd, BAR)) {314throw new SecurityException("test." + testnum + " failed");315}316testnum++;317318// security check against comparator followed by principal should pass319pd = new ProtectionDomain(cs, null, null, p4);320if (!policy.implies(pd, BAR)) {321throw new SecurityException("test." + testnum + " failed");322}323testnum++;324325// should not match FOO grant entry in policy326pd = new ProtectionDomain(cs, null, null, p4);327if (policy.implies(pd, FOO)) {328throw new SecurityException("test." + testnum + " failed");329}330testnum++;331332// security check against principal-principal-comparator should pass333pd = new ProtectionDomain(cs, null, null, p5);334if (!policy.implies(pd, HELLO)) {335throw new SecurityException("test." + testnum + " failed");336}337testnum++;338339// should not match WORLD grant entry in policy340pd = new ProtectionDomain(cs, null, null, p5);341if (policy.implies(pd, WORLD)) {342throw new SecurityException("test." + testnum + " failed");343}344testnum++;345346// security check against principal-principal-comparator should pass347pd = new ProtectionDomain(cs, null, null, p6);348if (!policy.implies(pd, WORLD)) {349throw new SecurityException("test." + testnum + " failed");350}351testnum++;352353// should not match HELLO grant entry in policy354pd = new ProtectionDomain(cs, null, null, p6);355if (policy.implies(pd, HELLO)) {356throw new SecurityException("test." + testnum + " failed");357}358testnum++;359360testnum = doBadTest(policy, testnum);361362return testnum;363}364}365366367