Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/provider/PolicyParser/PrincipalExpansionError.java
38853 views
/*1* Copyright (c) 2000, 2002, 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 437399626* @summary parser incorrectly ignores a principal if the principal name27* expands to nothing. this test is a bit complicated.28*29* 1) PrincipalExpansionError.java30* the test itself. this test creates a Subject with a31* SolarisPrincipal("TestPrincipal") and calls doAs32* with a PrincipalExpansionErrorAction.33*34* 2) PrincipalExpansionErrorAction35* this action tries to read the file, /testfile36*37* 3) to run the test:38* a) jtreg -verbose:all -testjdk:<your_jdk>/build/sparc39* PrincipalExpansionError.java40* b) PrincipalExpansionError is compiled and put into41* the "test.classes" directory42* c) PrincipalExpansionErrorAction is compiled and put into43* the "test.classes"/apackage directory44* (since it belongs to the 'apackage' package45* d) the PrincipalExpansionError shell script moves46* test.classes/apackage to test.src/apackage.47* this guarantees that the test will run48* with codebase test.classes, and the action49* will run with codebase test.src.50* e) the test is executed. permissions to read the file,51* /testfile, were granted to the PrincipalExpansionError.52* the policy entry for PrincipalExpansionErrorAction53* running as SolarisPrincipal("TestPrincipal")54* was also granted the file permission,55* but it has a bogus second SolarisPrincipal with56* a name that can't be property-expanded.57*58* the old behavior of the code would ignore the59* bogus entry and incorrectly grants the file permission60* to SolarisPrincipal("TestPrincipal").61* the new behavior correctly ignores the entire62* policy entry.63* Please note that the jtreg needs to be granted64* allpermissions for this test to succeed. If the codebase65* for jtreg changes, the PrincipalExpansionError.policy66* needs to be updated.67*68* f) original @ tags:69* compile PrincipalExpansionErrorAction.java70* run shell PrincipalExpansionError.sh71* run main/othervm/policy=PrincipalExpansionError.policy72* -Djava.security.debug=access,domain,failure73* PrincipalExpansionError74*75* @ignore unable to rely on location or javatest.jar76* (so we can grant it AllPermission)77*/7879import javax.security.auth.*;80import com.sun.security.auth.*;81import java.util.Set;82import apackage.PrincipalExpansionErrorAction;8384public class PrincipalExpansionError {8586public static void main(String[] args) {8788Subject s = new Subject();8990try {91Set principals = s.getPrincipals();92principals.add(new SolarisPrincipal("TestPrincipal"));93} catch (SecurityException se) {94// test incorrectly set up95throw new SecurityException96("PrincipalExpansionError test incorrectly set up:" + se);97}9899try {100Subject.doAs(s, new PrincipalExpansionErrorAction());101102// test failed103System.out.println("PrincipalExpansionError test failed");104throw new SecurityException("PrincipalExpansionError test failed");105106} catch (java.security.PrivilegedActionException pae) {107Exception e = pae.getException();108109if (e instanceof java.io.FileNotFoundException) {110System.out.println111("PrincipalExpansionError test failed (file not found)");112java.io.FileNotFoundException fnfe =113(java.io.FileNotFoundException)e;114throw new SecurityException("PrincipalExpansionError" +115"test failed (file not found)");116} else {117// i don't know???118System.out.println("what happened?");119pae.printStackTrace();120}121} catch (SecurityException se) {122// good! test succeeded123System.out.println("PrincipalExpansionError test succeeded");124se.printStackTrace();125}126}127}128129130