1 /*
2  * Copyright (C) 2017 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 static org.junit.Assert.assertTrue;
20 
21 import android.platform.test.annotations.AppModeFull;
22 import android.platform.test.annotations.AppModeInstant;
23 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
24 
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 /**
31  * Test that install of apps using major version codes is being handled properly.
32  */
33 @RunWith(DeviceJUnit4ClassRunner.class)
34 public class MajorVersionTest extends BaseAppSecurityTest {
35     private static final String PKG = "com.android.cts.majorversion";
36     private static final String APK_000000000000ffff = "CtsMajorVersion000000000000ffff.apk";
37     private static final String APK_00000000ffffffff = "CtsMajorVersion00000000ffffffff.apk";
38     private static final String APK_000000ff00000000 = "CtsMajorVersion000000ff00000000.apk";
39     private static final String APK_000000ffffffffff = "CtsMajorVersion000000ffffffffff.apk";
40 
41     @Before
setUp()42     public void setUp() throws Exception {
43         Utils.prepareSingleUser(getDevice());
44         getDevice().uninstallPackage(PKG);
45     }
46 
47     @After
tearDown()48     public void tearDown() throws Exception {
49         getDevice().uninstallPackage(PKG);
50     }
51 
52     @Test
53     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallMinorVersion_full()54     public void testInstallMinorVersion_full() throws Exception {
55         testInstallMinorVersion(false);
56     }
57     @Test
58     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallMinorVersion_instant()59     public void testInstallMinorVersion_instant() throws Exception {
60         testInstallMinorVersion(true);
61     }
testInstallMinorVersion(boolean instant)62     private void testInstallMinorVersion(boolean instant) throws Exception {
63         new InstallMultiple(instant).addFile(APK_000000000000ffff).run();
64         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
65         runVersionDeviceTests("testCheckVersion");
66     }
67 
68     @Test
69     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallMajorVersion_full()70     public void testInstallMajorVersion_full() throws Exception {
71         testInstallMajorVersion(false);
72     }
73     @Test
74     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallMajorVersion_instant()75     public void testInstallMajorVersion_instant() throws Exception {
76         testInstallMajorVersion(true);
77     }
testInstallMajorVersion(boolean instant)78     private void testInstallMajorVersion(boolean instant) throws Exception {
79         new InstallMultiple(instant).addFile(APK_000000ff00000000).run();
80         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
81         runVersionDeviceTests("testCheckVersion");
82     }
83 
84     @Test
85     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallUpdateAcrossMinorMajorVersion_full()86     public void testInstallUpdateAcrossMinorMajorVersion_full() throws Exception {
87         testInstallUpdateAcrossMinorMajorVersion(false);
88     }
89     @Test
90     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallUpdateAcrossMinorMajorVersion_instant()91     public void testInstallUpdateAcrossMinorMajorVersion_instant() throws Exception {
92         testInstallUpdateAcrossMinorMajorVersion(true);
93     }
testInstallUpdateAcrossMinorMajorVersion(boolean instant)94     private void testInstallUpdateAcrossMinorMajorVersion(boolean instant) throws Exception {
95         new InstallMultiple(instant).addFile(APK_000000000000ffff).run();
96         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
97         runVersionDeviceTests("testCheckVersion");
98         new InstallMultiple(instant).addFile(APK_00000000ffffffff).run();
99         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
100         runVersionDeviceTests("testCheckVersion");
101         new InstallMultiple(instant).addFile(APK_000000ff00000000).run();
102         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
103         runVersionDeviceTests("testCheckVersion");
104         new InstallMultiple(instant).addFile(APK_000000ffffffffff).run();
105         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
106         runVersionDeviceTests("testCheckVersion");
107     }
108 
109     @Test
110     @AppModeFull(reason = "'full' portion of the hostside test")
testInstallDowngradeAcrossMajorMinorVersion_full()111     public void testInstallDowngradeAcrossMajorMinorVersion_full() throws Exception {
112         testInstallDowngradeAcrossMajorMinorVersion(false);
113     }
114     @Test
115     @AppModeInstant(reason = "'instant' portion of the hostside test")
testInstallDowngradeAcrossMajorMinorVersion_instant()116     public void testInstallDowngradeAcrossMajorMinorVersion_instant() throws Exception {
117         testInstallDowngradeAcrossMajorMinorVersion(true);
118     }
testInstallDowngradeAcrossMajorMinorVersion(boolean instant)119     private void testInstallDowngradeAcrossMajorMinorVersion(boolean instant) throws Exception {
120         new InstallMultiple(instant).addFile(APK_000000ffffffffff).run();
121         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
122         runVersionDeviceTests("testCheckVersion");
123         new InstallMultiple(instant).addFile(APK_00000000ffffffff)
124                 .runExpectingFailure("INSTALL_FAILED_VERSION_DOWNGRADE");
125         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
126         runVersionDeviceTests("testCheckVersion");
127         new InstallMultiple(instant).addFile(APK_000000ff00000000)
128                 .runExpectingFailure("INSTALL_FAILED_VERSION_DOWNGRADE");
129         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
130         runVersionDeviceTests("testCheckVersion");
131         new InstallMultiple(instant).addFile(APK_000000000000ffff)
132                 .runExpectingFailure("INSTALL_FAILED_VERSION_DOWNGRADE");
133         assertTrue(getDevice().getInstalledPackageNames().contains(PKG));
134         runVersionDeviceTests("testCheckVersion");
135     }
136 
runVersionDeviceTests(String testMethodName)137     private void runVersionDeviceTests(String testMethodName) throws Exception {
138         runDeviceTests(PKG, PKG + ".VersionTest", testMethodName);
139     }
140 }
141