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 <gtest/gtest.h>
17 
18 #include "idm_database.h"
19 
20 typedef bool (*DuplicateCheckFunc)(LinkedList *collection, uint64_t value);
21 
22 extern "C" {
23     extern LinkedList *g_userInfoList;
24     extern UserInfo *g_currentUser;
25     extern GlobalConfigParamHal g_globalConfigArray[MAX_GLOBAL_CONFIG_NUM];
26     extern bool MatchUserInfo(const void *data, const void *condition);
27     extern bool IsUserInfoValid(UserInfo *userInfo);
28     extern UserInfo *QueryUserInfo(int32_t userId);
29     extern bool IsSecureUidDuplicate(LinkedList *userInfoList, uint64_t secureUid);
30     extern UserInfo *CreateUser(int32_t userId, int32_t userType);
31     extern ResultCode DeleteUser(int32_t userId);
32     extern bool IsCredentialIdDuplicate(LinkedList *userInfoList, uint64_t credentialId);
33     extern bool IsEnrolledIdDuplicate(LinkedList *enrolledList, uint64_t enrolledId);
34     extern ResultCode GenerateDeduplicateUint64(LinkedList *collection, uint64_t *destValue, DuplicateCheckFunc func);
35     extern ResultCode UpdateEnrolledId(LinkedList *enrolledList, uint32_t authType);
36     extern ResultCode AddCredentialToUser(UserInfo *user, CredentialInfoHal *credentialInfo);
37     extern ResultCode AddUser(int32_t userId, CredentialInfoHal *credentialInfo, int32_t userType);
38     extern bool MatchCredentialById(const void *data, const void *condition);
39     extern bool MatchEnrolledInfoByType(const void *data, const void *condition);
40     extern CredentialInfoHal *QueryCredentialById(uint64_t credentialId, LinkedList *credentialList);
41     extern CredentialInfoHal *QueryCredentialByAuthType(uint32_t authType, LinkedList *credentialList);
42     extern bool IsCredMatch(const CredentialCondition *limit, const CredentialInfoHal *credentialInfo);
43     extern bool IsUserMatch(const CredentialCondition *limit, const UserInfo *user);
44     extern ResultCode TraverseCredentialList(const CredentialCondition *limit, const LinkedList *credentialList,
45         LinkedList *credListGet);
46     extern void RemoveCachePin(UserInfo *user, bool *isRemoved);
47 }
48 
49 namespace OHOS {
50 namespace UserIam {
51 namespace UserAuth {
52 using namespace testing;
53 using namespace testing::ext;
54 
55 class IdmDatabaseTest : public testing::Test {
56 public:
SetUpTestCase()57     static void SetUpTestCase() {};
58 
TearDownTestCase()59     static void TearDownTestCase() {};
60 
SetUp()61     void SetUp() {};
62 
TearDown()63     void TearDown() {};
64 };
65 
66 HWTEST_F(IdmDatabaseTest, TestInitUserInfoList_001, TestSize.Level0)
67 {
68     EXPECT_EQ(InitUserInfoList(), RESULT_SUCCESS);
69     DestroyUserInfoList();
70 }
71 
72 HWTEST_F(IdmDatabaseTest, TestInitUserInfoList_002, TestSize.Level0)
73 {
74     constexpr int32_t userType = 1024;
75     UserInfo *userInfo = InitUserInfoNode();
76     EXPECT_EQ(InitUserInfoList(), RESULT_SUCCESS);
77     EXPECT_NE(userInfo->userType, userType);
78     DestroyUserInfoNode(userInfo);
79     DestroyUserInfoList();
80 }
81 
82 HWTEST_F(IdmDatabaseTest, TestMatchUserInfo, TestSize.Level0)
83 {
84     EXPECT_FALSE(MatchUserInfo(nullptr, nullptr));
85     int32_t condition = 4526;
86     constexpr int32_t userId = 1133;
87     UserInfo info = {};
88     info.userId = userId;
89     EXPECT_FALSE(MatchUserInfo(static_cast<void *>(&info), static_cast<void *>(&condition)));
90 }
91 
92 HWTEST_F(IdmDatabaseTest, TestIsUserInfoValid, TestSize.Level0)
93 {
94     UserInfo info = {};
95     info.credentialInfoList = nullptr;
96     info.enrolledInfoList = nullptr;
97     EXPECT_FALSE(IsUserInfoValid(&info));
98     info.credentialInfoList = new LinkedList();
99     EXPECT_FALSE(IsUserInfoValid(&info));
100     delete info.credentialInfoList;
101 }
102 
103 HWTEST_F(IdmDatabaseTest, TestGetSecureUid, TestSize.Level0)
104 {
105     constexpr int32_t userId = 1133;
106     EXPECT_EQ(GetSecureUid(userId, nullptr), RESULT_BAD_PARAM);
107 }
108 
109 HWTEST_F(IdmDatabaseTest, TestGetEnrolledInfoAuthType_001, TestSize.Level0)
110 {
111     g_userInfoList = nullptr;
112     constexpr int32_t userId = 1166;
113     constexpr uint32_t authType = 1;
114     EXPECT_EQ(GetEnrolledInfoAuthType(userId, authType, nullptr), RESULT_BAD_PARAM);
115     EnrolledInfoHal info = {};
116     EXPECT_EQ(GetEnrolledInfoAuthType(userId, authType, &info), RESULT_NOT_FOUND);
117 }
118 
119 HWTEST_F(IdmDatabaseTest, TestGetEnrolledInfoAuthType_002, TestSize.Level0)
120 {
121     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
122     EXPECT_NE(g_userInfoList, nullptr);
123     UserInfo userInfo = {};
124     constexpr int32_t userId = 1135;
125     constexpr uint32_t authType = 1;
126     userInfo.userId = userId;
127     userInfo.enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
128     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
129     EnrolledInfoHal info = {};
130     EXPECT_EQ(GetEnrolledInfoAuthType(userId, authType, &info), RESULT_NOT_FOUND);
131 }
132 
133 HWTEST_F(IdmDatabaseTest, TestGetEnrolledInfoAuthType_003, TestSize.Level0)
134 {
135     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
136     EXPECT_NE(g_userInfoList, nullptr);
137     UserInfo userInfo = {};
138     constexpr int32_t userId = 1135;
139     constexpr uint32_t authType1 = 1;
140     constexpr uint32_t authType2 = 2;
141     userInfo.userId = userId;
142     userInfo.enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
143     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
144     EnrolledInfoHal enrolledInfo = {};
145     enrolledInfo.authType = authType2;
146     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
147     g_userInfoList->insert(g_userInfoList, nullptr);
148     EnrolledInfoHal info = {};
149     EXPECT_EQ(GetEnrolledInfoAuthType(userId, authType1, &info), RESULT_NOT_FOUND);
150 }
151 
152 HWTEST_F(IdmDatabaseTest, TestGetEnrolledInfo, TestSize.Level0)
153 {
154     constexpr int32_t userId = 1211;
155     EXPECT_EQ(GetEnrolledInfo(userId, nullptr, nullptr), RESULT_BAD_PARAM);
156     g_userInfoList = nullptr;
157     g_currentUser = nullptr;
158     EnrolledInfoHal *enrolledInfos = nullptr;
159     uint32_t num = 0;
160     EXPECT_EQ(GetEnrolledInfo(userId, &enrolledInfos, &num), RESULT_NOT_FOUND);
161 }
162 
163 HWTEST_F(IdmDatabaseTest, TestDeleteUserInfo, TestSize.Level0)
164 {
165     constexpr int32_t userId = 1155;
166     EXPECT_EQ(DeleteUserInfo(userId, nullptr), RESULT_BAD_PARAM);
167 }
168 
169 HWTEST_F(IdmDatabaseTest, TestQueryUserInfo_001, TestSize.Level0)
170 {
171     g_userInfoList = nullptr;
172     constexpr int32_t userId1 = 123;
173     constexpr int32_t userId2 = 1123;
174     UserInfo userInfo = {};
175     userInfo.userId = userId1;
176     g_currentUser = &userInfo;
177     EXPECT_NE(QueryUserInfo(userId1), nullptr);
178     userInfo.userId = userId2;
179     EXPECT_EQ(QueryUserInfo(userId1), nullptr);
180 }
181 
182 HWTEST_F(IdmDatabaseTest, TestQueryUserInfo_002, TestSize.Level0)
183 {
184     g_currentUser = nullptr;
185     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
186     EXPECT_NE(g_userInfoList, nullptr);
187     constexpr int32_t userId1 = 123;
188     constexpr int32_t userId2 = 1336;
189     UserInfo userInfo1 = {};
190     userInfo1.userId = userId1;
191     userInfo1.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
192     userInfo1.enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
193     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo1));
194     UserInfo userInfo2 = {};
195     userInfo2.userId = userId2;
196     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo2));
197     g_userInfoList->insert(g_userInfoList, nullptr);
198     EXPECT_NE(QueryUserInfo(userId1), nullptr);
199 }
200 
201 HWTEST_F(IdmDatabaseTest, TestIsSecureUidDuplicate, TestSize.Level0)
202 {
203     constexpr uint64_t secUid = 1221;
204     constexpr uint64_t secUid1 = 111;
205     constexpr uint64_t secUid2 = 222;
206     EXPECT_FALSE(IsSecureUidDuplicate(nullptr, secUid));
207     LinkedList *userInfoList = CreateLinkedList(DestroyUserInfoNode);
208     EXPECT_NE(userInfoList, nullptr);
209     EXPECT_FALSE(IsSecureUidDuplicate(userInfoList, secUid));
210     UserInfo info1 = {};
211     info1.secUid = secUid1;
212     userInfoList->insert(userInfoList, static_cast<void *>(&info1));
213     UserInfo info2 = info1;
214     info2.secUid = secUid2;
215     userInfoList->insert(userInfoList, static_cast<void *>(&info2));
216     userInfoList->insert(userInfoList, nullptr);
217     EXPECT_TRUE(IsSecureUidDuplicate(userInfoList, secUid1));
218 }
219 
220 HWTEST_F(IdmDatabaseTest, TestCreateUser, TestSize.Level0)
221 {
222     g_userInfoList = nullptr;
223     constexpr int32_t userId = 123;
224     EXPECT_EQ(CreateUser(userId, 0), nullptr);
225 }
226 
227 HWTEST_F(IdmDatabaseTest, TestDeleteUser, TestSize.Level0)
228 {
229     g_userInfoList = nullptr;
230     constexpr int32_t userId = 123;
231     EXPECT_EQ(DeleteUser(userId), RESULT_BAD_PARAM);
232 }
233 
234 HWTEST_F(IdmDatabaseTest, TestIsCredentialIdDuplicate, TestSize.Level0)
235 {
236     g_userInfoList = nullptr;
237     constexpr uint64_t credentialId1 = 1221;
238     constexpr uint64_t credentialId2 = 10;
239     EXPECT_TRUE(IsCredentialIdDuplicate(nullptr, credentialId1));
240     g_userInfoList =  CreateLinkedList(DestroyUserInfoNode);
241     EXPECT_NE(g_userInfoList, nullptr);
242     UserInfo info = {};
243     info.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
244     EXPECT_NE(info.credentialInfoList, nullptr);
245     CredentialInfoHal credInfo = {};
246     credInfo.credentialId = credentialId2;
247     info.credentialInfoList->insert(info.credentialInfoList, static_cast<void *>(&credInfo));
248     g_userInfoList->insert(g_userInfoList, &info);
249     EXPECT_FALSE(IsCredentialIdDuplicate(nullptr, credentialId2));
250 }
251 
252 HWTEST_F(IdmDatabaseTest, TestIsEnrolledIdDuplicate, TestSize.Level0)
253 {
254     constexpr uint64_t enrolledId1 = 111;
255     constexpr uint64_t enrolledId2 = 222;
256     LinkedList *enrolledList = CreateLinkedList(DestroyEnrolledNode);
257     EXPECT_NE(enrolledList, nullptr);
258     EnrolledInfoHal info1 = {};
259     info1.enrolledId = enrolledId1;
260     enrolledList->insert(enrolledList, static_cast<void *>(&info1));
261     EnrolledInfoHal info2 = {};
262     info2.enrolledId = enrolledId2;
263     enrolledList->insert(enrolledList, static_cast<void *>(&info2));
264     enrolledList->insert(enrolledList, nullptr);
265     EXPECT_TRUE(IsEnrolledIdDuplicate(enrolledList, enrolledId1));
266 }
267 
268 HWTEST_F(IdmDatabaseTest, TestGenerateDeduplicateUint64, TestSize.Level0)
269 {
270     EXPECT_EQ(GenerateDeduplicateUint64(nullptr, nullptr, IsEnrolledIdDuplicate), RESULT_BAD_PARAM);
271 }
272 
273 HWTEST_F(IdmDatabaseTest, TestUpdateEnrolledId, TestSize.Level0)
274 {
275     constexpr uint32_t authType1 = 1;
276     constexpr uint32_t authType2 = 2;
277     LinkedList *enrolledList = CreateLinkedList(DestroyEnrolledNode);
278     EXPECT_NE(enrolledList, nullptr);
279     EnrolledInfoHal info1 = {};
280     info1.authType = authType1;
281     enrolledList->insert(enrolledList, static_cast<void *>(&info1));
282     EnrolledInfoHal info2 = {};
283     info2.authType = authType2;
284     enrolledList->insert(enrolledList, static_cast<void *>(&info2));
285     enrolledList->insert(enrolledList, nullptr);
286     EXPECT_EQ(UpdateEnrolledId(enrolledList, authType1), RESULT_SUCCESS);
287 }
288 
289 HWTEST_F(IdmDatabaseTest, TestAddCredentialToUser, TestSize.Level0)
290 {
291     g_userInfoList = nullptr;
292     EXPECT_EQ(AddCredentialToUser(nullptr, nullptr), RESULT_NEED_INIT);
293     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
294     UserInfo userInfo = {};
295     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
296     EXPECT_NE(userInfo.credentialInfoList, nullptr);
297     userInfo.enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
298     CredentialInfoHal credInfo = {};
299     constexpr uint32_t credNum = 102;
300     for (uint32_t i = 0; i < credNum; ++i) {
301         userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(&credInfo));
302     }
303     EXPECT_EQ(AddCredentialToUser(&userInfo, nullptr), RESULT_EXCEED_LIMIT);
304 }
305 
306 HWTEST_F(IdmDatabaseTest, TestAddUser, TestSize.Level0)
307 {
308     g_currentUser = nullptr;
309     g_userInfoList = nullptr;
310     EXPECT_EQ(AddUser(111, nullptr, 0), RESULT_NEED_INIT);
311     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
312     EXPECT_NE(g_userInfoList, nullptr);
313     constexpr uint32_t userNum = 1002;
314     UserInfo info = {};
315     for (uint32_t i = 0; i < userNum; ++i) {
316         g_userInfoList->insert(g_userInfoList, static_cast<void *>(&info));
317     }
318     EXPECT_EQ(AddUser(111, nullptr, 0), RESULT_EXCEED_LIMIT);
319 }
320 
321 HWTEST_F(IdmDatabaseTest, TestAddCredentialInfo_001, TestSize.Level0)
322 {
323     EXPECT_EQ(AddCredentialInfo(111, nullptr, 0), RESULT_BAD_PARAM);
324 }
325 
326 HWTEST_F(IdmDatabaseTest, TestAddCredentialInfo_002, TestSize.Level0)
327 {
328     g_currentUser = nullptr;
329     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
330     EXPECT_NE(g_userInfoList, nullptr);
331     constexpr int32_t userId = 100;
332     constexpr int32_t userType = 2;
333     constexpr uint32_t authType = 1;
334     UserInfo *user = QueryUserInfo(userId);
335     EXPECT_EQ(user, nullptr);
336     user = CreateUser(userId, userType);
337     EXPECT_NE(user->userType, 0);
338 
339     CredentialInfoHal credInfo = {};
340     credInfo.authType = authType;
341     EXPECT_EQ(AddUser(userId, &credInfo, userType), RESULT_SUCCESS);
342 
343     EXPECT_EQ(AddCredentialInfo(userId, &credInfo, userType), RESULT_SUCCESS);
344 }
345 
346 HWTEST_F(IdmDatabaseTest, TestMatchCredentialById, TestSize.Level0)
347 {
348     EXPECT_FALSE(MatchCredentialById(nullptr, nullptr));
349     constexpr uint64_t credentialId = 10;
350     CredentialInfoHal info = {};
351     info.credentialId = credentialId;
352     uint64_t condition = credentialId;
353     EXPECT_TRUE(MatchCredentialById(static_cast<void *>(&info), static_cast<void *>(&condition)));
354     condition = 20;
355     EXPECT_FALSE(MatchCredentialById(static_cast<void *>(&info), static_cast<void *>(&condition)));
356 }
357 
358 HWTEST_F(IdmDatabaseTest, TestMatchEnrolledInfoByType, TestSize.Level0)
359 {
360     EXPECT_FALSE(MatchEnrolledInfoByType(nullptr, nullptr));
361     constexpr uint32_t authType = 1;
362     EnrolledInfoHal info = {};
363     info.authType = authType;
364     uint32_t condition = 1;
365     EXPECT_TRUE(MatchEnrolledInfoByType(static_cast<void *>(&info), static_cast<void *>(&condition)));
366     condition = 2;
367     EXPECT_FALSE(MatchEnrolledInfoByType(static_cast<void *>(&info), static_cast<void *>(&condition)));
368 }
369 
370 HWTEST_F(IdmDatabaseTest, TestDeleteCredentialInfo_001, TestSize.Level0)
371 {
372     EXPECT_EQ(DeleteCredentialInfo(1, 1, nullptr), RESULT_BAD_PARAM);
373     CredentialInfoHal credInfo = {};
374     EXPECT_EQ(DeleteCredentialInfo(1, 1, &credInfo), RESULT_BAD_PARAM);
375 }
376 
377 HWTEST_F(IdmDatabaseTest, TestDeleteCredentialInfo_002, TestSize.Level0)
378 {
379     g_currentUser = nullptr;
380     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
381     constexpr int32_t userId = 112;
382     constexpr uint64_t credentialId = 1;
383     EXPECT_NE(g_userInfoList, nullptr);
384     UserInfo userInfo = {};
385     userInfo.userId = userId;
386     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
387     userInfo.enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
388     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
389     CredentialInfoHal credInfo = {};
390     EXPECT_EQ(DeleteCredentialInfo(userId, credentialId, &credInfo), RESULT_UNKNOWN);
391 }
392 
393 HWTEST_F(IdmDatabaseTest, TestDeleteCredentialInfo_003, TestSize.Level0)
394 {
395     g_currentUser = nullptr;
396     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
397     EXPECT_NE(g_userInfoList, nullptr);
398     constexpr int32_t userId = 113;
399     constexpr uint64_t credentialId = 10;
400     UserInfo userInfo = {};
401     userInfo.userId = userId;
402     userInfo.enrolledInfoList = nullptr;
403     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
404     userInfo.enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
405     EXPECT_NE(userInfo.credentialInfoList, nullptr);
406     auto *credInfo = static_cast<CredentialInfoHal *>(Malloc(sizeof(CredentialInfoHal)));
407     credInfo->credentialId = credentialId;
408     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(credInfo));
409     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
410     CredentialInfoHal info = {};
411     EXPECT_EQ(DeleteCredentialInfo(userId, credentialId, &info), RESULT_SUCCESS);
412 }
413 
414 HWTEST_F(IdmDatabaseTest, TestDeleteCredentialInfo_004, TestSize.Level0)
415 {
416     g_currentUser = nullptr;
417     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
418     EXPECT_NE(g_userInfoList, nullptr);
419     constexpr int32_t userId = 115;
420     constexpr uint32_t authType = 2;
421     constexpr uint64_t credentialId1 = 10;
422     constexpr uint64_t credentialId2 = 20;
423     UserInfo userInfo = {};
424     userInfo.userId = userId;
425     userInfo.enrolledInfoList = nullptr;
426     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
427     userInfo.enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
428     EXPECT_NE(userInfo.credentialInfoList, nullptr);
429     auto *credInfo1 = static_cast<CredentialInfoHal *>(Malloc(sizeof(CredentialInfoHal)));
430     credInfo1->credentialId = credentialId1;
431     credInfo1->authType = authType;
432     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(credInfo1));
433     auto *credInfo2 = static_cast<CredentialInfoHal *>(Malloc(sizeof(CredentialInfoHal)));
434     credInfo2->credentialId = credentialId2;
435     credInfo2->authType = authType;
436     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(credInfo2));
437     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
438     CredentialInfoHal info = {};
439     EXPECT_EQ(DeleteCredentialInfo(userId, credentialId1, &info), RESULT_SUCCESS);
440     Free(credInfo2);
441 }
442 
443 HWTEST_F(IdmDatabaseTest, TestClearCachePin, TestSize.Level0)
444 {
445     constexpr int32_t userId = 115;
446     ClearCachePin(userId);
447     g_currentUser = nullptr;
448     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
449     EXPECT_NE(g_userInfoList, nullptr);
450     constexpr uint64_t credentialId1 = 10;
451     UserInfo userInfo = {};
452     userInfo.userId = userId;
453     userInfo.enrolledInfoList = nullptr;
454     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
455     EXPECT_NE(userInfo.credentialInfoList, nullptr);
456     auto *credInfo1 = static_cast<CredentialInfoHal *>(Malloc(sizeof(CredentialInfoHal)));
457     credInfo1->credentialId = credentialId1;
458     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(credInfo1));
459     ClearCachePin(userId);
460 }
461 
462 HWTEST_F(IdmDatabaseTest, TestQueryCredentialById, TestSize.Level0)
463 {
464     constexpr uint64_t credentialId = 111;
465     constexpr uint64_t credentialId1 = 10;
466     constexpr uint64_t credentialId2 = 20;
467     EXPECT_EQ(QueryCredentialById(credentialId, nullptr), nullptr);
468     LinkedList *credentialList = CreateLinkedList(DestroyCredentialNode);
469     EXPECT_NE(credentialList, nullptr);
470     CredentialInfoHal credInfo1 = {};
471     credInfo1.credentialId = credentialId1;
472     credentialList->insert(credentialList, static_cast<void *>(&credInfo1));
473     CredentialInfoHal credInfo2 = {};
474     credInfo2.credentialId = credentialId2;
475     credentialList->insert(credentialList, static_cast<void *>(&credInfo2));
476     credentialList->insert(credentialList, nullptr);
477     EXPECT_NE(QueryCredentialById(credentialId1, credentialList), nullptr);
478 }
479 
480 HWTEST_F(IdmDatabaseTest, TestQueryCredentialByAuthType, TestSize.Level0)
481 {
482     constexpr uint32_t authType1 = 1;
483     constexpr uint32_t authType2 = 2;
484     EXPECT_EQ(QueryCredentialByAuthType(1, nullptr), nullptr);
485     LinkedList *credentialList = CreateLinkedList(DestroyCredentialNode);
486     EXPECT_NE(credentialList, nullptr);
487     CredentialInfoHal credInfo1 = {};
488     credInfo1.authType = authType1;
489     credentialList->insert(credentialList, static_cast<void *>(&credInfo1));
490     CredentialInfoHal credInfo2 = {};
491     credInfo2.authType = authType2;
492     credentialList->insert(credentialList, static_cast<void *>(&credInfo2));
493     credentialList->insert(credentialList, nullptr);
494     EXPECT_NE(QueryCredentialByAuthType(authType1, credentialList), nullptr);
495 }
496 
497 HWTEST_F(IdmDatabaseTest, TestIsCredMatch_001, TestSize.Level0)
498 {
499     constexpr uint64_t templateId1 = 20;
500     constexpr uint64_t templateId2 = 10;
501     CredentialInfoHal credInfo = {};
502     credInfo.templateId = templateId1;
503     CredentialCondition limit = {};
504     SetCredentialConditionTemplateId(&limit, templateId2);
505     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
506 }
507 
508 HWTEST_F(IdmDatabaseTest, TestIsCredMatch_002, TestSize.Level0)
509 {
510     constexpr uint32_t excutorSensorHint1 = 10;
511     constexpr uint32_t excutorSensorHint2 = 20;
512     CredentialInfoHal credInfo = {};
513     credInfo.executorSensorHint = excutorSensorHint2;
514     CredentialCondition limit = {};
515     SetCredentialConditionExecutorSensorHint(&limit, 0);
516     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
517     SetCredentialConditionExecutorSensorHint(&limit, excutorSensorHint1);
518     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
519     SetCredentialConditionExecutorSensorHint(&limit, excutorSensorHint2);
520     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
521 }
522 
523 HWTEST_F(IdmDatabaseTest, TestIsCredMatch_003, TestSize.Level0)
524 {
525     constexpr uint32_t executorMatcher1 = 10;
526     constexpr uint32_t executorMatcher2 = 20;
527     CredentialInfoHal credInfo = {};
528     credInfo.executorMatcher = executorMatcher2;
529     CredentialCondition limit = {};
530     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
531     SetCredentialConditionExecutorMatcher(&limit, executorMatcher1);
532     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
533     SetCredentialConditionExecutorMatcher(&limit, executorMatcher2);
534     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
535     SetCredentiaConditionNeedCachePin(nullptr);
536     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
537     SetCredentiaConditionNeedCachePin(&limit);
538     EXPECT_TRUE(IsCredMatch(&limit, &credInfo));
539 }
540 
541 HWTEST_F(IdmDatabaseTest, TestIsUserMatch, TestSize.Level0)
542 {
543     constexpr int32_t userId1 = 20;
544     constexpr int32_t userId2 = 10;
545     UserInfo userInfo = {};
546     userInfo.userId = userId1;
547     CredentialCondition limit = {};
548     SetCredentialConditionUserId(&limit, userId2);
549     EXPECT_FALSE(IsUserMatch(&limit, &userInfo));
550 }
551 
552 HWTEST_F(IdmDatabaseTest, TestTraverseCredentialList, TestSize.Level0)
553 {
554     EXPECT_EQ(TraverseCredentialList(nullptr, nullptr, nullptr), RESULT_GENERAL_ERROR);
555     LinkedList *credentialList = CreateLinkedList(DestroyCredentialNode);
556     EXPECT_NE(credentialList, nullptr);
557     credentialList->insert(credentialList, nullptr);
558     EXPECT_EQ(TraverseCredentialList(nullptr, credentialList, nullptr), RESULT_UNKNOWN);
559 }
560 
561 HWTEST_F(IdmDatabaseTest, TestQueryCredentialLimit_001, TestSize.Level0)
562 {
563     g_userInfoList = nullptr;
564     EXPECT_EQ(QueryCredentialLimit(nullptr), nullptr);
565     CredentialCondition limit = {};
566     EXPECT_EQ(QueryCredentialLimit(&limit), nullptr);
567     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
568     EXPECT_NE(g_userInfoList, nullptr);
569     g_userInfoList->insert(g_userInfoList, nullptr);
570     EXPECT_EQ(QueryCredentialLimit(&limit), nullptr);
571 }
572 
573 HWTEST_F(IdmDatabaseTest, TestQueryCredentialLimit_002, TestSize.Level0)
574 {
575     constexpr int32_t userId1 = 1001;
576     constexpr int32_t userId2 = 1002;
577     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
578     EXPECT_NE(g_userInfoList, nullptr);
579     UserInfo userInfo1 = {};
580     userInfo1.userId = userId1;
581     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo1));
582     UserInfo userInfo2 = {};
583     userInfo2.userId = userId2;
584     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo2));
585     CredentialCondition limit = {};
586     SetCredentialConditionUserId(&limit, userId1);
587     EXPECT_EQ(QueryCredentialLimit(&limit), nullptr);
588 }
589 
590 HWTEST_F(IdmDatabaseTest, TestQueryCredentialUserId_001, TestSize.Level0)
591 {
592     constexpr int32_t userId1 = 1001;
593     constexpr uint64_t credentialId = 10;
594     g_userInfoList = nullptr;
595     EXPECT_EQ(QueryCredentialUserId(credentialId, nullptr), RESULT_BAD_PARAM);
596     int32_t userId = userId1;
597     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_NEED_INIT);
598     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
599     EXPECT_NE(g_userInfoList, nullptr);
600     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_NOT_FOUND);
601     g_userInfoList->insert(g_userInfoList, nullptr);
602     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_UNKNOWN);
603 }
604 
605 HWTEST_F(IdmDatabaseTest, TestQueryCredentialUserId_002, TestSize.Level0)
606 {
607     constexpr int32_t userId1 = 1002;
608     constexpr int32_t userId2 = 1001;
609     constexpr uint64_t credentialId = 10;
610     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
611     EXPECT_NE(g_userInfoList, nullptr);
612     UserInfo userInfo = {};
613     userInfo.userId = userId1;
614     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
615     int32_t userId = userId2;
616     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_UNKNOWN);
617 }
618 
619 HWTEST_F(IdmDatabaseTest, TestQueryCredentialUserId_003, TestSize.Level0)
620 {
621     constexpr int32_t userId1 = 1002;
622     constexpr int32_t userId2 = 1001;
623     constexpr uint64_t credentialId = 10;
624     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
625     EXPECT_NE(g_userInfoList, nullptr);
626     UserInfo userInfo = {};
627     userInfo.userId = userId1;
628     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
629     EXPECT_NE(userInfo.credentialInfoList, nullptr);
630     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
631     int32_t userId = userId2;
632     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_NOT_FOUND);
633 }
634 
635 HWTEST_F(IdmDatabaseTest, TestSetPinSubType, TestSize.Level0)
636 {
637     g_userInfoList = nullptr;
638     g_currentUser = nullptr;
639     constexpr int32_t userId = 1003;
640     constexpr uint64_t pinSubType = 10000;
641     EXPECT_EQ(SetPinSubType(userId, pinSubType), RESULT_NOT_FOUND);
642 }
643 
644 HWTEST_F(IdmDatabaseTest, TestGetPinSubType, TestSize.Level0)
645 {
646     g_userInfoList = nullptr;
647     g_currentUser = nullptr;
648     constexpr int32_t userId = 1005;
649     constexpr uint64_t pinSubType = 10000;
650     EXPECT_EQ(GetPinSubType(userId, nullptr), RESULT_BAD_PARAM);
651     uint64_t subType = pinSubType;
652     EXPECT_EQ(GetPinSubType(userId, &subType), RESULT_NOT_FOUND);
653 }
654 
655 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionCredentialId, TestSize.Level0)
656 {
657     constexpr uint64_t credentialId = 10;
658     SetCredentialConditionCredentialId(nullptr, credentialId);
659     CredentialCondition condition = {};
660     SetCredentialConditionCredentialId(&condition, credentialId);
661     EXPECT_EQ(condition.credentialId, credentialId);
662     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_CREDENTIAL_ID, CREDENTIAL_CONDITION_CREDENTIAL_ID);
663 }
664 
665 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionTemplateId, TestSize.Level0)
666 {
667     constexpr uint64_t templateId = 20;
668     SetCredentialConditionTemplateId(nullptr, templateId);
669     CredentialCondition condition = {};
670     SetCredentialConditionTemplateId(&condition, templateId);
671     EXPECT_EQ(condition.templateId, templateId);
672     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_TEMPLATE_ID, CREDENTIAL_CONDITION_TEMPLATE_ID);
673 }
674 
675 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionAuthType, TestSize.Level0)
676 {
677     constexpr uint32_t authType = 2;
678     SetCredentialConditionAuthType(nullptr, authType);
679     CredentialCondition condition = {};
680     SetCredentialConditionAuthType(&condition, authType);
681     EXPECT_EQ(condition.authType, authType);
682     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_AUTH_TYPE, CREDENTIAL_CONDITION_AUTH_TYPE);
683 }
684 
685 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionExecutorSensorHint, TestSize.Level0)
686 {
687     constexpr uint32_t executorSensorHint = 20;
688     SetCredentialConditionExecutorSensorHint(nullptr, executorSensorHint);
689     CredentialCondition condition = {};
690     SetCredentialConditionExecutorSensorHint(&condition, executorSensorHint);
691     EXPECT_EQ(condition.executorSensorHint, executorSensorHint);
692     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_SENSOR_HINT, CREDENTIAL_CONDITION_SENSOR_HINT);
693 }
694 
695 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionExecutorMatcher, TestSize.Level0)
696 {
697     constexpr uint32_t executorMatcher = 20;
698     SetCredentialConditionExecutorMatcher(nullptr, executorMatcher);
699     CredentialCondition condition = {};
700     SetCredentialConditionExecutorMatcher(&condition, executorMatcher);
701     EXPECT_EQ(condition.executorMatcher, executorMatcher);
702     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_EXECUTOR_MATCHER, CREDENTIAL_CONDITION_EXECUTOR_MATCHER);
703 }
704 
705 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionUserId, TestSize.Level0)
706 {
707     constexpr int32_t userId = 50;
708     SetCredentialConditionUserId(nullptr, userId);
709     CredentialCondition condition = {};
710     SetCredentialConditionUserId(&condition, userId);
711     EXPECT_EQ(condition.userId, userId);
712     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_USER_ID, CREDENTIAL_CONDITION_USER_ID);
713 }
714 
715 HWTEST_F(IdmDatabaseTest, TestGetEnrolledState_001, TestSize.Level0)
716 {
717     constexpr int32_t userId = 1;
718     constexpr uint32_t authType = 1;
719     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
720     UserInfo userInfo = {};
721     userInfo.userId = userId;
722     userInfo.enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
723     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
724     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
725 
726     EnrolledStateHal enrolledState = {};
727     EXPECT_EQ(GetEnrolledState(0, authType, &enrolledState), RESULT_NOT_ENROLLED);
728     EXPECT_EQ(GetEnrolledState(userId, authType, &enrolledState), RESULT_NOT_ENROLLED);
729 }
730 
731 HWTEST_F(IdmDatabaseTest, TestGetEnrolledState_002, TestSize.Level0)
732 {
733     constexpr static int32_t expectCredentialCount = 2;
734     constexpr static int32_t testEnrolledId = 2;
735     constexpr int32_t userId = 1;
736     constexpr uint32_t authType = 1;
737     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
738     EXPECT_NE(g_userInfoList, nullptr);
739 
740     UserInfo userInfo = {};
741     userInfo.userId = userId;
742     userInfo.enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
743     EnrolledInfoHal enrolledInfo = {1, testEnrolledId};
744     userInfo.enrolledInfoList->insert(userInfo.enrolledInfoList, static_cast<void *>(&enrolledInfo));
745 
746     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
747     CredentialInfoHal credentialInfo = {0, 0, 1, 0, 0, 0};
748     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(&credentialInfo));
749     CredentialInfoHal credentialInfo1 = {1, 1, 1, 1, 1, 1};
750     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(&credentialInfo1));
751     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
752 
753     EnrolledStateHal enrolledState = {};
754     EXPECT_EQ(GetEnrolledState(userId, authType, &enrolledState), RESULT_SUCCESS);
755     EXPECT_EQ(enrolledState.credentialDigest, testEnrolledId);
756     EXPECT_EQ(enrolledState.credentialCount, expectCredentialCount);
757 }
758 
759 HWTEST_F(IdmDatabaseTest, TestRemoveCachePin_001, TestSize.Level0)
760 {
761     constexpr int32_t userId = 1;
762     UserInfo userInfo = {};
763     userInfo.userId = userId;
764     bool removed = false;
765 
766     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
767     CredentialInfoHal credentialInfo = {0, 0, 2, 2, 3, 4};
768     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(&credentialInfo));
769     CredentialInfoHal credentialInfo1 = {1, 1, 1, 1, 1, 1};
770     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(&credentialInfo1));
771 
772     RemoveCachePin(&userInfo, &removed);
773     EXPECT_EQ(removed, false);
774 }
775 
776 HWTEST_F(IdmDatabaseTest, TestSaveGlobalConfigParam, TestSize.Level0)
777 {
778     EXPECT_EQ(SaveGlobalConfigParam(nullptr), RESULT_BAD_PARAM);
779 
780     GlobalConfigParamHal param = {};
781     EXPECT_EQ(SaveGlobalConfigParam(&param), RESULT_GENERAL_ERROR);
782 
783     param.type = PIN_EXPIRED_PERIOD;
784     EXPECT_EQ(SaveGlobalConfigParam(&param), RESULT_SUCCESS);
785 }
786 
787 HWTEST_F(IdmDatabaseTest, TestGetPinExpiredInfo, TestSize.Level0)
788 {
789     int32_t userId = 1;
790     EXPECT_EQ(GetPinExpiredInfo(userId, nullptr), RESULT_BAD_PARAM);
791 
792     PinExpiredInfo info = {};
793     EXPECT_EQ(GetPinExpiredInfo(userId, &info), RESULT_SUCCESS);
794 
795     g_globalConfigArray[0].type = PIN_EXPIRED_PERIOD;
796     g_globalConfigArray[0].value.pinExpiredPeriod = 1;
797     g_currentUser = nullptr;
798     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
799     EXPECT_NE(g_userInfoList, nullptr);
800     UserInfo userInfo = {};
801     userInfo.userId = 1;
802     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
803     CredentialInfoHal credentialInfo1 = {1, 1, 1, 1, 0, 1, 0};
804     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(&credentialInfo1));
805     g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo));
806     EXPECT_EQ(GetPinExpiredInfo(userId, &info), RESULT_SUCCESS);
807 }
808 } // namespace UserAuth
809 } // namespace UserIam
810 } // namespace OHOS
811