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