diff options
author | 2022-03-04 11:00:30 +0000 | |
---|---|---|
committer | 2022-03-04 11:28:50 +0000 | |
commit | 0d386dcf211f772785a241477920f167639e2270 (patch) | |
tree | fbb322a22af036b8b69c350e83b4f1b1053bb653 /tests/apex | |
parent | 56aaa3e896a01ce30d6449d520f1665947e35fc0 (diff) |
Migrate Safety Center CTS tests inside Permission module
Nest APEX tests in their own folder
Test: atest PermissionApexTests
Test: atest CtsSafetyCenterTestCases
Bug: 222475656
Change-Id: I686d211a499dbd4fe2fbe088fe8ed0ef53696a97
Diffstat (limited to 'tests/apex')
-rw-r--r-- | tests/apex/Android.bp | 43 | ||||
-rw-r--r-- | tests/apex/AndroidManifest.xml | 32 | ||||
-rw-r--r-- | tests/apex/AndroidTest.xml | 35 | ||||
-rw-r--r-- | tests/apex/java/com/android/permission/persistence/RuntimePermissionsPersistenceTest.kt | 110 | ||||
-rw-r--r-- | tests/apex/java/com/android/role/persistence/RolesPersistenceTest.kt | 101 |
5 files changed, 321 insertions, 0 deletions
diff --git a/tests/apex/Android.bp b/tests/apex/Android.bp new file mode 100644 index 000000000..126fcb4b2 --- /dev/null +++ b/tests/apex/Android.bp @@ -0,0 +1,43 @@ +// Copyright (C) 2020 The Android Open Source Project +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package { + default_applicable_licenses: ["Android-Apache-2.0"], +} + +android_test { + name: "PermissionApexTests", + defaults: ["mts-target-sdk-version-current"], + sdk_version: "test_current", + min_sdk_version: "30", + srcs: [ + "java/**/*.kt", + ], + static_libs: [ + "service-permission.impl", + "androidx.test.rules", + "androidx.test.ext.junit", + "androidx.test.ext.truth", + "mockito-target-extended-minus-junit4", + ], + jni_libs: [ + "libdexmakerjvmtiagent", + "libstaticjvmtiagent", + ], + compile_multilib: "both", + test_suites: [ + "general-tests", + "mts-permission", + ], +} diff --git a/tests/apex/AndroidManifest.xml b/tests/apex/AndroidManifest.xml new file mode 100644 index 000000000..57ee6417a --- /dev/null +++ b/tests/apex/AndroidManifest.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> + +<!-- + ~ Copyright (C) 2020 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> + +<manifest + xmlns:android="http://schemas.android.com/apk/res/android" + package="com.android.permission.test"> + + <!-- The application has to be debuggable for static mocking to work. --> + <application android:debuggable="true"> + <uses-library android:name="android.test.runner" /> + </application> + + <instrumentation + android:name="androidx.test.runner.AndroidJUnitRunner" + android:targetPackage="com.android.permission.test" + android:label="Permission APEX Tests" /> +</manifest> diff --git a/tests/apex/AndroidTest.xml b/tests/apex/AndroidTest.xml new file mode 100644 index 000000000..283f008a5 --- /dev/null +++ b/tests/apex/AndroidTest.xml @@ -0,0 +1,35 @@ +<?xml version="1.0" encoding="utf-8"?> + +<!-- + ~ Copyright (C) 2021 The Android Open Source Project + ~ + ~ Licensed under the Apache License, Version 2.0 (the "License"); + ~ you may not use this file except in compliance with the License. + ~ You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> + +<configuration description="Runs tests for Permission APEX."> + <option name="test-suite-tag" value="apct" /> + <option name="test-suite-tag" value="apct-instrumentation" /> + <option name="test-tag" value="PermissionApexTests" /> + <object type="module_controller" class="com.android.tradefed.testtype.suite.module.Sdk30ModuleController" /> + + <!-- Install test --> + <target_preparer class="com.android.tradefed.targetprep.TestAppInstallSetup"> + <option name="test-file-name" value="PermissionApexTests.apk" /> + <option name="cleanup-apks" value="true" /> + </target_preparer> + + <test class="com.android.tradefed.testtype.AndroidJUnitTest" > + <option name="package" value="com.android.permission.test" /> + <option name="runner" value="androidx.test.runner.AndroidJUnitRunner" /> + </test> +</configuration> diff --git a/tests/apex/java/com/android/permission/persistence/RuntimePermissionsPersistenceTest.kt b/tests/apex/java/com/android/permission/persistence/RuntimePermissionsPersistenceTest.kt new file mode 100644 index 000000000..2987da087 --- /dev/null +++ b/tests/apex/java/com/android/permission/persistence/RuntimePermissionsPersistenceTest.kt @@ -0,0 +1,110 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.permission.persistence + +import android.content.ApexEnvironment +import android.content.Context +import android.os.Process +import android.os.UserHandle +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession +import com.google.common.truth.Truth.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers.any +import org.mockito.ArgumentMatchers.eq +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations.initMocks +import org.mockito.MockitoSession +import org.mockito.quality.Strictness +import java.io.File + +@RunWith(AndroidJUnit4::class) +class RuntimePermissionsPersistenceTest { + private val context = InstrumentationRegistry.getInstrumentation().context + + private lateinit var mockDataDirectory: File + + private lateinit var mockitoSession: MockitoSession + @Mock + lateinit var apexEnvironment: ApexEnvironment + + private val persistence = RuntimePermissionsPersistence.createInstance() + private val permissionState = RuntimePermissionsState.PermissionState("permission", true, 3) + private val state = RuntimePermissionsState( + 1, "fingerprint", mapOf("package" to listOf(permissionState)), + mapOf("sharedUser" to listOf(permissionState)) + ) + private val user = Process.myUserHandle() + + @Before + fun createMockDataDirectory() { + mockDataDirectory = context.getDir("mock_data", Context.MODE_PRIVATE) + mockDataDirectory.listFiles()!!.forEach { assertThat(it.deleteRecursively()).isTrue() } + } + + @Before + fun mockApexEnvironment() { + initMocks(this) + mockitoSession = mockitoSession() + .mockStatic(ApexEnvironment::class.java) + .strictness(Strictness.LENIENT) + .startMocking() + `when`(ApexEnvironment.getApexEnvironment(eq(APEX_MODULE_NAME))).thenReturn(apexEnvironment) + `when`(apexEnvironment.getDeviceProtectedDataDirForUser(any(UserHandle::class.java))).then { + File(mockDataDirectory, it.arguments[0].toString()).also { it.mkdirs() } + } + } + + @After + fun finishMockingApexEnvironment() { + mockitoSession.finishMocking() + } + + @Test + fun testReadWrite() { + persistence.writeForUser(state, user) + val persistedState = persistence.readForUser(user) + + assertThat(persistedState).isEqualTo(state) + assertThat(persistedState!!.version).isEqualTo(state.version) + assertThat(persistedState.fingerprint).isEqualTo(state.fingerprint) + assertThat(persistedState.packagePermissions).isEqualTo(state.packagePermissions) + val persistedPermissionState = persistedState.packagePermissions.values.first().first() + assertThat(persistedPermissionState.name).isEqualTo(permissionState.name) + assertThat(persistedPermissionState.isGranted).isEqualTo(permissionState.isGranted) + assertThat(persistedPermissionState.flags).isEqualTo(permissionState.flags) + assertThat(persistedState.sharedUserPermissions).isEqualTo(state.sharedUserPermissions) + } + + @Test + fun testDelete() { + persistence.writeForUser(state, user) + persistence.deleteForUser(user) + val persistedState = persistence.readForUser(user) + + assertThat(persistedState).isNull() + } + + companion object { + private const val APEX_MODULE_NAME = "com.android.permission" + } +} diff --git a/tests/apex/java/com/android/role/persistence/RolesPersistenceTest.kt b/tests/apex/java/com/android/role/persistence/RolesPersistenceTest.kt new file mode 100644 index 000000000..f9d9d5afb --- /dev/null +++ b/tests/apex/java/com/android/role/persistence/RolesPersistenceTest.kt @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.role.persistence + +import android.content.ApexEnvironment +import android.content.Context +import android.os.Process +import android.os.UserHandle +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession +import com.google.common.truth.Truth.assertThat +import org.junit.After +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.ArgumentMatchers.any +import org.mockito.ArgumentMatchers.eq +import org.mockito.Mock +import org.mockito.Mockito.`when` +import org.mockito.MockitoAnnotations.initMocks +import org.mockito.MockitoSession +import org.mockito.quality.Strictness +import java.io.File + +@RunWith(AndroidJUnit4::class) +class RolesPersistenceTest { + private val context = InstrumentationRegistry.getInstrumentation().context + + private lateinit var mockDataDirectory: File + + private lateinit var mockitoSession: MockitoSession + @Mock + lateinit var apexEnvironment: ApexEnvironment + + private val persistence = RolesPersistence.createInstance() + private val state = RolesState(1, "packagesHash", mapOf("role" to setOf("holder1", "holder2"))) + private val user = Process.myUserHandle() + + @Before + fun createMockDataDirectory() { + mockDataDirectory = context.getDir("mock_data", Context.MODE_PRIVATE) + mockDataDirectory.listFiles()!!.forEach { assertThat(it.deleteRecursively()).isTrue() } + } + + @Before + fun mockApexEnvironment() { + initMocks(this) + mockitoSession = mockitoSession() + .mockStatic(ApexEnvironment::class.java) + .strictness(Strictness.LENIENT) + .startMocking() + `when`(ApexEnvironment.getApexEnvironment(eq(APEX_MODULE_NAME))).thenReturn(apexEnvironment) + `when`(apexEnvironment.getDeviceProtectedDataDirForUser(any(UserHandle::class.java))).then { + File(mockDataDirectory, it.arguments[0].toString()).also { it.mkdirs() } + } + } + + @After + fun finishMockingApexEnvironment() { + mockitoSession.finishMocking() + } + + @Test + fun testReadWrite() { + persistence.writeForUser(state, user) + val persistedState = persistence.readForUser(user) + + assertThat(persistedState).isEqualTo(state) + assertThat(persistedState!!.version).isEqualTo(state.version) + assertThat(persistedState.packagesHash).isEqualTo(state.packagesHash) + assertThat(persistedState.roles).isEqualTo(state.roles) + } + + @Test + fun testDelete() { + persistence.writeForUser(state, user) + persistence.deleteForUser(user) + val persistedState = persistence.readForUser(user) + + assertThat(persistedState).isNull() + } + + companion object { + private const val APEX_MODULE_NAME = "com.android.permission" + } +} |