• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.appsecurity.cts;
18 
19 import android.platform.test.annotations.AsbSecurityTest;
20 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
21 import com.android.tradefed.build.IBuildInfo;
22 import com.android.tradefed.device.DeviceNotAvailableException;
23 import com.android.tradefed.testtype.DeviceTestCase;
24 import com.android.tradefed.testtype.IBuildReceiver;
25 
26 public class PermissionEscalationTest extends DeviceTestCase implements IBuildReceiver {
27     private static final String ESCALATE_PERMISSION_PKG = "com.android.cts.escalate.permission";
28 
29     private static final String APK_DECLARE_NON_RUNTIME_PERMISSIONS =
30             "CtsDeclareNonRuntimePermissions.apk";
31     private static final String APK_ESCLATE_TO_RUNTIME_PERMISSIONS =
32             "CtsEscalateToRuntimePermissions.apk";
33 
34     private CompatibilityBuildHelper mBuildHelper;
35 
36     @Override
setBuild(IBuildInfo buildInfo)37     public void setBuild(IBuildInfo buildInfo) {
38         mBuildHelper = new CompatibilityBuildHelper(buildInfo);
39     }
40 
41     @Override
setUp()42     protected void setUp() throws Exception {
43         super.setUp();
44 
45         Utils.prepareSingleUser(getDevice());
46         assertNotNull(mBuildHelper);
47 
48         getDevice().uninstallPackage(ESCALATE_PERMISSION_PKG);
49     }
50 
51     @Override
tearDown()52     protected void tearDown() throws Exception {
53         super.tearDown();
54 
55         getDevice().uninstallPackage(ESCALATE_PERMISSION_PKG);
56     }
57 
testNoPermissionEscalation()58     public void testNoPermissionEscalation() throws Exception {
59         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
60                 APK_DECLARE_NON_RUNTIME_PERMISSIONS), false, false));
61         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
62                 APK_ESCLATE_TO_RUNTIME_PERMISSIONS), true, false));
63         runDeviceTests(ESCALATE_PERMISSION_PKG,
64                 "com.android.cts.escalatepermission.PermissionEscalationTest",
65                 "testCannotEscalateNonRuntimePermissionsToRuntime");
66     }
67 
68     @AsbSecurityTest(cveBugId = {154505240, 168319670})
testNoPermissionEscalationAfterReboot()69     public void testNoPermissionEscalationAfterReboot() throws Exception {
70         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
71                 APK_DECLARE_NON_RUNTIME_PERMISSIONS), false, false));
72         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
73                 APK_ESCLATE_TO_RUNTIME_PERMISSIONS), true, false));
74         getDevice().reboot();
75         runDeviceTests(ESCALATE_PERMISSION_PKG,
76                 "com.android.cts.escalatepermission.PermissionEscalationTest",
77                 "testRuntimePermissionsAreNotGranted");
78     }
79 
runDeviceTests(String packageName, String testClassName, String testMethodName)80     private void runDeviceTests(String packageName, String testClassName, String testMethodName)
81             throws DeviceNotAvailableException {
82         Utils.runDeviceTestsAsCurrentUser(getDevice(), packageName, testClassName, testMethodName);
83     }
84 }
85