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 
30 // What each APK meant
31 // APK : pkg , with sharedUserId
32 // APK2: pkg2, with sharedUserId
33 // APK3: pkg , with sharedUserId removed
34 // APK4: pkg , with sharedUserMaxSdkVersion="32"
35 
36 object InstallTest {
37     const val APK = "$TMP_APK_PATH/InstallTestApp.apk"
38     const val APK2 = "$TMP_APK_PATH/InstallTestApp2.apk"
39     const val APK3 = "$TMP_APK_PATH/InstallTestApp3.apk"
40     const val APK4 = "$TMP_APK_PATH/InstallTestApp4.apk"
41 }
42 
43 @Suppress("NOTHING_TO_INLINE")
assertNotNullnull44 inline fun <T> T?.assertNotNull(): T {
45     assertNotNull(this)
46     return this!!
47 }
48 
49 @Suppress("NOTHING_TO_INLINE")
assertEqualsnull50 inline fun assertEquals(a: Int, b: Int) = assertEquals(a.toLong(), b.toLong())
51 
52 // Identical regardless of order
53 fun <T> Array<T>.sameAs(vararg items: T) =
54         size == items.size && all { items.contains(it) } && items.all { contains(it) }
55 
installPackagenull56 fun installPackage(apkPath: String): Boolean {
57     return runShellCommand("pm install --force-queryable -t $apkPath") == "Success\n"
58 }
59 
uninstallPackagenull60 fun uninstallPackage(packageName: String) {
61     runShellCommand("pm uninstall $packageName")
62 }
63 
64 @SharedUidMigration.Strategy
65 var migrationStrategy: Int
66     get() = SharedUidMigration.getCurrentStrategy()
67     set(value) { runShellCommand("setprop $PROPERTY_KEY $value") }
68 
withStrategynull69 inline fun withStrategy(strategy: Int? = null, body: () -> Unit) {
70     if (SharedUidMigration.isDisabled()) {
71         // Nothing to test if shared UID migration is disabled
72         return
73     }
74 
75     val backup = migrationStrategy
76     strategy?.let { migrationStrategy = it }
77     try {
78         body.invoke()
79     } finally {
80         // Always restore the device state no matter what happened
81         migrationStrategy = backup
82     }
83 }