01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.security.tests.java.security;
19:
20: import java.io.File;
21: import java.security.AccessController;
22: import java.security.AllPermission;
23: import java.security.PrivilegedAction;
24:
25: import tests.support.Support_Exec;
26:
27: public class AccessController2Test extends junit.framework.TestCase {
28:
29: /**
30: * @tests java.security.AccessController#doPrivileged(java.security.PrivilegedAction)
31: */
32: public void test_doPrivilegedLjava_security_PrivilegedAction()
33: throws Exception {
34:
35: // tmp user home to avoid presence of ${user.home}/.java.policy
36: String tmpUserHome = System.getProperty("java.io.tmpdir")
37: + File.separatorChar
38: + "tmpUserHomeForAccessController2Test";
39: File dir = new File(tmpUserHome);
40: if (!dir.exists()) {
41: dir.mkdirs();
42: dir.deleteOnExit();
43: }
44: String javaPolycy = tmpUserHome + File.separatorChar
45: + ".java.policy";
46: assertFalse("There should be no java policy file: "
47: + javaPolycy, new File(javaPolycy).exists());
48:
49: String[] arg = new String[] {
50: "-Duser.home=" + tmpUserHome,
51: doPrivilegedLjava_security_PrivilegedActionTesting.class
52: .getName() };
53:
54: Support_Exec.execJava(arg, null, true);
55: }
56:
57: public static class doPrivilegedLjava_security_PrivilegedActionTesting {
58: public static void main(String[] args) {
59:
60: // Pass fail flag...
61: Boolean pass;
62:
63: // First test 1 argument version (TBD).
64:
65: // Then test 2 argument version.
66: pass = (Boolean) AccessController.doPrivileged(
67: new PrivilegedAction() {
68: public Object run() {
69: try {
70: AccessController
71: .checkPermission(new AllPermission());
72: return new Boolean(false);
73: } catch (SecurityException ex) {
74: return new Boolean(true);
75: }
76: }
77: }, null);
78: assertTrue("Got AllPermission by passing in a null PD",
79: pass.booleanValue());
80:
81: }
82: }
83: }
|