1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #define private public
16 #define protected public
17 #include "screenlock_manager.h"
18 #undef private
19 #undef protected
20
21 #include <cstdint>
22 #include <list>
23 #include <string>
24 #include <sys/time.h>
25
26 #include "sclock_log.h"
27 #include "screenlock_callback_test.h"
28 #include "screenlock_client_test.h"
29 #include "screenlock_common.h"
30 #include "screenlock_event_list_test.h"
31 #include "screenlock_notify_test_instance.h"
32 #include "screenlock_system_ability.h"
33 #include "securec.h"
34
35 namespace OHOS {
36 namespace ScreenLock {
37 using namespace testing::ext;
38
39 static EventListenerTest g_unlockTestListener;
40
SetUpTestCase()41 void ScreenLockClientTest::SetUpTestCase()
42 {
43 }
44
TearDownTestCase()45 void ScreenLockClientTest::TearDownTestCase()
46 {
47 }
48
SetUp()49 void ScreenLockClientTest::SetUp()
50 {
51 }
52
TearDown()53 void ScreenLockClientTest::TearDown()
54 {
55 }
56
57 /**
58 * @tc.name: SetScreenLockTest001
59 * @tc.desc: set screen state and get state of the screen.
60 * @tc.type: FUNC
61 * @tc.require:
62 * @tc.author:
63 */
64 HWTEST_F(ScreenLockClientTest, SetScreenLockTest001, TestSize.Level0)
65 {
66 SCLOCK_HILOGD("Test IsScreenLocked state");
67 int32_t userId = ScreenLockSystemAbility::GetInstance()->GetState().GetCurrentUser();
68 ScreenLockSystemAbility::GetInstance()->SetScreenlocked(true, userId);
69 bool isLocked = ScreenLockSystemAbility::GetInstance()->IsScreenLocked();
70 EXPECT_EQ(isLocked, true);
71 }
72
73 /**
74 * @tc.name: GetSecureTest002
75 * @tc.desc: get secure.
76 * @tc.type: FUNC
77 * @tc.require:
78 * @tc.author:
79 */
80 HWTEST_F(ScreenLockClientTest, GetSecureTest002, TestSize.Level0)
81 {
82 SCLOCK_HILOGD("Test secure");
83 bool isLocked = false;
84 ScreenLockManager::GetInstance()->IsLocked(isLocked);
85 bool result = ScreenLockManager::GetInstance()->GetSecure();
86 SCLOCK_HILOGD(" result is-------->%{public}d", result);
87 EXPECT_EQ(result, false);
88 }
89
90 /**
91 * @tc.name: LockTest003
92 * @tc.desc: Test Lock and Unlock
93 * @tc.type: FUNC
94 * @tc.require:
95 * @tc.author:
96 */
97 HWTEST_F(ScreenLockClientTest, LockTest003, TestSize.Level0)
98 {
99 SCLOCK_HILOGD("Test RequestLock and RequestUnlock");
100 sptr<ScreenLockCallbackInterface> listener = nullptr;
101 int32_t result = ScreenLockManager::GetInstance()->Lock(listener);
102 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
103 result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCK, listener);
104 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
105 result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCKSCREEN, listener);
106 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
107 listener = new (std::nothrow) ScreenlockCallbackTest(g_unlockTestListener);
108 ASSERT_NE(listener, nullptr);
109 result = ScreenLockManager::GetInstance()->Lock(listener);
110 EXPECT_EQ(result, E_SCREENLOCK_NOT_SYSTEM_APP);
111 result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCK, listener);
112 EXPECT_EQ(result, E_SCREENLOCK_NOT_SYSTEM_APP);
113 result = ScreenLockManager::GetInstance()->Unlock(Action::UNLOCKSCREEN, listener);
114 EXPECT_EQ(result, E_SCREENLOCK_NOT_FOCUS_APP);
115 }
116
117 /**
118 * @tc.name: OnSystemEventTest004
119 * @tc.desc: Test OnSystemEvent.
120 * @tc.type: FUNC
121 * @tc.require:
122 * @tc.author:
123 */
124 HWTEST_F(ScreenLockClientTest, OnSystemEventTest004, TestSize.Level0)
125 {
126 SCLOCK_HILOGD("Test OnSystemEvent");
127 sptr<ScreenLockSystemAbilityInterface> listener = new (std::nothrow)
128 ScreenLockSystemAbilityTest(g_unlockTestListener);
129 ASSERT_NE(listener, nullptr);
130 int32_t result = ScreenLockManager::GetInstance()->OnSystemEvent(listener);
131 EXPECT_EQ(result, E_SCREENLOCK_NOT_SYSTEM_APP);
132 }
133
134 /**
135 * @tc.name: SendScreenLockEventTest005
136 * @tc.desc: Test SendScreenLockEvent.
137 * @tc.type: FUNC
138 * @tc.require:
139 * @tc.author:
140 */
141 HWTEST_F(ScreenLockClientTest, SendScreenLockEventTest005, TestSize.Level0)
142 {
143 SCLOCK_HILOGD("Test SendScreenLockEvent");
144 int testNum = 0;
145 int32_t result = ScreenLockManager::GetInstance()->SendScreenLockEvent("test", testNum);
146 EXPECT_EQ(result, E_SCREENLOCK_NOT_SYSTEM_APP);
147 }
148
149 /**
150 * @tc.name: OnSystemEventTest006
151 * @tc.desc: Test OnSystemEvent.
152 * @tc.type: FUNC
153 * @tc.require:
154 * @tc.author:
155 */
156 HWTEST_F(ScreenLockClientTest, OnSystemEventTest006, TestSize.Level0)
157 {
158 SCLOCK_HILOGD("Test OnSystemEvent");
159 sptr<ScreenLockSystemAbilityInterface> listener = nullptr;
160 int32_t result = ScreenLockManager::GetInstance()->OnSystemEvent(listener);
161 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
162 }
163
164 /**
165 * @tc.name: GetProxyTest007
166 * @tc.desc: Test GetProxy.
167 * @tc.type: FUNC
168 * @tc.require:
169 * @tc.author:
170 */
171 HWTEST_F(ScreenLockClientTest, GetProxyTest007, TestSize.Level0)
172 {
173 SCLOCK_HILOGD("Test GetProxy");
174 ScreenLockManager::GetInstance()->screenlockManagerProxy_ = nullptr;
175 sptr<ScreenLockManagerInterface> proxy = ScreenLockManager::GetInstance()->GetProxy();
176 EXPECT_NE(proxy, nullptr);
177 ScreenLockManager::GetInstance()->screenlockManagerProxy_ = nullptr;
178 proxy = nullptr;
179 proxy = ScreenLockManager::GetInstance()->GetProxy();
180 EXPECT_NE(proxy, nullptr);
181 }
182
183 /**
184 * @tc.name: ProxyTest008
185 * @tc.desc: Test Lock, UnLock and OnSystemEvent.
186 * @tc.type: FUNC
187 * @tc.require:
188 * @tc.author:
189 */
190 HWTEST_F(ScreenLockClientTest, ProxyTest008, TestSize.Level0)
191 {
192 SCLOCK_HILOGD("Test RequestLock, RequestUnLock and OnSystemEvent.");
193 auto proxy = ScreenLockManager::GetInstance()->GetProxy();
194 sptr<ScreenLockSystemAbilityInterface> listener = nullptr;
195 int32_t result = proxy->OnSystemEvent(listener);
196 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
197 sptr<ScreenLockCallbackInterface> callback = nullptr;
198 result = proxy->Lock(callback);
199 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
200 result = proxy->Unlock(callback);
201 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
202 result = proxy->Unlock(callback);
203 EXPECT_EQ(result, E_SCREENLOCK_NULLPTR);
204 }
205
206 /**
207 * @tc.name: LockTest009
208 * @tc.desc: Test Lock Screen.
209 * @tc.type: FUNC
210 * @tc.require:
211 * @tc.author:
212 */
213 HWTEST_F(ScreenLockClientTest, LockTest009, TestSize.Level0)
214 {
215 SCLOCK_HILOGD("Test RequestLock.");
216 auto proxy = ScreenLockManager::GetInstance()->GetProxy();
217 int32_t userId = 0;
218 int32_t result = proxy->Lock(userId);
219 EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
220 }
221
222 /**
223 * @tc.name: LockTest010
224 * @tc.desc: Test SetScreenLockDisabled.
225 * @tc.type: FUNC
226 * @tc.require:
227 * @tc.author:
228 */
229 HWTEST_F(ScreenLockClientTest, LockTest0010, TestSize.Level0)
230 {
231 SCLOCK_HILOGD("Test SetScreenLockDisabled.");
232 auto proxy = ScreenLockManager::GetInstance()->GetProxy();
233 int32_t userId = 0;
234 int32_t result = proxy->SetScreenLockDisabled(false, userId);
235 SCLOCK_HILOGD("SetScreenLockDisabled.[result]:%{public}d", result);
236 bool isDisabled = true;
237 result = proxy->IsScreenLockDisabled(userId, isDisabled);
238 SCLOCK_HILOGD("SetScreenLockDisabled.[result]:%{public}d", result);
239 EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
240 }
241
242
243 /**
244 * @tc.name: LockTest0011
245 * @tc.desc: Test SetScreenLockAuthState.
246 * @tc.type: FUNC
247 * @tc.require:
248 * @tc.author:
249 */
250 HWTEST_F(ScreenLockClientTest, LockTest0011, TestSize.Level0)
251 {
252 SCLOCK_HILOGD("Test SetScreenLockAuthState.");
253 auto proxy = ScreenLockManager::GetInstance()->GetProxy();
254 int32_t userId = 0;
255 std::string authtoken = "test";
256 int32_t result = proxy->SetScreenLockAuthState(1, userId, authtoken);
257 SCLOCK_HILOGD("SetScreenLockAuthState.[result]:%{public}d", result);
258 int32_t authState = 0;
259 result = proxy->GetScreenLockAuthState(userId, authState);
260 SCLOCK_HILOGD("SetScreenLockAuthState.[result]:%{public}d", result);
261 EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
262 }
263
264 /**
265 * @tc.name: LockTest0012
266 * @tc.desc: Test SetScreenLockAuthState.
267 * @tc.type: FUNC
268 * @tc.require:
269 * @tc.author:
270 */
271 HWTEST_F(ScreenLockClientTest, LockTest0012, TestSize.Level0)
272 {
273 SCLOCK_HILOGD("Test RequestStrongAuth.");
274 int32_t userId = 0;
275 std::string authtoken = "test";
276 int32_t result = ScreenLockManager::GetInstance()->RequestStrongAuth(1, userId);
277 SCLOCK_HILOGD("RequestStrongAuth.[result]:%{public}d", result);
278 int32_t reasonFlag = 0;
279 result = ScreenLockManager::GetInstance()->GetStrongAuth(userId, reasonFlag);
280 SCLOCK_HILOGD("GetStrongAuth.[result]:%{public}d", result);
281 EXPECT_EQ(result, E_SCREENLOCK_OK);
282 }
283
284 /**
285 * @tc.name: LockTest013
286 * @tc.desc: Test SetScreenLockDisabled.
287 * @tc.type: FUNC
288 * @tc.require:
289 * @tc.author:
290 */
291 HWTEST_F(ScreenLockClientTest, LockTest0013, TestSize.Level0)
292 {
293 SCLOCK_HILOGD("Test SetScreenLockDisabled.");
294 int32_t userId = 0;
295 int32_t result = ScreenLockManager::GetInstance()->SetScreenLockDisabled(false, userId);
296 SCLOCK_HILOGD("SetScreenLockDisabled.[result]:%{public}d", result);
297 bool isDisabled = true;
298 result = ScreenLockManager::GetInstance()->IsScreenLockDisabled(userId, isDisabled);
299 SCLOCK_HILOGD("SetScreenLockDisabled.[result]:%{public}d", result);
300 EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
301 }
302
303
304 /**
305 * @tc.name: LockTest0014
306 * @tc.desc: Test SetScreenLockAuthState.
307 * @tc.type: FUNC
308 * @tc.require:
309 * @tc.author:
310 */
311 HWTEST_F(ScreenLockClientTest, LockTest0014, TestSize.Level0)
312 {
313 SCLOCK_HILOGD("Test SetScreenLockAuthState.");
314 int32_t userId = 0;
315 std::string authtoken = "test";
316 int32_t result = ScreenLockManager::GetInstance()->SetScreenLockAuthState(1, userId, authtoken);
317 SCLOCK_HILOGD("SetScreenLockAuthState.[result]:%{public}d", result);
318 int32_t authState = 0;
319 result = ScreenLockManager::GetInstance()->GetScreenLockAuthState(userId, authState);
320 SCLOCK_HILOGD("SetScreenLockAuthState.[result]:%{public}d", result);
321 EXPECT_EQ(result, E_SCREENLOCK_NO_PERMISSION);
322 }
323
324 /**
325 * @tc.name: LockTest0015
326 * @tc.desc: Test RequestStrongAuth.
327 * @tc.type: FUNC
328 * @tc.require:
329 * @tc.author:
330 */
331 HWTEST_F(ScreenLockClientTest, LockTest0015, TestSize.Level0)
332 {
333 SCLOCK_HILOGD("Test SetScreenLockAuthState.");
334 auto proxy = ScreenLockManager::GetInstance()->GetProxy();
335 int32_t userId = 0;
336 int32_t result = proxy->RequestStrongAuth(1, userId);
337 SCLOCK_HILOGD("RequestStrongAuth.[result]:%{public}d", result);
338 int32_t reasonFlag = 0;
339 result = proxy->GetStrongAuth(userId, reasonFlag);
340 SCLOCK_HILOGD("GetStrongAuth.[result]:%{public}d", result);
341 EXPECT_EQ(result, E_SCREENLOCK_OK);
342 }
343
344 } // namespace ScreenLock
345 } // namespace OHOS