1 /*
2  * Copyright (C) 2021 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.pm.test
18 
19 import com.android.internal.util.test.SystemPreparer
20 import com.android.tradefed.testtype.DeviceJUnit4ClassRunner
21 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test
22 import com.google.common.truth.Truth.assertThat
23 import com.google.common.truth.Truth.assertWithMessage
24 import org.junit.After
25 import org.junit.Before
26 import org.junit.ClassRule
27 import org.junit.Rule
28 import org.junit.Test
29 import org.junit.rules.TemporaryFolder
30 import org.junit.runner.RunWith
31 import java.io.File
32 
33 @RunWith(DeviceJUnit4ClassRunner::class)
34 class OverlayActorVisibilityTest : BaseHostJUnit4Test() {
35 
36     companion object {
37         private const val ACTOR_PKG_NAME = "com.android.server.pm.test.overlay.actor"
38         private const val ACTOR_APK = "PackageManagerTestOverlayActor.apk"
39         private const val TARGET_APK = "PackageManagerTestOverlayTarget.apk"
40         private const val OVERLAY_APK = "PackageManagerTestOverlay.apk"
41         private const val TARGET_NO_OVERLAYABLE_APK =
42             "PackageManagerTestOverlayTargetNoOverlayable.apk"
43 
44         @get:ClassRule
45         val deviceRebootRule = SystemPreparer.TestRuleDelegate(true)
46     }
47 
48     @get:Rule
49     val tempFolder = TemporaryFolder()
50 
51     private val preparer: SystemPreparer = SystemPreparer(
52         tempFolder,
53         SystemPreparer.RebootStrategy.FULL,
54         deviceRebootRule
55     ) { this.device }
56 
57     private val namedActorFile = File(
58         "/system/etc/sysconfig/com.android.server.pm.test.OverlayActorVisibilityTest.xml"
59     )
60 
61     @Before
62     @After
63     fun uninstallPackages() {
64         device.uninstallPackages(ACTOR_APK, TARGET_APK, OVERLAY_APK)
65     }
66 
67     @Before
68     fun pushSysConfigFile() {
69         // In order for the test app to be the verification agent, it needs a permission file
70         // which can be pushed onto the system and removed afterwards.
71         // language=XML
72         val file = tempFolder.newFile().apply {
73             """
74             <config>
75                 <named-actor
76                     namespace="androidTest"
77                     name="OverlayActorVisibilityTest"
78                     package="$ACTOR_PKG_NAME"
79                     />
80             </config>
81             """
82                 .trimIndent()
83                 .let { writeText(it) }
84         }
85 
86         preparer.pushFile(file, namedActorFile.toString())
87             .reboot()
88     }
89 
90     @After
91     fun deleteSysConfigFile() {
92         preparer.deleteFile(namedActorFile.toString())
93             .reboot()
94     }
95 
96     @Test
97     fun testVisibilityByOverlayable() {
98         assertThat(device.installJavaResourceApk(tempFolder, ACTOR_APK, false)).isNull()
99         assertThat(device.installJavaResourceApk(tempFolder, OVERLAY_APK, false)).isNull()
100         assertThat(device.installJavaResourceApk(tempFolder, TARGET_NO_OVERLAYABLE_APK, false))
101             .isNull()
102 
103         runDeviceTests(
104             ACTOR_PKG_NAME, "$ACTOR_PKG_NAME.OverlayableVisibilityTest",
105             "verifyNotVisible"
106         )
107 
108         assertThat(device.installJavaResourceApk(tempFolder, TARGET_APK, true)).isNull()
109 
110         assertWithMessage(device.executeShellCommand("dumpsys package $OVERLAY_APK"))
111 
112         runDeviceTests(
113             ACTOR_PKG_NAME, "$ACTOR_PKG_NAME.OverlayableVisibilityTest",
114             "verifyVisible"
115         )
116     }
117 }
118