1 /* 2 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 4373996 27 * @summary parser incorrectly ignores a principal if the principal name 28 * expands to nothing. 29 * @run main/manual PrincipalExpansionError 30 */ 31 32 /* 33 * This test is a bit complicated. 34 * 1) PrincipalExpansionError.java 35 * the test itself. this test creates a Subject with a 36 * SolarisPrincipal("TestPrincipal") and calls doAs 37 * with a PrincipalExpansionErrorAction. 38 * 2) PrincipalExpansionErrorAction 39 * this action tries to read the file, /testfile 40 * 3) to run the test: 41 * a) jtreg -verbose:all -testjdk:<your_jdk>/build/sparc 42 * PrincipalExpansionError.java 43 * b) PrincipalExpansionError is compiled and put into 44 * the "test.classes" directory 45 * c) PrincipalExpansionErrorAction is compiled and put into 46 * the "test.classes"/apackage directory 47 * (since it belongs to the 'apackage' package 48 * d) the PrincipalExpansionError shell script moves 49 * test.classes/apackage to test.src/apackage. 50 * this guarantees that the test will run 51 * with codebase test.classes, and the action 52 * will run with codebase test.src. 53 * e) the test is executed. permissions to read the file, 54 * /testfile, were granted to the PrincipalExpansionError. 55 * the policy entry for PrincipalExpansionErrorAction 56 * running as SolarisPrincipal("TestPrincipal") 57 * was also granted the file permission, 58 * but it has a bogus second SolarisPrincipal with 59 * a name that can't be property-expanded. 60 * 61 * the old behavior of the code would ignore the 62 * bogus entry and incorrectly grants the file permission 63 * to SolarisPrincipal("TestPrincipal"). 64 * the new behavior correctly ignores the entire 65 * policy entry. 66 * Please note that the jtreg needs to be granted 67 * allpermissions for this test to succeed. If the codebase 68 * for jtreg changes, the PrincipalExpansionError.policy 69 * needs to be updated. 70 * f) original @ tags: 71 * compile PrincipalExpansionErrorAction.java 72 * run shell PrincipalExpansionError.sh 73 * run main/othervm/policy=PrincipalExpansionError.policy 74 * -Djava.security.debug=access,domain,failure 75 * PrincipalExpansionError 76 */ 77 78 import javax.security.auth.*; 79 import com.sun.security.auth.*; 80 import java.util.Set; 81 import apackage.PrincipalExpansionErrorAction; 82 83 public class PrincipalExpansionError { 84 85 public static void main(String[] args) { 86 87 Subject s = new Subject(); 88 89 try { 90 Set principals = s.getPrincipals(); 91 principals.add(new SolarisPrincipal("TestPrincipal")); 92 } catch (SecurityException se) { 93 // test incorrectly set up 94 throw new SecurityException 95 ("PrincipalExpansionError test incorrectly set up:" + se); 96 } 97 98 try { 99 Subject.doAs(s, new PrincipalExpansionErrorAction()); 100 101 // test failed 102 System.out.println("PrincipalExpansionError test failed"); 103 throw new SecurityException("PrincipalExpansionError test failed"); 104 105 } catch (java.security.PrivilegedActionException pae) { 106 Exception e = pae.getException(); 107 108 if (e instanceof java.io.FileNotFoundException) { 109 System.out.println 110 ("PrincipalExpansionError test failed (file not found)"); 111 java.io.FileNotFoundException fnfe = 112 (java.io.FileNotFoundException)e; 113 throw new SecurityException("PrincipalExpansionError" + 114 "test failed (file not found)"); 115 } else { 116 // i don't know??? 117 System.out.println("what happened?"); 118 pae.printStackTrace(); 119 } 120 } catch (SecurityException se) { 121 // good! test succeeded 122 System.out.println("PrincipalExpansionError test succeeded"); 123 se.printStackTrace(); 124 } 125 } 126 }