test/jdk/javax/security/auth/Subject/DoAs.java
author jjg
Fri, 09 Aug 2019 15:22:43 -0700
changeset 57706 7ac414640ad5
parent 53068 7496df94b3b7
permissions -rw-r--r--
8229386: Typo "lables" in doc comment Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53068
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     1
/*
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     4
 *
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     8
 *
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    13
 * accompanied this code).
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    14
 *
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    18
 *
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    21
 * questions.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    22
 */
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    23
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    24
/*
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    25
 * @test
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    26
 * @bug 8214583
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    27
 * @summary Check that getSubject works after JIT compiler escape analysis.
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    28
 */
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    29
import java.security.AccessControlContext;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    30
import java.security.AccessController;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    31
import java.security.PrivilegedAction;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    32
import java.security.PrivilegedExceptionAction;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    33
import java.util.Arrays;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    34
import java.util.Collections;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    35
import java.util.HashSet;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    36
import java.util.Set;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    37
import javax.security.auth.Subject;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    38
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    39
public class DoAs {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    40
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    41
    public static void main(String[] args) throws Exception {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    42
        final Set<String> outer = new HashSet<>(Arrays.asList("Outer"));
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    43
        final Subject subject = new Subject(true, Collections.EMPTY_SET, outer, Collections.EMPTY_SET);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    44
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    45
        for (int i = 0; i < 100_000; ++i) {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    46
            final int index = i;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    47
            Subject.doAs(subject, (PrivilegedExceptionAction<Integer>)() -> {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    48
                AccessControlContext c1 = AccessController.getContext();
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    49
                Subject s = Subject.getSubject(c1);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    50
                if (s != subject) {
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    51
                    throw new AssertionError("outer Oops! " + "iteration " + index + " " + s + " != " + subject);
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    52
                }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    53
                return 0;
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    54
            });
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    55
        }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    56
    }
7496df94b3b7 8214583: AccessController.getContext may return wrong value after JDK-8212605
dlong
parents:
diff changeset
    57
}