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