1 /*
2  * Copyright (C) 2018 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.testtype.DeviceTestCase;
23 import com.android.tradefed.testtype.IBuildReceiver;
24 
25 public class AccessSerialNumberTest extends DeviceTestCase implements IBuildReceiver {
26     private static final String APK_ACCESS_SERIAL_LEGACY = "CtsAccessSerialLegacy.apk";
27     private static final String APK_ACCESS_SERIAL_MODERN = "CtsAccessSerialModern.apk";
28     private static final String ACCESS_SERIAL_PKG = "android.os.cts";
29 
30     private CompatibilityBuildHelper mBuildHelper;
31 
32     @Override
setBuild(IBuildInfo buildInfo)33     public void setBuild(IBuildInfo buildInfo) {
34         mBuildHelper = new CompatibilityBuildHelper(buildInfo);
35     }
36 
runDeviceTests(String packageName, String testClassName, String testMethodName)37     private void runDeviceTests(String packageName, String testClassName, String testMethodName)
38             throws DeviceNotAvailableException {
39         Utils.runDeviceTests(getDevice(), packageName, testClassName, testMethodName);
40     }
41 
42     @Override
setUp()43     protected void setUp() throws Exception {
44         super.setUp();
45 
46         Utils.prepareSingleUser(getDevice());
47         assertNotNull(mBuildHelper);
48 
49         getDevice().uninstallPackage(ACCESS_SERIAL_PKG);
50     }
51 
52     @Override
tearDown()53     protected void tearDown() throws Exception {
54         super.tearDown();
55 
56         getDevice().uninstallPackage(ACCESS_SERIAL_PKG);
57     }
58 
testSerialAccessPolicy()59     public void testSerialAccessPolicy() throws Exception {
60         // Verify legacy app behavior
61         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
62                 APK_ACCESS_SERIAL_LEGACY), false, false));
63         runDeviceTests(ACCESS_SERIAL_PKG,
64                 "android.os.cts.AccessSerialLegacyTest",
65                 "testAccessSerialNoPermissionNeeded");
66 
67         // Verify modern app behavior
68         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
69                 APK_ACCESS_SERIAL_MODERN), true, false));
70         runDeviceTests(ACCESS_SERIAL_PKG,
71                 "android.os.cts.AccessSerialModernTest",
72                 "testAccessSerialPermissionNeeded");
73     }
74 }
75