jdk/test/java/lang/StackWalker/SecurityExceptions.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 34372 ccdd9223ab7a
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34372
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     1
/*
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     4
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     8
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    13
 * accompanied this code).
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    14
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    18
 *
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    21
 * questions.
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    22
 */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    23
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    24
/*
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    25
 * @test 8020968
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    26
 * @summary Test security permission check
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    27
 * @run main/othervm/java.security.policy=noperms.policy SecurityExceptions true
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    28
 * @run main/othervm/java.security.policy=stackwalk.policy SecurityExceptions false
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    29
 */
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    30
public class SecurityExceptions {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    31
    public static void main(String[] args) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    32
        boolean expectException = Boolean.parseBoolean(args[0]);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    33
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    34
        StackWalker sw = StackWalker.getInstance();
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    35
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    36
        try {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    37
            sw = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    38
            if (expectException) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    39
                throw new RuntimeException("Expected SecurityException, but none thrown");
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    40
            }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    41
        } catch (SecurityException e) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    42
            if (!expectException) {
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    43
                System.err.println("Unexpected security exception:");
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    44
                throw e;
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    45
            }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    46
        }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    47
    }
ccdd9223ab7a 8143911: Reintegrate JEP 259: Stack-Walking API
mchung
parents:
diff changeset
    48
}