• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "securec.h"
20 #include "adaptor_time.h"
21 
22 typedef bool (*DuplicateCheckFunc)(LinkedList *collection, uint64_t value);
23 
24 extern "C" {
25     extern LinkedList *g_userInfoList;
26     extern UserInfo *g_currentUser;
27     extern GlobalConfigInfo g_globalConfigArray[MAX_GLOBAL_CONFIG_NUM];
28     extern bool MatchUserInfo(const void *data, const void *condition);
29     extern bool IsUserInfoValid(UserInfo *userInfo);
30     extern UserInfo *QueryUserInfo(int32_t userId);
31     extern bool IsSecureUidDuplicate(LinkedList *userInfoList, uint64_t secureUid);
32     extern UserInfo *CreateUser(int32_t userId, int32_t userType);
33     extern ResultCode DeleteUser(int32_t userId);
34     extern bool IsCredentialIdDuplicate(LinkedList *userInfoList, uint64_t credentialId);
35     extern bool IsEnrolledIdDuplicate(LinkedList *enrolledList, uint64_t enrolledId);
36     extern ResultCode GenerateDeduplicateUint64(LinkedList *collection, uint64_t *destValue, DuplicateCheckFunc func);
37     extern ResultCode UpdateEnrolledId(LinkedList *enrolledList, uint32_t authType);
38     extern ResultCode AddCredentialToUser(UserInfo *user, CredentialInfoHal *credentialInfo);
39     extern ResultCode AddUser(int32_t userId, CredentialInfoHal *credentialInfo, int32_t userType);
40     extern bool MatchCredentialById(const void *data, const void *condition);
41     extern bool MatchEnrolledInfoByType(const void *data, const void *condition);
42     extern CredentialInfoHal *QueryCredentialById(uint64_t credentialId, LinkedList *credentialList);
43     extern CredentialInfoHal *QueryCredentialByAuthType(uint32_t authType, LinkedList *credentialList);
44     extern bool IsCredMatch(const CredentialCondition *limit, const CredentialInfoHal *credentialInfo);
45     extern bool IsUserMatch(const CredentialCondition *limit, const UserInfo *user);
46     extern ResultCode TraverseCredentialList(const CredentialCondition *limit, const LinkedList *credentialList,
47         LinkedList *credListGet);
48     extern void RemoveCachePin(UserInfo *user, bool *isRemoved);
49 }
50 
51 namespace OHOS {
52 namespace UserIam {
53 namespace UserAuth {
54 using namespace testing;
55 using namespace testing::ext;
56 
57 class IdmDatabaseTest : public testing::Test {
58 public:
SetUpTestCase()59     static void SetUpTestCase() {};
60 
TearDownTestCase()61     static void TearDownTestCase() {};
62 
SetUp()63     void SetUp() {};
64 
TearDown()65     void TearDown() {};
66 };
67 
68 HWTEST_F(IdmDatabaseTest, TestInitUserInfoList_001, TestSize.Level0)
69 {
70     EXPECT_EQ(InitUserInfoList(), RESULT_SUCCESS);
71     DestroyUserInfoList();
72 }
73 
74 HWTEST_F(IdmDatabaseTest, TestInitUserInfoList_002, TestSize.Level0)
75 {
76     constexpr int32_t userType = 1024;
77     UserInfo *userInfo = InitUserInfoNode();
78     EXPECT_EQ(InitUserInfoList(), RESULT_SUCCESS);
79     EXPECT_NE(userInfo->userType, userType);
80     DestroyUserInfoNode(userInfo);
81     DestroyUserInfoList();
82 }
83 
84 HWTEST_F(IdmDatabaseTest, TestMatchUserInfo, TestSize.Level0)
85 {
86     EXPECT_FALSE(MatchUserInfo(nullptr, nullptr));
87     int32_t condition = 4526;
88     constexpr int32_t userId = 1133;
89     UserInfo info = {};
90     info.userId = userId;
91     EXPECT_FALSE(MatchUserInfo(static_cast<void *>(&info), static_cast<void *>(&condition)));
92 }
93 
94 HWTEST_F(IdmDatabaseTest, TestIsUserInfoValid, TestSize.Level0)
95 {
96     UserInfo info = {};
97     info.credentialInfoList = nullptr;
98     info.enrolledInfoList = nullptr;
99     EXPECT_FALSE(IsUserInfoValid(&info));
100     info.credentialInfoList = new LinkedList();
101     EXPECT_FALSE(IsUserInfoValid(&info));
102     delete info.credentialInfoList;
103 }
104 
105 HWTEST_F(IdmDatabaseTest, TestGetSecureUid, TestSize.Level0)
106 {
107     constexpr int32_t userId = 1133;
108     EXPECT_EQ(GetSecureUid(userId, nullptr), RESULT_BAD_PARAM);
109 }
110 
111 HWTEST_F(IdmDatabaseTest, TestGetEnrolledInfoAuthType_001, TestSize.Level0)
112 {
113     g_userInfoList = nullptr;
114     constexpr int32_t userId = 1166;
115     constexpr uint32_t authType = 1;
116     EXPECT_EQ(GetEnrolledInfoAuthType(userId, authType, nullptr), RESULT_BAD_PARAM);
117     EnrolledInfoHal info = {};
118     EXPECT_EQ(GetEnrolledInfoAuthType(userId, authType, &info), RESULT_NOT_FOUND);
119 }
120 
121 HWTEST_F(IdmDatabaseTest, TestGetEnrolledInfoAuthType_002, TestSize.Level0)
122 {
123     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
124     EXPECT_NE(g_userInfoList, nullptr);
125     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
126     EXPECT_NE(userInfo, nullptr);
127     constexpr int32_t userId = 1135;
128     constexpr uint32_t authType = 1;
129     userInfo->userId = userId;
130     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
131     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
132     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
133     EnrolledInfoHal info = {};
134     EXPECT_EQ(GetEnrolledInfoAuthType(userId, authType, &info), RESULT_NOT_FOUND);
135     DestroyUserInfoList();
136 }
137 
138 HWTEST_F(IdmDatabaseTest, TestGetEnrolledInfoAuthType_003, TestSize.Level0)
139 {
140     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
141     EXPECT_NE(g_userInfoList, nullptr);
142     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
143     EXPECT_NE(userInfo, nullptr);
144     constexpr int32_t userId = 1135;
145     constexpr uint32_t authType1 = 1;
146     userInfo->userId = userId;
147     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
148     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
149     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
150     EnrolledInfoHal info = {};
151     EXPECT_EQ(GetEnrolledInfoAuthType(userId, authType1, &info), RESULT_NOT_FOUND);
152     DestroyUserInfoList();
153 }
154 
155 HWTEST_F(IdmDatabaseTest, TestGetEnrolledInfo, TestSize.Level0)
156 {
157     constexpr int32_t userId = 1211;
158     EXPECT_EQ(GetEnrolledInfo(userId, nullptr, nullptr), RESULT_BAD_PARAM);
159     g_userInfoList = nullptr;
160     g_currentUser = nullptr;
161     EnrolledInfoHal *enrolledInfos = nullptr;
162     uint32_t num = 0;
163     EXPECT_EQ(GetEnrolledInfo(userId, &enrolledInfos, &num), RESULT_NOT_FOUND);
164 }
165 
166 HWTEST_F(IdmDatabaseTest, TestDeleteUserInfo, TestSize.Level0)
167 {
168     constexpr int32_t userId = 1155;
169     EXPECT_EQ(DeleteUserInfo(userId, nullptr), RESULT_BAD_PARAM);
170 }
171 
172 HWTEST_F(IdmDatabaseTest, TestQueryUserInfo_001, TestSize.Level0)
173 {
174     g_userInfoList = nullptr;
175     g_currentUser = nullptr;
176     constexpr int32_t userId1 = 123;
177     constexpr int32_t userId2 = 1123;
178     UserInfo userInfo = {};
179     userInfo.userId = userId1;
180     g_currentUser = &userInfo;
181     EXPECT_NE(QueryUserInfo(userId1), nullptr);
182     userInfo.userId = userId2;
183     EXPECT_EQ(QueryUserInfo(userId1), nullptr);
184 }
185 
186 HWTEST_F(IdmDatabaseTest, TestQueryUserInfo_002, TestSize.Level0)
187 {
188     g_userInfoList = nullptr;
189     g_currentUser = nullptr;
190     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
191     EXPECT_NE(g_userInfoList, nullptr);
192     constexpr int32_t userId1 = 123;
193     constexpr int32_t userId2 = 1336;
194     UserInfo *userInfo1 = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
195     EXPECT_NE(userInfo1, nullptr);
196     userInfo1->userId = userId1;
197     userInfo1->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
198     userInfo1->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
199     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo1));
200     UserInfo *userInfo2 = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
201     EXPECT_NE(userInfo2, nullptr);
202     userInfo2->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
203     userInfo2->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
204     userInfo2->userId = userId2;
205     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo2));
206     EXPECT_NE(QueryUserInfo(userId1), nullptr);
207     DestroyUserInfoList();
208 }
209 
210 HWTEST_F(IdmDatabaseTest, TestIsSecureUidDuplicate, TestSize.Level0)
211 {
212     constexpr uint64_t secUid = 1221;
213     constexpr uint64_t secUid1 = 111;
214     constexpr uint64_t secUid2 = 222;
215     EXPECT_FALSE(IsSecureUidDuplicate(nullptr, secUid));
216     LinkedList *userInfoList = CreateLinkedList(DestroyUserInfoNode);
217     EXPECT_NE(userInfoList, nullptr);
218     EXPECT_FALSE(IsSecureUidDuplicate(userInfoList, secUid));
219     UserInfo *info1 = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
220     EXPECT_NE(info1, nullptr);
221     info1->secUid = secUid1;
222     info1->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
223     info1->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
224     userInfoList->insert(userInfoList, static_cast<void *>(info1));
225     UserInfo *info2 = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
226     EXPECT_NE(info2, nullptr);
227     info2->secUid = secUid2;
228     info2->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
229     info2->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
230     userInfoList->insert(userInfoList, static_cast<void *>(info2));
231     EXPECT_TRUE(IsSecureUidDuplicate(userInfoList, secUid1));
232     DestroyLinkedList(userInfoList);
233 }
234 
235 HWTEST_F(IdmDatabaseTest, TestCreateUser, TestSize.Level0)
236 {
237     g_userInfoList = nullptr;
238     g_currentUser = nullptr;
239     constexpr int32_t userId = 123;
240     EXPECT_EQ(CreateUser(userId, 0), nullptr);
241     DestroyUserInfoList();
242 }
243 
244 HWTEST_F(IdmDatabaseTest, TestDeleteUser, TestSize.Level0)
245 {
246     g_userInfoList = nullptr;
247     g_currentUser = nullptr;
248     constexpr int32_t userId = 123;
249     EXPECT_EQ(DeleteUser(userId), RESULT_BAD_PARAM);
250     DestroyUserInfoList();
251 }
252 
253 HWTEST_F(IdmDatabaseTest, TestIsCredentialIdDuplicate, TestSize.Level0)
254 {
255     g_userInfoList = nullptr;
256     g_currentUser = nullptr;
257     constexpr uint64_t credentialId1 = 1221;
258     constexpr uint64_t credentialId2 = 10;
259     EXPECT_TRUE(IsCredentialIdDuplicate(nullptr, credentialId1));
260     g_userInfoList =  CreateLinkedList(DestroyUserInfoNode);
261     EXPECT_NE(g_userInfoList, nullptr);
262     UserInfo *info = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
263     EXPECT_NE(info, nullptr);
264     info->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
265     info->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
266     EXPECT_NE(info->credentialInfoList, nullptr);
267     CredentialInfoHal *credInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
268     EXPECT_NE(credInfo, nullptr);
269     credInfo->credentialId = credentialId2;
270     credInfo->isAbandoned = false;
271     info->credentialInfoList->insert(info->credentialInfoList, static_cast<void *>(credInfo));
272     g_userInfoList->insert(g_userInfoList, info);
273     EXPECT_TRUE(IsCredentialIdDuplicate(nullptr, credentialId2));
274     DestroyUserInfoList();
275 }
276 
277 HWTEST_F(IdmDatabaseTest, TestIsEnrolledIdDuplicate, TestSize.Level0)
278 {
279     constexpr uint64_t enrolledId1 = 111;
280     constexpr uint64_t enrolledId2 = 222;
281     LinkedList *enrolledList = CreateLinkedList(DestroyEnrolledNode);
282     EXPECT_NE(enrolledList, nullptr);
283     EnrolledInfoHal *info1 = static_cast<EnrolledInfoHal *>(malloc(sizeof(EnrolledInfoHal)));
284     EXPECT_NE(info1, nullptr);
285     info1->enrolledId = enrolledId1;
286     enrolledList->insert(enrolledList, static_cast<void *>(info1));
287     EnrolledInfoHal *info2 = static_cast<EnrolledInfoHal *>(malloc(sizeof(EnrolledInfoHal)));
288     EXPECT_NE(info2, nullptr);
289     info2->enrolledId = enrolledId2;
290     enrolledList->insert(enrolledList, static_cast<void *>(info2));
291     EXPECT_TRUE(IsEnrolledIdDuplicate(enrolledList, enrolledId1));
292     DestroyLinkedList(enrolledList);
293 }
294 
295 HWTEST_F(IdmDatabaseTest, TestGenerateDeduplicateUint64, TestSize.Level0)
296 {
297     EXPECT_EQ(GenerateDeduplicateUint64(nullptr, nullptr, IsEnrolledIdDuplicate), RESULT_BAD_PARAM);
298 }
299 
300 HWTEST_F(IdmDatabaseTest, TestUpdateEnrolledId, TestSize.Level0)
301 {
302     constexpr uint32_t authType1 = 1;
303     constexpr uint32_t authType2 = 2;
304     LinkedList *enrolledList = CreateLinkedList(DestroyEnrolledNode);
305     EXPECT_NE(enrolledList, nullptr);
306     EnrolledInfoHal *info1 = static_cast<EnrolledInfoHal *>(malloc(sizeof(EnrolledInfoHal)));
307     EXPECT_NE(info1, nullptr);
308     info1->authType = authType1;
309     enrolledList->insert(enrolledList, static_cast<void *>(info1));
310     EnrolledInfoHal *info2 = static_cast<EnrolledInfoHal *>(malloc(sizeof(EnrolledInfoHal)));
311     EXPECT_NE(info2, nullptr);
312     info2->authType = authType2;
313     enrolledList->insert(enrolledList, static_cast<void *>(info2));
314     enrolledList->insert(enrolledList, nullptr);
315     EXPECT_EQ(UpdateEnrolledId(enrolledList, authType1), RESULT_SUCCESS);
316     DestroyLinkedList(enrolledList);
317 }
318 
319 HWTEST_F(IdmDatabaseTest, TestAddCredentialToUser, TestSize.Level0)
320 {
321     g_userInfoList = nullptr;
322     g_currentUser = nullptr;
323     EXPECT_EQ(AddCredentialToUser(nullptr, nullptr), RESULT_NEED_INIT);
324     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
325     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
326     EXPECT_NE(userInfo, nullptr);
327     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
328     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
329     EXPECT_NE(userInfo->credentialInfoList, nullptr);
330     EXPECT_NE(userInfo->enrolledInfoList, nullptr);
331     constexpr uint32_t credNum = 102;
332     for (uint32_t i = 0; i < credNum; ++i) {
333         CredentialInfoHal *credInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
334         EXPECT_NE(credInfo, nullptr);
335         userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credInfo));
336     }
337     EXPECT_EQ(AddCredentialToUser(userInfo, nullptr), RESULT_EXCEED_LIMIT);
338     DestroyUserInfoList();
339 }
340 
341 HWTEST_F(IdmDatabaseTest, TestAddUser, TestSize.Level0)
342 {
343     g_currentUser = nullptr;
344     g_userInfoList = nullptr;
345     EXPECT_EQ(AddUser(111, nullptr, 0), RESULT_NEED_INIT);
346     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
347     EXPECT_NE(g_userInfoList, nullptr);
348     constexpr uint32_t userNum = 1002;
349     for (uint32_t i = 0; i < userNum; ++i) {
350         UserInfo *info = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
351         EXPECT_NE(info, nullptr);
352         info->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
353         info->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
354         g_userInfoList->insert(g_userInfoList, static_cast<void *>(info));
355     }
356     EXPECT_EQ(AddUser(111, nullptr, 0), RESULT_EXCEED_LIMIT);
357     DestroyUserInfoList();
358 }
359 
360 HWTEST_F(IdmDatabaseTest, TestAddCredentialInfo_001, TestSize.Level0)
361 {
362     EXPECT_EQ(AddCredentialInfo(111, nullptr, 0), RESULT_BAD_PARAM);
363 }
364 
365 HWTEST_F(IdmDatabaseTest, TestAddCredentialInfo_002, TestSize.Level0)
366 {
367     g_currentUser = nullptr;
368     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
369     EXPECT_NE(g_userInfoList, nullptr);
370     constexpr int32_t userId = 100;
371     constexpr int32_t userType = 2;
372     constexpr uint32_t authType = 1;
373     UserInfo *user = QueryUserInfo(userId);
374     EXPECT_EQ(user, nullptr);
375     user = CreateUser(userId, userType);
376     EXPECT_NE(user->userType, 0);
377 
378     CredentialInfoHal credInfo = {};
379     credInfo.authType = authType;
380     EXPECT_EQ(AddUser(userId, &credInfo, userType), RESULT_SUCCESS);
381 
382     EXPECT_EQ(AddCredentialInfo(userId, &credInfo, userType), RESULT_SUCCESS);
383     DestroyUserInfoList();
384 }
385 
386 HWTEST_F(IdmDatabaseTest, TestMatchCredentialById, TestSize.Level0)
387 {
388     EXPECT_FALSE(MatchCredentialById(nullptr, nullptr));
389     constexpr uint64_t credentialId = 10;
390     CredentialInfoHal info = {};
391     info.credentialId = credentialId;
392     uint64_t condition = credentialId;
393     EXPECT_TRUE(MatchCredentialById(static_cast<void *>(&info), static_cast<void *>(&condition)));
394     condition = 20;
395     EXPECT_FALSE(MatchCredentialById(static_cast<void *>(&info), static_cast<void *>(&condition)));
396 }
397 
398 HWTEST_F(IdmDatabaseTest, TestMatchEnrolledInfoByType, TestSize.Level0)
399 {
400     EXPECT_FALSE(MatchEnrolledInfoByType(nullptr, nullptr));
401     constexpr uint32_t authType = 1;
402     EnrolledInfoHal info = {};
403     info.authType = authType;
404     uint32_t condition = 1;
405     EXPECT_TRUE(MatchEnrolledInfoByType(static_cast<void *>(&info), static_cast<void *>(&condition)));
406     condition = 2;
407     EXPECT_FALSE(MatchEnrolledInfoByType(static_cast<void *>(&info), static_cast<void *>(&condition)));
408 }
409 
410 HWTEST_F(IdmDatabaseTest, TestDeleteCredentialInfo_001, TestSize.Level0)
411 {
412     EXPECT_EQ(DeleteCredentialInfo(1, 1, nullptr), RESULT_BAD_PARAM);
413     CredentialInfoHal credInfo = {};
414     EXPECT_EQ(DeleteCredentialInfo(1, 1, &credInfo), RESULT_BAD_PARAM);
415 }
416 
417 HWTEST_F(IdmDatabaseTest, TestDeleteCredentialInfo_002, TestSize.Level0)
418 {
419     g_currentUser = nullptr;
420     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
421     constexpr int32_t userId = 112;
422     constexpr uint64_t credentialId = 1;
423     EXPECT_NE(g_userInfoList, nullptr);
424     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
425     EXPECT_NE(userInfo, nullptr);
426     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
427     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
428     userInfo->userId = userId;
429     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
430     CredentialInfoHal credInfo = {};
431     EXPECT_EQ(DeleteCredentialInfo(userId, credentialId, &credInfo), RESULT_UNKNOWN);
432     DestroyUserInfoList();
433 }
434 
435 HWTEST_F(IdmDatabaseTest, TestDeleteCredentialInfo_003, TestSize.Level0)
436 {
437     g_currentUser = nullptr;
438     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
439     EXPECT_NE(g_userInfoList, nullptr);
440     constexpr int32_t userId = 113;
441     constexpr uint64_t credentialId = 10;
442     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
443     EXPECT_NE(userInfo, nullptr);
444     userInfo->userId = userId;
445     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
446     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
447     EXPECT_NE(userInfo->credentialInfoList, nullptr);
448     auto *credInfo = static_cast<CredentialInfoHal *>(Malloc(sizeof(CredentialInfoHal)));
449     credInfo->credentialId = credentialId;
450     credInfo->isAbandoned = false;
451     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credInfo));
452     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
453     CredentialInfoHal info = {};
454     EXPECT_EQ(DeleteCredentialInfo(userId, credentialId, &info), RESULT_SUCCESS);
455     DestroyUserInfoList();
456 }
457 
458 HWTEST_F(IdmDatabaseTest, TestDeleteCredentialInfo_004, TestSize.Level0)
459 {
460     g_currentUser = nullptr;
461     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
462     EXPECT_NE(g_userInfoList, nullptr);
463     constexpr int32_t userId = 115;
464     constexpr uint32_t authType = 2;
465     constexpr uint64_t credentialId1 = 10;
466     constexpr uint64_t credentialId2 = 20;
467     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
468     EXPECT_NE(userInfo, nullptr);
469     userInfo->userId = userId;
470     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
471     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
472     EXPECT_NE(userInfo->credentialInfoList, nullptr);
473     auto *credInfo1 = static_cast<CredentialInfoHal *>(Malloc(sizeof(CredentialInfoHal)));
474     credInfo1->credentialId = credentialId1;
475     credInfo1->authType = authType;
476     credInfo1->isAbandoned = false;
477     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credInfo1));
478     auto *credInfo2 = static_cast<CredentialInfoHal *>(Malloc(sizeof(CredentialInfoHal)));
479     credInfo2->credentialId = credentialId2;
480     credInfo2->authType = authType;
481     credInfo2->isAbandoned = false;
482     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credInfo2));
483     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
484     CredentialInfoHal info = {};
485     EXPECT_EQ(DeleteCredentialInfo(userId, credentialId1, &info), RESULT_SUCCESS);
486     DestroyUserInfoList();
487 }
488 
489 HWTEST_F(IdmDatabaseTest, TestClearCachePin, TestSize.Level0)
490 {
491     constexpr int32_t userId = 115;
492     ClearCachePin(userId);
493     g_currentUser = nullptr;
494     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
495     EXPECT_NE(g_userInfoList, nullptr);
496     constexpr uint64_t credentialId1 = 10;
497     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
498     EXPECT_NE(userInfo, nullptr);
499     userInfo->userId = userId;
500     userInfo->enrolledInfoList = nullptr;
501     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
502     EXPECT_NE(userInfo->credentialInfoList, nullptr);
503     auto *credInfo1 = static_cast<CredentialInfoHal *>(Malloc(sizeof(CredentialInfoHal)));
504     credInfo1->credentialId = credentialId1;
505     credInfo1->isAbandoned = false;
506     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credInfo1));
507     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
508     ClearCachePin(userId);
509     DestroyUserInfoList();
510 }
511 
512 HWTEST_F(IdmDatabaseTest, TestQueryCredentialById, TestSize.Level0)
513 {
514     constexpr uint64_t credentialId = 111;
515     constexpr uint64_t credentialId1 = 10;
516     constexpr uint64_t credentialId2 = 20;
517     EXPECT_EQ(QueryCredentialById(credentialId, nullptr), nullptr);
518     LinkedList *credentialList = CreateLinkedList(DestroyCredentialNode);
519     EXPECT_NE(credentialList, nullptr);
520     CredentialInfoHal *credInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
521     EXPECT_NE(credInfo1, nullptr);
522     credInfo1->credentialId = credentialId1;
523     credentialList->insert(credentialList, static_cast<void *>(credInfo1));
524     CredentialInfoHal *credInfo2 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
525     EXPECT_NE(credInfo2, nullptr);
526     credInfo2->credentialId = credentialId2;
527     credentialList->insert(credentialList, static_cast<void *>(credInfo2));
528     EXPECT_NE(QueryCredentialById(credentialId1, credentialList), nullptr);
529     DestroyLinkedList(credentialList);
530 }
531 
532 HWTEST_F(IdmDatabaseTest, TestQueryCredentialByAuthType, TestSize.Level0)
533 {
534     constexpr uint32_t authType1 = 1;
535     constexpr uint32_t authType2 = 2;
536     EXPECT_EQ(QueryCredentialByAuthType(1, nullptr), nullptr);
537     LinkedList *credentialList = CreateLinkedList(DestroyCredentialNode);
538     EXPECT_NE(credentialList, nullptr);
539     CredentialInfoHal *credInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
540     EXPECT_NE(credInfo1, nullptr);
541     credInfo1->authType = authType1;
542     credentialList->insert(credentialList, static_cast<void *>(credInfo1));
543     CredentialInfoHal *credInfo2 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
544     EXPECT_NE(credInfo2, nullptr);
545     credInfo2->authType = authType2;
546     credentialList->insert(credentialList, static_cast<void *>(credInfo2));
547     EXPECT_NE(QueryCredentialByAuthType(authType1, credentialList), nullptr);
548     DestroyLinkedList(credentialList);
549 }
550 
551 HWTEST_F(IdmDatabaseTest, TestIsCredMatch_001, TestSize.Level0)
552 {
553     constexpr uint64_t templateId1 = 20;
554     constexpr uint64_t templateId2 = 10;
555     CredentialInfoHal credInfo = {};
556     credInfo.templateId = templateId1;
557     CredentialCondition limit = {};
558     SetCredentialConditionTemplateId(&limit, templateId2);
559     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
560 }
561 
562 HWTEST_F(IdmDatabaseTest, TestIsCredMatch_002, TestSize.Level0)
563 {
564     constexpr uint32_t excutorSensorHint1 = 10;
565     constexpr uint32_t excutorSensorHint2 = 20;
566     CredentialInfoHal credInfo = {};
567     credInfo.executorSensorHint = excutorSensorHint2;
568     CredentialCondition limit = {};
569     SetCredentialConditionExecutorSensorHint(&limit, 0);
570     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
571     SetCredentialConditionExecutorSensorHint(&limit, excutorSensorHint1);
572     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
573     SetCredentialConditionExecutorSensorHint(&limit, excutorSensorHint2);
574     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
575 }
576 
577 HWTEST_F(IdmDatabaseTest, TestIsCredMatch_003, TestSize.Level0)
578 {
579     constexpr uint32_t executorMatcher1 = 10;
580     constexpr uint32_t executorMatcher2 = 20;
581     CredentialInfoHal credInfo = {};
582     credInfo.executorMatcher = executorMatcher2;
583     CredentialCondition limit = {};
584     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
585     SetCredentialConditionExecutorMatcher(&limit, executorMatcher1);
586     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
587     SetCredentialConditionExecutorMatcher(&limit, executorMatcher2);
588     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
589     SetCredentialConditionNeedCachePin(nullptr);
590     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
591     SetCredentialConditionNeedCachePin(&limit);
592     EXPECT_TRUE(IsCredMatch(&limit, &credInfo));
593 }
594 
595 HWTEST_F(IdmDatabaseTest, TestIsCredMatch_004, TestSize.Level0)
596 {
597     CredentialInfoHal credInfo = {};
598     credInfo.authType = PIN_AUTH;
599     credInfo.isAbandoned = false;
600     CredentialCondition limit = {};
601     EXPECT_TRUE(IsCredMatch(&limit, &credInfo));
602     SetCredentialConditionNeedAbandonPin(nullptr);
603     EXPECT_TRUE(IsCredMatch(&limit, &credInfo));
604     SetCredentialConditionNeedAbandonPin(&limit);
605     EXPECT_TRUE(IsCredMatch(&limit, &credInfo));
606     credInfo.isAbandoned = true;
607     EXPECT_TRUE(IsCredMatch(&limit, &credInfo));
608 }
609 
610 HWTEST_F(IdmDatabaseTest, TestIsCredMatch_005, TestSize.Level0)
611 {
612     CredentialInfoHal credInfo = {};
613     credInfo.authType = PIN_AUTH;
614     credInfo.isAbandoned = false;
615     CredentialCondition limit = {};
616     EXPECT_TRUE(IsCredMatch(&limit, &credInfo));
617     SetCredentialConditionAbandonPin(nullptr);
618     SetCredentialConditionAbandonPin(&limit);
619     EXPECT_FALSE(IsCredMatch(&limit, &credInfo));
620     credInfo.isAbandoned = true;
621     EXPECT_TRUE(IsCredMatch(&limit, &credInfo));
622 }
623 
624 HWTEST_F(IdmDatabaseTest, TestIsUserMatch, TestSize.Level0)
625 {
626     constexpr int32_t userId1 = 20;
627     constexpr int32_t userId2 = 10;
628     UserInfo userInfo = {};
629     userInfo.userId = userId1;
630     CredentialCondition limit = {};
631     SetCredentialConditionUserId(&limit, userId2);
632     EXPECT_FALSE(IsUserMatch(&limit, &userInfo));
633 }
634 
635 HWTEST_F(IdmDatabaseTest, TestTraverseCredentialList, TestSize.Level0)
636 {
637     EXPECT_EQ(TraverseCredentialList(nullptr, nullptr, nullptr), RESULT_GENERAL_ERROR);
638     LinkedList *credentialList = CreateLinkedList(DestroyCredentialNode);
639     EXPECT_NE(credentialList, nullptr);
640     credentialList->insert(credentialList, nullptr);
641     EXPECT_EQ(TraverseCredentialList(nullptr, credentialList, nullptr), RESULT_UNKNOWN);
642     DestroyLinkedList(credentialList);
643 }
644 
645 HWTEST_F(IdmDatabaseTest, TestQueryCredentialLimit_001, TestSize.Level0)
646 {
647     g_currentUser = nullptr;
648     g_userInfoList = nullptr;
649     EXPECT_EQ(QueryCredentialLimit(nullptr), nullptr);
650     CredentialCondition limit = {};
651     EXPECT_EQ(QueryCredentialLimit(&limit), nullptr);
652     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
653     EXPECT_NE(g_userInfoList, nullptr);
654     g_userInfoList->insert(g_userInfoList, nullptr);
655     EXPECT_EQ(QueryCredentialLimit(&limit), nullptr);
656     DestroyUserInfoList();
657 }
658 
659 HWTEST_F(IdmDatabaseTest, TestQueryCredentialLimit_002, TestSize.Level0)
660 {
661     constexpr int32_t userId1 = 1001;
662     constexpr int32_t userId2 = 1002;
663     g_currentUser = nullptr;
664     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
665     EXPECT_NE(g_userInfoList, nullptr);
666     UserInfo *userInfo1 = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
667     EXPECT_NE(userInfo1, nullptr);
668     userInfo1->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
669     userInfo1->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
670     userInfo1->userId = userId1;
671     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo1));
672     UserInfo *userInfo2 = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
673     EXPECT_NE(userInfo2, nullptr);
674     userInfo2->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
675     userInfo2->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
676     userInfo2->userId = userId2;
677     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo2));
678     CredentialCondition limit = {};
679     SetCredentialConditionUserId(&limit, userId1);
680     EXPECT_NE(QueryCredentialLimit(&limit), nullptr);
681     DestroyUserInfoList();
682 }
683 
684 HWTEST_F(IdmDatabaseTest, TestQueryCredentialLimit_003, TestSize.Level0)
685 {
686     constexpr int32_t userId1 = 1001;
687     g_currentUser = nullptr;
688     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
689     EXPECT_NE(g_userInfoList, nullptr);
690     UserInfo *userInfo1 = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
691     EXPECT_NE(userInfo1, nullptr);
692     userInfo1->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
693     userInfo1->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
694     userInfo1->userId = userId1;
695     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo1));
696     CredentialInfoHal *credInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
697     EXPECT_NE(credInfo1, nullptr);
698     credInfo1->authType = PIN_AUTH;
699     credInfo1->isAbandoned = true;
700     userInfo1->credentialInfoList->insert(userInfo1->credentialInfoList, static_cast<void *>(credInfo1));
701     CredentialInfoHal *credInfo2 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
702     EXPECT_NE(credInfo2, nullptr);
703     credInfo2->authType = PIN_AUTH;
704     credInfo2->isAbandoned = false;
705     userInfo1->credentialInfoList->insert(userInfo1->credentialInfoList, static_cast<void *>(credInfo2));
706     CredentialCondition limit = {};
707     SetCredentialConditionUserId(&limit, userId1);
708     SetCredentialConditionAbandonPin(&limit);
709     EXPECT_NE(QueryCredentialLimit(&limit), nullptr);
710     DestroyUserInfoList();
711 }
712 
713 HWTEST_F(IdmDatabaseTest, TestQueryCredentialLimit_004, TestSize.Level0)
714 {
715     constexpr int32_t userId1 = 1001;
716     g_currentUser = nullptr;
717     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
718     EXPECT_NE(g_userInfoList, nullptr);
719     UserInfo *userInfo1 = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
720     EXPECT_NE(userInfo1, nullptr);
721     userInfo1->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
722     userInfo1->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
723     userInfo1->userId = userId1;
724     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo1));
725     CredentialInfoHal *credInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
726     EXPECT_NE(credInfo1, nullptr);
727     credInfo1->authType = PIN_AUTH;
728     credInfo1->isAbandoned = true;
729     userInfo1->credentialInfoList->insert(userInfo1->credentialInfoList, static_cast<void *>(credInfo1));
730     CredentialInfoHal *credInfo2 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
731     EXPECT_NE(credInfo2, nullptr);
732     credInfo2->authType = PIN_AUTH;
733     credInfo2->isAbandoned = false;
734     userInfo1->credentialInfoList->insert(userInfo1->credentialInfoList, static_cast<void *>(credInfo2));
735     CredentialCondition limit = {};
736     SetCredentialConditionUserId(&limit, userId1);
737     SetCredentialConditionNeedAbandonPin(&limit);
738     EXPECT_NE(QueryCredentialLimit(&limit), nullptr);
739     DestroyUserInfoList();
740 }
741 
742 HWTEST_F(IdmDatabaseTest, TestQueryCredentialUserId_001, TestSize.Level0)
743 {
744     constexpr int32_t userId1 = 1001;
745     constexpr uint64_t credentialId = 10;
746     g_currentUser = nullptr;
747     g_userInfoList = nullptr;
748     EXPECT_EQ(QueryCredentialUserId(credentialId, nullptr), RESULT_BAD_PARAM);
749     int32_t userId = userId1;
750     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_NEED_INIT);
751     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
752     EXPECT_NE(g_userInfoList, nullptr);
753     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_NOT_FOUND);
754     g_userInfoList->insert(g_userInfoList, nullptr);
755     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_UNKNOWN);
756     DestroyUserInfoList();
757 }
758 
759 HWTEST_F(IdmDatabaseTest, TestQueryCredentialUserId_002, TestSize.Level0)
760 {
761     constexpr int32_t userId1 = 1002;
762     constexpr int32_t userId2 = 1001;
763     constexpr uint64_t credentialId = 10;
764     g_currentUser = nullptr;
765     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
766     EXPECT_NE(g_userInfoList, nullptr);
767     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
768     EXPECT_NE(userInfo, nullptr);
769     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
770     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
771     userInfo->userId = userId1;
772     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
773     int32_t userId = userId2;
774     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_NOT_FOUND);
775     DestroyUserInfoList();
776 }
777 
778 HWTEST_F(IdmDatabaseTest, TestQueryCredentialUserId_003, TestSize.Level0)
779 {
780     constexpr int32_t userId1 = 1002;
781     constexpr int32_t userId2 = 1001;
782     constexpr uint64_t credentialId = 10;
783     g_currentUser = nullptr;
784     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
785     EXPECT_NE(g_userInfoList, nullptr);
786     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
787     EXPECT_NE(userInfo, nullptr);
788     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
789     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
790     userInfo->userId = userId1;
791     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
792     int32_t userId = userId2;
793     EXPECT_EQ(QueryCredentialUserId(credentialId, &userId), RESULT_NOT_FOUND);
794     DestroyUserInfoList();
795 }
796 
797 HWTEST_F(IdmDatabaseTest, TestSetPinSubType, TestSize.Level0)
798 {
799     g_userInfoList = nullptr;
800     g_currentUser = nullptr;
801     constexpr int32_t userId = 1003;
802     constexpr uint64_t pinSubType = 10000;
803     EXPECT_EQ(SetPinSubType(userId, pinSubType), RESULT_NOT_FOUND);
804 }
805 
806 HWTEST_F(IdmDatabaseTest, TestGetPinSubType, TestSize.Level0)
807 {
808     g_userInfoList = nullptr;
809     g_currentUser = nullptr;
810     constexpr int32_t userId = 1005;
811     constexpr uint64_t pinSubType = 10000;
812     EXPECT_EQ(GetPinSubType(userId, nullptr), RESULT_BAD_PARAM);
813     uint64_t subType = pinSubType;
814     EXPECT_EQ(GetPinSubType(userId, &subType), RESULT_NOT_FOUND);
815 }
816 
817 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionCredentialId, TestSize.Level0)
818 {
819     constexpr uint64_t credentialId = 10;
820     SetCredentialConditionCredentialId(nullptr, credentialId);
821     CredentialCondition condition = {};
822     SetCredentialConditionCredentialId(&condition, credentialId);
823     EXPECT_EQ(condition.credentialId, credentialId);
824     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_CREDENTIAL_ID, CREDENTIAL_CONDITION_CREDENTIAL_ID);
825 }
826 
827 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionTemplateId, TestSize.Level0)
828 {
829     constexpr uint64_t templateId = 20;
830     SetCredentialConditionTemplateId(nullptr, templateId);
831     CredentialCondition condition = {};
832     SetCredentialConditionTemplateId(&condition, templateId);
833     EXPECT_EQ(condition.templateId, templateId);
834     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_TEMPLATE_ID, CREDENTIAL_CONDITION_TEMPLATE_ID);
835 }
836 
837 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionAuthType, TestSize.Level0)
838 {
839     constexpr uint32_t authType = 2;
840     SetCredentialConditionAuthType(nullptr, authType);
841     CredentialCondition condition = {};
842     SetCredentialConditionAuthType(&condition, authType);
843     EXPECT_EQ(condition.authType, authType);
844     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_AUTH_TYPE, CREDENTIAL_CONDITION_AUTH_TYPE);
845 }
846 
847 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionExecutorSensorHint, TestSize.Level0)
848 {
849     constexpr uint32_t executorSensorHint = 20;
850     SetCredentialConditionExecutorSensorHint(nullptr, executorSensorHint);
851     CredentialCondition condition = {};
852     SetCredentialConditionExecutorSensorHint(&condition, executorSensorHint);
853     EXPECT_EQ(condition.executorSensorHint, executorSensorHint);
854     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_SENSOR_HINT, CREDENTIAL_CONDITION_SENSOR_HINT);
855 }
856 
857 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionExecutorMatcher, TestSize.Level0)
858 {
859     constexpr uint32_t executorMatcher = 20;
860     SetCredentialConditionExecutorMatcher(nullptr, executorMatcher);
861     CredentialCondition condition = {};
862     SetCredentialConditionExecutorMatcher(&condition, executorMatcher);
863     EXPECT_EQ(condition.executorMatcher, executorMatcher);
864     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_EXECUTOR_MATCHER, CREDENTIAL_CONDITION_EXECUTOR_MATCHER);
865 }
866 
867 HWTEST_F(IdmDatabaseTest, TestSetCredentialConditionUserId, TestSize.Level0)
868 {
869     constexpr int32_t userId = 50;
870     SetCredentialConditionUserId(nullptr, userId);
871     CredentialCondition condition = {};
872     SetCredentialConditionUserId(&condition, userId);
873     EXPECT_EQ(condition.userId, userId);
874     EXPECT_EQ(condition.conditionFactor & CREDENTIAL_CONDITION_USER_ID, CREDENTIAL_CONDITION_USER_ID);
875 }
876 
877 HWTEST_F(IdmDatabaseTest, TestGetEnrolledState_001, TestSize.Level0)
878 {
879     constexpr int32_t userId = 1;
880     constexpr uint32_t authType = 1;
881     g_currentUser = nullptr;
882     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
883     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
884     EXPECT_NE(userInfo, nullptr);
885     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
886     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
887     userInfo->userId = userId;
888     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
889     EnrolledStateHal enrolledState = {};
890     EXPECT_EQ(GetEnrolledState(0, authType, &enrolledState), RESULT_NOT_ENROLLED);
891     EXPECT_EQ(GetEnrolledState(userId, authType, &enrolledState), RESULT_NOT_ENROLLED);
892     DestroyUserInfoList();
893 }
894 
895 HWTEST_F(IdmDatabaseTest, TestGetEnrolledState_002, TestSize.Level0)
896 {
897     constexpr static int32_t expectCredentialCount = 2;
898     constexpr static int32_t testEnrolledId = 2;
899     constexpr int32_t userId = 1;
900     constexpr uint32_t authType = 1;
901     g_currentUser = nullptr;
902     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
903     EXPECT_NE(g_userInfoList, nullptr);
904 
905     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
906     EXPECT_NE(userInfo, nullptr);
907     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
908     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
909     userInfo->userId = userId;
910     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
911 
912     EnrolledInfoHal *enrolledInfo = static_cast<EnrolledInfoHal *>(malloc(sizeof(EnrolledInfoHal)));
913     EXPECT_NE(enrolledInfo, nullptr);
914     enrolledInfo->authType = 1;
915     enrolledInfo->enrolledId = testEnrolledId;
916     userInfo->enrolledInfoList->insert(userInfo->enrolledInfoList, static_cast<void *>(enrolledInfo));
917 
918     CredentialInfoHal *credentialInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
919     EXPECT_NE(credentialInfo, nullptr);
920     credentialInfo->credentialId = 0;
921     credentialInfo->templateId = 0;
922     credentialInfo->authType = 1;
923     credentialInfo->executorSensorHint = 0;
924     credentialInfo->executorMatcher = 0;
925     credentialInfo->capabilityLevel = 0;
926     credentialInfo->isAbandoned = false;
927     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo));
928 
929     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
930     EXPECT_NE(credentialInfo1, nullptr);
931     credentialInfo1->credentialId = 1;
932     credentialInfo1->templateId = 1;
933     credentialInfo1->authType = 1;
934     credentialInfo1->executorSensorHint = 1;
935     credentialInfo1->executorMatcher = 1;
936     credentialInfo1->capabilityLevel = 1;
937     credentialInfo1->isAbandoned = false;
938     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
939     EnrolledStateHal enrolledState = {};
940     EXPECT_EQ(GetEnrolledState(userId, authType, &enrolledState), RESULT_SUCCESS);
941     EXPECT_EQ(enrolledState.credentialDigest, testEnrolledId);
942     EXPECT_EQ(enrolledState.credentialCount, expectCredentialCount);
943     DestroyUserInfoList();
944 }
945 
946 HWTEST_F(IdmDatabaseTest, TestRemoveCachePin_001, TestSize.Level0)
947 {
948     constexpr int32_t userId = 1;
949     UserInfo userInfo = {};
950     userInfo.userId = userId;
951     bool removed = false;
952 
953     userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode);
954     CredentialInfoHal *credentialInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
955     EXPECT_NE(credentialInfo, nullptr);
956     credentialInfo->credentialId = 0;
957     credentialInfo->templateId = 0;
958     credentialInfo->authType = FACE_AUTH;
959     credentialInfo->executorSensorHint = 2;
960     credentialInfo->executorMatcher = 3;
961     credentialInfo->capabilityLevel = 4;
962     credentialInfo->isAbandoned = false;
963     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(credentialInfo));
964     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
965     EXPECT_NE(credentialInfo1, nullptr);
966     credentialInfo1->credentialId = 1;
967     credentialInfo1->templateId = 1;
968     credentialInfo1->authType = FACE_AUTH;
969     credentialInfo1->executorSensorHint = 1;
970     credentialInfo1->executorMatcher = 1;
971     credentialInfo1->capabilityLevel = 1;
972     credentialInfo1->isAbandoned = false;
973     userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(credentialInfo1));
974     RemoveCachePin(&userInfo, &removed);
975     EXPECT_EQ(removed, false);
976     DestroyLinkedList(userInfo.credentialInfoList);
977 }
978 
979 HWTEST_F(IdmDatabaseTest, TestSaveGlobalConfigParam, TestSize.Level0)
980 {
981     memset_s(g_globalConfigArray, sizeof(GlobalConfigInfo) * MAX_GLOBAL_CONFIG_NUM, 0,
982         sizeof(GlobalConfigInfo) * MAX_GLOBAL_CONFIG_NUM);
983     EXPECT_EQ(SaveGlobalConfigParam(nullptr), RESULT_BAD_PARAM);
984 
985     GlobalConfigParamHal param = {};
986     param.type = ENABLE_STATUS;
987     param.value.enableStatus = true;
988     param.userIdNum = 1;
989     param.userIds[0] = 1;
990     param.authTypeNum = 1;
991     param.authTypes[0] = 1;
992     EXPECT_EQ(SaveGlobalConfigParam(&param), RESULT_SUCCESS);
993     EXPECT_EQ(SaveGlobalConfigParam(&param), RESULT_SUCCESS);
994     param.authTypeNum = MAX_AUTH_TYPE_LEN + 1;
995     EXPECT_EQ(SaveGlobalConfigParam(&param), RESULT_BAD_PARAM);
996     param.userIdNum = MAX_USER + 1;
997     EXPECT_EQ(SaveGlobalConfigParam(&param), RESULT_BAD_PARAM);
998 
999     GlobalConfigParamHal param1 = {};
1000     param1.type = PIN_EXPIRED_PERIOD;
1001     param1.value.pinExpiredPeriod = 1;
1002     EXPECT_EQ(SaveGlobalConfigParam(&param1), RESULT_BAD_PARAM);
1003 
1004     GlobalConfigParamHal param2 = {};
1005     EXPECT_EQ(SaveGlobalConfigParam(&param2), RESULT_BAD_PARAM);
1006 }
1007 
1008 HWTEST_F(IdmDatabaseTest, TestGetPinExpiredInfo, TestSize.Level0)
1009 {
1010     int32_t userId = 1;
1011     EXPECT_EQ(GetPinExpiredInfo(userId, nullptr), RESULT_BAD_PARAM);
1012 
1013     PinExpiredInfo info = {};
1014     EXPECT_EQ(GetPinExpiredInfo(userId, &info), RESULT_SUCCESS);
1015 
1016     g_globalConfigArray[0].type = PIN_EXPIRED_PERIOD;
1017     g_globalConfigArray[0].value.pinExpiredPeriod = 1;
1018     g_currentUser = nullptr;
1019     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
1020     EXPECT_NE(g_userInfoList, nullptr);
1021     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
1022     EXPECT_NE(userInfo, nullptr);
1023     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
1024     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
1025     userInfo->userId = userId;
1026     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1027     EXPECT_NE(credentialInfo1, nullptr);
1028     credentialInfo1->credentialId = 1;
1029     credentialInfo1->templateId = 1;
1030     credentialInfo1->authType = PIN_AUTH;
1031     credentialInfo1->executorSensorHint = 0;
1032     credentialInfo1->executorMatcher = 1;
1033     credentialInfo1->capabilityLevel = 0;
1034     credentialInfo1->isAbandoned = false;
1035     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
1036     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
1037     EXPECT_EQ(GetPinExpiredInfo(userId, &info), RESULT_SUCCESS);
1038     DestroyUserInfoList();
1039 }
1040 
1041 HWTEST_F(IdmDatabaseTest, TestGetEnableStatus, TestSize.Level0)
1042 {
1043     int32_t userId = 1;
1044     uint32_t authType = 1;
1045     EXPECT_EQ(GetEnableStatus(userId, authType), true);
1046 
1047     g_globalConfigArray[0].type = PIN_EXPIRED_PERIOD;
1048     g_globalConfigArray[0].value.pinExpiredPeriod = 1;
1049     EXPECT_EQ(GetEnableStatus(userId, authType), true);
1050 
1051     g_globalConfigArray[0].type = ENABLE_STATUS;
1052     g_globalConfigArray[0].value.enableStatus = false;
1053     g_globalConfigArray[0].authType = 0;
1054     g_globalConfigArray[0].userIds[0] = 0;
1055     EXPECT_EQ(GetEnableStatus(userId, authType), true);
1056 
1057     g_globalConfigArray[0].authType = 1;
1058     EXPECT_EQ(GetEnableStatus(userId, authType), false);
1059 }
1060 
1061 HWTEST_F(IdmDatabaseTest, TestGetCredentialByUserIdAndCredId_001, TestSize.Level0)
1062 {
1063     constexpr int32_t userId = 1;
1064     constexpr uint64_t credentialId = 1;
1065     CredentialInfoHal info = {};
1066     EXPECT_EQ(GetCredentialByUserIdAndCredId(userId, credentialId, &info), RESULT_NOT_ENROLLED);
1067     g_currentUser = nullptr;
1068     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
1069     EXPECT_NE(g_userInfoList, nullptr);
1070     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
1071     EXPECT_NE(userInfo, nullptr);
1072     userInfo->userId = userId;
1073     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
1074     EXPECT_EQ(GetCredentialByUserIdAndCredId(userId, credentialId, &info), RESULT_NOT_ENROLLED);
1075     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
1076     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
1077     EXPECT_EQ(GetCredentialByUserIdAndCredId(userId, credentialId, &info), RESULT_NOT_ENROLLED);
1078     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1079     EXPECT_NE(credentialInfo1, nullptr);
1080     credentialInfo1->credentialId = credentialId;
1081     credentialInfo1->templateId = 1;
1082     credentialInfo1->authType = PIN_AUTH;
1083     credentialInfo1->isAbandoned = false;
1084     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
1085     EXPECT_EQ(GetCredentialByUserIdAndCredId(userId, credentialId, &info), RESULT_SUCCESS);
1086     DestroyUserInfoList();
1087 }
1088 
1089 HWTEST_F(IdmDatabaseTest, TestGetCredentialByUserIdAndCredId, TestSize.Level0)
1090 {
1091     constexpr int32_t userId = 1;
1092     constexpr uint64_t credentialId = 1;
1093     CredentialInfoHal info = {};
1094     EXPECT_EQ(GetCredentialByUserIdAndCredId(userId, credentialId, &info), RESULT_NOT_ENROLLED);
1095     g_currentUser = nullptr;
1096     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
1097     EXPECT_NE(g_userInfoList, nullptr);
1098     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
1099     EXPECT_NE(userInfo, nullptr);
1100     userInfo->userId = userId;
1101     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
1102     EXPECT_EQ(GetCredentialByUserIdAndCredId(userId, credentialId, &info), RESULT_NOT_ENROLLED);
1103     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
1104     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
1105     EXPECT_EQ(GetCredentialByUserIdAndCredId(userId, credentialId, &info), RESULT_NOT_ENROLLED);
1106     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1107     EXPECT_NE(credentialInfo1, nullptr);
1108     credentialInfo1->credentialId = credentialId;
1109     credentialInfo1->templateId = 1;
1110     credentialInfo1->authType = PIN_AUTH;
1111     credentialInfo1->isAbandoned = false;
1112     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
1113     EXPECT_EQ(GetCredentialByUserIdAndCredId(userId, credentialId, &info), RESULT_SUCCESS);
1114     DestroyUserInfoList();
1115 }
1116 
1117 HWTEST_F(IdmDatabaseTest, TestUpdateAbandonResultForReset_001, TestSize.Level0)
1118 {
1119     constexpr int32_t userId = 1;
1120     CredentialInfoHal info = {};
1121     g_currentUser = nullptr;
1122     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
1123     EXPECT_NE(g_userInfoList, nullptr);
1124     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
1125     EXPECT_NE(userInfo, nullptr);
1126     userInfo->userId = userId;
1127     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
1128     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
1129     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
1130     CredentialInfoHal *credentialInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1131     EXPECT_NE(credentialInfo, nullptr);
1132     credentialInfo->credentialId = 1;
1133     credentialInfo->templateId = 1;
1134     credentialInfo->authType = PIN_AUTH;
1135     credentialInfo->isAbandoned = true;
1136     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo));
1137     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1138     EXPECT_NE(credentialInfo1, nullptr);
1139     credentialInfo1->credentialId = 2;
1140     credentialInfo1->templateId = 2;
1141     credentialInfo1->authType = PIN_AUTH;
1142     credentialInfo1->isAbandoned = false;
1143     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
1144     CredentialInfoHal *credentialInfo2 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1145     EXPECT_NE(credentialInfo2, nullptr);
1146     credentialInfo2->credentialId = 3;
1147     credentialInfo2->templateId = 3;
1148     credentialInfo2->authType = DEFAULT_AUTH_TYPE;
1149     credentialInfo2->isAbandoned = false;
1150     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo2));
1151     bool isDelete = false;
1152     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_SUCCESS);
1153     DestroyUserInfoList();
1154 }
1155 
1156 HWTEST_F(IdmDatabaseTest, TestUpdateAbandonResultForReset_002, TestSize.Level0)
1157 {
1158     constexpr int32_t userId = 1;
1159     CredentialInfoHal info = {};
1160     bool isDelete = false;
1161     g_currentUser = nullptr;
1162     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
1163     EXPECT_NE(g_userInfoList, nullptr);
1164     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_BAD_PARAM);
1165     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
1166     EXPECT_NE(userInfo, nullptr);
1167     userInfo->userId = userId;
1168     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
1169     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
1170     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
1171     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1172     CredentialInfoHal *credentialInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1173     EXPECT_NE(credentialInfo, nullptr);
1174     credentialInfo->credentialId = 1;
1175     credentialInfo->templateId = 1;
1176     credentialInfo->authType = PIN_AUTH;
1177     credentialInfo->isAbandoned = true;
1178     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo));
1179     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1180     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1181     EXPECT_NE(credentialInfo1, nullptr);
1182     credentialInfo1->credentialId = 2;
1183     credentialInfo1->templateId = 2;
1184     credentialInfo1->authType = PIN_AUTH;
1185     credentialInfo1->isAbandoned = false;
1186     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
1187     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1188     DestroyUserInfoList();
1189 }
1190 
1191 HWTEST_F(IdmDatabaseTest, TestUpdateAbandonResultForReset_003, TestSize.Level0)
1192 {
1193     constexpr int32_t userId = 1;
1194     CredentialInfoHal info = {};
1195     bool isDelete = false;
1196     g_currentUser = nullptr;
1197     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
1198     EXPECT_NE(g_userInfoList, nullptr);
1199     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
1200     EXPECT_NE(userInfo, nullptr);
1201     userInfo->userId = userId;
1202     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
1203     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
1204     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
1205     CredentialInfoHal *credentialInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1206     EXPECT_NE(credentialInfo, nullptr);
1207     credentialInfo->credentialId = 1;
1208     credentialInfo->templateId = 1;
1209     credentialInfo->authType = PIN_AUTH;
1210     credentialInfo->isAbandoned = true;
1211     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo));
1212     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1213     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1214     EXPECT_NE(credentialInfo1, nullptr);
1215     credentialInfo1->credentialId = 2;
1216     credentialInfo1->templateId = 2;
1217     credentialInfo1->authType = DEFAULT_AUTH_TYPE;
1218     credentialInfo1->isAbandoned = false;
1219     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
1220     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1221     DestroyUserInfoList();
1222 }
1223 
1224 HWTEST_F(IdmDatabaseTest, TestUpdateAbandonResultForUpdate_001, TestSize.Level0)
1225 {
1226     constexpr int32_t userId = 1;
1227     CredentialInfoHal info = {};
1228     g_currentUser = nullptr;
1229     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
1230     EXPECT_NE(g_userInfoList, nullptr);
1231     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
1232     EXPECT_NE(userInfo, nullptr);
1233     userInfo->userId = userId;
1234     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
1235     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
1236     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
1237     CredentialInfoHal *credentialInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1238     EXPECT_NE(credentialInfo, nullptr);
1239     credentialInfo->credentialId = 1;
1240     credentialInfo->templateId = 1;
1241     credentialInfo->authType = PIN_AUTH;
1242     credentialInfo->isAbandoned = true;
1243     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo));
1244     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1245     EXPECT_NE(credentialInfo1, nullptr);
1246     credentialInfo1->credentialId = 2;
1247     credentialInfo1->templateId = 2;
1248     credentialInfo1->authType = PIN_AUTH;
1249     credentialInfo1->isAbandoned = false;
1250     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
1251     CredentialInfoHal *credentialInfo2 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1252     EXPECT_NE(credentialInfo2, nullptr);
1253     credentialInfo2->credentialId = 3;
1254     credentialInfo2->templateId = 3;
1255     credentialInfo2->authType = DEFAULT_AUTH_TYPE;
1256     credentialInfo2->isAbandoned = false;
1257     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo2));
1258     bool isDelete = false;
1259     EXPECT_EQ(UpdateAbandonResultForUpdate(userId, &isDelete, &info), RESULT_SUCCESS);
1260     DestroyUserInfoList();
1261 }
1262 
1263 HWTEST_F(IdmDatabaseTest, TestUpdateAbandonResultForUpdate_002, TestSize.Level0)
1264 {
1265     constexpr int32_t userId = 1;
1266     CredentialInfoHal info = {};
1267     bool isDelete = false;
1268     g_currentUser = nullptr;
1269     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
1270     EXPECT_NE(g_userInfoList, nullptr);
1271     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_BAD_PARAM);
1272     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
1273     EXPECT_NE(userInfo, nullptr);
1274     userInfo->userId = userId;
1275     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
1276     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
1277     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
1278     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1279     CredentialInfoHal *credentialInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1280     EXPECT_NE(credentialInfo, nullptr);
1281     credentialInfo->credentialId = 1;
1282     credentialInfo->templateId = 1;
1283     credentialInfo->authType = PIN_AUTH;
1284     credentialInfo->isAbandoned = true;
1285     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo));
1286     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1287     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1288     EXPECT_NE(credentialInfo1, nullptr);
1289     credentialInfo1->credentialId = 2;
1290     credentialInfo1->templateId = 2;
1291     credentialInfo1->authType = PIN_AUTH;
1292     credentialInfo1->isAbandoned = false;
1293     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
1294     EXPECT_EQ(UpdateAbandonResultForUpdate(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1295     DestroyUserInfoList();
1296 }
1297 
1298 HWTEST_F(IdmDatabaseTest, TestUpdateAbandonResultForUpdate_003, TestSize.Level0)
1299 {
1300     constexpr int32_t userId = 1;
1301     CredentialInfoHal info = {};
1302     bool isDelete = false;
1303     g_currentUser = nullptr;
1304     g_userInfoList = CreateLinkedList(DestroyUserInfoNode);
1305     EXPECT_NE(g_userInfoList, nullptr);
1306     UserInfo *userInfo = static_cast<UserInfo *>(malloc(sizeof(UserInfo)));
1307     EXPECT_NE(userInfo, nullptr);
1308     userInfo->userId = userId;
1309     userInfo->credentialInfoList = CreateLinkedList(DestroyCredentialNode);
1310     userInfo->enrolledInfoList = CreateLinkedList(DestroyEnrolledNode);
1311     g_userInfoList->insert(g_userInfoList, static_cast<void *>(userInfo));
1312     CredentialInfoHal *credentialInfo = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1313     EXPECT_NE(credentialInfo, nullptr);
1314     credentialInfo->credentialId = 1;
1315     credentialInfo->templateId = 1;
1316     credentialInfo->authType = PIN_AUTH;
1317     credentialInfo->isAbandoned = true;
1318     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo));
1319     EXPECT_EQ(UpdateAbandonResultForReset(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1320     CredentialInfoHal *credentialInfo1 = static_cast<CredentialInfoHal *>(malloc(sizeof(CredentialInfoHal)));
1321     EXPECT_NE(credentialInfo1, nullptr);
1322     credentialInfo1->credentialId = 2;
1323     credentialInfo1->templateId = 2;
1324     credentialInfo1->authType = DEFAULT_AUTH_TYPE;
1325     credentialInfo1->isAbandoned = false;
1326     userInfo->credentialInfoList->insert(userInfo->credentialInfoList, static_cast<void *>(credentialInfo1));
1327     EXPECT_EQ(UpdateAbandonResultForUpdate(userId, &isDelete, &info), RESULT_GENERAL_ERROR);
1328     DestroyUserInfoList();
1329 }
1330 
1331 HWTEST_F(IdmDatabaseTest, TestCalcCredentialValidPeriod_001, TestSize.Level0)
1332 {
1333     CredentialInfoHal credentialInfo = {0};
1334     credentialInfo.isAbandoned = false;
1335     credentialInfo.abandonedSysTime = 0;
1336     EXPECT_EQ(CalcCredentialValidPeriod(&credentialInfo), -1);
1337     credentialInfo.isAbandoned = true;
1338     credentialInfo.abandonedSysTime = GetReeTime();
1339     EXPECT_NE(CalcCredentialValidPeriod(&credentialInfo), 0);
1340     credentialInfo.abandonedSysTime = GetReeTime() + 1;
1341     EXPECT_EQ(CalcCredentialValidPeriod(&credentialInfo), 0);
1342 }
1343 } // namespace UserAuth
1344 } // namespace UserIam
1345 } // namespace OHOS
1346