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