Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3562068a authored by Hai Zhang's avatar Hai Zhang
Browse files

Add test for permission APEX.

Bug: 152055112
Test: atest PermissionApexTests
Change-Id: I4201501d586fe62df7a04d0e2da955d6d873d3df
parent 8a6ef8e5
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
{
  "presubmit" : [
    {
      "name" : "PermissionApexTests"
    }
  ]
}
+37 −0
Original line number Diff line number Diff line
// 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.

android_test {
    name: "PermissionApexTests",
    sdk_version: "test_current",
    srcs: [
        "java/**/*.kt",
    ],
    static_libs: [
        "service-permission",
        "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",
    ],
}
+32 −0
Original line number Diff line number Diff line
<?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>
+110 −0
Original line number Diff line number Diff line
/*
 * 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"
    }
}
+101 −0
Original line number Diff line number Diff line
/*
 * 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"
    }
}
Loading