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
16 #include <cstdint>
17 #include <list>
18 #include <string>
19 #include <sys/time.h>
20
21 #include "sclock_log.h"
22 #include "screenlock_common.h"
23 #include "securec.h"
24 #ifndef IS_SO_CROP_H
25 #define private public
26 #define protected public
27 #include "strongauthmanager.h"
28 #undef private
29 #undef protected
30 #endif // IS_SO_CROP_H
31 #include "commeventsubscriber.h"
32 #include "screenlock_strongauth_test.h"
33 #include "user_idm_client_defines.h"
34 #include "os_account_manager.h"
35
36 namespace OHOS {
37 namespace ScreenLock {
38 const std::string AUTH_PIN = "1";
39 const std::string NO_AUTH_PIN = "0";
40 const std::string TAG_AUTHTYPE = "authType";
41 const std::string HAS_CREDENTIAL = "1";
42 const std::string HAS_NO_CREDENTIAL = "0";
43 const std::string USER_CREDENTIAL_UPDATED_EVENT = "USER_CREDENTIAL_UPDATED_EVENT";
44 using namespace OHOS::UserIam::UserAuth;
45 using namespace OHOS::AccountSA;
46 using namespace testing::ext;
47
SetUpTestCase()48 void ScreenLockStrongAuthTest::SetUpTestCase()
49 {
50 }
51
TearDownTestCase()52 void ScreenLockStrongAuthTest::TearDownTestCase()
53 {
54 }
55
SetUp()56 void ScreenLockStrongAuthTest::SetUp()
57 {
58 }
59
TearDown()60 void ScreenLockStrongAuthTest::TearDown()
61 {
62 }
63
64 /**
65 * @tc.name: ScreenLockStrongAuthTest001
66 * @tc.desc: ScreenLockStrongAuthTest RmvAll.
67 * @tc.type: FUNC
68 * @tc.require:
69 * @tc.author:
70 */
71 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest001, TestSize.Level0)
72 {
73 SCLOCK_HILOGD("ScreenLockStrongAuthTest");
74 #ifndef IS_SO_CROP_H
75 auto authmanager = DelayedSingleton<StrongAuthManger>::GetInstance();
76 ASSERT_NE(authmanager, nullptr);
77
78 int32_t userId = 100;
79 int32_t defaulVal = 1;
80 int64_t timerInterval = 1;
81 int32_t reasonFlag = 0;
82 authmanager->RegistIamEventListener();
83 authmanager->StartStrongAuthTimer(userId);
84 authmanager->InitStrongAuthStat(userId, reasonFlag);
85 authmanager->GetCredInfo(userId);
86 authmanager->GetTimerId(userId);
87 authmanager->ResetStrongAuthTimer(userId, timerInterval);
88 authmanager->DestroyStrongAuthTimer(userId);
89 authmanager->DestroyAllStrongAuthTimer();
90 authmanager->UnRegistIamEventListener();
91 authmanager->SetStrongAuthStat(userId, defaulVal);
92 int32_t val = authmanager->GetStrongAuthStat(userId);
93
94 Singleton<CommeventMgr>::GetInstance().SubscribeEvent();
95 Singleton<CommeventMgr>::GetInstance().UnSubscribeEvent();
96 EXPECT_EQ(defaulVal, val);
97 #endif // IS_SO_CROP_H
98 }
99
100 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest002, TestSize.Level0)
101 {
102 #ifndef IS_SO_CROP_H
103 StrongAuthManger::authTimer timer(true, 1000, true, true);
104 int type = 1;
105 timer.SetType(type);
106 timer.SetUserId(1);
107 timer.GetUserId();
108 EXPECT_EQ(timer.repeat, true);
109 EXPECT_EQ(timer.interval, 1000);
110 #endif // IS_SO_CROP_H
111 }
112
113 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest003, TestSize.Level0)
114 {
115 #ifndef IS_SO_CROP_H
116 StrongAuthManger::authTimer timer;
117 timer.OnTrigger();
118 StrongAuthManger::authTimer timer1(true, 1000, true, true);
119 EXPECT_EQ(timer1.repeat, true);
120 EXPECT_EQ(timer1.interval, 1000);
121 #endif // IS_SO_CROP_H
122 }
123
StrongAuthTimerCallbackTest(int32_t userId)124 static void StrongAuthTimerCallbackTest(int32_t userId)
125 {
126 SCLOCK_HILOGI("%{public}s, enter", __FUNCTION__);
127 return;
128 }
129
130 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest004, TestSize.Level0)
131 {
132 #ifndef IS_SO_CROP_H
133 StrongAuthManger::authTimer timer;
134 timer.SetCallbackInfo(StrongAuthTimerCallbackTest);
135 timer.OnTrigger();
136 StrongAuthManger::authTimer timer1(true, 1000, true, true);
137 EXPECT_EQ(timer1.repeat, true);
138 EXPECT_EQ(timer1.interval, 1000);
139 #endif // IS_SO_CROP_H
140 }
141
142 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest005, TestSize.Level0)
143 {
144 #ifndef IS_SO_CROP_H
145 AAFwk::Want want;
146 want.SetAction(USER_CREDENTIAL_UPDATED_EVENT);
147 want.SetParam("userId", 0);
148 want.SetParam("authType", AUTH_PIN);
149 want.SetParam("credentialCount", HAS_CREDENTIAL);
150 Singleton<CommeventMgr>::GetInstance().OnReceiveEvent(want);
151 StrongAuthManger::authTimer timer(true, 1000, true, true);
152 EXPECT_EQ(timer.repeat, true);
153 EXPECT_EQ(timer.interval, 1000);
154 #endif // IS_SO_CROP_H
155 }
156
157 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest006, TestSize.Level0)
158 {
159 #ifndef IS_SO_CROP_H
160 AAFwk::Want want;
161 want.SetAction(USER_CREDENTIAL_UPDATED_EVENT);
162 want.SetParam("userId", 0);
163 want.SetParam("authType", AUTH_PIN);
164 want.SetParam("credentialCount", HAS_CREDENTIAL);
165 Singleton<CommeventMgr>::GetInstance().OnReceiveEvent(want);
166 StrongAuthManger::authTimer timer(true, 1000, true, true);
167 EXPECT_EQ(timer.repeat, true);
168 EXPECT_EQ(timer.interval, 1000);
169 #endif // IS_SO_CROP_H
170 }
171
172 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest007, TestSize.Level0)
173 {
174 #ifndef IS_SO_CROP_H
175 AAFwk::Want want;
176 want.SetAction(USER_CREDENTIAL_UPDATED_EVENT);
177 want.SetParam("userId", 0);
178 want.SetParam("authType", NO_AUTH_PIN);
179 want.SetParam("credentialCount", HAS_CREDENTIAL);
180 Singleton<CommeventMgr>::GetInstance().OnReceiveEvent(want);
181 StrongAuthManger::authTimer timer(true, 1000, true, true);
182 EXPECT_EQ(timer.repeat, true);
183 EXPECT_EQ(timer.interval, 1000);
184 #endif // IS_SO_CROP_H
185 }
186
187 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest008, TestSize.Level0)
188 {
189 #ifndef IS_SO_CROP_H
190 AAFwk::Want want;
191 const std::string action = "test";
192 want.SetAction(action);
193 want.SetParam("userId", 0);
194 want.SetParam("authType", NO_AUTH_PIN);
195 want.SetParam("credentialCount", HAS_CREDENTIAL);
196 Singleton<CommeventMgr>::GetInstance().OnReceiveEvent(want);
197 StrongAuthManger::authTimer timer(true, 1000, true, true);
198 EXPECT_EQ(timer.repeat, true);
199 #endif // IS_SO_CROP_H
200 }
201
202 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest009, TestSize.Level0)
203 {
204 #ifndef IS_SO_CROP_H
205 AAFwk::Want want;
206 want.SetAction(USER_CREDENTIAL_UPDATED_EVENT);
207 want.SetParam("userId", 0);
208 want.SetParam("authType", AUTH_PIN);
209 want.SetParam("credentialCount", HAS_NO_CREDENTIAL);
210 Singleton<CommeventMgr>::GetInstance().OnReceiveEvent(want);
211 StrongAuthManger::authTimer timer(true, 1000, true, true);
212 EXPECT_EQ(timer.repeat, true);
213 #endif // IS_SO_CROP_H
214 }
215
216 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest10, TestSize.Level0)
217 {
218 #ifndef IS_SO_CROP_H
219 std::shared_ptr<StrongAuthManger::AuthEventListenerService> authSuccessListener =
220 std::make_shared<StrongAuthManger::AuthEventListenerService>();
221 std::string bundleName = "test";
222 authSuccessListener->OnNotifyAuthSuccessEvent(1, AuthType::FACE, 1, bundleName);
223 StrongAuthManger::authTimer timer(true, 1000, true, true);
224 EXPECT_EQ(timer.repeat, true);
225 #endif // IS_SO_CROP_H
226 }
227
228 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest11, TestSize.Level0)
229 {
230 #ifndef IS_SO_CROP_H
231 std::shared_ptr<StrongAuthManger::AuthEventListenerService> authSuccessListener =
232 std::make_shared<StrongAuthManger::AuthEventListenerService>();;
233 std::string bundleName = "test";
234 authSuccessListener->OnNotifyAuthSuccessEvent(1, AuthType::PIN, 1, bundleName);
235 StrongAuthManger::authTimer timer(true, 1000, true, true);
236 EXPECT_EQ(timer.repeat, true);
237 #endif // IS_SO_CROP_H
238 }
239
240 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest12, TestSize.Level0)
241 {
242 #ifndef IS_SO_CROP_H
243 std::shared_ptr<StrongAuthManger::CredChangeListenerService> creChangeListener =
244 std::make_shared<StrongAuthManger::CredChangeListenerService>();
245 CredChangeEventType eventType = CredChangeEventType::ADD_CRED;
246 UserIam::UserAuth::CredChangeEventInfo changeInfo = {};
247 creChangeListener->OnNotifyCredChangeEvent(1, AuthType::PIN, eventType, changeInfo);
248 StrongAuthManger::authTimer timer(true, 1000, true, true);
249 EXPECT_EQ(timer.repeat, true);
250 #endif // IS_SO_CROP_H
251 }
252
253 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest13, TestSize.Level0)
254 {
255 #ifndef IS_SO_CROP_H
256 std::shared_ptr<StrongAuthManger::CredChangeListenerService> creChangeListener =
257 std::make_shared<StrongAuthManger::CredChangeListenerService>();
258 CredChangeEventType eventType = CredChangeEventType::ADD_CRED;
259 UserIam::UserAuth::CredChangeEventInfo changeInfo = {};
260 creChangeListener->OnNotifyCredChangeEvent(1, AuthType::FACE, eventType, changeInfo);
261 StrongAuthManger::authTimer timer(true, 1000, true, true);
262 EXPECT_EQ(timer.repeat, true);
263 #endif
264 }
265
266 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest14, TestSize.Level0)
267 {
268 #ifndef IS_SO_CROP_H
269 std::shared_ptr<StrongAuthManger::CredChangeListenerService> creChangeListener =
270 std::make_shared<StrongAuthManger::CredChangeListenerService>();
271 CredChangeEventType eventType = CredChangeEventType::UPDATE_CRED;
272 UserIam::UserAuth::CredChangeEventInfo changeInfo = {};
273 creChangeListener->OnNotifyCredChangeEvent(1, AuthType::FACE, eventType, changeInfo);
274 StrongAuthManger::authTimer timer(true, 1000, true, true);
275 EXPECT_EQ(timer.repeat, true);
276 #endif
277 }
278
279 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest015, TestSize.Level0)
280 {
281 #ifndef IS_SO_CROP_H
282 auto authmanager = DelayedSingleton<StrongAuthManger>::GetInstance();
283 ASSERT_NE(authmanager, nullptr);
284
285 int32_t otherUserId = 102;
286 int32_t reasonFlag = 0;
287 authmanager->GetStrongAuthTimeTrigger(otherUserId);
288
289 authmanager->IsUserHasStrongAuthTimer(otherUserId);
290
291 authmanager->ResetStrongAuthTimer(otherUserId, DEFAULT_STRONG_AUTH_TIMEOUT_MS);
292
293 auto flag = authmanager->IsUserHasStrongAuthTimer(otherUserId);
294
295 authmanager->GetStrongAuthTimeTrigger(otherUserId);
296
297 authmanager->GetCredInfo(otherUserId);
298
299 authmanager->InitStrongAuthStat(otherUserId, reasonFlag);
300
301 authmanager->GetCredInfo(otherUserId);
302
303 authmanager->DestroyStrongAuthTimer(otherUserId);
304
305 EXPECT_EQ(flag, true);
306 #endif // IS_SO_CROP_H
307 }
308
309 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest016, TestSize.Level0)
310 {
311 #ifndef IS_SO_CROP_H
312 auto authmanager = DelayedSingleton<StrongAuthManger>::GetInstance();
313 ASSERT_NE(authmanager, nullptr);
314
315 int32_t otherUserId = 102;
316 int32_t reasonFlag = 0;
317 authmanager->DestroyStrongAuthStateInfo(otherUserId);
318
319 authmanager->IsUserExitInStrongAuthInfo(otherUserId);
320
321 authmanager->InitStrongAuthStat(otherUserId, reasonFlag);
322
323 auto flag = authmanager->IsUserExitInStrongAuthInfo(otherUserId);
324
325 authmanager->InitStrongAuthStat(otherUserId, reasonFlag);
326
327 authmanager->DestroyStrongAuthStateInfo(otherUserId);
328
329 EXPECT_EQ(flag, true);
330 #endif // IS_SO_CROP_H
331 }
332
333 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest017, TestSize.Level0)
334 {
335 #ifndef IS_SO_CROP_H
336 auto authmanager = DelayedSingleton<StrongAuthManger>::GetInstance();
337 ASSERT_NE(authmanager, nullptr);
338
339 int32_t otherUserId = 102;
340 int32_t reasonFlag = 0;
341
342 authmanager->GetStrongAuthTriggerPeriod(otherUserId);
343
344 authmanager->ResetStrongAuthTimer(otherUserId, DEFAULT_STRONG_AUTH_TIMEOUT_MS);
345
346 authmanager->GetStrongAuthTriggerPeriod(otherUserId);
347
348 authmanager->ResetStrongAuthTimer(otherUserId, CRED_CHANGE_FIRST_STRONG_AUTH_TIMEOUT_MS);
349
350 authmanager->GetStrongAuthTriggerPeriod(otherUserId);
351
352 authmanager->ResetStrongAuthTimer(otherUserId, CRED_CHANGE_SECOND_STRONG_AUTH_TIMEOUT_MS);
353
354 authmanager->GetStrongAuthTriggerPeriod(otherUserId);
355
356 authmanager->GetStrongAuthTimeTrigger(otherUserId);
357
358 auto flag = authmanager->IsUserHasStrongAuthTimer(otherUserId);
359
360 authmanager->DestroyStrongAuthTimer(otherUserId);
361
362 EXPECT_EQ(flag, true);
363 #endif // IS_SO_CROP_H
364 }
365
366 HWTEST_F(ScreenLockStrongAuthTest, ScreenLockStrongAuthTest018, TestSize.Level0)
367 {
368 #ifndef IS_SO_CROP_H
369 auto authmanager = DelayedSingleton<StrongAuthManger>::GetInstance();
370 ASSERT_NE(authmanager, nullptr);
371
372 int32_t otherUserId = 102;
373 int32_t reasonFlag = 1;
374
375 authmanager->SetStrongAuthStat(otherUserId, reasonFlag);
376
377 authmanager->SetStrongAuthStat(otherUserId, reasonFlag);
378
379 authmanager->SetStrongAuthStat(otherUserId, 2);
380
381 authmanager->SetStrongAuthStat(otherUserId, 0);
382
383 authmanager->SetStrongAuthStat(otherUserId, 2);
384
385 authmanager->SetStrongAuthStat(otherUserId, 0);
386
387 authmanager->SetStrongAuthStat(otherUserId, 0);
388
389 auto flag = authmanager->IsUserExitInStrongAuthInfo(otherUserId);
390
391 authmanager->DestroyStrongAuthStateInfo(otherUserId);
392
393 EXPECT_EQ(flag, true);
394 #endif // IS_SO_CROP_H
395 }
396 } // namespace ScreenLock
397 } // namespace OHOS
398