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 android.uidmigration.cts
18
19 import android.content.pm.PackageManager
20 import com.android.compatibility.common.util.SystemUtil.runShellCommand
21 import com.android.server.pm.SharedUidMigration
22 import com.android.server.pm.SharedUidMigration.PROPERTY_KEY
23 import org.junit.Assert.assertEquals
24 import org.junit.Assert.assertNotNull
25
26 const val TMP_APK_PATH = "/data/local/tmp/cts/uidmigration"
27
28 val FLAG_ZERO = PackageManager.PackageInfoFlags.of(0)
29 val FLAG_MATCH_UNINSTALLED_PACKAGES = PackageManager.PackageInfoFlags.of(
30 PackageManager.MATCH_UNINSTALLED_PACKAGES.toLong()
31 )
32
33 // What each APK meant
34 // APK : pkg , with sharedUserId
35 // APK2: pkg2, with sharedUserId
36 // APK3: pkg , with sharedUserId removed
37 // APK4: pkg , with sharedUserMaxSdkVersion="32"
38
39 object InstallTest {
40 const val APK = "$TMP_APK_PATH/InstallTestApp.apk"
41 const val APK2 = "$TMP_APK_PATH/InstallTestApp2.apk"
42 const val APK3 = "$TMP_APK_PATH/InstallTestApp3.apk"
43 const val APK4 = "$TMP_APK_PATH/InstallTestApp4.apk"
44 }
45
46 @Suppress("NOTHING_TO_INLINE")
assertNotNullnull47 inline fun <T> T?.assertNotNull(): T {
48 assertNotNull(this)
49 return this!!
50 }
51
52 @Suppress("NOTHING_TO_INLINE")
assertEqualsnull53 inline fun assertEquals(a: Int, b: Int) = assertEquals(a.toLong(), b.toLong())
54
55 // Identical regardless of order
56 fun <T> Array<T>.sameAs(vararg items: T) =
57 size == items.size && all { items.contains(it) } && items.all { contains(it) }
58
installPackagenull59 fun installPackage(apkPath: String): Boolean {
60 return runShellCommand("pm install --force-queryable -t $apkPath") == "Success\n"
61 }
62
uninstallPackagenull63 fun uninstallPackage(packageName: String) {
64 runShellCommand("pm uninstall $packageName")
65 }
66
uninstallPackageWithKeepDatanull67 fun uninstallPackageWithKeepData(packageName: String) {
68 runShellCommand("pm uninstall -k $packageName")
69 }
70
71 @SharedUidMigration.Strategy
72 var migrationStrategy: Int
73 get() = SharedUidMigration.getCurrentStrategy()
74 set(value) { runShellCommand("setprop $PROPERTY_KEY $value") }
75
withStrategynull76 inline fun withStrategy(strategy: Int? = null, body: () -> Unit) {
77 if (SharedUidMigration.isDisabled()) {
78 // Nothing to test if shared UID migration is disabled
79 return
80 }
81
82 val backup = migrationStrategy
83 strategy?.let { migrationStrategy = it }
84 try {
85 body.invoke()
86 } finally {
87 // Always restore the device state no matter what happened
88 migrationStrategy = backup
89 }
90 }
91