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.packageinstaller.test
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.content.pm.PackageManager
22 import android.net.Uri
23 import androidx.test.InstrumentationRegistry
24 import com.google.common.truth.Truth.assertThat
25 import com.google.common.truth.Truth.assertWithMessage
26 import org.junit.Test
27 
28 class ExportedComponentTest {
29 
30     private val context: Context = InstrumentationRegistry.getContext()
31 
32     @Test
33     fun verifyNoExportedReceivers() {
34         val intent = Intent(Intent.ACTION_INSTALL_PACKAGE).apply {
35             data = Uri.parse("content://mockForTest")
36         }
37         val packageInstallers = context.packageManager.queryIntentActivities(intent,
38             PackageManager.MATCH_DEFAULT_ONLY or PackageManager.MATCH_DISABLED_COMPONENTS)
39             .map { it.activityInfo.packageName }
40             .distinct()
41             .map { context.packageManager.getPackageInfo(it, PackageManager.GET_RECEIVERS) }
42 
43         assertThat(packageInstallers).isNotEmpty()
44 
45         packageInstallers.forEach {
46             val exported = it.receivers.filter { it.exported }
47             assertWithMessage("Receivers should not be exported").that(exported).isEmpty()
48         }
49     }
50 }
51