doprivileged.js revision 2:da1e581c933b
11541Srgrimes/*
21541Srgrimes * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
31541Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
41541Srgrimes *
51541Srgrimes * This code is free software; you can redistribute it and/or modify it
61541Srgrimes * under the terms of the GNU General Public License version 2 only, as
71541Srgrimes * published by the Free Software Foundation.
81541Srgrimes *
91541Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
101541Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
111541Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
121541Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
131541Srgrimes * accompanied this code).
141541Srgrimes *
151541Srgrimes * You should have received a copy of the GNU General Public License version
161541Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
171541Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
181541Srgrimes *
191541Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
201541Srgrimes * or visit www.oracle.com if you need additional information or have any
211541Srgrimes * questions.
221541Srgrimes */
231541Srgrimes
241541Srgrimes/**
251541Srgrimes * doPrivileged checks.
261541Srgrimes *
271541Srgrimes * @test
281541Srgrimes * @security
291541Srgrimes */
301541Srgrimes
311541Srgrimesfunction check(e) {
321541Srgrimes    if (e instanceof java.lang.SecurityException) {
331541Srgrimes        print(e);
3444510Swollman    } else {
351541Srgrimes        fail("expected SecurityException, got " + e);
361541Srgrimes    }
37116182Sobrien}
38116182Sobrien
39116182Sobrienvar AccessController = java.security.AccessController;
401541Srgrimesvar PrivilegedAction = java.security.PrivilegedAction;
411541Srgrimes
4233392SphkAccessController.doPrivileged(new PrivilegedAction() {
43127969Scperciva    run: function() {
441541Srgrimes        // try something sensitive
45133229Srwatson        try {
4674914Sjhb            java.lang.System.exit(0);
4768840Sjhb        } catch(e) {
48150188Sjhb            check(e);
49115810Sphk        }
501541Srgrimes
51115810Sphk	try {
52115810Sphk	    var prop = java.lang.System.getProperty("user.dir");
53115810Sphk            fail("can get user.dir " + prop);
54115810Sphk	} catch(e) {
55115810Sphk            print(e);
56115810Sphk	}
57141428Siedowse    }
58141428Siedowse});
59141428Siedowse