blob: 3cd24b858f390a9302ea570b317ff78d6cd3588a [file] [log] [blame]
Amith Yamasani483f3b02012-03-13 16:08:00 -07001/*
2 * Copyright (C) 2012 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
17package com.android.server.pm;
18
19import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DEFAULT;
20import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
21import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER;
22import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
23
Todd Kennedy266ceaf2016-07-26 15:39:33 -070024import static org.hamcrest.CoreMatchers.is;
Todd Kennedy13715d52016-08-01 13:38:57 -070025import static org.hamcrest.CoreMatchers.not;
Todd Kennedy266ceaf2016-07-26 15:39:33 -070026import static org.hamcrest.CoreMatchers.notNullValue;
Todd Kennedy13715d52016-08-01 13:38:57 -070027import static org.hamcrest.CoreMatchers.nullValue;
Sudheer Shankab6f06812017-05-16 14:37:42 -070028import static org.junit.Assert.assertEquals;
Todd Kennedy3cd658e2016-08-16 15:00:31 -070029import static org.junit.Assert.assertNotSame;
Sudheer Shankab6f06812017-05-16 14:37:42 -070030import static org.junit.Assert.assertNull;
Todd Kennedy3cd658e2016-08-16 15:00:31 -070031import static org.junit.Assert.assertSame;
Todd Kennedy266ceaf2016-07-26 15:39:33 -070032import static org.junit.Assert.assertThat;
Sudheer Shankab6f06812017-05-16 14:37:42 -070033import static org.junit.Assert.assertTrue;
Todd Kennedy266ceaf2016-07-26 15:39:33 -070034import static org.junit.Assert.fail;
35
Xiaohui Chen594f2082015-08-18 11:04:20 -070036import android.annotation.NonNull;
Todd Kennedy13715d52016-08-01 13:38:57 -070037import android.content.pm.ApplicationInfo;
dcashmane7b02172015-04-27 14:02:49 -070038import android.content.pm.PackageParser;
Todd Kennedy13715d52016-08-01 13:38:57 -070039import android.content.pm.PackageUserState;
Xiaohui Chen594f2082015-08-18 11:04:20 -070040import android.content.pm.UserInfo;
41import android.os.UserHandle;
Todd Kennedy13715d52016-08-01 13:38:57 -070042import android.os.UserManagerInternal;
Sudheer Shankab6f06812017-05-16 14:37:42 -070043import android.security.keystore.ArrayUtils;
Todd Kennedy266ceaf2016-07-26 15:39:33 -070044import android.support.test.InstrumentationRegistry;
45import android.support.test.runner.AndroidJUnit4;
46import android.test.suitebuilder.annotation.SmallTest;
dcashmane7b02172015-04-27 14:02:49 -070047import android.util.ArrayMap;
Jeff Sharkey036ebd72014-10-27 13:49:56 -070048import android.util.ArraySet;
49import android.util.Log;
dcashmane7b02172015-04-27 14:02:49 -070050import android.util.LongSparseArray;
Jeff Sharkey036ebd72014-10-27 13:49:56 -070051
Amith Yamasani483f3b02012-03-13 16:08:00 -070052import com.android.internal.os.AtomicFile;
Todd Kennedy13715d52016-08-01 13:38:57 -070053import com.android.server.LocalServices;
Amith Yamasani483f3b02012-03-13 16:08:00 -070054
Todd Kennedy13715d52016-08-01 13:38:57 -070055import org.junit.Before;
Todd Kennedy266ceaf2016-07-26 15:39:33 -070056import org.junit.Test;
57import org.junit.runner.RunWith;
58
Amith Yamasani483f3b02012-03-13 16:08:00 -070059import java.io.File;
60import java.io.FileOutputStream;
61import java.io.IOException;
dcashmane7b02172015-04-27 14:02:49 -070062import java.security.PublicKey;
Xiaohui Chen594f2082015-08-18 11:04:20 -070063import java.util.ArrayList;
Sudheer Shankab6f06812017-05-16 14:37:42 -070064import java.util.Arrays;
Xiaohui Chen594f2082015-08-18 11:04:20 -070065import java.util.List;
Amith Yamasani483f3b02012-03-13 16:08:00 -070066
Todd Kennedy266ceaf2016-07-26 15:39:33 -070067@RunWith(AndroidJUnit4.class)
68@SmallTest
69public class PackageManagerSettingsTests {
Amith Yamasani483f3b02012-03-13 16:08:00 -070070 private static final String PACKAGE_NAME_2 = "com.google.app2";
71 private static final String PACKAGE_NAME_3 = "com.android.app3";
72 private static final String PACKAGE_NAME_1 = "com.google.app1";
Amith Yamasani483f3b02012-03-13 16:08:00 -070073 public static final String TAG = "PackageManagerSettingsTests";
74 protected final String PREFIX = "android.content.pm";
75
Todd Kennedy266ceaf2016-07-26 15:39:33 -070076 /** make sure our initialized KeySetManagerService metadata matches packages.xml */
77 @Test
78 public void testReadKeySetSettings()
79 throws ReflectiveOperationException, IllegalAccessException {
80 /* write out files and read */
81 writeOldFiles();
Todd Kennedy266ceaf2016-07-26 15:39:33 -070082 Settings settings =
83 new Settings(InstrumentationRegistry.getContext().getFilesDir(), new Object());
84 assertThat(settings.readLPw(createFakeUsers()), is(true));
85 verifyKeySetMetaData(settings);
86 }
87
88 /** read in data, write it out, and read it back in. Verify same. */
89 @Test
90 public void testWriteKeySetSettings()
91 throws ReflectiveOperationException, IllegalAccessException {
92 // write out files and read
93 writeOldFiles();
Todd Kennedy266ceaf2016-07-26 15:39:33 -070094 Settings settings =
95 new Settings(InstrumentationRegistry.getContext().getFilesDir(), new Object());
96 assertThat(settings.readLPw(createFakeUsers()), is(true));
97
98 // write out, read back in and verify the same
99 settings.writeLPr();
100 assertThat(settings.readLPw(createFakeUsers()), is(true));
101 verifyKeySetMetaData(settings);
102 }
103
104 @Test
105 public void testSettingsReadOld() {
106 // Write the package files and make sure they're parsed properly the first time
107 writeOldFiles();
108 Settings settings =
109 new Settings(InstrumentationRegistry.getContext().getFilesDir(), new Object());
110 assertThat(settings.readLPw(createFakeUsers()), is(true));
Todd Kennedy8cad6232016-10-06 11:27:07 -0700111 assertThat(settings.getPackageLPr(PACKAGE_NAME_3), is(notNullValue()));
112 assertThat(settings.getPackageLPr(PACKAGE_NAME_1), is(notNullValue()));
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700113
Todd Kennedy8cad6232016-10-06 11:27:07 -0700114 PackageSetting ps = settings.getPackageLPr(PACKAGE_NAME_1);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700115 assertThat(ps.getEnabled(0), is(COMPONENT_ENABLED_STATE_DEFAULT));
116 assertThat(ps.getNotLaunched(0), is(true));
117
Todd Kennedy8cad6232016-10-06 11:27:07 -0700118 ps = settings.getPackageLPr(PACKAGE_NAME_2);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700119 assertThat(ps.getStopped(0), is(false));
120 assertThat(ps.getEnabled(0), is(COMPONENT_ENABLED_STATE_DISABLED_USER));
121 assertThat(ps.getEnabled(1), is(COMPONENT_ENABLED_STATE_DEFAULT));
122 }
123
124 @Test
125 public void testNewPackageRestrictionsFile() throws ReflectiveOperationException {
126 // Write the package files and make sure they're parsed properly the first time
127 writeOldFiles();
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700128 Settings settings =
129 new Settings(InstrumentationRegistry.getContext().getFilesDir(), new Object());
130 assertThat(settings.readLPw(createFakeUsers()), is(true));
131 settings.writeLPr();
132
133 // Create Settings again to make it read from the new files
134 settings = new Settings(InstrumentationRegistry.getContext().getFilesDir(), new Object());
135 assertThat(settings.readLPw(createFakeUsers()), is(true));
136
Todd Kennedy8cad6232016-10-06 11:27:07 -0700137 PackageSetting ps = settings.getPackageLPr(PACKAGE_NAME_2);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700138 assertThat(ps.getEnabled(0), is(COMPONENT_ENABLED_STATE_DISABLED_USER));
139 assertThat(ps.getEnabled(1), is(COMPONENT_ENABLED_STATE_DEFAULT));
140 }
141
142 @Test
143 public void testEnableDisable() {
144 // Write the package files and make sure they're parsed properly the first time
145 writeOldFiles();
146 Settings settings =
147 new Settings(InstrumentationRegistry.getContext().getFilesDir(), new Object());
148 assertThat(settings.readLPw(createFakeUsers()), is(true));
149
150 // Enable/Disable a package
Todd Kennedy8cad6232016-10-06 11:27:07 -0700151 PackageSetting ps = settings.getPackageLPr(PACKAGE_NAME_1);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700152 ps.setEnabled(COMPONENT_ENABLED_STATE_DISABLED, 0, null);
153 ps.setEnabled(COMPONENT_ENABLED_STATE_ENABLED, 1, null);
154 assertThat(ps.getEnabled(0), is(COMPONENT_ENABLED_STATE_DISABLED));
155 assertThat(ps.getEnabled(1), is(COMPONENT_ENABLED_STATE_ENABLED));
156
157 // Enable/Disable a component
158 ArraySet<String> components = new ArraySet<String>();
159 String component1 = PACKAGE_NAME_1 + "/.Component1";
160 components.add(component1);
161 ps.setDisabledComponents(components, 0);
162 ArraySet<String> componentsDisabled = ps.getDisabledComponents(0);
163 assertThat(componentsDisabled.size(), is(1));
164 assertThat(componentsDisabled.toArray()[0], is(component1));
165 boolean hasEnabled =
166 ps.getEnabledComponents(0) != null && ps.getEnabledComponents(1).size() > 0;
167 assertThat(hasEnabled, is(false));
168
169 // User 1 should not have any disabled components
170 boolean hasDisabled =
171 ps.getDisabledComponents(1) != null && ps.getDisabledComponents(1).size() > 0;
172 assertThat(hasDisabled, is(false));
173 ps.setEnabledComponents(components, 1);
174 assertThat(ps.getEnabledComponents(1).size(), is(1));
175 hasEnabled = ps.getEnabledComponents(0) != null && ps.getEnabledComponents(0).size() > 0;
176 assertThat(hasEnabled, is(false));
177 }
178
Todd Kennedy13715d52016-08-01 13:38:57 -0700179 private static final String PACKAGE_NAME = "com.android.bar";
180 private static final String REAL_PACKAGE_NAME = "com.android.foo";
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700181 private static final String PARENT_PACKAGE_NAME = "com.android.bar.parent";
182 private static final String CHILD_PACKAGE_NAME_01 = "com.android.bar.child01";
183 private static final String CHILD_PACKAGE_NAME_02 = "com.android.bar.child02";
184 private static final String CHILD_PACKAGE_NAME_03 = "com.android.bar.child03";
Todd Kennedy13715d52016-08-01 13:38:57 -0700185 private static final File INITIAL_CODE_PATH =
186 new File(InstrumentationRegistry.getContext().getFilesDir(), "com.android.bar-1");
187 private static final File UPDATED_CODE_PATH =
188 new File(InstrumentationRegistry.getContext().getFilesDir(), "com.android.bar-2");
189 private static final int INITIAL_VERSION_CODE = 10023;
190 private static final int UPDATED_VERSION_CODE = 10025;
191
Todd Kennedy13715d52016-08-01 13:38:57 -0700192 @Test
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700193 public void testPackageStateCopy01() {
194 final List<String> childPackageNames = new ArrayList<>();
195 childPackageNames.add(CHILD_PACKAGE_NAME_01);
196 childPackageNames.add(CHILD_PACKAGE_NAME_02);
197 childPackageNames.add(CHILD_PACKAGE_NAME_03);
198 final PackageSetting origPkgSetting01 = new PackageSetting(
Todd Kennedy13715d52016-08-01 13:38:57 -0700199 PACKAGE_NAME,
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700200 REAL_PACKAGE_NAME,
201 INITIAL_CODE_PATH /*codePath*/,
202 INITIAL_CODE_PATH /*resourcePath*/,
203 null /*legacyNativeLibraryPathString*/,
204 "x86_64" /*primaryCpuAbiString*/,
205 "x86" /*secondaryCpuAbiString*/,
206 null /*cpuAbiOverrideString*/,
207 INITIAL_VERSION_CODE,
208 ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_HAS_CODE,
209 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED|ApplicationInfo.PRIVATE_FLAG_HIDDEN,
210 PARENT_PACKAGE_NAME,
211 childPackageNames,
Svet Ganov67882122016-12-11 16:36:34 -0800212 0,
213 null /*usesStaticLibraries*/,
214 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700215 final PackageSetting testPkgSetting01 = new PackageSetting(origPkgSetting01);
216 verifySettingCopy(origPkgSetting01, testPkgSetting01);
217 }
218
219 @Test
220 public void testPackageStateCopy02() {
221 final List<String> childPackageNames = new ArrayList<>();
222 childPackageNames.add(CHILD_PACKAGE_NAME_01);
223 childPackageNames.add(CHILD_PACKAGE_NAME_02);
224 childPackageNames.add(CHILD_PACKAGE_NAME_03);
225 final PackageSetting origPkgSetting01 = new PackageSetting(
226 PACKAGE_NAME /*pkgName*/,
227 REAL_PACKAGE_NAME /*realPkgName*/,
228 INITIAL_CODE_PATH /*codePath*/,
229 INITIAL_CODE_PATH /*resourcePath*/,
230 null /*legacyNativeLibraryPathString*/,
231 "x86_64" /*primaryCpuAbiString*/,
232 "x86" /*secondaryCpuAbiString*/,
233 null /*cpuAbiOverrideString*/,
234 INITIAL_VERSION_CODE,
235 ApplicationInfo.FLAG_SYSTEM|ApplicationInfo.FLAG_HAS_CODE,
236 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED|ApplicationInfo.PRIVATE_FLAG_HIDDEN,
237 PARENT_PACKAGE_NAME,
238 childPackageNames,
Svet Ganov67882122016-12-11 16:36:34 -0800239 0,
240 null /*usesStaticLibraries*/,
241 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700242 final PackageSetting testPkgSetting01 = new PackageSetting(
243 PACKAGE_NAME /*pkgName*/,
244 REAL_PACKAGE_NAME /*realPkgName*/,
245 UPDATED_CODE_PATH /*codePath*/,
246 UPDATED_CODE_PATH /*resourcePath*/,
247 null /*legacyNativeLibraryPathString*/,
248 null /*primaryCpuAbiString*/,
249 null /*secondaryCpuAbiString*/,
250 null /*cpuAbiOverrideString*/,
251 UPDATED_VERSION_CODE,
252 0 /*pkgFlags*/,
253 0 /*pkgPrivateFlags*/,
254 null /*parentPkgName*/,
255 null /*childPkgNames*/,
Svet Ganov67882122016-12-11 16:36:34 -0800256 0,
257 null /*usesStaticLibraries*/,
258 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700259 testPkgSetting01.copyFrom(origPkgSetting01);
260 verifySettingCopy(origPkgSetting01, testPkgSetting01);
261 }
262
263 /** Update package */
264 @Test
265 public void testUpdatePackageSetting01() throws PackageManagerException {
266 final PackageSetting testPkgSetting01 =
267 createPackageSetting(0 /*sharedUserId*/, 0 /*pkgFlags*/);
268 testPkgSetting01.setInstalled(false /*installed*/, 0 /*userId*/);
269 assertThat(testPkgSetting01.pkgFlags, is(0));
270 assertThat(testPkgSetting01.pkgPrivateFlags, is(0));
271 final PackageSetting oldPkgSetting01 = new PackageSetting(testPkgSetting01);
272 Settings.updatePackageSetting(
273 testPkgSetting01,
Todd Kennedy13715d52016-08-01 13:38:57 -0700274 null /*disabledPkg*/,
275 null /*sharedUser*/,
276 UPDATED_CODE_PATH /*codePath*/,
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700277 null /*legacyNativeLibraryPath*/,
278 "arm64-v8a" /*primaryCpuAbi*/,
279 "armeabi" /*secondaryCpuAbi*/,
280 0 /*pkgFlags*/,
281 0 /*pkgPrivateFlags*/,
282 null /*childPkgNames*/,
Svet Ganov67882122016-12-11 16:36:34 -0800283 UserManagerService.getInstance(),
284 null /*usesStaticLibraries*/,
285 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700286 assertThat(testPkgSetting01.primaryCpuAbiString, is("arm64-v8a"));
287 assertThat(testPkgSetting01.secondaryCpuAbiString, is("armeabi"));
288 assertThat(testPkgSetting01.origPackage, is(nullValue()));
289 assertThat(testPkgSetting01.pkgFlags, is(0));
290 assertThat(testPkgSetting01.pkgPrivateFlags, is(0));
291 final PackageUserState userState = testPkgSetting01.readUserState(0);
292 final PackageUserState oldUserState = oldPkgSetting01.readUserState(0);
293 verifyUserState(userState, oldUserState, false /*userStateChanged*/, false /*notLaunched*/,
294 false /*stopped*/, false /*installed*/);
295 }
296
297 /** Update package; package now on /system, install for user '0' */
298 @Test
299 public void testUpdatePackageSetting02() throws PackageManagerException {
300 final PackageSetting testPkgSetting01 =
301 createPackageSetting(0 /*sharedUserId*/, 0 /*pkgFlags*/);
302 testPkgSetting01.setInstalled(false /*installed*/, 0 /*userId*/);
303 assertThat(testPkgSetting01.pkgFlags, is(0));
304 assertThat(testPkgSetting01.pkgPrivateFlags, is(0));
305 final PackageSetting oldPkgSetting01 = new PackageSetting(testPkgSetting01);
306 Settings.updatePackageSetting(
307 testPkgSetting01,
308 null /*disabledPkg*/,
309 null /*sharedUser*/,
310 UPDATED_CODE_PATH /*codePath*/,
311 null /*legacyNativeLibraryPath*/,
312 "arm64-v8a" /*primaryCpuAbi*/,
313 "armeabi" /*secondaryCpuAbi*/,
314 ApplicationInfo.FLAG_SYSTEM /*pkgFlags*/,
315 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED /*pkgPrivateFlags*/,
316 null /*childPkgNames*/,
Svet Ganov67882122016-12-11 16:36:34 -0800317 UserManagerService.getInstance(),
318 null /*usesStaticLibraries*/,
319 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700320 assertThat(testPkgSetting01.primaryCpuAbiString, is("arm64-v8a"));
321 assertThat(testPkgSetting01.secondaryCpuAbiString, is("armeabi"));
322 assertThat(testPkgSetting01.origPackage, is(nullValue()));
323 assertThat(testPkgSetting01.pkgFlags, is(ApplicationInfo.FLAG_SYSTEM));
324 assertThat(testPkgSetting01.pkgPrivateFlags, is(ApplicationInfo.PRIVATE_FLAG_PRIVILEGED));
325 final PackageUserState userState = testPkgSetting01.readUserState(0);
326 final PackageUserState oldUserState = oldPkgSetting01.readUserState(0);
327 // WARNING: When creating a shallow copy of the PackageSetting we do NOT create
328 // new contained objects. For example, this means that changes to the user state
329 // in testPkgSetting01 will also change the user state in its copy.
330 verifyUserState(userState, oldUserState, false /*userStateChanged*/, false /*notLaunched*/,
331 false /*stopped*/, true /*installed*/);
332 }
333
334 /** Update package; changing shared user throws exception */
335 @Test
336 public void testUpdatePackageSetting03() {
337 final Settings testSettings01 = new Settings(new Object() /*lock*/);
338 final SharedUserSetting testUserSetting01 = createSharedUserSetting(
339 testSettings01, "TestUser", 10064, 0 /*pkgFlags*/, 0 /*pkgPrivateFlags*/);
340 final PackageSetting testPkgSetting01 =
341 createPackageSetting(0 /*sharedUserId*/, 0 /*pkgFlags*/);
342 try {
343 Settings.updatePackageSetting(
344 testPkgSetting01,
345 null /*disabledPkg*/,
346 testUserSetting01 /*sharedUser*/,
347 UPDATED_CODE_PATH /*codePath*/,
348 null /*legacyNativeLibraryPath*/,
349 "arm64-v8a" /*primaryCpuAbi*/,
350 "armeabi" /*secondaryCpuAbi*/,
351 0 /*pkgFlags*/,
352 0 /*pkgPrivateFlags*/,
353 null /*childPkgNames*/,
Svet Ganov67882122016-12-11 16:36:34 -0800354 UserManagerService.getInstance(),
355 null /*usesStaticLibraries*/,
356 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700357 fail("Expected a PackageManagerException");
358 } catch (PackageManagerException expected) {
359 }
360 }
361
362 /** Create a new PackageSetting based on an original package setting */
363 @Test
364 public void testCreateNewSetting01() {
365 final PackageSetting originalPkgSetting01 =
366 createPackageSetting(0 /*sharedUserId*/, 0 /*pkgFlags*/);
367 final PackageSignatures originalSignatures = originalPkgSetting01.signatures;
368 final PackageSetting testPkgSetting01 = Settings.createNewSetting(
369 REAL_PACKAGE_NAME,
370 originalPkgSetting01 /*originalPkg*/,
371 null /*disabledPkg*/,
372 null /*realPkgName*/,
373 null /*sharedUser*/,
374 UPDATED_CODE_PATH /*codePath*/,
Todd Kennedy13715d52016-08-01 13:38:57 -0700375 UPDATED_CODE_PATH /*resourcePath*/,
376 null /*legacyNativeLibraryPath*/,
377 "arm64-v8a" /*primaryCpuAbi*/,
378 "armeabi" /*secondaryCpuAbi*/,
379 UPDATED_VERSION_CODE /*versionCode*/,
380 ApplicationInfo.FLAG_SYSTEM /*pkgFlags*/,
381 ApplicationInfo.PRIVATE_FLAG_PRIVILEGED /*pkgPrivateFlags*/,
382 null /*installUser*/,
383 false /*allowInstall*/,
Todd Kennedybe0b8892017-02-15 14:13:52 -0800384 false /*instantApp*/,
Todd Kennedy5eb5a7d2017-08-01 07:42:47 -0700385 false /*virtualPreload*/,
Todd Kennedy13715d52016-08-01 13:38:57 -0700386 null /*parentPkgName*/,
387 null /*childPkgNames*/,
Svet Ganov67882122016-12-11 16:36:34 -0800388 UserManagerService.getInstance(),
389 null /*usesStaticLibraries*/,
390 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700391 assertThat(testPkgSetting01.codePath, is(UPDATED_CODE_PATH));
392 assertThat(testPkgSetting01.name, is(PACKAGE_NAME));
Todd Kennedy13715d52016-08-01 13:38:57 -0700393 assertThat(testPkgSetting01.pkgFlags, is(ApplicationInfo.FLAG_SYSTEM));
394 assertThat(testPkgSetting01.pkgPrivateFlags, is(ApplicationInfo.PRIVATE_FLAG_PRIVILEGED));
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700395 assertThat(testPkgSetting01.primaryCpuAbiString, is("arm64-v8a"));
396 assertThat(testPkgSetting01.resourcePath, is(UPDATED_CODE_PATH));
397 assertThat(testPkgSetting01.secondaryCpuAbiString, is("armeabi"));
398 assertSame(testPkgSetting01.origPackage, originalPkgSetting01);
399 // signatures object must be different
400 assertNotSame(testPkgSetting01.signatures, originalSignatures);
401 assertThat(testPkgSetting01.versionCode, is(UPDATED_VERSION_CODE));
402 final PackageUserState userState = testPkgSetting01.readUserState(0);
403 verifyUserState(userState, null /*oldUserState*/, false /*userStateChanged*/,
404 false /*notLaunched*/, false /*stopped*/, true /*installed*/);
Todd Kennedy13715d52016-08-01 13:38:57 -0700405 }
406
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700407 /** Create a new non-system PackageSetting */
Todd Kennedy13715d52016-08-01 13:38:57 -0700408 @Test
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700409 public void testCreateNewSetting02() {
410 final PackageSetting testPkgSetting01 = Settings.createNewSetting(
Todd Kennedy13715d52016-08-01 13:38:57 -0700411 PACKAGE_NAME,
Todd Kennedy13715d52016-08-01 13:38:57 -0700412 null /*originalPkg*/,
413 null /*disabledPkg*/,
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700414 null /*realPkgName*/,
Todd Kennedy13715d52016-08-01 13:38:57 -0700415 null /*sharedUser*/,
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700416 INITIAL_CODE_PATH /*codePath*/,
417 INITIAL_CODE_PATH /*resourcePath*/,
Todd Kennedy13715d52016-08-01 13:38:57 -0700418 null /*legacyNativeLibraryPath*/,
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700419 "x86_64" /*primaryCpuAbiString*/,
420 "x86" /*secondaryCpuAbiString*/,
421 INITIAL_VERSION_CODE /*versionCode*/,
Todd Kennedy13715d52016-08-01 13:38:57 -0700422 0 /*pkgFlags*/,
423 0 /*pkgPrivateFlags*/,
424 UserHandle.SYSTEM /*installUser*/,
425 true /*allowInstall*/,
Todd Kennedybe0b8892017-02-15 14:13:52 -0800426 false /*instantApp*/,
Todd Kennedy5eb5a7d2017-08-01 07:42:47 -0700427 false /*virtualPreload*/,
Todd Kennedy13715d52016-08-01 13:38:57 -0700428 null /*parentPkgName*/,
429 null /*childPkgNames*/,
Svet Ganov67882122016-12-11 16:36:34 -0800430 UserManagerService.getInstance(),
431 null /*usesStaticLibraries*/,
432 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700433 assertThat(testPkgSetting01.appId, is(0));
434 assertThat(testPkgSetting01.codePath, is(INITIAL_CODE_PATH));
435 assertThat(testPkgSetting01.name, is(PACKAGE_NAME));
Todd Kennedy13715d52016-08-01 13:38:57 -0700436 assertThat(testPkgSetting01.origPackage, is(nullValue()));
437 assertThat(testPkgSetting01.pkgFlags, is(0));
438 assertThat(testPkgSetting01.pkgPrivateFlags, is(0));
Todd Kennedy13715d52016-08-01 13:38:57 -0700439 assertThat(testPkgSetting01.primaryCpuAbiString, is("x86_64"));
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700440 assertThat(testPkgSetting01.resourcePath, is(INITIAL_CODE_PATH));
Todd Kennedy13715d52016-08-01 13:38:57 -0700441 assertThat(testPkgSetting01.secondaryCpuAbiString, is("x86"));
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700442 assertThat(testPkgSetting01.versionCode, is(INITIAL_VERSION_CODE));
443 // by default, the package is considered stopped
444 final PackageUserState userState = testPkgSetting01.readUserState(0);
445 verifyUserState(userState, null /*oldUserState*/, false /*userStateChanged*/,
446 true /*notLaunched*/, true /*stopped*/, true /*installed*/);
Todd Kennedy13715d52016-08-01 13:38:57 -0700447 }
448
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700449 /** Create PackageSetting for a shared user */
Todd Kennedy13715d52016-08-01 13:38:57 -0700450 @Test
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700451 public void testCreateNewSetting03() {
452 final Settings testSettings01 = new Settings(new Object() /*lock*/);
453 final SharedUserSetting testUserSetting01 = createSharedUserSetting(
454 testSettings01, "TestUser", 10064, 0 /*pkgFlags*/, 0 /*pkgPrivateFlags*/);
455 final PackageSetting testPkgSetting01 = Settings.createNewSetting(
Todd Kennedy13715d52016-08-01 13:38:57 -0700456 PACKAGE_NAME,
Todd Kennedy13715d52016-08-01 13:38:57 -0700457 null /*originalPkg*/,
458 null /*disabledPkg*/,
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700459 null /*realPkgName*/,
460 testUserSetting01 /*sharedUser*/,
461 INITIAL_CODE_PATH /*codePath*/,
462 INITIAL_CODE_PATH /*resourcePath*/,
463 null /*legacyNativeLibraryPath*/,
464 "x86_64" /*primaryCpuAbiString*/,
465 "x86" /*secondaryCpuAbiString*/,
466 INITIAL_VERSION_CODE /*versionCode*/,
467 0 /*pkgFlags*/,
468 0 /*pkgPrivateFlags*/,
469 null /*installUser*/,
470 false /*allowInstall*/,
Todd Kennedybe0b8892017-02-15 14:13:52 -0800471 false /*instantApp*/,
Todd Kennedy5eb5a7d2017-08-01 07:42:47 -0700472 false /*virtualPreload*/,
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700473 null /*parentPkgName*/,
474 null /*childPkgNames*/,
Svet Ganov67882122016-12-11 16:36:34 -0800475 UserManagerService.getInstance(),
476 null /*usesStaticLibraries*/,
477 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700478 assertThat(testPkgSetting01.appId, is(10064));
479 assertThat(testPkgSetting01.codePath, is(INITIAL_CODE_PATH));
480 assertThat(testPkgSetting01.name, is(PACKAGE_NAME));
481 assertThat(testPkgSetting01.origPackage, is(nullValue()));
482 assertThat(testPkgSetting01.pkgFlags, is(0));
483 assertThat(testPkgSetting01.pkgPrivateFlags, is(0));
484 assertThat(testPkgSetting01.primaryCpuAbiString, is("x86_64"));
485 assertThat(testPkgSetting01.resourcePath, is(INITIAL_CODE_PATH));
486 assertThat(testPkgSetting01.secondaryCpuAbiString, is("x86"));
487 assertThat(testPkgSetting01.versionCode, is(INITIAL_VERSION_CODE));
488 final PackageUserState userState = testPkgSetting01.readUserState(0);
489 verifyUserState(userState, null /*oldUserState*/, false /*userStateChanged*/,
490 false /*notLaunched*/, false /*stopped*/, true /*installed*/);
491 }
492
493 /** Create a new PackageSetting based on a disabled package setting */
494 @Test
495 public void testCreateNewSetting04() {
496 final PackageSetting disabledPkgSetting01 =
497 createPackageSetting(0 /*sharedUserId*/, 0 /*pkgFlags*/);
498 disabledPkgSetting01.appId = 10064;
499 final PackageSignatures disabledSignatures = disabledPkgSetting01.signatures;
500 final PackageSetting testPkgSetting01 = Settings.createNewSetting(
501 PACKAGE_NAME,
502 null /*originalPkg*/,
503 disabledPkgSetting01 /*disabledPkg*/,
504 null /*realPkgName*/,
Todd Kennedy13715d52016-08-01 13:38:57 -0700505 null /*sharedUser*/,
506 UPDATED_CODE_PATH /*codePath*/,
507 UPDATED_CODE_PATH /*resourcePath*/,
508 null /*legacyNativeLibraryPath*/,
509 "arm64-v8a" /*primaryCpuAbi*/,
510 "armeabi" /*secondaryCpuAbi*/,
511 UPDATED_VERSION_CODE /*versionCode*/,
512 0 /*pkgFlags*/,
513 0 /*pkgPrivateFlags*/,
514 null /*installUser*/,
515 false /*allowInstall*/,
Todd Kennedybe0b8892017-02-15 14:13:52 -0800516 false /*instantApp*/,
Todd Kennedy5eb5a7d2017-08-01 07:42:47 -0700517 false /*virtualPreload*/,
Todd Kennedy13715d52016-08-01 13:38:57 -0700518 null /*parentPkgName*/,
519 null /*childPkgNames*/,
Svet Ganov67882122016-12-11 16:36:34 -0800520 UserManagerService.getInstance(),
521 null /*usesStaticLibraries*/,
522 null /*usesStaticLibrariesVersions*/);
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700523 assertThat(testPkgSetting01.appId, is(10064));
524 assertThat(testPkgSetting01.codePath, is(UPDATED_CODE_PATH));
525 assertThat(testPkgSetting01.name, is(PACKAGE_NAME));
Todd Kennedy13715d52016-08-01 13:38:57 -0700526 assertThat(testPkgSetting01.origPackage, is(nullValue()));
527 assertThat(testPkgSetting01.pkgFlags, is(0));
528 assertThat(testPkgSetting01.pkgPrivateFlags, is(0));
Todd Kennedy13715d52016-08-01 13:38:57 -0700529 assertThat(testPkgSetting01.primaryCpuAbiString, is("arm64-v8a"));
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700530 assertThat(testPkgSetting01.resourcePath, is(UPDATED_CODE_PATH));
Todd Kennedy13715d52016-08-01 13:38:57 -0700531 assertThat(testPkgSetting01.secondaryCpuAbiString, is("armeabi"));
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700532 assertNotSame(testPkgSetting01.signatures, disabledSignatures);
533 assertThat(testPkgSetting01.versionCode, is(UPDATED_VERSION_CODE));
534 final PackageUserState userState = testPkgSetting01.readUserState(0);
535 verifyUserState(userState, null /*oldUserState*/, false /*userStateChanged*/,
536 false /*notLaunched*/, false /*stopped*/, true /*installed*/);
Todd Kennedy13715d52016-08-01 13:38:57 -0700537 }
538
Sudheer Shankab6f06812017-05-16 14:37:42 -0700539 @Test
540 public void testInsertPackageSetting() {
541 final PackageSetting ps = createPackageSetting(0 /*sharedUserId*/, 0 /*pkgFlags*/);
542 final PackageParser.Package pkg = new PackageParser.Package(PACKAGE_NAME);
543 pkg.applicationInfo.setCodePath(ps.codePathString);
544 pkg.applicationInfo.setResourcePath(ps.resourcePathString);
545 final Settings settings =
546 new Settings(InstrumentationRegistry.getContext().getFilesDir(), new Object());
547 pkg.usesStaticLibraries = new ArrayList<>(
548 Arrays.asList("foo.bar1", "foo.bar2", "foo.bar3"));
549 pkg.usesStaticLibrariesVersions = new int[] {2, 4, 6};
550 settings.insertPackageSettingLPw(ps, pkg);
551 assertEquals(pkg, ps.pkg);
552 assertArrayEquals(pkg.usesStaticLibraries.toArray(new String[0]), ps.usesStaticLibraries);
553 assertArrayEquals(pkg.usesStaticLibrariesVersions, ps.usesStaticLibrariesVersions);
554
555 pkg.usesStaticLibraries = null;
556 pkg.usesStaticLibrariesVersions = null;
557 settings.insertPackageSettingLPw(ps, pkg);
558 assertEquals(pkg, ps.pkg);
559 assertNull("Actual: " + Arrays.toString(ps.usesStaticLibraries), ps.usesStaticLibraries);
560 assertNull("Actual: " + Arrays.toString(ps.usesStaticLibrariesVersions),
561 ps.usesStaticLibrariesVersions);
562 }
563
564 private <T> void assertArrayEquals(T[] a, T[] b) {
565 assertTrue("Expected: " + Arrays.toString(a) + ", actual: " + Arrays.toString(b),
566 Arrays.equals(a, b));
567 }
568
569 private void assertArrayEquals(int[] a, int[] b) {
570 assertTrue("Expected: " + Arrays.toString(a) + ", actual: " + Arrays.toString(b),
571 Arrays.equals(a, b));
572 }
573
Todd Kennedy13715d52016-08-01 13:38:57 -0700574 private void verifyUserState(PackageUserState userState, PackageUserState oldUserState,
575 boolean userStateChanged) {
576 verifyUserState(userState, oldUserState, userStateChanged, false /*notLaunched*/,
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700577 false /*stopped*/, true /*installed*/);
Todd Kennedy13715d52016-08-01 13:38:57 -0700578 }
579
580 private void verifyUserState(PackageUserState userState, PackageUserState oldUserState,
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700581 boolean userStateChanged, boolean notLaunched, boolean stopped, boolean installed) {
Todd Kennedy13715d52016-08-01 13:38:57 -0700582 assertThat(userState.enabled, is(0));
583 assertThat(userState.hidden, is(false));
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700584 assertThat(userState.installed, is(installed));
Todd Kennedy13715d52016-08-01 13:38:57 -0700585 assertThat(userState.notLaunched, is(notLaunched));
586 assertThat(userState.stopped, is(stopped));
587 assertThat(userState.suspended, is(false));
588 if (oldUserState != null) {
589 assertThat(userState.equals(oldUserState), is(not(userStateChanged)));
590 }
591 }
592
Todd Kennedy3cd658e2016-08-16 15:00:31 -0700593 private void verifySettingCopy(PackageSetting origPkgSetting, PackageSetting testPkgSetting) {
594 assertThat(origPkgSetting, is(not(testPkgSetting)));
595 assertThat(origPkgSetting.appId, is(testPkgSetting.appId));
596 // different but equal objects
597 assertNotSame(origPkgSetting.childPackageNames, testPkgSetting.childPackageNames);
598 assertThat(origPkgSetting.childPackageNames, is(testPkgSetting.childPackageNames));
599 assertSame(origPkgSetting.codePath, testPkgSetting.codePath);
600 assertThat(origPkgSetting.codePath, is(testPkgSetting.codePath));
601 assertSame(origPkgSetting.codePathString, testPkgSetting.codePathString);
602 assertThat(origPkgSetting.codePathString, is(testPkgSetting.codePathString));
603 assertSame(origPkgSetting.cpuAbiOverrideString, testPkgSetting.cpuAbiOverrideString);
604 assertThat(origPkgSetting.cpuAbiOverrideString, is(testPkgSetting.cpuAbiOverrideString));
605 assertThat(origPkgSetting.firstInstallTime, is(testPkgSetting.firstInstallTime));
606 assertSame(origPkgSetting.installerPackageName, testPkgSetting.installerPackageName);
607 assertThat(origPkgSetting.installerPackageName, is(testPkgSetting.installerPackageName));
608 assertThat(origPkgSetting.installPermissionsFixed,
609 is(testPkgSetting.installPermissionsFixed));
610 assertThat(origPkgSetting.installStatus, is(testPkgSetting.installStatus));
611 assertThat(origPkgSetting.isOrphaned, is(testPkgSetting.isOrphaned));
612 assertSame(origPkgSetting.keySetData, testPkgSetting.keySetData);
613 assertThat(origPkgSetting.keySetData, is(testPkgSetting.keySetData));
614 assertThat(origPkgSetting.lastUpdateTime, is(testPkgSetting.lastUpdateTime));
615 assertSame(origPkgSetting.legacyNativeLibraryPathString,
616 testPkgSetting.legacyNativeLibraryPathString);
617 assertThat(origPkgSetting.legacyNativeLibraryPathString,
618 is(testPkgSetting.legacyNativeLibraryPathString));
619 assertNotSame(origPkgSetting.mPermissionsState, testPkgSetting.mPermissionsState);
620 assertThat(origPkgSetting.mPermissionsState, is(testPkgSetting.mPermissionsState));
621 assertThat(origPkgSetting.name, is(testPkgSetting.name));
622 // oldCodePaths is _not_ copied
623 // assertNotSame(origPkgSetting.oldCodePaths, testPkgSetting.oldCodePaths);
624 // assertThat(origPkgSetting.oldCodePaths, is(not(testPkgSetting.oldCodePaths)));
625 assertSame(origPkgSetting.origPackage, testPkgSetting.origPackage);
626 assertThat(origPkgSetting.origPackage, is(testPkgSetting.origPackage));
627 assertSame(origPkgSetting.parentPackageName, testPkgSetting.parentPackageName);
628 assertThat(origPkgSetting.parentPackageName, is(testPkgSetting.parentPackageName));
629 assertSame(origPkgSetting.pkg, testPkgSetting.pkg);
630 // No equals() method for this object
631 // assertThat(origPkgSetting.pkg, is(testPkgSetting.pkg));
632 assertThat(origPkgSetting.pkgFlags, is(testPkgSetting.pkgFlags));
633 assertThat(origPkgSetting.pkgPrivateFlags, is(testPkgSetting.pkgPrivateFlags));
634 assertSame(origPkgSetting.primaryCpuAbiString, testPkgSetting.primaryCpuAbiString);
635 assertThat(origPkgSetting.primaryCpuAbiString, is(testPkgSetting.primaryCpuAbiString));
636 assertThat(origPkgSetting.realName, is(testPkgSetting.realName));
637 assertSame(origPkgSetting.resourcePath, testPkgSetting.resourcePath);
638 assertThat(origPkgSetting.resourcePath, is(testPkgSetting.resourcePath));
639 assertSame(origPkgSetting.resourcePathString, testPkgSetting.resourcePathString);
640 assertThat(origPkgSetting.resourcePathString, is(testPkgSetting.resourcePathString));
641 assertSame(origPkgSetting.secondaryCpuAbiString, testPkgSetting.secondaryCpuAbiString);
642 assertThat(origPkgSetting.secondaryCpuAbiString, is(testPkgSetting.secondaryCpuAbiString));
643 assertSame(origPkgSetting.sharedUser, testPkgSetting.sharedUser);
644 assertThat(origPkgSetting.sharedUser, is(testPkgSetting.sharedUser));
645 assertSame(origPkgSetting.signatures, testPkgSetting.signatures);
646 assertThat(origPkgSetting.signatures, is(testPkgSetting.signatures));
647 assertThat(origPkgSetting.timeStamp, is(testPkgSetting.timeStamp));
648 assertThat(origPkgSetting.uidError, is(testPkgSetting.uidError));
649 assertNotSame(origPkgSetting.getUserState(), is(testPkgSetting.getUserState()));
650 // No equals() method for SparseArray object
651 // assertThat(origPkgSetting.getUserState(), is(testPkgSetting.getUserState()));
652 assertSame(origPkgSetting.verificationInfo, testPkgSetting.verificationInfo);
653 assertThat(origPkgSetting.verificationInfo, is(testPkgSetting.verificationInfo));
654 assertThat(origPkgSetting.versionCode, is(testPkgSetting.versionCode));
655 assertSame(origPkgSetting.volumeUuid, testPkgSetting.volumeUuid);
656 assertThat(origPkgSetting.volumeUuid, is(testPkgSetting.volumeUuid));
657 }
658
659 private SharedUserSetting createSharedUserSetting(Settings settings, String userName,
660 int sharedUserId, int pkgFlags, int pkgPrivateFlags) {
661 return settings.addSharedUserLPw(
662 userName,
663 sharedUserId,
664 pkgFlags,
665 pkgPrivateFlags);
666 }
667 private PackageSetting createPackageSetting(int sharedUserId, int pkgFlags) {
Todd Kennedy13715d52016-08-01 13:38:57 -0700668 return new PackageSetting(
669 PACKAGE_NAME,
670 REAL_PACKAGE_NAME,
671 INITIAL_CODE_PATH /*codePath*/,
672 INITIAL_CODE_PATH /*resourcePath*/,
673 null /*legacyNativeLibraryPathString*/,
674 "x86_64" /*primaryCpuAbiString*/,
675 "x86" /*secondaryCpuAbiString*/,
676 null /*cpuAbiOverrideString*/,
677 INITIAL_VERSION_CODE,
678 pkgFlags,
679 0 /*privateFlags*/,
680 null /*parentPackageName*/,
Todd Kennedy788c8422016-08-10 10:52:34 -0700681 null /*childPackageNames*/,
Svet Ganov67882122016-12-11 16:36:34 -0800682 sharedUserId,
683 null /*usesStaticLibraries*/,
684 null /*usesStaticLibrariesVersions*/);
Todd Kennedy13715d52016-08-01 13:38:57 -0700685 }
686
Xiaohui Chen594f2082015-08-18 11:04:20 -0700687 private @NonNull List<UserInfo> createFakeUsers() {
688 ArrayList<UserInfo> users = new ArrayList<>();
689 users.add(new UserInfo(UserHandle.USER_SYSTEM, "test user", UserInfo.FLAG_INITIALIZED));
690 return users;
691 }
692
Amith Yamasani483f3b02012-03-13 16:08:00 -0700693 private void writeFile(File file, byte[] data) {
694 file.mkdirs();
695 try {
696 AtomicFile aFile = new AtomicFile(file);
697 FileOutputStream fos = aFile.startWrite();
698 fos.write(data);
699 aFile.finishWrite(fos);
700 } catch (IOException ioe) {
701 Log.e(TAG, "Cannot write file " + file.getPath());
702 }
703 }
704
705 private void writePackagesXml() {
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700706 writeFile(new File(InstrumentationRegistry.getContext().getFilesDir(), "system/packages.xml"),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700707 ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?>"
708 + "<packages>"
Svetoslavc6d1c342015-02-26 14:44:43 -0800709 + "<last-platform-version internal=\"15\" external=\"0\" fingerprint=\"foo\" />"
Amith Yamasani483f3b02012-03-13 16:08:00 -0700710 + "<permission-trees>"
711 + "<item name=\"com.google.android.permtree\" package=\"com.google.android.permpackage\" />"
712 + "</permission-trees>"
713 + "<permissions>"
714 + "<item name=\"android.permission.WRITE_CALL_LOG\" package=\"android\" protection=\"1\" />"
715 + "<item name=\"android.permission.ASEC_ACCESS\" package=\"android\" protection=\"2\" />"
716 + "<item name=\"android.permission.ACCESS_WIMAX_STATE\" package=\"android\" />"
717 + "<item name=\"android.permission.REBOOT\" package=\"android\" protection=\"18\" />"
718 + "</permissions>"
719 + "<package name=\"com.google.app1\" codePath=\"/system/app/app1.apk\" nativeLibraryPath=\"/data/data/com.google.app1/lib\" flags=\"1\" ft=\"1360e2caa70\" it=\"135f2f80d08\" ut=\"1360e2caa70\" version=\"1109\" sharedUserId=\"11000\">"
720 + "<sigs count=\"1\">"
dcashmane7b02172015-04-27 14:02:49 -0700721 + "<cert index=\"0\" key=\"" + KeySetStrings.ctsKeySetCertA + "\" />"
Amith Yamasani483f3b02012-03-13 16:08:00 -0700722 + "</sigs>"
dcashmane7b02172015-04-27 14:02:49 -0700723 + "<proper-signing-keyset identifier=\"1\" />"
Amith Yamasani483f3b02012-03-13 16:08:00 -0700724 + "</package>"
725 + "<package name=\"com.google.app2\" codePath=\"/system/app/app2.apk\" nativeLibraryPath=\"/data/data/com.google.app2/lib\" flags=\"1\" ft=\"1360e578718\" it=\"135f2f80d08\" ut=\"1360e578718\" version=\"15\" enabled=\"3\" userId=\"11001\">"
726 + "<sigs count=\"1\">"
727 + "<cert index=\"0\" />"
728 + "</sigs>"
dcashmane7b02172015-04-27 14:02:49 -0700729 + "<proper-signing-keyset identifier=\"1\" />"
730 + "<defined-keyset alias=\"AB\" identifier=\"4\" />"
Amith Yamasani483f3b02012-03-13 16:08:00 -0700731 + "</package>"
732 + "<package name=\"com.android.app3\" codePath=\"/system/app/app3.apk\" nativeLibraryPath=\"/data/data/com.android.app3/lib\" flags=\"1\" ft=\"1360e577b60\" it=\"135f2f80d08\" ut=\"1360e577b60\" version=\"15\" userId=\"11030\">"
733 + "<sigs count=\"1\">"
dcashmane7b02172015-04-27 14:02:49 -0700734 + "<cert index=\"1\" key=\"" + KeySetStrings.ctsKeySetCertB + "\" />"
Amith Yamasani483f3b02012-03-13 16:08:00 -0700735 + "</sigs>"
dcashmane7b02172015-04-27 14:02:49 -0700736 + "<proper-signing-keyset identifier=\"2\" />"
737 + "<upgrade-keyset identifier=\"3\" />"
738 + "<defined-keyset alias=\"C\" identifier=\"3\" />"
Amith Yamasani483f3b02012-03-13 16:08:00 -0700739 + "</package>"
740 + "<shared-user name=\"com.android.shared1\" userId=\"11000\">"
741 + "<sigs count=\"1\">"
742 + "<cert index=\"1\" />"
743 + "</sigs>"
744 + "<perms>"
745 + "<item name=\"android.permission.REBOOT\" />"
746 + "</perms>"
747 + "</shared-user>"
dcashmane7b02172015-04-27 14:02:49 -0700748 + "<keyset-settings version=\"1\">"
749 + "<keys>"
750 + "<public-key identifier=\"1\" value=\"" + KeySetStrings.ctsKeySetPublicKeyA + "\" />"
751 + "<public-key identifier=\"2\" value=\"" + KeySetStrings.ctsKeySetPublicKeyB + "\" />"
752 + "<public-key identifier=\"3\" value=\"" + KeySetStrings.ctsKeySetPublicKeyC + "\" />"
753 + "</keys>"
754 + "<keysets>"
755 + "<keyset identifier=\"1\">"
756 + "<key-id identifier=\"1\" />"
757 + "</keyset>"
758 + "<keyset identifier=\"2\">"
759 + "<key-id identifier=\"2\" />"
760 + "</keyset>"
761 + "<keyset identifier=\"3\">"
762 + "<key-id identifier=\"3\" />"
763 + "</keyset>"
764 + "<keyset identifier=\"4\">"
765 + "<key-id identifier=\"1\" />"
766 + "<key-id identifier=\"2\" />"
767 + "</keyset>"
768 + "</keysets>"
769 + "<lastIssuedKeyId value=\"3\" />"
770 + "<lastIssuedKeySetId value=\"4\" />"
771 + "</keyset-settings>"
Amith Yamasani483f3b02012-03-13 16:08:00 -0700772 + "</packages>").getBytes());
773 }
774
775 private void writeStoppedPackagesXml() {
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700776 writeFile(new File(InstrumentationRegistry.getContext().getFilesDir(), "system/packages-stopped.xml"),
Amith Yamasani483f3b02012-03-13 16:08:00 -0700777 ( "<?xml version='1.0' encoding='utf-8' standalone='yes' ?>"
778 + "<stopped-packages>"
779 + "<pkg name=\"com.google.app1\" nl=\"1\" />"
780 + "<pkg name=\"com.android.app3\" nl=\"1\" />"
781 + "</stopped-packages>")
782 .getBytes());
783 }
784
785 private void writePackagesList() {
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700786 writeFile(new File(InstrumentationRegistry.getContext().getFilesDir(), "system/packages.list"),
Robert Craig4a453732013-03-26 08:21:37 -0400787 ( "com.google.app1 11000 0 /data/data/com.google.app1 seinfo1"
788 + "com.google.app2 11001 0 /data/data/com.google.app2 seinfo2"
789 + "com.android.app3 11030 0 /data/data/com.android.app3 seinfo3")
Amith Yamasani483f3b02012-03-13 16:08:00 -0700790 .getBytes());
791 }
792
Svetoslavc6d1c342015-02-26 14:44:43 -0800793 private void deleteSystemFolder() {
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700794 File systemFolder = new File(InstrumentationRegistry.getContext().getFilesDir(), "system");
Svetoslavc6d1c342015-02-26 14:44:43 -0800795 deleteFolder(systemFolder);
796 }
797
798 private static void deleteFolder(File folder) {
799 File[] files = folder.listFiles();
800 if (files != null) {
801 for (File file : files) {
802 deleteFolder(file);
803 }
804 }
805 folder.delete();
Amith Yamasani483f3b02012-03-13 16:08:00 -0700806 }
807
808 private void writeOldFiles() {
Svetoslavc6d1c342015-02-26 14:44:43 -0800809 deleteSystemFolder();
Amith Yamasani483f3b02012-03-13 16:08:00 -0700810 writePackagesXml();
811 writeStoppedPackagesXml();
812 writePackagesList();
813 }
814
Todd Kennedy13715d52016-08-01 13:38:57 -0700815 @Before
816 public void createUserManagerServiceRef() throws ReflectiveOperationException {
Fyodor Kupolov2fbfb102017-05-04 12:22:25 -0700817 InstrumentationRegistry.getInstrumentation().runOnMainSync((Runnable) () -> {
818 try {
819 // unregister the user manager from the local service
820 LocalServices.removeServiceForTest(UserManagerInternal.class);
821 new UserManagerService(InstrumentationRegistry.getContext());
822 } catch (Exception e) {
823 e.printStackTrace();
824 fail("Could not create user manager service; " + e);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700825 }
826 });
dcashmane7b02172015-04-27 14:02:49 -0700827 }
828
829 private void verifyKeySetMetaData(Settings settings)
830 throws ReflectiveOperationException, IllegalAccessException {
831 ArrayMap<String, PackageSetting> packages = settings.mPackages;
832 KeySetManagerService ksms = settings.mKeySetManagerService;
833
834 /* verify keyset and public key ref counts */
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700835 assertThat(KeySetUtils.getKeySetRefCount(ksms, 1), is(2));
836 assertThat(KeySetUtils.getKeySetRefCount(ksms, 2), is(1));
837 assertThat(KeySetUtils.getKeySetRefCount(ksms, 3), is(1));
838 assertThat(KeySetUtils.getKeySetRefCount(ksms, 4), is(1));
839 assertThat(KeySetUtils.getPubKeyRefCount(ksms, 1), is(2));
840 assertThat(KeySetUtils.getPubKeyRefCount(ksms, 2), is(2));
841 assertThat(KeySetUtils.getPubKeyRefCount(ksms, 3), is(1));
dcashmane7b02172015-04-27 14:02:49 -0700842
843 /* verify public keys properly read */
844 PublicKey keyA = PackageParser.parsePublicKey(KeySetStrings.ctsKeySetPublicKeyA);
845 PublicKey keyB = PackageParser.parsePublicKey(KeySetStrings.ctsKeySetPublicKeyB);
846 PublicKey keyC = PackageParser.parsePublicKey(KeySetStrings.ctsKeySetPublicKeyC);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700847 assertThat(KeySetUtils.getPubKey(ksms, 1), is(keyA));
848 assertThat(KeySetUtils.getPubKey(ksms, 2), is(keyB));
849 assertThat(KeySetUtils.getPubKey(ksms, 3), is(keyC));
dcashmane7b02172015-04-27 14:02:49 -0700850
851 /* verify mapping is correct (ks -> pub keys) */
852 LongSparseArray<ArraySet<Long>> ksMapping = KeySetUtils.getKeySetMapping(ksms);
853 ArraySet<Long> mapping = ksMapping.get(1);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700854 assertThat(mapping.size(), is(1));
855 assertThat(mapping.contains(new Long(1)), is(true));
dcashmane7b02172015-04-27 14:02:49 -0700856 mapping = ksMapping.get(2);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700857 assertThat(mapping.size(), is(1));
858 assertThat(mapping.contains(new Long(2)), is(true));
dcashmane7b02172015-04-27 14:02:49 -0700859 mapping = ksMapping.get(3);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700860 assertThat(mapping.size(), is(1));
861 assertThat(mapping.contains(new Long(3)), is(true));
dcashmane7b02172015-04-27 14:02:49 -0700862 mapping = ksMapping.get(4);
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700863 assertThat(mapping.size(), is(2));
864 assertThat(mapping.contains(new Long(1)), is(true));
865 assertThat(mapping.contains(new Long(2)), is(true));
dcashmane7b02172015-04-27 14:02:49 -0700866
867 /* verify lastIssuedIds are consistent */
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700868 assertThat(KeySetUtils.getLastIssuedKeyId(ksms), is(3L));
869 assertThat(KeySetUtils.getLastIssuedKeySetId(ksms), is(4L));
dcashmane7b02172015-04-27 14:02:49 -0700870
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700871 /* verify packages have been given the appropriate information */
dcashmane7b02172015-04-27 14:02:49 -0700872 PackageSetting ps = packages.get("com.google.app1");
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700873 assertThat(ps.keySetData.getProperSigningKeySet(), is(1L));
dcashmane7b02172015-04-27 14:02:49 -0700874 ps = packages.get("com.google.app2");
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700875 assertThat(ps.keySetData.getProperSigningKeySet(), is(1L));
876 assertThat(ps.keySetData.getAliases().get("AB"), is(4L));
dcashmane7b02172015-04-27 14:02:49 -0700877 ps = packages.get("com.android.app3");
Todd Kennedy266ceaf2016-07-26 15:39:33 -0700878 assertThat(ps.keySetData.getProperSigningKeySet(), is(2L));
879 assertThat(ps.keySetData.getAliases().get("C"), is(3L));
880 assertThat(ps.keySetData.getUpgradeKeySets().length, is(1));
881 assertThat(ps.keySetData.getUpgradeKeySets()[0], is(3L));
Amith Yamasani483f3b02012-03-13 16:08:00 -0700882 }
883}