1 /*
2  * Copyright (C) 2024 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.xts.root
18 
19 import com.android.bedstead.adb.adb
20 import com.android.bedstead.harrier.BedsteadJUnit4
21 import com.android.bedstead.harrier.DeviceState
22 import com.android.bedstead.nene.TestApis
23 import com.android.bedstead.nene.utils.ShellCommandUtils
24 import com.android.xts.root.annotations.RequireAdbRoot
25 import com.android.xts.root.annotations.RequireRootInstrumentation
26 import com.google.common.truth.Truth.assertThat
27 import org.junit.ClassRule
28 import org.junit.Rule
29 import org.junit.Test
30 import org.junit.runner.RunWith
31 
32 @RunWith(BedsteadJUnit4::class)
33 class RootAnnotationExecutorTest {
34 
35     @Test
36     @RequireAdbRoot
requireAdbRootAnnotation_hasAdbRootnull37     fun requireAdbRootAnnotation_hasAdbRoot() {
38         assertThat(TestApis.adb().isRootAvailable()).isTrue()
39     }
40 
41     @Test
42     @RequireAdbRoot
testUsesAdbRoot_usesRequireAdbRootAnnotation_returnsTruenull43     fun testUsesAdbRoot_usesRequireAdbRootAnnotation_returnsTrue() {
44         assertThat(deviceState.testUsesAdbRoot()).isTrue()
45     }
46 
47     @Test
testUsesAdbRoot_doesNotUseRequireAdbRootAnnotation_returnsFalsenull48     fun testUsesAdbRoot_doesNotUseRequireAdbRootAnnotation_returnsFalse() {
49         assertThat(deviceState.testUsesAdbRoot()).isFalse()
50     }
51 
52     @Test
53     @RequireRootInstrumentation(reason = "testing root instrumentation")
requireRootInstrumentation_isRecognisedAsRootBySystemServernull54     fun requireRootInstrumentation_isRecognisedAsRootBySystemServer() {
55         // This will throw if not instrumented as root
56         ShellCommandUtils.uiAutomation().clearOverridePermissionStates(/* uid = */ -1)
57     }
58 
59     @Test
60     @RequireRootInstrumentation(reason = "testing root instrumentation")
testUsesRootInstrumentation_usesRequireRootInstrumentationAnnotation_returnsTruenull61     fun testUsesRootInstrumentation_usesRequireRootInstrumentationAnnotation_returnsTrue() {
62         assertThat(deviceState.testUsesRootInstrumentation()).isTrue()
63     }
64 
65     @Test
testUsesRootInstrumentation_doesNotUseRequireRootInstrumentationAnnotation_returnsFalsenull66     fun testUsesRootInstrumentation_doesNotUseRequireRootInstrumentationAnnotation_returnsFalse() {
67         assertThat(deviceState.testUsesRootInstrumentation()).isFalse()
68     }
69 
70     companion object {
71         @ClassRule @Rule @JvmField
72         val deviceState = DeviceState()
73     }
74 }
75