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

Commit 37873686 authored by Eric Biggers's avatar Eric Biggers
Browse files

Improve LockStateTrackingRule

- Make assertLocked and assertUnlocked check
  KeyguardManager#isDeviceLocked, in addition to what they were checking
  before.  This is important, as this verifies what TrustManagerService
  (and thus also Keystore) considers the device locked state to be.

- Rename assertUnlocked to assertUnlockedAndTrusted.  This makes it
  clear that it checks for trusted (which implies unlocked), not just
  unlocked (which does not necessarily imply trusted).

- Rename the inner class LockState to TrustState.  This makes it clear
  what it actually is.

- Improve the class comment.

Bug: 296464083
Bug: 298249081
Flag: TEST_ONLY
Test: atest TrustTests
Change-Id: I865ec19dff7ebe00ff083da29154e3c9cb846574
parent 3391fd85
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -72,7 +72,7 @@ class GrantAndRevokeTrustTest {
        trustAgentRule.agent.grantTrust(GRANT_MESSAGE, 10000, 0) {}
        uiDevice.sleep()

        lockStateTrackingRule.assertUnlocked()
        lockStateTrackingRule.assertUnlockedAndTrusted()
    }

    @Test
+2 −2
Original line number Diff line number Diff line
@@ -102,7 +102,7 @@ class TemporaryAndRenewableTrustTest {
        trustAgentRule.agent.grantTrust(
            GRANT_MESSAGE, 0, FLAG_GRANT_TRUST_TEMPORARY_AND_RENEWABLE) {}

        lockStateTrackingRule.assertUnlocked()
        lockStateTrackingRule.assertUnlockedAndTrusted()
    }

    @Test
@@ -125,7 +125,7 @@ class TemporaryAndRenewableTrustTest {
            Log.i(TAG, "Callback received; status=${it.status}")
            result = it
        }
        lockStateTrackingRule.assertUnlocked()
        lockStateTrackingRule.assertUnlockedAndTrusted()

        wait("callback triggered") { result?.status == STATUS_UNLOCKED_BY_GRANT }
    }
+22 −10
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.trust.test.lib

import android.app.KeyguardManager
import android.app.trust.TrustManager
import android.app.trust.TrustManager.TrustListener
import android.content.Context
@@ -27,18 +28,23 @@ import org.junit.runner.Description
import org.junit.runners.model.Statement

/**
 * Rule for tracking the lock state of the device based on events emitted to [TrustListener].
 * Rule for tracking the trusted state of the device based on events emitted to
 * [TrustListener].  Provides helper methods for verifying that the trusted
 * state has a particular value and is consistent with (a) the keyguard "locked"
 * (i.e. showing) value when applicable, and (b) the device locked value that is
 * tracked by TrustManagerService and is queryable via KeyguardManager.
 */
class LockStateTrackingRule : TestRule {
    private val context: Context = getApplicationContext()
    private val windowManager = checkNotNull(WindowManagerGlobal.getWindowManagerService())
    private val keyguardManager = context.getSystemService(KeyguardManager::class.java) as KeyguardManager

    @Volatile lateinit var lockState: LockState
    @Volatile lateinit var trustState: TrustState
        private set

    override fun apply(base: Statement, description: Description) = object : Statement() {
        override fun evaluate() {
            lockState = LockState(locked = windowManager.isKeyguardLocked)
            trustState = TrustState()
            val trustManager = context.getSystemService(TrustManager::class.java) as TrustManager
            val listener = Listener()

@@ -52,12 +58,18 @@ class LockStateTrackingRule : TestRule {
    }

    fun assertLocked() {
        wait("un-locked per TrustListener") { lockState.locked == true }
        wait("keyguard lock") { windowManager.isKeyguardLocked }
        wait("device locked") { keyguardManager.isDeviceLocked }
        // isDeviceLocked implies isKeyguardLocked && !trusted.
        wait("keyguard locked") { windowManager.isKeyguardLocked }
        wait("not trusted") { trustState.trusted == false }
    }

    fun assertUnlocked() {
        wait("locked per TrustListener") { lockState.locked == false }
    fun assertUnlockedAndTrusted() {
        wait("device unlocked") { !keyguardManager.isDeviceLocked }
        wait("trusted") { trustState.trusted == true }
        // Can't check for !isKeyguardLocked here, since isKeyguardLocked
        // returns true in the case where the keyguard is dismissible with
        // swipe, which is considered "device unlocked"!
    }

    inner class Listener : TrustListener {
@@ -69,7 +81,7 @@ class LockStateTrackingRule : TestRule {
            trustGrantedMessages: MutableList<String>
        ) {
            Log.d(TAG, "Device became trusted=$enabled")
            lockState = lockState.copy(locked = !enabled)
            trustState = trustState.copy(trusted=enabled)
        }

        override fun onTrustManagedChanged(enabled: Boolean, userId: Int) {
@@ -79,8 +91,8 @@ class LockStateTrackingRule : TestRule {
        }
    }

    data class LockState(
        val locked: Boolean? = null
    data class TrustState(
        val trusted: Boolean? = null
    )

    companion object {