jdk/test/java/lang/SecurityManager/CheckPackageMatching.java
author alanb
Fri, 07 Apr 2017 08:05:54 +0000
changeset 44545 83b611b88ac8
parent 43221 eef9383d25cb
permissions -rw-r--r--
8177530: Module system implementation refresh (4/2017) Reviewed-by: mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
     1
/*
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
     4
 *
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
     8
 *
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    14
 *
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    18
 *
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    21
 * questions.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    22
 */
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    23
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    24
/*
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    25
 * @test
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    26
 * @bug 8072692
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    27
 * @summary Check the matching implemented by SecurityManager.checkPackageAccess
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    28
 * @run main/othervm CheckPackageMatching
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    29
 */
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    30
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    31
import java.security.Security;
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    32
import java.util.ArrayList;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    33
import java.util.Arrays;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    34
import java.util.Collection;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    35
import java.util.Collections;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    36
import java.util.List;
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    37
import java.util.StringTokenizer;
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    38
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    39
/*
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    40
 * The purpose of this test is not to verify the content of the package
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    41
 * access list - but to ensure that the matching implemented by the
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    42
 * SecurityManager is correct. This is why we have our own pattern matching
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    43
 * algorithm here.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    44
 */
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    45
public class CheckPackageMatching {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    46
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    47
    /**
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    48
     * The restricted packages listed in the package.access property of the
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    49
     * java.security file.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    50
     */
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    51
    private static final String[] packages = actual().toArray(new String[0]);
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    52
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    53
    /**
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    54
     * Returns the list of restricted packages in the package.access property.
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    55
     */
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    56
    private static List<String> actual() {
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    57
        String prop = Security.getProperty("package.access");
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    58
        List<String> packages = new ArrayList<>();
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    59
        if (prop != null && !prop.equals("")) {
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    60
            StringTokenizer tok = new StringTokenizer(prop, ",");
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    61
            while (tok.hasMoreElements()) {
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    62
                String s = tok.nextToken().trim();
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    63
                packages.add(s);
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    64
            }
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    65
        }
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    66
        return packages;
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
    67
    }
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    68
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    69
    /**
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    70
     * PackageMatcher implements a state machine that matches package
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    71
     * names against packages parsed from the package access list.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    72
     */
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 31180
diff changeset
    73
    private abstract static class PackageMatcher {
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    74
        // For each state, chars[state] contains the chars that matches.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    75
        private final char[][] chars;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    76
        // For each state, states[state][i] contains the next state to go
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    77
        // to when chars[state][i] matches the current character.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    78
        private final int[][] states;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    79
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    80
        // Some markers. We're making the assumption that 0
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    81
        // cannot be a valid character for a package name.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    82
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    83
        // We use 0 for marking that we expect an end of string in
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    84
        // char[state][i].
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    85
        private static final char END_OF_STRING = 0;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    86
        // This special state value indicates that we expect the string to end
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    87
        // there.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    88
        private static final int END_STATE = -1;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    89
        // This special state value indicates that we can accept any character
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    90
        // from now on.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    91
        private static final int WILDCARD_STATE = Integer.MIN_VALUE;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    92
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    93
        // Create the data for a new state machine to match package names from
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    94
        // the array of package names passed as argument.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    95
        // Each package name in the array is expected to end with '.'
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    96
        // For each package in packages we're going to compile state data
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    97
        // that will match the regexp:
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    98
        // ^packages[i].substring(0, packages[i].length()-1).replace(".","\\.")$|^packages[i].replace(".","\\.").*
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
    99
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   100
        // Let's say the package array is:
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   101
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   102
        // String[] packages = { "sun.", "com.sun.jmx.", "com.sun.proxy.",
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   103
        //                       "apple." };
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   104
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   105
        // then the state machine will need data that looks like:
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   106
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   107
        // char[][] chars = {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   108
        //    { 'a', 'c', 's' }, { 'p' }, { 'p' }, { 'l' }, { 'e' }, { 0, '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   109
        //    { 'o' }, { 'm' }, { '.' }, { 's' }, { 'u' }, { 'n' }, { '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   110
        //    { 'j', 'p'},
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   111
        //    { 'm' }, { 'x' }, { 0, '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   112
        //    { 'r' }, { 'o' }, { 'x' }, { 'y' }, { 0, '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   113
        //    { 'u' }, { 'n' }, { 0, '.' }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   114
        // }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   115
        // int[][] states = {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   116
        //    { 1, 6, 22 }, { 2 }, { 3 }, { 4 }, { 5 },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   117
        //    { END_STATE, WILDCARD_STATE },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   118
        //    { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 }, { 13 }, { 14, 17 },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   119
        //    { 15 }, { 16 }, { END_STATE, WILDCARD_STATE },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   120
        //    { 18 }, { 19 }, { 20 }, { 21 }, { END_STATE, WILDCARD_STATE },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   121
        //    { 23 }, { 24 }, { END_STATE, WILDCARD_STATE }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   122
        // }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   123
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   124
        // The machine will start by loading the chars and states for state 0
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   125
        // chars[0] => { 'a', 'c', 's' } states[0] => { 1, 6, 22 }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   126
        // then it examines the char at index 0 in the candidate name.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   127
        // if the char matches one of the characters in chars[0], then it goes
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   128
        // to the corresponding state in states[0]. For instance - if the first
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   129
        // char in the candidate name is 's', which corresponds to chars[0][2] -
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   130
        // then it will proceed with the next char in the candidate name and go
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   131
        // to state 22 (as indicated by states[0][2]) - where it will load the
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   132
        // chars and states for states 22: chars[22] = { 'u' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   133
        // states[22] = { 23 } etc... until the candidate char at the current
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   134
        // index matches no char in chars[states] => the candidate name doesn't
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   135
        // match - or until it finds a success termination condition: the
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   136
        // candidate chars are exhausted and states[state][0] is END_STATE, or
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   137
        // the candidate chars are not exhausted - and
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   138
        // states[state][chars[state]] is WILDCARD_STATE indicating a '.*' like
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   139
        // regexp.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   140
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   141
        // [Note that the chars in chars[i] are sorted]
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   142
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   143
        // The compile(...) method is reponsible for building the state machine
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   144
        // data and is called only once in the constructor.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   145
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   146
        // The matches(String candidate) method will tell whether the candidate
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   147
        // matches by implementing the algorithm described above.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   148
        //
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   149
        PackageMatcher(String[] packages) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   150
            final boolean[] selected = new boolean[packages.length];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   151
            Arrays.fill(selected, true);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   152
            final ArrayList<char[]> charList = new ArrayList<>();
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   153
            final ArrayList<int[]> stateList = new ArrayList<>();
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   154
            compile(0, 0, packages, selected, charList, stateList);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   155
            chars = charList.toArray(new char[0][0]);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   156
            states = stateList.toArray(new int[0][0]);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   157
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   158
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   159
        /**
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   160
         * Compiles the state machine data (recursive).
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   161
         *
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   162
         * @param step  The index of the character which we're looking at in
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   163
         *              this step.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   164
         * @param state The current state (starts at 0).
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   165
         * @param pkgs  The list of packages from which the automaton is built.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   166
         * @param selected  Indicates which packages we're looking at in this
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   167
                            step.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   168
         * @param charList  The list from which we will build
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   169
                            {@code char[][] chars;}
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   170
         * @param stateList The list from which we will build
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   171
                            {@code int[][]  states;}
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   172
         * @return the next available state.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   173
         */
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   174
        private int compile(int step, int state, String[] pkgs,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   175
                            boolean[] selected, ArrayList<char[]> charList,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   176
                            ArrayList<int[]> stateList) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   177
            final char[] next = new char[pkgs.length];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   178
            final int[] nexti = new int[pkgs.length];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   179
            int j = 0;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   180
            char min = Character.MAX_VALUE; char max = 0;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   181
            for (int i = 0; i < pkgs.length; i++) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   182
                if (!selected[i]) continue;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   183
                final String p = pkgs[i];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   184
                final int len = p.length();
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   185
                if (step > len) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   186
                    selected[i] = false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   187
                    continue;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   188
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   189
                if (len - 1 == step) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   190
                    boolean unknown = true;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   191
                    for (int k = 0; k < j ; k++) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   192
                        if (next[k] == END_OF_STRING) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   193
                            unknown = false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   194
                            break;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   195
                        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   196
                    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   197
                    if (unknown) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   198
                        next[j] = END_OF_STRING;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   199
                        j++;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   200
                    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   201
                    nexti[i] = END_STATE;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   202
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   203
                final char c = p.charAt(step);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   204
                nexti[i] = len - 1 == step ? END_STATE : c;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   205
                boolean unknown = j == 0 || c < min || c > max;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   206
                if (!unknown) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   207
                    if (c != min || c != max) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   208
                        unknown = true;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   209
                        for (int k = 0; k < j ; k++) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   210
                            if (next[k] == c) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   211
                                unknown = false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   212
                                break;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   213
                            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   214
                        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   215
                    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   216
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   217
                if (unknown) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   218
                    min = min > c ? c : min;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   219
                    max = max < c ? c : max;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   220
                    next[j] = c;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   221
                    j++;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   222
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   223
            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   224
            final char[] nc = new char[j];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   225
            final int[]  nst = new int[j];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   226
            System.arraycopy(next, 0, nc, 0, nc.length);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   227
            Arrays.sort(nc);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   228
            final boolean ns[] = new boolean[pkgs.length];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   229
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   230
            charList.ensureCapacity(state + 1);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   231
            stateList.ensureCapacity(state + 1);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   232
            charList.add(state, nc);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   233
            stateList.add(state, nst);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   234
            state = state + 1;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   235
            for (int k = 0; k < nc.length; k++) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   236
                int selectedCount = 0;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   237
                boolean endStateFound = false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   238
                boolean wildcardFound = false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   239
                for (int l = 0; l < nexti.length; l++) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   240
                    if (!(ns[l] = selected[l])) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   241
                        continue;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   242
                    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   243
                    ns[l] = nexti[l] == nc[k] || nexti[l] == END_STATE
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   244
                            && nc[k] == '.';
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   245
                    endStateFound = endStateFound || nc[k] == END_OF_STRING
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   246
                                    && nexti[l] == END_STATE;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   247
                    wildcardFound = wildcardFound || nc[k] == '.'
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   248
                                    && nexti[l] == END_STATE;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   249
                    if (ns[l]) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   250
                        selectedCount++;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   251
                    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   252
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   253
                nst[k] = (endStateFound ? END_STATE
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   254
                         : wildcardFound ? WILDCARD_STATE : state);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   255
                if (selectedCount == 0 || wildcardFound) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   256
                    continue;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   257
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   258
                state = compile(step + 1, state, pkgs, ns, charList, stateList);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   259
            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   260
            return state;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   261
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   262
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   263
        /**
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   264
         * Matches 'pkg' against the list of package names compiled in the
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   265
         * state machine data.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   266
         *
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   267
         * @param pkg The package name to match. Must not end with '.'.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   268
         * @return true if the package name matches, false otherwise.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   269
         */
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   270
        public boolean matches(String pkg) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   271
            int state = 0;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   272
            int i;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   273
            final int len = pkg.length();
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   274
            next: for (i = 0; i <= len; i++) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   275
                if (state == WILDCARD_STATE) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   276
                    return true; // all characters will match.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   277
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   278
                if (state == END_STATE) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   279
                    return i == len;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   280
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   281
                final char[] ch = chars[state];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   282
                final int[] st = states[state];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   283
                if (i == len) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   284
                    // matches only if we have exhausted the string.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   285
                    return st[0] == END_STATE;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   286
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   287
                if (st[0] == END_STATE && st.length == 1) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   288
                    // matches only if we have exhausted the string.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   289
                    return i == len;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   290
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   291
                final char c = pkg.charAt(i); // look at next char...
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   292
                for (int j = st[0] == END_STATE ? 1 : 0; j < ch.length; j++) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   293
                    final char n = ch[j];
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   294
                    if (c == n) {      // found a match
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   295
                        state = st[j]; // get the next state.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   296
                        continue next; // go to next state
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   297
                    } else if (c < n) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   298
                        break; // chars are sorted. we won't find it. no match.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   299
                    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   300
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   301
                break; // no match
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   302
            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   303
            return false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   304
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   305
    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   306
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   307
    private static final class TestPackageMatcher extends PackageMatcher {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   308
        private final List<String> list;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   309
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   310
        TestPackageMatcher(String[] packages) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   311
            super(packages);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   312
            this.list = Collections.unmodifiableList(Arrays.asList(packages));
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   313
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   314
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   315
        @Override
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   316
        public boolean matches(String pkg) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   317
            final boolean match1 = super.matches(pkg);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   318
            boolean match2 = false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   319
            String p2 = pkg + ".";
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   320
            for (String p : list) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   321
                if (pkg.startsWith(p) || p2.equals(p)) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   322
                    match2 = true;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   323
                    break;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   324
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   325
            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   326
            if (match1 != match2) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   327
                System.err.println("Test Bug: PackageMatcher.matches(\"" +
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   328
                                   pkg + "\") returned " + match1);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   329
                System.err.println("Package Access List is: " + list);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   330
                throw new Error("Test Bug: PackageMatcher.matches(\"" +
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   331
                                pkg + "\") returned " + match1);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   332
            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   333
            return match1;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   334
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   335
    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   336
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   337
    private static void smokeTest() {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   338
        // these checks should pass.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   339
        System.getSecurityManager().checkPackageAccess("com.sun.blah");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   340
        System.getSecurityManager().checkPackageAccess("com.sun.jm");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   341
        System.getSecurityManager().checkPackageAccess("com.sun.jmxa");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   342
        System.getSecurityManager().checkPackageAccess("jmx");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   343
        List<String> actual = Arrays.asList(packages);
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
   344
        if (!actual.contains("sun.misc.")) {
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
   345
            throw new Error("package.access does not contain 'sun.misc.'");
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   346
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   347
    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   348
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   349
    // This is a sanity test for our own test code.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   350
    private static void testTheTest(String[] pkgs, char[][] chars,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   351
                                    int[][] states) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   352
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   353
        PackageMatcher m = new TestPackageMatcher(pkgs);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   354
        String unexpected = "";
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   355
        if (!Arrays.deepEquals(chars, m.chars)) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   356
            System.err.println("Char arrays differ");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   357
            if (chars.length != m.chars.length) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   358
                System.err.println("Char array lengths differ: expected="
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   359
                        + chars.length + " actual=" + m.chars.length);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   360
            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   361
            System.err.println(Arrays.deepToString(m.chars).replace((char)0,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   362
                                                   '0'));
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   363
            unexpected = "chars[]";
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   364
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   365
        if (!Arrays.deepEquals(states, m.states)) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   366
            System.err.println("State arrays differ");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   367
            if (states.length != m.states.length) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   368
                System.err.println("Char array lengths differ: expected="
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   369
                        + states.length + " actual=" + m.states.length);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   370
            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   371
            System.err.println(Arrays.deepToString(m.states));
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   372
            if (unexpected.length() > 0) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   373
                unexpected = unexpected + " and ";
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   374
            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   375
            unexpected = unexpected + "states[]";
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   376
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   377
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   378
        if (unexpected.length() > 0) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   379
            throw new Error("Unexpected "+unexpected+" in PackageMatcher");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   380
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   381
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   382
        testMatches(m, pkgs);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   383
    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   384
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   385
    // This is a sanity test for our own test code.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   386
    private static void testTheTest() {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   387
        final String[] packages2 = { "sun.", "com.sun.jmx.",
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   388
                                     "com.sun.proxy.", "apple." };
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   389
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   390
        final int END_STATE = PackageMatcher.END_STATE;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   391
        final int WILDCARD_STATE = PackageMatcher.WILDCARD_STATE;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   392
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   393
        final char[][] chars2 = {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   394
            { 'a', 'c', 's' }, { 'p' }, { 'p' }, { 'l' }, { 'e' }, { 0, '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   395
            { 'o' }, { 'm' }, { '.' }, { 's' }, { 'u' }, { 'n' }, { '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   396
            { 'j', 'p'},
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   397
            { 'm' }, { 'x' }, { 0, '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   398
            { 'r' }, { 'o' }, { 'x' }, { 'y' }, { 0, '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   399
            { 'u' }, { 'n' }, { 0, '.' }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   400
         };
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   401
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   402
         final int[][] states2 = {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   403
            { 1, 6, 22 }, { 2 }, { 3 }, { 4 }, { 5 },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   404
            { END_STATE, WILDCARD_STATE },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   405
            { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 }, { 13 }, { 14, 17 },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   406
            { 15 }, { 16 }, { END_STATE, WILDCARD_STATE },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   407
            { 18 }, { 19 }, { 20 }, { 21 }, { END_STATE, WILDCARD_STATE },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   408
            { 23 }, { 24 }, { END_STATE, WILDCARD_STATE }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   409
         };
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   410
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   411
         testTheTest(packages2, chars2, states2);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   412
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   413
         final String[] packages3 = { "sun.", "com.sun.pro.",
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   414
                                      "com.sun.proxy.", "apple." };
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   415
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   416
         final char[][] chars3 = {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   417
            { 'a', 'c', 's' }, { 'p' }, { 'p' }, { 'l' }, { 'e' }, { 0, '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   418
            { 'o' }, { 'm' }, { '.' }, { 's' }, { 'u' }, { 'n' }, { '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   419
            { 'p' }, { 'r' }, { 'o' }, { 0, '.', 'x' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   420
            { 'y' }, { 0, '.' },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   421
            { 'u' }, { 'n' }, { 0, '.' }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   422
         };
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   423
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   424
         final int[][] states3 = {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   425
            { 1, 6, 19 }, { 2 }, { 3 }, { 4 }, { 5 },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   426
            { END_STATE, WILDCARD_STATE },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   427
            { 7 }, { 8 }, { 9 }, { 10 }, { 11 }, { 12 }, { 13 }, { 14 },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   428
            { 15 }, { 16 }, { END_STATE, WILDCARD_STATE, 17 },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   429
            { 18 }, { END_STATE, WILDCARD_STATE },
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   430
            { 20 }, { 21 }, { END_STATE, WILDCARD_STATE }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   431
         };
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   432
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   433
         testTheTest(packages3, chars3, states3);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   434
    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   435
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   436
    private static volatile boolean sanityTesting = false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   437
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   438
    public static void main(String[] args) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   439
        System.setSecurityManager(new SecurityManager());
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   440
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   441
        // Some smoke tests.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   442
        smokeTest();
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   443
        System.out.println("Smoke tests passed.");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   444
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   445
        // Test our own pattern matching algorithm. Here we actually test
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   446
        // the PackageMatcher class from our own test code.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   447
        sanityTesting = true;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   448
        try {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   449
            testTheTest();
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   450
            System.out.println("Sanity tests passed.");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   451
        } finally {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   452
            sanityTesting = false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   453
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   454
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   455
        // Now test the package matching in the security manager.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   456
        PackageMatcher matcher = new TestPackageMatcher(packages);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   457
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   458
        // These should not match.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   459
        for (String pkg : new String[] {"gloups.machin", "su",
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
   460
                                        "org.jcp.xml.dsig.inter",
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   461
                                        "com.sun.jm", "com.sun.jmxa"}) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   462
            testMatch(matcher, pkg, false, true);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   463
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   464
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   465
        // These should match.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   466
        for (String pkg : Arrays.asList(
43221
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
   467
                new String[] {"sun.misc.gloups.machin", "sun.misc",
eef9383d25cb 8055206: Update SecurityManager::checkPackageAccess to restrict non-exported JDK packages by default
mullan
parents: 32649
diff changeset
   468
                              "sun.reflect"})) {
31180
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   469
            testMatch(matcher, pkg, true, true);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   470
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   471
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   472
        // Derive a list of packages that should match or not match from
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   473
        // the list in 'packages' - and check that the security manager
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   474
        // throws the appropriate exception.
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   475
        testMatches(matcher, packages);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   476
    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   477
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   478
    private static void testMatches(PackageMatcher matcher, String[] pkgs) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   479
        Collection<String> pkglist = Arrays.asList(pkgs);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   480
        PackageMatcher ref = new TestPackageMatcher(packages);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   481
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   482
        for (String pkg : pkgs) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   483
            String candidate = pkg + "toto";
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   484
            boolean expected = true;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   485
            testMatch(matcher, candidate, expected,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   486
                      ref.matches(candidate) == expected);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   487
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   488
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   489
        for (String pkg : pkgs) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   490
            String candidate = pkg.substring(0, pkg.length() - 1);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   491
            boolean expected = pkglist.contains(candidate + ".");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   492
            testMatch(matcher, candidate, expected,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   493
                      ref.matches(candidate) == expected);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   494
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   495
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   496
        for (String pkg : pkgs) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   497
            String candidate = pkg.substring(0, pkg.length() - 2);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   498
            boolean expected = pkglist.contains(candidate + ".");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   499
            testMatch(matcher, candidate, expected,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   500
                      ref.matches(candidate) == expected);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   501
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   502
    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   503
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   504
    private static void testMatch(PackageMatcher matcher, String candidate,
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   505
                                  boolean expected, boolean testSecurityManager)
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   506
    {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   507
        final boolean m = matcher.matches(candidate);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   508
        if (m != expected) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   509
            final String msg = "\"" + candidate + "\": " +
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   510
                (m ? "matches" : "does not match");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   511
            throw new Error("PackageMatcher does not give expected results: "
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   512
                            + msg);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   513
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   514
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   515
        if (sanityTesting) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   516
            testSecurityManager = false;
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   517
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   518
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   519
        if (testSecurityManager) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   520
            System.out.println("Access to " + candidate + " should be " +
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   521
                               (expected ? "rejected" : "granted"));
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   522
            final String errormsg = "\"" + candidate + "\" : " +
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   523
                (expected ? "granted" : "not granted");
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   524
            try {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   525
                System.getSecurityManager().checkPackageAccess(candidate);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   526
                if (expected) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   527
                    System.err.println(errormsg);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   528
                    throw new Error("Expected exception not thrown: " +
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   529
                                    errormsg);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   530
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   531
            } catch (SecurityException x) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   532
                if (!expected) {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   533
                    System.err.println(errormsg);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   534
                    throw new Error(errormsg + " - unexpected exception: " +
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   535
                                    x, x);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   536
                } else {
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   537
                    System.out.println("Got expected exception: " + x);
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   538
                }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   539
            }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   540
        }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   541
    }
316a8c3e572a 8072692: Improve performance of SecurityManager.checkPackageAccess
dfuchs
parents:
diff changeset
   542
}