jdk/test/sun/security/provider/PolicyParser/PrincipalExpansionError.java
changeset 2 90ce3da70b43
child 5506 202f599c92aa
equal deleted inserted replaced
0:fd16c54261b3 2:90ce3da70b43
       
     1 /*
       
     2  * Copyright 2000-2002 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 
       
    24 /*
       
    25  * @test
       
    26  * @bug 4373996
       
    27  * @summary parser incorrectly ignores a principal if the principal name
       
    28  *      expands to nothing.  this test is a bit complicated.
       
    29  *
       
    30  *      1) PrincipalExpansionError.java
       
    31  *              the test itself.  this test creates a Subject with a
       
    32  *              SolarisPrincipal("TestPrincipal") and calls doAs
       
    33  *              with a PrincipalExpansionErrorAction.
       
    34  *
       
    35  *      2) PrincipalExpansionErrorAction
       
    36  *              this action tries to read the file, /testfile
       
    37  *
       
    38  *      3) to run the test:
       
    39  *              a) jtreg -verbose:all -testjdk:<your_jdk>/build/sparc
       
    40  *                      PrincipalExpansionError.java
       
    41  *              b) PrincipalExpansionError is compiled and put into
       
    42  *                      the "test.classes" directory
       
    43  *              c) PrincipalExpansionErrorAction is compiled and put into
       
    44  *                      the "test.classes"/apackage directory
       
    45  *                      (since it belongs to the 'apackage' package
       
    46  *              d) the PrincipalExpansionError shell script moves
       
    47  *                      test.classes/apackage to test.src/apackage.
       
    48  *                      this guarantees that the test will run
       
    49  *                      with codebase test.classes, and the action
       
    50  *                      will run with codebase test.src.
       
    51  *              e) the test is executed.  permissions to read the file,
       
    52  *                      /testfile, were granted to the PrincipalExpansionError.
       
    53  *                      the policy entry for PrincipalExpansionErrorAction
       
    54  *                      running as SolarisPrincipal("TestPrincipal")
       
    55  *                      was also granted the file permission,
       
    56  *                      but it has a bogus second SolarisPrincipal with
       
    57  *                      a name that can't be property-expanded.
       
    58  *
       
    59  *                      the old behavior of the code would ignore the
       
    60  *                      bogus entry and incorrectly grants the file permission
       
    61  *                      to SolarisPrincipal("TestPrincipal").
       
    62  *                      the new behavior correctly ignores the entire
       
    63  *                      policy entry.
       
    64  *                      Please note that the jtreg needs to be granted
       
    65  *                      allpermissions for this test to succeed. If the codebase
       
    66  *                      for jtreg changes, the PrincipalExpansionError.policy
       
    67  *                      needs to be updated.
       
    68  *
       
    69  *              f) original @ tags:
       
    70  *                      compile PrincipalExpansionErrorAction.java
       
    71  *                      run shell PrincipalExpansionError.sh
       
    72  *                      run main/othervm/policy=PrincipalExpansionError.policy
       
    73  *                              -Djava.security.debug=access,domain,failure
       
    74  *                              PrincipalExpansionError
       
    75  *
       
    76  * @ignore unable to rely on location or javatest.jar
       
    77  *              (so we can grant it AllPermission)
       
    78  */
       
    79 
       
    80 import javax.security.auth.*;
       
    81 import com.sun.security.auth.*;
       
    82 import java.util.Set;
       
    83 import apackage.PrincipalExpansionErrorAction;
       
    84 
       
    85 public class PrincipalExpansionError {
       
    86 
       
    87     public static void main(String[] args) {
       
    88 
       
    89         Subject s = new Subject();
       
    90 
       
    91         try {
       
    92             Set principals = s.getPrincipals();
       
    93             principals.add(new SolarisPrincipal("TestPrincipal"));
       
    94         } catch (SecurityException se) {
       
    95             // test incorrectly set up
       
    96             throw new SecurityException
       
    97                 ("PrincipalExpansionError test incorrectly set up:" + se);
       
    98         }
       
    99 
       
   100         try {
       
   101             Subject.doAs(s, new PrincipalExpansionErrorAction());
       
   102 
       
   103             // test failed
       
   104             System.out.println("PrincipalExpansionError test failed");
       
   105             throw new SecurityException("PrincipalExpansionError test failed");
       
   106 
       
   107         } catch (java.security.PrivilegedActionException pae) {
       
   108             Exception e = pae.getException();
       
   109 
       
   110             if (e instanceof java.io.FileNotFoundException) {
       
   111                 System.out.println
       
   112                     ("PrincipalExpansionError test failed (file not found)");
       
   113                 java.io.FileNotFoundException fnfe =
       
   114                         (java.io.FileNotFoundException)e;
       
   115                 throw new SecurityException("PrincipalExpansionError" +
       
   116                         "test failed (file not found)");
       
   117             } else {
       
   118                 // i don't know???
       
   119                 System.out.println("what happened?");
       
   120                 pae.printStackTrace();
       
   121             }
       
   122         } catch (SecurityException se) {
       
   123                 // good!  test succeeded
       
   124                 System.out.println("PrincipalExpansionError test succeeded");
       
   125                 se.printStackTrace();
       
   126         }
       
   127     }
       
   128 }