• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 com.android.server.pm.parsing.library;
18 
19 import static com.android.server.pm.parsing.library.SharedLibraryNames.ANDROID_TEST_BASE;
20 import static com.android.server.pm.parsing.library.SharedLibraryNames.ANDROID_TEST_MOCK;
21 import static com.android.server.pm.parsing.library.SharedLibraryNames.ANDROID_TEST_RUNNER;
22 import static com.android.server.pm.parsing.library.SharedLibraryNames.ORG_APACHE_HTTP_LEGACY;
23 
24 import static com.google.common.truth.Truth.assertThat;
25 
26 import com.android.server.pm.pkg.parsing.ParsingPackage;
27 import android.os.Build;
28 import android.platform.test.annotations.Presubmit;
29 
30 import androidx.test.filters.SmallTest;
31 
32 import com.android.server.pm.parsing.library.PackageBackwardCompatibility.RemoveUnnecessaryAndroidTestBaseLibrary;
33 import com.android.server.pm.parsing.pkg.AndroidPackage;
34 import com.android.server.pm.parsing.pkg.PackageImpl;
35 import com.android.server.pm.parsing.pkg.ParsedPackage;
36 
37 import org.junit.Assume;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.junit.runners.JUnit4;
41 
42 @Presubmit
43 @SmallTest
44 @RunWith(JUnit4.class)
45 public class PackageBackwardCompatibilityTest extends PackageSharedLibraryUpdaterTest {
46 
47     @Test
null_usesLibraries_and_usesOptionalLibraries()48     public void null_usesLibraries_and_usesOptionalLibraries() {
49         ParsedPackage before = ((ParsedPackage) PackageImpl.forTesting(PACKAGE_NAME)
50                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT)
51                 .hideAsParsed());
52 
53         AndroidPackage after = ((ParsedPackage) PackageImpl.forTesting(PACKAGE_NAME)
54                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT)
55                 .hideAsParsed())
56                 .hideAsFinal();
57 
58         checkBackwardsCompatibility(before, after);
59     }
60 
61     /**
62      * Detect when the android.test.base is not on the bootclasspath.
63      *
64      * <p>This test will be ignored when org.apache.http.legacy is not on the bootclasspath and
65      * succeed otherwise. This allows a developer to ensure that the tests are being run in the
66      * correct environment.
67      */
68     @Test
detectWhenATBisOnBCP()69     public void detectWhenATBisOnBCP() {
70         Assume.assumeTrue(PackageBackwardCompatibility.bootClassPathContainsATB());
71     }
72 
73     /**
74      * Ensures that the {@link PackageBackwardCompatibility} uses {@link OrgApacheHttpLegacyUpdater}
75      * and {@link AndroidTestBaseUpdater} when necessary.
76      *
77      * <p>More comprehensive tests for that class can be found in
78      * {@link OrgApacheHttpLegacyUpdaterTest}.
79      */
80     @Test
targeted_at_O()81     public void targeted_at_O() {
82         ParsedPackage before = ((ParsedPackage) PackageImpl.forTesting(PACKAGE_NAME)
83                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT)
84                 .setTargetSdkVersion(Build.VERSION_CODES.O)
85                 .hideAsParsed());
86 
87         ParsingPackage after = PackageImpl.forTesting(PACKAGE_NAME)
88                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT)
89                 .setTargetSdkVersion(Build.VERSION_CODES.O);
90 
91         if (!PackageBackwardCompatibility.bootClassPathContainsATB()) {
92             after.addUsesLibrary(ANDROID_TEST_BASE);
93         }
94         after.addUsesLibrary(ORG_APACHE_HTTP_LEGACY);
95 
96         checkBackwardsCompatibility(before, ((ParsedPackage) after.hideAsParsed()).hideAsFinal());
97     }
98 
99     /**
100      * Ensures that the {@link PackageBackwardCompatibility} uses
101      * {@link RemoveUnnecessaryAndroidTestBaseLibrary}
102      * when necessary.
103      *
104      * <p>More comprehensive tests for that class can be found in
105      * {@link RemoveUnnecessaryAndroidTestBaseLibraryTest}.
106      */
107     @Test
android_test_base_in_usesLibraries()108     public void android_test_base_in_usesLibraries() {
109         Assume.assumeTrue("Test requires that "
110                         + ANDROID_TEST_BASE + " is on the bootclasspath",
111                 PackageBackwardCompatibility.bootClassPathContainsATB());
112 
113         ParsedPackage before = ((ParsedPackage) PackageImpl.forTesting(PACKAGE_NAME)
114                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT)
115                 .addUsesLibrary(ANDROID_TEST_BASE)
116                 .hideAsParsed());
117 
118         // android.test.base should be removed from the libraries because it is provided
119         // on the bootclasspath and providing both increases start up cost unnecessarily.
120         AndroidPackage after = ((ParsedPackage) PackageImpl.forTesting(PACKAGE_NAME)
121                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT)
122                 .hideAsParsed())
123                 .hideAsFinal();
124 
125         checkBackwardsCompatibility(before, after);
126     }
127 
128     /**
129      * Ensures that the {@link PackageBackwardCompatibility} uses a
130      * {@link PackageBackwardCompatibility.AndroidTestRunnerSplitUpdater}.
131      *
132      * <p>More comprehensive tests for that class can be found in
133      * {@link AndroidTestRunnerSplitUpdaterTest}.
134      */
135     @Test
android_test_runner_in_usesLibraries()136     public void android_test_runner_in_usesLibraries() {
137         ParsedPackage before = ((ParsedPackage) PackageImpl.forTesting(PACKAGE_NAME)
138                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT)
139                 .addUsesLibrary(ANDROID_TEST_RUNNER)
140                 .hideAsParsed());
141 
142         ParsingPackage after = PackageImpl.forTesting(PACKAGE_NAME)
143                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT);
144         if (!PackageBackwardCompatibility.bootClassPathContainsATB()) {
145             after.addUsesLibrary(ANDROID_TEST_BASE);
146         }
147         after.addUsesLibrary(ANDROID_TEST_MOCK);
148         after.addUsesLibrary(ANDROID_TEST_RUNNER);
149 
150         checkBackwardsCompatibility(before, ((ParsedPackage) after.hideAsParsed()).hideAsFinal());
151     }
152 
153     /**
154      * Ensures that the {@link PackageBackwardCompatibility} uses a
155      * {@link ComGoogleAndroidMapsUpdater}.
156      */
157     @Test
com_google_android_maps_in_usesLibraries()158     public void com_google_android_maps_in_usesLibraries() {
159         ParsedPackage before = ((ParsedPackage) PackageImpl.forTesting(PACKAGE_NAME)
160                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT)
161                 .addUsesLibrary("com.google.android.maps")
162                 .hideAsParsed());
163 
164         ParsingPackage after = PackageImpl.forTesting(PACKAGE_NAME)
165                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT);
166 
167         checkBackwardsCompatibility(before, ((ParsedPackage) after.hideAsParsed()).hideAsFinal());
168     }
169 
170     /**
171      * Ensures that the {@link PackageBackwardCompatibility} uses a
172      * {@link AndroidNetIpSecIkeUpdater}.
173      */
174     @Test
android_net_ipsec_ike_in_usesLibraries()175     public void android_net_ipsec_ike_in_usesLibraries() {
176         ParsedPackage before = ((ParsedPackage) PackageImpl.forTesting(PACKAGE_NAME)
177                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT)
178                 .addUsesLibrary("android.net.ipsec.ike")
179                 .hideAsParsed());
180 
181         ParsingPackage after = PackageImpl.forTesting(PACKAGE_NAME)
182                 .setTargetSdkVersion(Build.VERSION_CODES.CUR_DEVELOPMENT);
183 
184         checkBackwardsCompatibility(before, ((ParsedPackage) after.hideAsParsed()).hideAsFinal());
185     }
186 
187     /**
188      * Ensures that ApexSharedLibraryUpdater is the last updater in the list of package updaters
189      * used by PackageBackwardCompatibility.
190      *
191      * This is required so mainline can add and remove libraries installed by the platform updaters.
192      */
193     @Test
testApexPackageUpdaterOrdering()194     public void testApexPackageUpdaterOrdering() {
195         PackageBackwardCompatibility instance =
196                 (PackageBackwardCompatibility) PackageBackwardCompatibility.getInstance();
197         PackageSharedLibraryUpdater[] updaterArray = instance.getPackageUpdaters();
198 
199         PackageSharedLibraryUpdater lastUpdater = updaterArray[updaterArray.length - 1];
200         assertThat(lastUpdater).isInstanceOf(ApexSharedLibraryUpdater.class);
201     }
202 
checkBackwardsCompatibility(ParsedPackage before, AndroidPackage after)203     private void checkBackwardsCompatibility(ParsedPackage before, AndroidPackage after) {
204         checkBackwardsCompatibility(before, after, PackageBackwardCompatibility::getInstance);
205     }
206 }
207