Debian Codegraph @

tests/TrustTests/src/android/trust/test/GrantAndRevokeTrustTest.kt

raw
1 /*
2 * Copyright (C) 2022 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 android.trust.test
18
19 import android.service.trust.GrantTrustResult
20 import android.trust.BaseTrustAgentService
21 import android.trust.TrustTestActivity
22 import android.trust.test.lib.LockStateTrackingRule
23 import android.trust.test.lib.ScreenLockRule
24 import android.trust.test.lib.TrustAgentRule
25 import androidx.test.ext.junit.rules.ActivityScenarioRule
26 import androidx.test.ext.junit.runners.AndroidJUnit4
27 import androidx.test.platform.app.InstrumentationRegistry.getInstrumentation
28 import androidx.test.uiautomator.UiDevice
29 import com.android.server.testutils.mock
30 import org.junit.Before
31 import org.junit.Rule
32 import org.junit.Test
33 import org.junit.rules.RuleChain
34 import org.junit.runner.RunWith
35 import org.mockito.Mockito.verifyZeroInteractions
36
37 /**
38 * Test for testing revokeTrust & grantTrust for non-renewable trust.
39 *
40 * atest TrustTests:GrantAndRevokeTrustTest
41 */
42 @RunWith(AndroidJUnit4::class)
43 class GrantAndRevokeTrustTest {
44 private val uiDevice = UiDevice.getInstance(getInstrumentation())
45 private val activityScenarioRule = ActivityScenarioRule(TrustTestActivity::class.java)
46 private val lockStateTrackingRule = LockStateTrackingRule()
47 private val trustAgentRule = TrustAgentRule<GrantAndRevokeTrustAgent>()
48
49 @get:Rule
50 val rule: RuleChain = RuleChain
51 .outerRule(activityScenarioRule)
52 .around(ScreenLockRule())
53 .around(lockStateTrackingRule)
54 .around(trustAgentRule)
55
56 @Before
57 fun manageTrust() {
58 trustAgentRule.agent.setManagingTrust(true)
59 }
60
61 // This test serves a baseline for Grant tests, verifying that the default behavior of the
62 // device is to lock when put to sleep
63 @Test
64 fun sleepingDeviceWithoutGrantLocksDevice() {
65 uiDevice.sleep()
66
67 lockStateTrackingRule.assertLocked()
68 }
69
70 @Test
71 fun grantKeepsDeviceUnlocked() {
72 trustAgentRule.agent.grantTrust(GRANT_MESSAGE, 10000, 0) {}
73 uiDevice.sleep()
74
75 lockStateTrackingRule.assertUnlocked()
76 }
77
78 @Test
79 fun grantKeepsDeviceUnlocked_untilRevoked() {
80 trustAgentRule.agent.grantTrust(GRANT_MESSAGE, 0, 0) {}
81 await()
82 uiDevice.sleep()
83 trustAgentRule.agent.revokeTrust()
84
85 lockStateTrackingRule.assertLocked()
86 }
87
88 @Test
89 fun grantDoesNotCallBack() {
90 val callback = mock<(GrantTrustResult) -> Unit>()
91 trustAgentRule.agent.grantTrust(GRANT_MESSAGE, 0, 0, callback)
92 await()
93
94 verifyZeroInteractions(callback)
95 }
96
97 companion object {
98 private const val TAG = "GrantAndRevokeTrustTest"
99 private const val GRANT_MESSAGE = "granted by test"
100 private fun await() = Thread.sleep(250)
101 }
102 }
103
104 class GrantAndRevokeTrustAgent : BaseTrustAgentService()