jdk/test/sun/security/provider/PolicyParser/PrincipalExpansionError.java
author jlaskey
Fri, 20 May 2016 11:41:29 -0300
changeset 38445 0a88d86065f9
parent 23357 7225e28e91dd
permissions -rw-r--r--
8156602: javac crashes again on Windows 32-bit with ClosedChannelException Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23357
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
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
23357
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    28
 *      expands to nothing.
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    29
 * @run main/manual PrincipalExpansionError
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    30
 */
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    31
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    32
/*
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    33
 * This test is a bit complicated.
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    34
 * 1) PrincipalExpansionError.java
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    35
 *         the test itself.  this test creates a Subject with a
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    36
 *         SolarisPrincipal("TestPrincipal") and calls doAs
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    37
 *         with a PrincipalExpansionErrorAction.
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    38
 * 2) PrincipalExpansionErrorAction
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    39
 *         this action tries to read the file, /testfile
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    40
 * 3) to run the test:
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    41
 *         a) jtreg -verbose:all -testjdk:<your_jdk>/build/sparc
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    42
 *                 PrincipalExpansionError.java
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    43
 *         b) PrincipalExpansionError is compiled and put into
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    44
 *                 the "test.classes" directory
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    45
 *         c) PrincipalExpansionErrorAction is compiled and put into
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    46
 *                 the "test.classes"/apackage directory
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    47
 *                 (since it belongs to the 'apackage' package
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    48
 *         d) the PrincipalExpansionError shell script moves
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    49
 *                 test.classes/apackage to test.src/apackage.
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    50
 *                 this guarantees that the test will run
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    51
 *                 with codebase test.classes, and the action
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    52
 *                 will run with codebase test.src.
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    53
 *         e) the test is executed.  permissions to read the file,
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    54
 *                 /testfile, were granted to the PrincipalExpansionError.
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    55
 *                 the policy entry for PrincipalExpansionErrorAction
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    56
 *                 running as SolarisPrincipal("TestPrincipal")
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    57
 *                 was also granted the file permission,
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    58
 *                 but it has a bogus second SolarisPrincipal with
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    59
 *                 a name that can't be property-expanded.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
23357
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    61
 *                 the old behavior of the code would ignore the
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    62
 *                 bogus entry and incorrectly grants the file permission
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    63
 *                 to SolarisPrincipal("TestPrincipal").
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    64
 *                 the new behavior correctly ignores the entire
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    65
 *                 policy entry.
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    66
 *                 Please note that the jtreg needs to be granted
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    67
 *                 allpermissions for this test to succeed. If the codebase
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    68
 *                 for jtreg changes, the PrincipalExpansionError.policy
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    69
 *                 needs to be updated.
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    70
 *         f) original @ tags:
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    71
 *                 compile PrincipalExpansionErrorAction.java
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    72
 *                 run shell PrincipalExpansionError.sh
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    73
 *                 run main/othervm/policy=PrincipalExpansionError.policy
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    74
 *                         -Djava.security.debug=access,domain,failure
7225e28e91dd 8033271: Manual security tests have @ignore rather than @run main/manual
wetmore
parents: 5506
diff changeset
    75
 *                         PrincipalExpansionError
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
import javax.security.auth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
import com.sun.security.auth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
import apackage.PrincipalExpansionErrorAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
public class PrincipalExpansionError {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        Subject s = new Subject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Set principals = s.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            principals.add(new SolarisPrincipal("TestPrincipal"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            // test incorrectly set up
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new SecurityException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                ("PrincipalExpansionError test incorrectly set up:" + se);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            Subject.doAs(s, new PrincipalExpansionErrorAction());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // test failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            System.out.println("PrincipalExpansionError test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new SecurityException("PrincipalExpansionError test failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        } catch (java.security.PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            Exception e = pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (e instanceof java.io.FileNotFoundException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
                System.out.println
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                    ("PrincipalExpansionError test failed (file not found)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                java.io.FileNotFoundException fnfe =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                        (java.io.FileNotFoundException)e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
                throw new SecurityException("PrincipalExpansionError" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                        "test failed (file not found)");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                // i don't know???
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                System.out.println("what happened?");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                pae.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        } catch (SecurityException se) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                // good!  test succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                System.out.println("PrincipalExpansionError test succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                se.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
}