1 /* 2 * Copyright (C) 2019 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.log.LogUtil; 24 import com.android.tradefed.testtype.DeviceTestCase; 25 import com.android.tradefed.testtype.IBuildReceiver; 26 27 /** 28 * Hostside test to verify an app with the READ_DEVICE_IDENTIFIERS app op set can read device 29 * identifiers. 30 */ 31 public class DeviceIdentifierTest extends DeviceTestCase implements IBuildReceiver { 32 private static final String DEVICE_IDENTIFIER_APK = "CtsAccessDeviceIdentifiers.apk"; 33 private static final String DEVICE_IDENTIFIER_PKG = "android.appsecurity.cts.deviceids"; 34 private static final String DEVICE_IDENTIFIER_CLASS = 35 DEVICE_IDENTIFIER_PKG + ".DeviceIdentifierAppOpTest"; 36 private static final String DEVICE_IDENTIFIER_TEST_METHOD = 37 "testAccessToDeviceIdentifiersWithAppOp"; 38 39 private CompatibilityBuildHelper mBuildHelper; 40 41 @Override setBuild(IBuildInfo buildInfo)42 public void setBuild(IBuildInfo buildInfo) { 43 mBuildHelper = new CompatibilityBuildHelper(buildInfo); 44 } 45 46 @Override setUp()47 protected void setUp() throws Exception { 48 super.setUp(); 49 assertNotNull(mBuildHelper); 50 assertNull( 51 getDevice().installPackage(mBuildHelper.getTestFile(DEVICE_IDENTIFIER_APK), false, 52 true)); 53 } 54 55 @Override tearDown()56 protected void tearDown() throws Exception { 57 super.tearDown(); 58 getDevice().uninstallPackage(DEVICE_IDENTIFIER_PKG); 59 } 60 61 @AsbSecurityTest(cveBugId = 173421434) testDeviceIdentifierAccessWithAppOpGranted()62 public void testDeviceIdentifierAccessWithAppOpGranted() throws Exception { 63 setDeviceIdentifierAccessAppOp(DEVICE_IDENTIFIER_PKG, true); 64 Utils.runDeviceTestsAsCurrentUser(getDevice(), DEVICE_IDENTIFIER_PKG, 65 DEVICE_IDENTIFIER_CLASS, 66 DEVICE_IDENTIFIER_TEST_METHOD); 67 } 68 setDeviceIdentifierAccessAppOp(String packageName, boolean allowed)69 private void setDeviceIdentifierAccessAppOp(String packageName, boolean allowed) 70 throws DeviceNotAvailableException { 71 String command = 72 "appops set --user " + getDevice().getCurrentUser() + " " + packageName + " " 73 + "READ_DEVICE_IDENTIFIERS " + (allowed ? "allow" : "deny"); 74 LogUtil.CLog.d( 75 "Output for command " + command + ": " + getDevice().executeShellCommand(command)); 76 } 77 } 78