1 /*
2  * Copyright (C) 2021 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.devicepolicy.cts;
18 
19 import static android.content.pm.PackageManager.FEATURE_AUTOMOTIVE;
20 
21 import static com.android.bedstead.metricsrecorder.truth.MetricQueryBuilderSubject.assertThat;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.testng.Assert.assertThrows;
26 
27 import android.app.KeyguardManager;
28 import android.app.admin.DevicePolicyManager;
29 import android.stats.devicepolicy.EventId;
30 
31 import com.android.bedstead.harrier.BedsteadJUnit4;
32 import com.android.bedstead.harrier.DeviceState;
33 import com.android.bedstead.harrier.annotations.EnsurePasswordNotSet;
34 import com.android.bedstead.harrier.annotations.EnsurePasswordSet;
35 import com.android.bedstead.harrier.annotations.EnsureScreenIsOn;
36 import com.android.bedstead.harrier.annotations.Postsubmit;
37 import com.android.bedstead.harrier.annotations.RequireDoesNotHaveFeature;
38 import com.android.bedstead.harrier.annotations.RequireFeature;
39 import com.android.bedstead.enterprise.annotations.CannotSetPolicyTest;
40 import com.android.bedstead.enterprise.annotations.PolicyAppliesTest;
41 import com.android.bedstead.enterprise.annotations.PolicyDoesNotApplyTest;
42 import com.android.bedstead.harrier.policies.LockNow;
43 import com.android.bedstead.harrier.policies.MaximumTimeToLock;
44 import com.android.bedstead.metricsrecorder.EnterpriseMetricsRecorder;
45 import com.android.bedstead.nene.TestApis;
46 import com.android.bedstead.nene.utils.Poll;
47 import com.android.compatibility.common.util.ApiTest;
48 
49 import org.junit.Assume;
50 import org.junit.ClassRule;
51 import org.junit.Ignore;
52 import org.junit.Rule;
53 import org.junit.runner.RunWith;
54 
55 @RunWith(BedsteadJUnit4.class)
56 @RequireFeature("android.software.secure_lock_screen")
57 public class LockTest {
58 
59     private static final long TIMEOUT = 10000;
60 
61     @ClassRule @Rule
62     public static final DeviceState sDeviceState = new DeviceState();
63 
64     private static final DevicePolicyManager sLocalDevicePolicyManager =
65             TestApis.context().instrumentedContext().getSystemService(DevicePolicyManager.class);
66     private static final KeyguardManager sLocalKeyguardManager =
67             TestApis.context().instrumentedContext().getSystemService(KeyguardManager.class);
68 
69     @CannotSetPolicyTest(policy = LockNow.class, includeNonDeviceAdminStates = false)
70     @Postsubmit(reason = "New test")
71     @ApiTest(apis = "android.app.admin.DevicePolicyManager#lockNow")
lockNow_notPermitted_throwsException()72     public void lockNow_notPermitted_throwsException() {
73         assertThrows(SecurityException.class,
74                 () -> sDeviceState.dpc().devicePolicyManager().lockNow());
75     }
76 
77     @PolicyAppliesTest(policy = LockNow.class)
78     @Postsubmit(reason = "New test")
79     @EnsurePasswordNotSet
80     @ApiTest(apis = "android.app.admin.DevicePolicyManager#lockNow")
lockNow_logsMetric()81     public void lockNow_logsMetric() {
82         try (EnterpriseMetricsRecorder metrics = EnterpriseMetricsRecorder.create()) {
83             sDeviceState.dpc().devicePolicyManager().lockNow(/* flags= */ 0);
84 
85             assertThat(metrics.query()
86                     .whereType().isEqualTo(EventId.LOCK_NOW_VALUE)
87                     .whereAdminPackageName().isEqualTo(sDeviceState.dpc().packageName())
88                     .whereInteger().isEqualTo(0)
89             ).wasLogged();
90         }
91     }
92 
93     @RequireDoesNotHaveFeature(FEATURE_AUTOMOTIVE)
94     @EnsureScreenIsOn
95     @EnsurePasswordNotSet
96     @Postsubmit(reason = "New test")
97     @PolicyAppliesTest(policy = LockNow.class)
98     @ApiTest(apis = "android.app.admin.DevicePolicyManager#lockNow")
lockNow_noPasswordSet_turnsScreenOff()99     public void lockNow_noPasswordSet_turnsScreenOff() throws Exception {
100         Assume.assumeFalse("LockNow on profile won't turn off screen",
101                 sDeviceState.dpc().user().isProfile());
102         sDeviceState.dpc().devicePolicyManager().lockNow();
103 
104         Poll.forValue("isScreenOn", () -> TestApis.device().isScreenOn())
105                 .toBeEqualTo(false)
106                 .errorOnFail()
107                 .await();
108     }
109 
110     @RequireFeature(FEATURE_AUTOMOTIVE)
111     @EnsureScreenIsOn
112     @EnsurePasswordNotSet
113     @Postsubmit(reason = "New test")
114     @PolicyAppliesTest(policy = LockNow.class)
115     @ApiTest(apis = "android.app.admin.DevicePolicyManager#lockNow")
lockNow_automotive_noPasswordSet_doesNotTurnScreenOff()116     public void lockNow_automotive_noPasswordSet_doesNotTurnScreenOff() throws Exception {
117         sDeviceState.dpc().devicePolicyManager().lockNow();
118 
119         assertThat(TestApis.device().isScreenOn()).isTrue();
120     }
121 
122     @RequireDoesNotHaveFeature(FEATURE_AUTOMOTIVE)
123     @EnsureScreenIsOn
124     @EnsurePasswordSet
125     @Postsubmit(reason = "New test")
126     @PolicyAppliesTest(policy = LockNow.class)
127     @ApiTest(apis = "android.app.admin.DevicePolicyManager#lockNow")
lockNow_passwordSet_locksDevice()128     public void lockNow_passwordSet_locksDevice() throws Exception {
129         sDeviceState.dpc().devicePolicyManager().lockNow();
130 
131         Poll.forValue("isDeviceLocked", sLocalKeyguardManager::isDeviceLocked)
132                 .toBeEqualTo(true)
133                 .errorOnFail()
134                 .await();
135     }
136 
137     @CannotSetPolicyTest(policy = MaximumTimeToLock.class, includeNonDeviceAdminStates = false)
138     @Postsubmit(reason = "New test")
139     @ApiTest(apis = "android.app.admin.DevicePolicyManager#setMaximumTimeToLock")
setMaximumTimeToLock_notPermitted_throwsException()140     public void setMaximumTimeToLock_notPermitted_throwsException() {
141         assertThrows(SecurityException.class,
142                 () -> sDeviceState.dpc().devicePolicyManager()
143                         .setMaximumTimeToLock(sDeviceState.dpc().componentName(), TIMEOUT));
144     }
145 
146     @PolicyAppliesTest(policy = MaximumTimeToLock.class)
147     @Postsubmit(reason = "New test")
148     @ApiTest(apis = {"android.app.admin.DevicePolicyManager#setMaximumTimeToLock",
149             "android.app.admin.DevicePolicyManager#getMaximumTimeToLock"})
150     @Ignore // Incorrect logic
setMaximumTimeToLock_maximumTimeToLockIsSet()151     public void setMaximumTimeToLock_maximumTimeToLockIsSet() {
152         long originalTimeout = sDeviceState.dpc().devicePolicyManager()
153                 .getMaximumTimeToLock(sDeviceState.dpc().componentName());
154 
155         assertThat(TestApis.devicePolicy().getMaximumTimeToLock()).isEqualTo(TIMEOUT);
156 
157         try {
158             sDeviceState.dpc().devicePolicyManager()
159                     .setMaximumTimeToLock(sDeviceState.dpc().componentName(), TIMEOUT);
160 
161         } finally {
162             sDeviceState.dpc().devicePolicyManager().setMaximumTimeToLock(
163                     sDeviceState.dpc().componentName(), originalTimeout);
164         }
165     }
166 
167     @PolicyDoesNotApplyTest(policy = MaximumTimeToLock.class)
168     @Postsubmit(reason = "New test")
169     @ApiTest(apis = {"android.app.admin.DevicePolicyManager#setMaximumTimeToLock",
170             "android.app.admin.DevicePolicyManager#getMaximumTimeToLock"})
setMaximumTimeToLock_doesNotApply_maximumTimeToLockIsNotSet()171     public void setMaximumTimeToLock_doesNotApply_maximumTimeToLockIsNotSet() {
172         long originalTimeout = sDeviceState.dpc().devicePolicyManager()
173                 .getMaximumTimeToLock(sDeviceState.dpc().componentName());
174 
175         try {
176             sDeviceState.dpc().devicePolicyManager()
177                     .setMaximumTimeToLock(sDeviceState.dpc().componentName(), TIMEOUT);
178 
179             assertThat(TestApis.devicePolicy().getMaximumTimeToLock()).isNotEqualTo(TIMEOUT);
180 
181         } finally {
182             sDeviceState.dpc().devicePolicyManager().setMaximumTimeToLock(
183                     sDeviceState.dpc().componentName(), originalTimeout);
184         }
185     }
186 }
187