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.compatibility.common.util.CddTest;
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 AccessSerialNumberTest extends DeviceTestCase implements IBuildReceiver {
27     private static final String APK_ACCESS_SERIAL_LEGACY = "CtsAccessSerialLegacy.apk";
28     private static final String APK_ACCESS_SERIAL_MODERN = "CtsAccessSerialModern.apk";
29     private static final String ACCESS_SERIAL_PKG = "android.os.cts";
30 
31     private CompatibilityBuildHelper mBuildHelper;
32 
33     @Override
setBuild(IBuildInfo buildInfo)34     public void setBuild(IBuildInfo buildInfo) {
35         mBuildHelper = new CompatibilityBuildHelper(buildInfo);
36     }
37 
runDeviceTests(String packageName, String testClassName, String testMethodName)38     private void runDeviceTests(String packageName, String testClassName, String testMethodName)
39             throws DeviceNotAvailableException {
40         Utils.runDeviceTests(getDevice(), packageName, testClassName, testMethodName);
41     }
42 
43     @Override
setUp()44     protected void setUp() throws Exception {
45         super.setUp();
46 
47         Utils.prepareSingleUser(getDevice());
48         assertNotNull(mBuildHelper);
49 
50         getDevice().uninstallPackage(ACCESS_SERIAL_PKG);
51     }
52 
53     @Override
tearDown()54     protected void tearDown() throws Exception {
55         super.tearDown();
56 
57         getDevice().uninstallPackage(ACCESS_SERIAL_PKG);
58     }
59 
testSerialAccessPolicy()60     public void testSerialAccessPolicy() throws Exception {
61         // Verify legacy app behavior
62         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
63                 APK_ACCESS_SERIAL_LEGACY), false, false));
64         runDeviceTests(ACCESS_SERIAL_PKG,
65                 "android.os.cts.AccessSerialLegacyTest",
66                 "testAccessSerialNoPermissionNeeded");
67 
68         // Verify modern app behavior
69         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
70                 APK_ACCESS_SERIAL_MODERN), true, false));
71         runDeviceTests(ACCESS_SERIAL_PKG,
72                 "android.os.cts.AccessSerialModernTest",
73                 "testAccessSerialPermissionNeeded");
74     }
75 
76     @CddTest(requirement = "3.2.2/C-0-1")
testGetSerialReturnsExpectedFormat()77     public void testGetSerialReturnsExpectedFormat() throws Exception {
78         // Verify the result from Build#getSerial matches the expected regular expression
79         // as defined in the CDD.
80         assertNull(getDevice().installPackage(mBuildHelper.getTestFile(
81                 APK_ACCESS_SERIAL_MODERN), true, false));
82         runDeviceTests(ACCESS_SERIAL_PKG,
83                 "android.os.cts.AccessSerialModernTest",
84                 "testGetSerialReturnsExpectedFormat");
85     }
86 }
87