Debian Codegraph @

libartservice/service/javatests/com/android/server/art/DumpHelperTest.java

raw
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.DexUseManagerLocal.DexLoader;
20 import static com.android.server.art.DexUseManagerLocal.SecondaryDexInfo;
21 import static com.android.server.art.model.DexoptStatus.DexContainerFileDexoptStatus;
22
23 import static com.google.common.truth.Truth.assertThat;
24
25 import static org.mockito.Mockito.any;
26 import static org.mockito.Mockito.argThat;
27 import static org.mockito.Mockito.eq;
28 import static org.mockito.Mockito.lenient;
29 import static org.mockito.Mockito.mock;
30
31 import android.annotation.NonNull;
32 import android.os.SystemProperties;
33
34 import androidx.test.filters.SmallTest;
35 import androidx.test.runner.AndroidJUnit4;
36
37 import com.android.server.art.model.DexoptStatus;
38 import com.android.server.art.testing.StaticMockitoRule;
39 import com.android.server.pm.PackageManagerLocal;
40 import com.android.server.pm.pkg.AndroidPackage;
41 import com.android.server.pm.pkg.PackageState;
42
43 import org.junit.Before;
44 import org.junit.Rule;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48
49 import java.io.PrintWriter;
50 import java.io.StringWriter;
51 import java.util.HashMap;
52 import java.util.HashSet;
53 import java.util.List;
54 import java.util.Map;
55 import java.util.Set;
56
57 @SmallTest
58 @RunWith(AndroidJUnit4.class)
59 public class DumpHelperTest {
60 private static final String PKG_NAME_FOO = "com.example1.foo";
61 private static final String PKG_NAME_BAR = "com.example2.bar";
62
63 @Rule
64 public StaticMockitoRule mockitoRule =
65 new StaticMockitoRule(SystemProperties.class, Constants.class);
66
67 @Mock private DumpHelper.Injector mInjector;
68 @Mock private ArtManagerLocal mArtManagerLocal;
69 @Mock private DexUseManagerLocal mDexUseManagerLocal;
70 @Mock private PackageManagerLocal.FilteredSnapshot mSnapshot;
71 @Mock private IArtd mArtd;
72
73 private DumpHelper mDumpHelper;
74
75 @Before
76 public void setUp() throws Exception {
77 lenient().when(Constants.getPreferredAbi()).thenReturn("arm64-v8a");
78 lenient().when(Constants.getNative64BitAbi()).thenReturn("arm64-v8a");
79 lenient().when(Constants.getNative32BitAbi()).thenReturn("armeabi-v7a");
80
81 // No ISA translation.
82 lenient()
83 .when(SystemProperties.get(argThat(arg -> arg.startsWith("ro.dalvik.vm.isa."))))
84 .thenReturn("");
85
86 lenient().when(mInjector.getArtManagerLocal()).thenReturn(mArtManagerLocal);
87 lenient().when(mInjector.getDexUseManager()).thenReturn(mDexUseManagerLocal);
88 lenient().when(mInjector.getArtd()).thenReturn(mArtd);
89
90 Map<String, PackageState> pkgStates = createPackageStates();
91 lenient().when(mSnapshot.getPackageStates()).thenReturn(pkgStates);
92 for (var entry : pkgStates.entrySet()) {
93 lenient().when(mSnapshot.getPackageState(entry.getKey())).thenReturn(entry.getValue());
94 }
95
96 setUpForFoo();
97 setUpForBar();
98
99 mDumpHelper = new DumpHelper(mInjector);
100 }
101
102 @Test
103 public void testDump() throws Exception {
104 String expected = "[com.example1.foo]\n"
105 + " path: /data/app/foo/base.apk\n"
106 + " arm64: [status=speed-profile] [reason=bg-dexopt] [primary-abi]\n"
107 + " [location is /data/app/foo/oat/arm64/base.odex]\n"
108 + " arm: [status=verify] [reason=install]\n"
109 + " [location is /data/app/foo/oat/arm/base.odex]\n"
110 + " path: /data/app/foo/split_0.apk\n"
111 + " arm64: [status=verify] [reason=vdex] [primary-abi]\n"
112 + " [location is primary.vdex in /data/app/foo/split_0.dm]\n"
113 + " arm: [status=verify] [reason=vdex]\n"
114 + " [location is primary.vdex in /data/app/foo/split_0.dm]\n"
115 + " used by other apps: [com.example2.bar (isa=arm)]\n"
116 + " known secondary dex files:\n"
117 + " /data/user_de/0/foo/1.apk (removed)\n"
118 + " arm: [status=run-from-apk] [reason=unknown]\n"
119 + " [location is unknown]\n"
120 + " class loader context: =VaryingClassLoaderContexts=\n"
121 + " com.example1.foo (isolated): CLC1\n"
122 + " com.example3.baz: CLC2\n"
123 + " used by other apps: [com.example1.foo (isolated) (isa=arm64), com.example3.baz (removed)]\n"
124 + " /data/user_de/0/foo/2.apk (public)\n"
125 + " arm64: [status=speed-profile] [reason=bg-dexopt] [primary-abi]\n"
126 + " [location is /data/user_de/0/foo/oat/arm64/2.odex]\n"
127 + " arm: [status=verify] [reason=vdex]\n"
128 + " [location is /data/user_de/0/foo/oat/arm/2.vdex]\n"
129 + " class loader context: PCL[]\n"
130 + "[com.example2.bar]\n"
131 + " path: /data/app/bar/base.apk\n"
132 + " arm: [status=verify] [reason=install] [primary-abi]\n"
133 + " [location is /data/app/bar/oat/arm/base.odex]\n"
134 + " arm64: [status=verify] [reason=install]\n"
135 + " [location is /data/app/bar/oat/arm64/base.odex]\n";
136
137 var stringWriter = new StringWriter();
138 mDumpHelper.dump(new PrintWriter(stringWriter), mSnapshot);
139 assertThat(stringWriter.toString()).isEqualTo(expected);
140 }
141
142 private PackageState createPackageState(@NonNull String packageName, int appId,
143 boolean hasPackage, @NonNull String primaryAbi, @NonNull String secondaryAbi) {
144 var pkgState = mock(PackageState.class);
145 lenient().when(pkgState.getPackageName()).thenReturn(packageName);
146 lenient().when(pkgState.getAppId()).thenReturn(appId);
147 lenient()
148 .when(pkgState.getAndroidPackage())
149 .thenReturn(hasPackage ? mock(AndroidPackage.class) : null);
150 lenient().when(pkgState.getPrimaryCpuAbi()).thenReturn(primaryAbi);
151 lenient().when(pkgState.getSecondaryCpuAbi()).thenReturn(secondaryAbi);
152 return pkgState;
153 }
154
155 private Map<String, PackageState> createPackageStates() {
156 var pkgStates = new HashMap<String, PackageState>();
157 pkgStates.put(PKG_NAME_FOO,
158 createPackageState(PKG_NAME_FOO, 10001 /* appId */, true /* hasPackage */,
159 "arm64-v8a", "armeabi-v7a"));
160 pkgStates.put(PKG_NAME_BAR,
161 createPackageState(PKG_NAME_BAR, 10003 /* appId */, true /* hasPackage */,
162 "armeabi-v7a", "arm64-v8a"));
163 // This should not be included in the output because it has a negative app id.
164 pkgStates.put("com.android.art",
165 createPackageState("com.android.art", -1 /* appId */, true /* hasPackage */,
166 "arm64-v8a", "armeabi-v7a"));
167 // This should not be included in the output because it does't have AndroidPackage.
168 pkgStates.put("com.example.null",
169 createPackageState("com.example.null", 10010 /* appId */, false /* hasPackage */,
170 "arm64-v8a", "armeabi-v7a"));
171 return pkgStates;
172 }
173
174 private void setUpForFoo() throws Exception {
175 // The order of the primary dex files and the ABIs should be kept in the output. Secondary
176 // dex files should be reordered in lexicographical order.
177 var status = DexoptStatus.create(List.of(
178 DexContainerFileDexoptStatus.create("/data/app/foo/base.apk",
179 true /* isPrimaryDex */, true /* isPrimaryAbi */, "arm64-v8a",
180 "speed-profile", "bg-dexopt", "/data/app/foo/oat/arm64/base.odex"),
181 DexContainerFileDexoptStatus.create("/data/app/foo/base.apk",
182 true /* isPrimaryDex */, false /* isPrimaryAbi */, "armeabi-v7a", "verify",
183 "install", "/data/app/foo/oat/arm/base.odex"),
184 DexContainerFileDexoptStatus.create("/data/app/foo/split_0.apk",
185 true /* isPrimaryDex */, true /* isPrimaryAbi */, "arm64-v8a", "verify",
186 "vdex", "primary.vdex in /data/app/foo/split_0.dm"),
187 DexContainerFileDexoptStatus.create("/data/app/foo/split_0.apk",
188 true /* isPrimaryDex */, false /* isPrimaryAbi */, "armeabi-v7a", "verify",
189 "vdex", "primary.vdex in /data/app/foo/split_0.dm"),
190 DexContainerFileDexoptStatus.create("/data/user_de/0/foo/2.apk",
191 false /* isPrimaryDex */, true /* isPrimaryAbi */, "arm64-v8a",
192 "speed-profile", "bg-dexopt", "/data/user_de/0/foo/oat/arm64/2.odex"),
193 DexContainerFileDexoptStatus.create("/data/user_de/0/foo/2.apk",
194 false /* isPrimaryDex */, false /* isPrimaryAbi */, "armeabi-v7a", "verify",
195 "vdex", "/data/user_de/0/foo/oat/arm/2.vdex"),
196 DexContainerFileDexoptStatus.create("/data/user_de/0/foo/1.apk",
197 false /* isPrimaryDex */, false /* isPrimaryAbi */, "armeabi-v7a",
198 "run-from-apk", "unknown", "unknown")));
199
200 lenient()
201 .when(mArtManagerLocal.getDexoptStatus(any(), eq(PKG_NAME_FOO)))
202 .thenReturn(status);
203
204 // The output should not show "used by other apps:".
205 lenient()
206 .when(mDexUseManagerLocal.getPrimaryDexLoaders(
207 PKG_NAME_FOO, "/data/app/foo/base.apk"))
208 .thenReturn(Set.of());
209
210 // The output should not show "foo" in "used by other apps:".
211 lenient()
212 .when(mDexUseManagerLocal.getPrimaryDexLoaders(
213 PKG_NAME_FOO, "/data/app/foo/split_0.apk"))
214 .thenReturn(Set.of(DexLoader.create(PKG_NAME_FOO, false /* isolatedProcess */),
215 DexLoader.create(PKG_NAME_BAR, false /* isolatedProcess */)));
216
217 var info1 = mock(SecondaryDexInfo.class);
218 lenient().when(info1.dexPath()).thenReturn("/data/user_de/0/foo/1.apk");
219 lenient()
220 .when(info1.displayClassLoaderContext())
221 .thenReturn(SecondaryDexInfo.VARYING_CLASS_LOADER_CONTEXTS);
222
223 lenient()
224 .when(mDexUseManagerLocal.getSecondaryClassLoaderContext(PKG_NAME_FOO,
225 "/data/user_de/0/foo/1.apk",
226 DexLoader.create(PKG_NAME_FOO, true /* isolatedProcess */)))
227 .thenReturn("CLC1");
228 lenient()
229 .when(mDexUseManagerLocal.getSecondaryClassLoaderContext(PKG_NAME_FOO,
230 "/data/user_de/0/foo/1.apk",
231 DexLoader.create("com.example3.baz", false /* isolatedProcess */)))
232 .thenReturn("CLC2");
233
234 var loaders = new HashSet<DexLoader>();
235 // The output should show "foo" with "(isolated)" in "used by other apps:".
236 loaders.add(DexLoader.create(PKG_NAME_FOO, true /* isolatedProcess */));
237 // The output should show "baz" with "(removed)" in "used by other apps:".
238 loaders.add(DexLoader.create("com.example3.baz", false /* isolatedProcess */));
239 lenient().when(info1.loaders()).thenReturn(loaders);
240
241 // The output should show the dex path with "(removed)".
242 lenient()
243 .when(mArtd.getDexFileVisibility("/data/user_de/0/foo/1.apk"))
244 .thenReturn(FileVisibility.NOT_FOUND);
245
246 var info2 = mock(SecondaryDexInfo.class);
247 lenient().when(info2.dexPath()).thenReturn("/data/user_de/0/foo/2.apk");
248 lenient().when(info2.displayClassLoaderContext()).thenReturn("PCL[]");
249 lenient()
250 .when(mDexUseManagerLocal.getSecondaryClassLoaderContext(PKG_NAME_FOO,
251 "/data/user_de/0/foo/2.apk",
252 DexLoader.create(PKG_NAME_FOO, false /* isolatedProcess */)))
253 .thenReturn("PCL[]");
254 // The output should not show "used by other apps:".
255 lenient()
256 .when(info2.loaders())
257 .thenReturn(Set.of(DexLoader.create(PKG_NAME_FOO, false /* isolatedProcess */)));
258 lenient()
259 .when(mArtd.getDexFileVisibility("/data/user_de/0/foo/2.apk"))
260 .thenReturn(FileVisibility.OTHER_READABLE);
261
262 lenient()
263 .doReturn(List.of(info1, info2))
264 .when(mDexUseManagerLocal)
265 .getSecondaryDexInfo(PKG_NAME_FOO);
266 }
267
268 private void setUpForBar() {
269 // The order of the ABI should be kept in the output, despite that it's different from the
270 // order for package "foo".
271 // The output should not show "known secondary dex files:".
272 var status = DexoptStatus.create(
273 List.of(DexContainerFileDexoptStatus.create("/data/app/bar/base.apk",
274 true /* isPrimaryDex */, true /* isPrimaryAbi */, "armeabi-v7a",
275 "verify", "install", "/data/app/bar/oat/arm/base.odex"),
276 DexContainerFileDexoptStatus.create("/data/app/bar/base.apk",
277 true /* isPrimaryDex */, false /* isPrimaryAbi */, "arm64-v8a",
278 "verify", "install", "/data/app/bar/oat/arm64/base.odex")));
279
280 lenient()
281 .when(mArtManagerLocal.getDexoptStatus(any(), eq(PKG_NAME_BAR)))
282 .thenReturn(status);
283
284 lenient()
285 .when(mDexUseManagerLocal.getPrimaryDexLoaders(
286 PKG_NAME_BAR, "/data/app/bar/base.apk"))
287 .thenReturn(Set.of());
288 }
289 }