Path: blob/master/test/jdk/sun/security/provider/PolicyParser/PrincipalExpansionError.java
41152 views
/*1* Copyright (c) 2000, 2020, 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.28* @run main/manual PrincipalExpansionError29*/3031/*32* This test is a bit complicated.33* 1) PrincipalExpansionError.java34* the test itself. this test creates a Subject with a35* UnixPrincipal("TestPrincipal") and calls doAs36* with a PrincipalExpansionErrorAction.37* 2) PrincipalExpansionErrorAction38* this action tries to read the file, /testfile39* 3) to run the test:40* a) jtreg -verbose:all -testjdk:<your_jdk>/build/sparc41* PrincipalExpansionError.java42* b) PrincipalExpansionError is compiled and put into43* the "test.classes" directory44* c) PrincipalExpansionErrorAction is compiled and put into45* the "test.classes"/apackage directory46* (since it belongs to the 'apackage' package47* d) the PrincipalExpansionError shell script moves48* test.classes/apackage to test.src/apackage.49* this guarantees that the test will run50* with codebase test.classes, and the action51* will run with codebase test.src.52* e) the test is executed. permissions to read the file,53* /testfile, were granted to the PrincipalExpansionError.54* the policy entry for PrincipalExpansionErrorAction55* running as UnixPrincipal("TestPrincipal")56* was also granted the file permission,57* but it has a bogus second UnixPrincipal with58* a name that can't be property-expanded.59*60* the old behavior of the code would ignore the61* bogus entry and incorrectly grants the file permission62* to UnixPrincipal("TestPrincipal").63* the new behavior correctly ignores the entire64* policy entry.65* Please note that the jtreg needs to be granted66* allpermissions for this test to succeed. If the codebase67* for jtreg changes, the PrincipalExpansionError.policy68* needs to be updated.69* f) original @ tags:70* compile PrincipalExpansionErrorAction.java71* run shell PrincipalExpansionError.sh72* run main/othervm/policy=PrincipalExpansionError.policy73* -Djava.security.debug=access,domain,failure74* PrincipalExpansionError75*/7677import javax.security.auth.*;78import com.sun.security.auth.*;79import java.util.Set;80import apackage.PrincipalExpansionErrorAction;8182public class PrincipalExpansionError {8384public static void main(String[] args) {8586Subject s = new Subject();8788try {89Set principals = s.getPrincipals();90principals.add(new UnixPrincipal("TestPrincipal"));91} catch (SecurityException se) {92// test incorrectly set up93throw new SecurityException94("PrincipalExpansionError test incorrectly set up:" + se);95}9697try {98Subject.doAs(s, new PrincipalExpansionErrorAction());99100// test failed101System.out.println("PrincipalExpansionError test failed");102throw new SecurityException("PrincipalExpansionError test failed");103104} catch (java.security.PrivilegedActionException pae) {105Exception e = pae.getException();106107if (e instanceof java.io.FileNotFoundException) {108System.out.println109("PrincipalExpansionError test failed (file not found)");110java.io.FileNotFoundException fnfe =111(java.io.FileNotFoundException)e;112throw new SecurityException("PrincipalExpansionError" +113"test failed (file not found)");114} else {115// i don't know???116System.out.println("what happened?");117pae.printStackTrace();118}119} catch (SecurityException se) {120// good! test succeeded121System.out.println("PrincipalExpansionError test succeeded");122se.printStackTrace();123}124}125}126127128