AccessControlContextTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements.  See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License.  You may obtain a copy of the License at
8 *
9 *     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
18package tests.api.java.security;
19
20import java.security.AccessControlContext;
21import java.security.AccessController;
22import java.security.Permission;
23import java.security.PermissionCollection;
24import java.security.ProtectionDomain;
25import java.util.PropertyPermission;
26
27public class AccessControlContextTest extends junit.framework.TestCase {
28
29	/**
30	 * @tests java.security.AccessControlContext#AccessControlContext(java.security.ProtectionDomain[])
31	 */
32	public void test_Constructor$Ljava_security_ProtectionDomain() {
33		// Test for method
34		// java.security.AccessControlContext(java.security.ProtectionDomain [])
35
36		// Create a permission which is not normally granted
37		final Permission perm = new PropertyPermission("java.class.path",
38				"read");
39		PermissionCollection col = perm.newPermissionCollection();
40		col.add(perm);
41		final ProtectionDomain pd = new ProtectionDomain(null, col);
42		AccessControlContext acc = new AccessControlContext(
43				new ProtectionDomain[] { pd });
44		try {
45			acc.checkPermission(perm);
46		} catch (SecurityException e) {
47			fail("Should have permission");
48		}
49
50		final boolean[] result = new boolean[] { false };
51		Thread th = new Thread(new Runnable() {
52			public void run() {
53				AccessControlContext acc = new AccessControlContext(
54						new ProtectionDomain[] { pd });
55				try {
56					acc.checkPermission(perm);
57					result[0] = true;
58				} catch (SecurityException e) {
59				}
60			}
61		});
62		th.start();
63		try {
64			th.join();
65		} catch (InterruptedException e) {
66			// ignore
67		}
68		assertTrue("Thread should have permission", result[0]);
69	}
70
71	/**
72	 * @tests java.security.AccessControlContext#AccessControlContext(java.security.AccessControlContext,
73	 *        java.security.DomainCombiner)
74	 */
75	public void test_ConstructorLjava_security_AccessControlContextLjava_security_DomainCombiner() {
76		AccessControlContext context = AccessController.getContext();
77		try {
78			new AccessControlContext(context, null);
79		} catch (NullPointerException e) {
80			fail("should not throw NullPointerException");
81		}
82	}
83}