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.AppModeFull;
20 import android.platform.test.annotations.RequiresFlagsEnabled;
21 import android.platform.test.flag.junit.CheckFlagsRule;
22 import android.platform.test.flag.junit.host.HostFlagsValueProvider;
23 
24 import com.android.internal.pm.pkg.component.flags.Flags;
25 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner;
26 
27 import org.junit.After;
28 import org.junit.Rule;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 
32 @RunWith(DeviceJUnit4ClassRunner.class)
33 @AppModeFull
34 public final class UseEmbeddedDexTest extends BaseAppSecurityTest {
35 
36     private static final String PACKAGE_NAME = "com.android.cts.useembeddeddex";
37     private static final String APK_CANONICAL = "CtsUseEmbeddedDexApp_Canonical.apk";
38     private static final String APK_CANONICAL_PER_PROCESS =
39             "CtsUseEmbeddedDexApp_Canonical_PerProcess.apk";
40     private static final String APK_DEX_COMPRESSED = "CtsUseEmbeddedDexApp_DexCompressed.apk";
41     private static final String APK_DEX_COMPRESSED_PER_PROCESS =
42             "CtsUseEmbeddedDexApp_DexCompressed_PerProcess.apk";
43     private static final String APK_SPLIT_CANONICAL =
44             "CtsUseEmbeddedDexAppSplit_Canonical.apk";
45     private static final String APK_SPLIT_COMPRESSED_DEX =
46             "CtsUseEmbeddedDexAppSplit_CompressedDex.apk";
47 
48     @Rule
49     public final CheckFlagsRule mCheckFlagsRule =
50             HostFlagsValueProvider.createCheckFlagsRule(this::getDevice);
51 
52     @After
tearDown()53     public void tearDown() throws Exception {
54         getDevice().uninstallPackage(PACKAGE_NAME);
55     }
56 
57     @Test
testCanonicalInstall()58     public void testCanonicalInstall() throws Exception {
59         new InstallMultiple().addFile(APK_CANONICAL).run();
60     }
61 
62     @Test
63     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_PER_PROCESS_USE_EMBEDDED_DEX_ATTR)
testCanonicalInstall_perProcess()64     public void testCanonicalInstall_perProcess() throws Exception {
65         new InstallMultiple().addFile(APK_CANONICAL_PER_PROCESS).run();
66     }
67 
68     @Test
testIsolatedService()69     public void testIsolatedService() throws Exception {
70         new InstallMultiple().addFile(APK_CANONICAL).run();
71         getDevice().executeShellCommand("cmd package compile -f -m speed " + PACKAGE_NAME);
72         runDeviceTests(PACKAGE_NAME, PACKAGE_NAME + ".EmbeddedDexTest", "testIsolatedService");
73     }
74 
75     @Test
76     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_PER_PROCESS_USE_EMBEDDED_DEX_ATTR)
testIsolatedService_perProcess()77     public void testIsolatedService_perProcess() throws Exception {
78         new InstallMultiple().addFile(APK_CANONICAL_PER_PROCESS).run();
79         getDevice().executeShellCommand("cmd package compile -f -m speed " + PACKAGE_NAME);
80         runDeviceTests(PACKAGE_NAME, PACKAGE_NAME + ".EmbeddedDexTest", "testIsolatedService");
81     }
82 
83     @Test
testBadInstallWithCompressedDex()84     public void testBadInstallWithCompressedDex() throws Exception {
85         new InstallMultiple().addFile(APK_DEX_COMPRESSED).runExpectingFailure();
86     }
87 
88     @Test
89     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_PER_PROCESS_USE_EMBEDDED_DEX_ATTR)
testBadInstallWithCompressedDex_perProcess()90     public void testBadInstallWithCompressedDex_perProcess() throws Exception {
91         new InstallMultiple().addFile(APK_DEX_COMPRESSED_PER_PROCESS).runExpectingFailure();
92     }
93 
94     @Test
testCanonicalInstallWithSplit()95     public void testCanonicalInstallWithSplit() throws Exception {
96         new InstallMultiple().addFile(APK_CANONICAL).addFile(APK_SPLIT_CANONICAL).run();
97     }
98 
99     @Test
100     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_PER_PROCESS_USE_EMBEDDED_DEX_ATTR)
testCanonicalInstallWithSplit_perProcess()101     public void testCanonicalInstallWithSplit_perProcess() throws Exception {
102         new InstallMultiple().addFile(APK_CANONICAL_PER_PROCESS).addFile(APK_SPLIT_CANONICAL).run();
103     }
104 
105     @Test
testBadInstallWithDexCompressedSplit()106     public void testBadInstallWithDexCompressedSplit() throws Exception {
107         new InstallMultiple().addFile(APK_CANONICAL).addFile(APK_SPLIT_COMPRESSED_DEX)
108                 .runExpectingFailure();
109     }
110 
111     @Test
112     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_PER_PROCESS_USE_EMBEDDED_DEX_ATTR)
testBadInstallWithDexCompressedSplit_perProcess()113     public void testBadInstallWithDexCompressedSplit_perProcess() throws Exception {
114         new InstallMultiple().addFile(APK_CANONICAL_PER_PROCESS).addFile(APK_SPLIT_COMPRESSED_DEX)
115                 .runExpectingFailure();
116     }
117 
118     @Test
testCanonicalInstallWithBaseThenSplit()119     public void testCanonicalInstallWithBaseThenSplit() throws Exception {
120         new InstallMultiple().addFile(APK_CANONICAL).run();
121         new InstallMultiple().inheritFrom(PACKAGE_NAME).addFile(APK_SPLIT_CANONICAL).run();
122     }
123 
124     @Test
125     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_PER_PROCESS_USE_EMBEDDED_DEX_ATTR)
testCanonicalInstallWithBaseThenSplit_perProcess()126     public void testCanonicalInstallWithBaseThenSplit_perProcess() throws Exception {
127         new InstallMultiple().addFile(APK_CANONICAL_PER_PROCESS).run();
128         new InstallMultiple().inheritFrom(PACKAGE_NAME).addFile(APK_SPLIT_CANONICAL).run();
129     }
130 
131     @Test
testBadInstallWithBaseThenDexCompressedSplit()132     public void testBadInstallWithBaseThenDexCompressedSplit() throws Exception {
133         new InstallMultiple().addFile(APK_CANONICAL).run();
134         new InstallMultiple().inheritFrom(PACKAGE_NAME).addFile(APK_SPLIT_COMPRESSED_DEX)
135                 .runExpectingFailure();
136     }
137 
138     @Test
139     @RequiresFlagsEnabled(Flags.FLAG_ENABLE_PER_PROCESS_USE_EMBEDDED_DEX_ATTR)
testBadInstallWithBaseThenDexCompressedSplit_perProcess()140     public void testBadInstallWithBaseThenDexCompressedSplit_perProcess() throws Exception {
141         new InstallMultiple().addFile(APK_CANONICAL_PER_PROCESS).run();
142         new InstallMultiple().inheritFrom(PACKAGE_NAME).addFile(APK_SPLIT_COMPRESSED_DEX)
143                 .runExpectingFailure();
144     }
145 }
146