18556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson/* 28556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * Copyright (C) 2010 The Android Open Source Project 38556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * 48556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * Licensed under the Apache License, Version 2.0 (the "License"); 58556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * you may not use this file except in compliance with the License. 68556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * You may obtain a copy of the License at 78556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * 88556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * http://www.apache.org/licenses/LICENSE-2.0 98556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * 108556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * Unless required by applicable law or agreed to in writing, software 118556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * distributed under the License is distributed on an "AS IS" BASIS, 128556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 138556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * See the License for the specific language governing permissions and 148556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson * limitations under the License. 158556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson */ 168556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson 174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.security; 188556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson 194557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.AccessControlContext; 204557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.AccessController; 214557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.DomainCombiner; 224557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Permission; 234557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Permissions; 244557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.PrivilegedAction; 254557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.ProtectionDomain; 2687ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilsonimport java.util.concurrent.atomic.AtomicInteger; 2787ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilsonimport junit.framework.AssertionFailedError; 288556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilsonimport junit.framework.TestCase; 298556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson 3087ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson/** 3187ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson * Android doesn't fully support access controller. This tests that actions are 3287ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson * passed through without permission enforcement. 3387ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson */ 348556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilsonpublic final class AccessControllerTest extends TestCase { 358556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson 368556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson public void testDoPrivilegedWithCombiner() { 378556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson final Permission permission = new RuntimePermission("do stuff"); 388556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson final DomainCombiner union = new DomainCombiner() { 398556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson public ProtectionDomain[] combine(ProtectionDomain[] a, ProtectionDomain[] b) { 4087ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson throw new AssertionFailedError("Expected combiner to be unused"); 418556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson } 428556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson }; 438556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson 448556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson ProtectionDomain protectionDomain = new ProtectionDomain(null, new Permissions()); 458556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson AccessControlContext accessControlContext = new AccessControlContext( 468556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson new AccessControlContext(new ProtectionDomain[] { protectionDomain }), union); 478556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson 4887ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson final AtomicInteger actionCount = new AtomicInteger(); 4987ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson 508556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson AccessController.doPrivileged(new PrivilegedAction<Void>() { 518556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson public Void run() { 5287ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson assertEquals(null, AccessController.getContext().getDomainCombiner()); 5387ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson AccessController.getContext().checkPermission(permission); 548556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson 5587ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson // Calling doPrivileged again would have exercised the combiner 568556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson AccessController.doPrivileged(new PrivilegedAction<Void>() { 578556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson public Void run() { 5887ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson actionCount.incrementAndGet(); 5987ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson assertEquals(null, AccessController.getContext().getDomainCombiner()); 6087ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson AccessController.getContext().checkPermission(permission); 618556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson return null; 628556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson } 638556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson }); 648556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson 658556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson return null; 668556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson } 678556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson }, accessControlContext); 688556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson 6987ac762d0ba5fd6c2c4bc5d3265df41f5580054dJesse Wilson assertEquals(1, actionCount.get()); 708556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson } 718556e0aed87cbbf5338e053f9df55a936f788f5fJesse Wilson} 72