| 1 |
/* |
| 2 |
* Copyright (C) 2022 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.art; |
| 18 |
|
| 19 |
import static com.android.server.art.PrimaryDexUtils.DetailedPrimaryDexInfo; |
| 20 |
import static com.android.server.art.PrimaryDexUtils.PrimaryDexInfo; |
| 21 |
|
| 22 |
import static com.google.common.truth.Truth.assertThat; |
| 23 |
|
| 24 |
import static org.mockito.Mockito.lenient; |
| 25 |
import static org.mockito.Mockito.mock; |
| 26 |
|
| 27 |
import androidx.test.filters.SmallTest; |
| 28 |
|
| 29 |
import com.android.server.pm.pkg.AndroidPackage; |
| 30 |
import com.android.server.pm.pkg.AndroidPackageSplit; |
| 31 |
import com.android.server.pm.pkg.PackageState; |
| 32 |
import com.android.server.pm.pkg.SharedLibrary; |
| 33 |
|
| 34 |
import dalvik.system.DelegateLastClassLoader; |
| 35 |
import dalvik.system.DexClassLoader; |
| 36 |
import dalvik.system.PathClassLoader; |
| 37 |
|
| 38 |
import org.junit.Before; |
| 39 |
import org.junit.Test; |
| 40 |
import org.junit.runner.RunWith; |
| 41 |
import org.mockito.junit.MockitoJUnitRunner; |
| 42 |
|
| 43 |
import java.util.ArrayList; |
| 44 |
import java.util.List; |
| 45 |
|
| 46 |
@SmallTest |
| 47 |
@RunWith(MockitoJUnitRunner.StrictStubs.class) |
| 48 |
public class PrimaryDexUtilsTest { |
| 49 |
@Before |
| 50 |
public void setUp() {} |
| 51 |
|
| 52 |
@Test |
| 53 |
public void testGetDexInfo() { |
| 54 |
List<PrimaryDexInfo> infos = |
| 55 |
PrimaryDexUtils.getDexInfo(createPackage(false /* isIsolatedSplitLoading */)); |
| 56 |
checkBasicInfo(infos); |
| 57 |
} |
| 58 |
|
| 59 |
@Test |
| 60 |
public void testGetDetailedDexInfo() { |
| 61 |
List<DetailedPrimaryDexInfo> infos = PrimaryDexUtils.getDetailedDexInfo( |
| 62 |
createPackageState(), createPackage(false /* isIsolatedSplitLoading */)); |
| 63 |
checkBasicInfo(infos); |
| 64 |
|
| 65 |
String sharedLibrariesContext = "{" |
| 66 |
+ "PCL[library_2.jar]{PCL[library_1_dex_1.jar:library_1_dex_2.jar]}#" |
| 67 |
+ "PCL[library_3.jar]#" |
| 68 |
+ "PCL[library_4.jar]{PCL[library_1_dex_1.jar:library_1_dex_2.jar]}" |
| 69 |
+ "}"; |
| 70 |
|
| 71 |
assertThat(infos.get(0).classLoaderContext()).isEqualTo("PCL[]" + sharedLibrariesContext); |
| 72 |
assertThat(infos.get(1).classLoaderContext()) |
| 73 |
.isEqualTo("PCL[base.apk]" + sharedLibrariesContext); |
| 74 |
assertThat(infos.get(2).classLoaderContext()).isEqualTo(null); |
| 75 |
assertThat(infos.get(3).classLoaderContext()) |
| 76 |
.isEqualTo("PCL[base.apk:split_0.apk:split_1.apk]" + sharedLibrariesContext); |
| 77 |
assertThat(infos.get(4).classLoaderContext()) |
| 78 |
.isEqualTo("PCL[base.apk:split_0.apk:split_1.apk:split_2.apk]" |
| 79 |
+ sharedLibrariesContext); |
| 80 |
} |
| 81 |
|
| 82 |
@Test |
| 83 |
public void testGetDetailedDexInfoIsolated() { |
| 84 |
List<DetailedPrimaryDexInfo> infos = PrimaryDexUtils.getDetailedDexInfo( |
| 85 |
createPackageState(), createPackage(true /* isIsolatedSplitLoading */)); |
| 86 |
checkBasicInfo(infos); |
| 87 |
|
| 88 |
String sharedLibrariesContext = "{" |
| 89 |
+ "PCL[library_2.jar]{PCL[library_1_dex_1.jar:library_1_dex_2.jar]}#" |
| 90 |
+ "PCL[library_3.jar]#" |
| 91 |
+ "PCL[library_4.jar]{PCL[library_1_dex_1.jar:library_1_dex_2.jar]}" |
| 92 |
+ "}"; |
| 93 |
|
| 94 |
assertThat(infos.get(0).classLoaderContext()).isEqualTo("PCL[]" + sharedLibrariesContext); |
| 95 |
assertThat(infos.get(1).classLoaderContext()) |
| 96 |
.isEqualTo("PCL[];DLC[split_2.apk];PCL[base.apk]" + sharedLibrariesContext); |
| 97 |
assertThat(infos.get(2).classLoaderContext()).isEqualTo(null); |
| 98 |
assertThat(infos.get(3).classLoaderContext()) |
| 99 |
.isEqualTo("DLC[];PCL[base.apk]" + sharedLibrariesContext); |
| 100 |
assertThat(infos.get(4).classLoaderContext()).isEqualTo("PCL[]"); |
| 101 |
assertThat(infos.get(5).classLoaderContext()).isEqualTo("PCL[];PCL[split_3.apk]"); |
| 102 |
} |
| 103 |
|
| 104 |
private <T extends PrimaryDexInfo> void checkBasicInfo(List<T> infos) { |
| 105 |
assertThat(infos.get(0).dexPath()).isEqualTo("/data/app/foo/base.apk"); |
| 106 |
assertThat(infos.get(0).hasCode()).isTrue(); |
| 107 |
assertThat(infos.get(0).splitName()).isNull(); |
| 108 |
|
| 109 |
assertThat(infos.get(1).dexPath()).isEqualTo("/data/app/foo/split_0.apk"); |
| 110 |
assertThat(infos.get(1).hasCode()).isTrue(); |
| 111 |
assertThat(infos.get(1).splitName()).isEqualTo("split_0"); |
| 112 |
|
| 113 |
assertThat(infos.get(2).dexPath()).isEqualTo("/data/app/foo/split_1.apk"); |
| 114 |
assertThat(infos.get(2).hasCode()).isFalse(); |
| 115 |
assertThat(infos.get(2).splitName()).isEqualTo("split_1"); |
| 116 |
|
| 117 |
assertThat(infos.get(3).dexPath()).isEqualTo("/data/app/foo/split_2.apk"); |
| 118 |
assertThat(infos.get(3).hasCode()).isTrue(); |
| 119 |
assertThat(infos.get(3).splitName()).isEqualTo("split_2"); |
| 120 |
|
| 121 |
assertThat(infos.get(4).dexPath()).isEqualTo("/data/app/foo/split_3.apk"); |
| 122 |
assertThat(infos.get(4).hasCode()).isTrue(); |
| 123 |
assertThat(infos.get(4).splitName()).isEqualTo("split_3"); |
| 124 |
|
| 125 |
assertThat(infos.get(5).dexPath()).isEqualTo("/data/app/foo/split_4.apk"); |
| 126 |
assertThat(infos.get(5).hasCode()).isTrue(); |
| 127 |
assertThat(infos.get(5).splitName()).isEqualTo("split_4"); |
| 128 |
} |
| 129 |
|
| 130 |
private AndroidPackage createPackage(boolean isIsolatedSplitLoading) { |
| 131 |
AndroidPackage pkg = mock(AndroidPackage.class); |
| 132 |
|
| 133 |
var baseSplit = mock(AndroidPackageSplit.class); |
| 134 |
lenient().when(baseSplit.getPath()).thenReturn("/data/app/foo/base.apk"); |
| 135 |
lenient().when(baseSplit.isHasCode()).thenReturn(true); |
| 136 |
lenient().when(baseSplit.getClassLoaderName()).thenReturn(PathClassLoader.class.getName()); |
| 137 |
|
| 138 |
var split0 = mock(AndroidPackageSplit.class); |
| 139 |
lenient().when(split0.getName()).thenReturn("split_0"); |
| 140 |
lenient().when(split0.getPath()).thenReturn("/data/app/foo/split_0.apk"); |
| 141 |
lenient().when(split0.isHasCode()).thenReturn(true); |
| 142 |
|
| 143 |
var split1 = mock(AndroidPackageSplit.class); |
| 144 |
lenient().when(split1.getName()).thenReturn("split_1"); |
| 145 |
lenient().when(split1.getPath()).thenReturn("/data/app/foo/split_1.apk"); |
| 146 |
lenient().when(split1.isHasCode()).thenReturn(false); |
| 147 |
|
| 148 |
var split2 = mock(AndroidPackageSplit.class); |
| 149 |
lenient().when(split2.getName()).thenReturn("split_2"); |
| 150 |
lenient().when(split2.getPath()).thenReturn("/data/app/foo/split_2.apk"); |
| 151 |
lenient().when(split2.isHasCode()).thenReturn(true); |
| 152 |
|
| 153 |
var split3 = mock(AndroidPackageSplit.class); |
| 154 |
lenient().when(split3.getName()).thenReturn("split_3"); |
| 155 |
lenient().when(split3.getPath()).thenReturn("/data/app/foo/split_3.apk"); |
| 156 |
lenient().when(split3.isHasCode()).thenReturn(true); |
| 157 |
|
| 158 |
var split4 = mock(AndroidPackageSplit.class); |
| 159 |
lenient().when(split4.getName()).thenReturn("split_4"); |
| 160 |
lenient().when(split4.getPath()).thenReturn("/data/app/foo/split_4.apk"); |
| 161 |
lenient().when(split4.isHasCode()).thenReturn(true); |
| 162 |
|
| 163 |
var splits = List.of(baseSplit, split0, split1, split2, split3, split4); |
| 164 |
lenient().when(pkg.getSplits()).thenReturn(splits); |
| 165 |
|
| 166 |
if (isIsolatedSplitLoading) { |
| 167 |
// split_0: PCL(PathClassLoader), depends on split_2. |
| 168 |
// split_1: no code. |
| 169 |
// split_2: DLC(DelegateLastClassLoader), depends on base. |
| 170 |
// split_3: PCL(DexClassLoader), no dependency. |
| 171 |
// split_4: PCL(null), depends on split_3. |
| 172 |
lenient().when(split0.getClassLoaderName()).thenReturn(PathClassLoader.class.getName()); |
| 173 |
lenient().when(split1.getClassLoaderName()).thenReturn(null); |
| 174 |
lenient() |
| 175 |
.when(split2.getClassLoaderName()) |
| 176 |
.thenReturn(DelegateLastClassLoader.class.getName()); |
| 177 |
lenient().when(split3.getClassLoaderName()).thenReturn(DexClassLoader.class.getName()); |
| 178 |
lenient().when(split4.getClassLoaderName()).thenReturn(null); |
| 179 |
|
| 180 |
lenient().when(split0.getDependencies()).thenReturn(List.of(split2)); |
| 181 |
lenient().when(split2.getDependencies()).thenReturn(List.of(baseSplit)); |
| 182 |
lenient().when(split4.getDependencies()).thenReturn(List.of(split3)); |
| 183 |
lenient().when(pkg.isIsolatedSplitLoading()).thenReturn(true); |
| 184 |
} else { |
| 185 |
lenient().when(pkg.isIsolatedSplitLoading()).thenReturn(false); |
| 186 |
} |
| 187 |
|
| 188 |
return pkg; |
| 189 |
} |
| 190 |
|
| 191 |
private PackageState createPackageState() { |
| 192 |
PackageState pkgState = mock(PackageState.class); |
| 193 |
|
| 194 |
lenient().when(pkgState.getPackageName()).thenReturn("com.example.foo"); |
| 195 |
|
| 196 |
// Base depends on library 2, 3, 4. |
| 197 |
// Library 2, 4 depends on library 1. |
| 198 |
List<SharedLibrary> usesLibraryInfos = new ArrayList<>(); |
| 199 |
|
| 200 |
// The native library should not be added to the CLC. |
| 201 |
SharedLibrary libraryNative = mock(SharedLibrary.class); |
| 202 |
lenient().when(libraryNative.getAllCodePaths()).thenReturn(List.of("library_native.so")); |
| 203 |
lenient().when(libraryNative.getDependencies()).thenReturn(null); |
| 204 |
lenient().when(libraryNative.isNative()).thenReturn(true); |
| 205 |
usesLibraryInfos.add(libraryNative); |
| 206 |
|
| 207 |
SharedLibrary library1 = mock(SharedLibrary.class); |
| 208 |
lenient() |
| 209 |
.when(library1.getAllCodePaths()) |
| 210 |
.thenReturn(List.of("library_1_dex_1.jar", "library_1_dex_2.jar")); |
| 211 |
lenient().when(library1.getDependencies()).thenReturn(null); |
| 212 |
lenient().when(library1.isNative()).thenReturn(false); |
| 213 |
|
| 214 |
SharedLibrary library2 = mock(SharedLibrary.class); |
| 215 |
lenient().when(library2.getAllCodePaths()).thenReturn(List.of("library_2.jar")); |
| 216 |
lenient().when(library2.getDependencies()).thenReturn(List.of(library1, libraryNative)); |
| 217 |
lenient().when(library2.isNative()).thenReturn(false); |
| 218 |
usesLibraryInfos.add(library2); |
| 219 |
|
| 220 |
SharedLibrary library3 = mock(SharedLibrary.class); |
| 221 |
lenient().when(library3.getAllCodePaths()).thenReturn(List.of("library_3.jar")); |
| 222 |
lenient().when(library3.getDependencies()).thenReturn(null); |
| 223 |
lenient().when(library3.isNative()).thenReturn(false); |
| 224 |
usesLibraryInfos.add(library3); |
| 225 |
|
| 226 |
SharedLibrary library4 = mock(SharedLibrary.class); |
| 227 |
lenient().when(library4.getAllCodePaths()).thenReturn(List.of("library_4.jar")); |
| 228 |
lenient().when(library4.getDependencies()).thenReturn(List.of(library1)); |
| 229 |
lenient().when(library4.isNative()).thenReturn(false); |
| 230 |
usesLibraryInfos.add(library4); |
| 231 |
|
| 232 |
lenient().when(pkgState.getSharedLibraryDependencies()).thenReturn(usesLibraryInfos); |
| 233 |
|
| 234 |
return pkgState; |
| 235 |
} |
| 236 |
} |