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