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 "securec.h" 19 20 #include "adaptor_memory.h" 21 #include "coauth.h" 22 #include "context_manager.h" 23 #include "idm_common.h" 24 #include "pool.h" 25 26 extern "C" { 27 extern LinkedList *g_contextList; 28 extern LinkedList *g_poolList; 29 extern LinkedList *g_scheduleList; 30 extern LinkedList *g_userInfoList; 31 extern void DestroyExecutorInfo(void *data); 32 extern void DestroyContextNode(void *data); 33 extern LinkedList *GetAuthCredentialList(const UserAuthContext *context); 34 extern ResultCode CheckCredentialSize(LinkedList *credList); 35 extern ResultCode QueryAuthTempletaInfo(UserAuthContext *context, Uint64Array *templateIds, 36 uint32_t *sensorHint, uint32_t *matcher, uint32_t *acl); 37 extern bool IsContextDuplicate(uint64_t contextId); 38 extern bool MatchSchedule(const void *data, const void *condition); 39 extern void DestroyContextNode(void *data); 40 } 41 42 namespace OHOS { 43 namespace UserIam { 44 namespace UserAuth { 45 using namespace testing; 46 using namespace testing::ext; 47 48 class ContextManagerTest : public testing::Test { 49 public: SetUpTestCase()50 static void SetUpTestCase() {}; 51 TearDownTestCase()52 static void TearDownTestCase() {}; 53 SetUp()54 void SetUp() {}; 55 TearDown()56 void TearDown() {}; 57 }; 58 59 HWTEST_F(ContextManagerTest, TestInitUserAuthContextList, TestSize.Level0) 60 { 61 EXPECT_EQ(InitUserAuthContextList(), RESULT_SUCCESS); 62 EXPECT_EQ(InitUserAuthContextList(), RESULT_SUCCESS); 63 DestoryUserAuthContextList(); 64 } 65 66 HWTEST_F(ContextManagerTest, TestGenerateAuthContext, TestSize.Level0) 67 { 68 AuthSolutionHal param = {}; 69 EXPECT_EQ(GenerateAuthContext(param, nullptr), RESULT_BAD_PARAM); 70 UserAuthContext *context = nullptr; 71 g_contextList = nullptr; 72 EXPECT_EQ(GenerateAuthContext(param, &context), RESULT_NEED_INIT); 73 g_contextList = CreateLinkedList(DestroyContextNode); 74 EXPECT_NE(g_contextList, nullptr); 75 UserAuthContext authContext = {}; 76 authContext.contextId = 321566; 77 g_contextList->insert(g_contextList, static_cast<void *>(&authContext)); 78 param.contextId = 321566; 79 EXPECT_EQ(GenerateAuthContext(param, &context), RESULT_DUPLICATE_CHECK_FAILED); 80 } 81 82 HWTEST_F(ContextManagerTest, TestGenerateIdentifyContext, TestSize.Level0) 83 { 84 g_contextList = nullptr; 85 IdentifyParam param = {}; 86 param.contextId = 234562; 87 EXPECT_EQ(GenerateIdentifyContext(param), nullptr); 88 g_contextList = CreateLinkedList(DestroyContextNode); 89 EXPECT_NE(g_contextList, nullptr); 90 UserAuthContext context = {}; 91 context.contextId = param.contextId; 92 EXPECT_EQ(GenerateIdentifyContext(param), nullptr); 93 } 94 95 HWTEST_F(ContextManagerTest, TestGetContext, TestSize.Level0) 96 { 97 g_contextList = nullptr; 98 uint64_t contextId = 324112; 99 EXPECT_EQ(GetContext(contextId), nullptr); 100 g_contextList = CreateLinkedList(DestroyContextNode); 101 EXPECT_NE(g_contextList, nullptr); 102 UserAuthContext context = {}; 103 context.contextId = 31157; 104 g_contextList->insert(g_contextList, static_cast<void *>(&context)); 105 g_contextList->insert(g_contextList, nullptr); 106 EXPECT_EQ(GetContext(contextId), nullptr); 107 } 108 109 HWTEST_F(ContextManagerTest, TestGetAuthCredentialList_001, TestSize.Level0) 110 { 111 g_poolList = nullptr; 112 UserAuthContext context = {}; 113 context.collectorSensorHint = 10; 114 EXPECT_EQ(GetAuthCredentialList(&context), nullptr); 115 } 116 117 HWTEST_F(ContextManagerTest, TestGetAuthCredentialList_002, TestSize.Level0) 118 { 119 g_poolList = CreateLinkedList(DestroyExecutorInfo); 120 EXPECT_NE(g_poolList, nullptr); 121 ExecutorInfoHal executorInfo = {}; 122 executorInfo.authType = 2; 123 executorInfo.executorSensorHint = 10; 124 executorInfo.executorRole = ALL_IN_ONE; 125 g_poolList->insert(g_poolList, static_cast<void *>(&executorInfo)); 126 UserAuthContext context = {}; 127 context.authType = 2; 128 context.collectorSensorHint = 10; 129 EXPECT_EQ(GetAuthCredentialList(&context), nullptr); 130 } 131 132 HWTEST_F(ContextManagerTest, TestCheckCredentialSize, TestSize.Level0) 133 { 134 LinkedList *credList = CreateLinkedList(DestroyCredentialNode); 135 EXPECT_NE(credList, nullptr); 136 CredentialInfoHal info = {}; 137 uint32_t credNum = 102; 138 for (uint32_t i = 0; i < credNum; ++i) { 139 credList->insert(credList, static_cast<void *>(&info)); 140 } 141 EXPECT_EQ(CheckCredentialSize(credList), RESULT_EXCEED_LIMIT); 142 } 143 144 HWTEST_F(ContextManagerTest, TestQueryAuthTempletaInfo, TestSize.Level0) 145 { 146 g_poolList = nullptr; 147 UserAuthContext context = {}; 148 context.authType = 2; 149 context.contextId = 21245; 150 context.userId = 76256; 151 Uint64Array array = {}; 152 uint32_t hint = 0; 153 uint32_t matcher = 0; 154 uint32_t acl = 0; 155 EXPECT_EQ(QueryAuthTempletaInfo(&context, &array, &hint, &matcher, &acl), RESULT_UNKNOWN); 156 } 157 158 HWTEST_F(ContextManagerTest, TestIsContextDuplicate, TestSize.Level0) 159 { 160 g_contextList = nullptr; 161 uint64_t contextId = 36517; 162 EXPECT_FALSE(IsContextDuplicate(contextId)); 163 164 g_contextList = CreateLinkedList(DestroyContextNode); 165 EXPECT_NE(g_contextList, nullptr); 166 UserAuthContext context1 = {}; 167 context1.contextId = contextId; 168 g_contextList->insert(g_contextList, static_cast<void *>(&context1)); 169 UserAuthContext context2 = {}; 170 context2.contextId = 36529; 171 g_contextList->insert(g_contextList, static_cast<void *>(&context2)); 172 g_contextList->insert(g_contextList, nullptr); 173 EXPECT_TRUE(IsContextDuplicate(contextId)); 174 } 175 176 HWTEST_F(ContextManagerTest, TestCopySchedules_001, TestSize.Level0) 177 { 178 EXPECT_EQ(CopySchedules(nullptr, nullptr), RESULT_BAD_PARAM); 179 UserAuthContext context = {}; 180 context.scheduleList = nullptr; 181 EXPECT_EQ(CopySchedules(&context, nullptr), RESULT_BAD_PARAM); 182 context.scheduleList = CreateLinkedList(DestroyScheduleNode); 183 EXPECT_NE(context.scheduleList, nullptr); 184 EXPECT_EQ(CopySchedules(&context, nullptr), RESULT_BAD_PARAM); 185 } 186 187 HWTEST_F(ContextManagerTest, TestCopySchedules_002, TestSize.Level0) 188 { 189 UserAuthContext context = {}; 190 context.scheduleList = CreateLinkedList(DestroyScheduleNode); 191 EXPECT_NE(context.scheduleList, nullptr); 192 uint32_t scheduleNum = 6; 193 CoAuthSchedule schedule = {}; 194 for (uint32_t i = 0; i < scheduleNum; ++i) { 195 context.scheduleList->insert(context.scheduleList, static_cast<void *>(&schedule)); 196 } 197 LinkedList *getSchedule = nullptr; 198 EXPECT_EQ(CopySchedules(&context, &getSchedule), RESULT_UNKNOWN); 199 } 200 201 HWTEST_F(ContextManagerTest, TestCopySchedules_003, TestSize.Level0) 202 { 203 UserAuthContext context = {}; 204 context.scheduleList = CreateLinkedList(DestroyScheduleNode); 205 EXPECT_NE(context.scheduleList, nullptr); 206 LinkedList *getSchedule = nullptr; 207 EXPECT_EQ(CopySchedules(&context, &getSchedule), RESULT_SUCCESS); 208 } 209 210 HWTEST_F(ContextManagerTest, TestCopySchedules_004, TestSize.Level0) 211 { 212 UserAuthContext context = {}; 213 context.scheduleList = CreateLinkedList(DestroyScheduleNode); 214 EXPECT_NE(context.scheduleList, nullptr); 215 context.scheduleList->insert(context.scheduleList, nullptr); 216 LinkedList *getSchedule = nullptr; 217 EXPECT_EQ(CopySchedules(&context, &getSchedule), RESULT_GENERAL_ERROR); 218 } 219 220 HWTEST_F(ContextManagerTest, TestCopySchedules_005, TestSize.Level0) 221 { 222 UserAuthContext context = {}; 223 context.scheduleList = CreateLinkedList(DestroyScheduleNode); 224 EXPECT_NE(context.scheduleList, nullptr); 225 CoAuthSchedule schedule = {}; 226 schedule.templateIds.len = 12; 227 context.scheduleList->insert(context.scheduleList, static_cast<void *>(&schedule)); 228 LinkedList *getSchedule = nullptr; 229 EXPECT_EQ(CopySchedules(&context, &getSchedule), RESULT_GENERAL_ERROR); 230 } 231 232 HWTEST_F(ContextManagerTest, TestMatchSchedule, TestSize.Level0) 233 { 234 EXPECT_FALSE(MatchSchedule(nullptr, nullptr)); 235 CoAuthSchedule schedule = {}; 236 EXPECT_FALSE(MatchSchedule(static_cast<void *>(&schedule), nullptr)); 237 schedule.scheduleId = 10; 238 uint64_t condition = 20; 239 EXPECT_FALSE(MatchSchedule(static_cast<void *>(&schedule), static_cast<void *>(&condition))); 240 } 241 242 HWTEST_F(ContextManagerTest, TestScheduleOnceFinish, TestSize.Level0) 243 { 244 uint64_t scheduleId = 10; 245 EXPECT_EQ(ScheduleOnceFinish(nullptr, scheduleId), RESULT_BAD_PARAM); 246 UserAuthContext context = {}; 247 context.scheduleList = nullptr; 248 EXPECT_EQ(ScheduleOnceFinish(&context, scheduleId), RESULT_BAD_PARAM); 249 } 250 251 HWTEST_F(ContextManagerTest, TestDestoryContext, TestSize.Level0) 252 { 253 DestoryContext(nullptr); 254 UserAuthContext *context = nullptr; 255 g_contextList = nullptr; 256 DestoryContext(context); 257 258 g_contextList = CreateLinkedList(DestroyContextNode); 259 ASSERT_NE(g_contextList, nullptr); 260 context = (UserAuthContext *)Malloc(sizeof(UserAuthContext)); 261 ASSERT_NE(context, nullptr); 262 (void)memset_s(context, sizeof(UserAuthContext), 0, sizeof(UserAuthContext)); 263 g_contextList->insert(g_contextList, static_cast<void *>(context)); 264 EXPECT_EQ(g_contextList->getSize(g_contextList), 1); 265 DestoryContext(context); 266 EXPECT_EQ(g_contextList->getSize(g_contextList), 0); 267 DestoryUserAuthContextList(); 268 } 269 270 HWTEST_F(ContextManagerTest, TestDestroyContextNode, TestSize.Level0) 271 { 272 DestroyContextNode(nullptr); 273 UserAuthContext context = {}; 274 context.scheduleList = nullptr; 275 DestroyContextNode(nullptr); 276 context.scheduleList = CreateLinkedList(DestroyScheduleNode); 277 EXPECT_NE(context.scheduleList, nullptr); 278 context.scheduleList->insert(context.scheduleList, nullptr); 279 DestroyContextNode(nullptr); 280 } 281 282 HWTEST_F(ContextManagerTest, TestFillInContext_001, TestSize.Level0) 283 { 284 EXPECT_EQ(FillInContext(nullptr, nullptr, nullptr, SCHEDULE_MODE_ENROLL), RESULT_BAD_PARAM); 285 g_scheduleList = nullptr; 286 UserAuthContext context = {}; 287 uint64_t credentialId = 10; 288 ExecutorResultInfo info = {}; 289 info.scheduleId = 2135; 290 EXPECT_EQ(FillInContext(&context, &credentialId, &info, SCHEDULE_MODE_ENROLL), RESULT_GENERAL_ERROR); 291 } 292 293 HWTEST_F(ContextManagerTest, TestFillInContext_002, TestSize.Level0) 294 { 295 g_scheduleList = CreateLinkedList(DestroyScheduleNode); 296 EXPECT_NE(g_scheduleList, nullptr); 297 CoAuthSchedule schedule = {}; 298 schedule.scheduleId = 2135; 299 schedule.executorSize = 0; 300 g_scheduleList->insert(g_scheduleList, static_cast<void *>(&schedule)); 301 302 UserAuthContext context = {}; 303 uint64_t credentialId = 10; 304 ExecutorResultInfo info = {}; 305 info.scheduleId = 2135; 306 EXPECT_EQ(FillInContext(&context, &credentialId, &info, SCHEDULE_MODE_ENROLL), RESULT_BAD_PARAM); 307 } 308 309 HWTEST_F(ContextManagerTest, TestFillInContext_003, TestSize.Level0) 310 { 311 g_scheduleList = CreateLinkedList(DestroyScheduleNode); 312 EXPECT_NE(g_scheduleList, nullptr); 313 CoAuthSchedule schedule = {}; 314 schedule.scheduleId = 2135; 315 schedule.executorSize = 1; 316 ExecutorInfoHal executorInfo = {}; 317 executorInfo.authType = 1; 318 executorInfo.esl = 2; 319 executorInfo.executorSensorHint = 10; 320 executorInfo.executorRole = ALL_IN_ONE; 321 schedule.executors[0] = executorInfo; 322 g_scheduleList->insert(g_scheduleList, static_cast<void *>(&schedule)); 323 324 g_userInfoList = nullptr; 325 326 UserAuthContext context = {}; 327 uint64_t credentialId = 10; 328 ExecutorResultInfo info = {}; 329 info.scheduleId = 2135; 330 EXPECT_EQ(FillInContext(&context, &credentialId, &info, SCHEDULE_MODE_ENROLL), RESULT_UNKNOWN); 331 } 332 333 HWTEST_F(ContextManagerTest, TestFillInContext_004, TestSize.Level0) 334 { 335 g_scheduleList = CreateLinkedList(DestroyScheduleNode); 336 EXPECT_NE(g_scheduleList, nullptr); 337 CoAuthSchedule schedule = {}; 338 schedule.scheduleId = 2135; 339 schedule.executorSize = 1; 340 ExecutorInfoHal executorInfo = {}; 341 executorInfo.authType = 1; 342 executorInfo.esl = 2; 343 executorInfo.executorSensorHint = 10; 344 executorInfo.executorRole = ALL_IN_ONE; 345 schedule.executors[0] = executorInfo; 346 g_scheduleList->insert(g_scheduleList, static_cast<void *>(&schedule)); 347 348 g_userInfoList = CreateLinkedList(DestroyUserInfoNode); 349 EXPECT_NE(g_userInfoList, nullptr); 350 UserInfo userInfo = {}; 351 userInfo.credentialInfoList = CreateLinkedList(DestroyCredentialNode); 352 EXPECT_NE(userInfo.credentialInfoList, nullptr); 353 CredentialInfoHal credInfo = {}; 354 credInfo.authType = 2; 355 credInfo.templateId = 20; 356 credInfo.executorSensorHint = 10; 357 userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(&credInfo)); 358 userInfo.credentialInfoList->insert(userInfo.credentialInfoList, static_cast<void *>(&credInfo)); 359 g_userInfoList->insert(g_userInfoList, static_cast<void *>(&userInfo)); 360 361 UserAuthContext context = {}; 362 context.authType = 2; 363 uint64_t credentialId = 10; 364 ExecutorResultInfo info = {}; 365 info.scheduleId = 2135; 366 info.templateId = 20; 367 EXPECT_EQ(FillInContext(&context, &credentialId, &info, SCHEDULE_MODE_ENROLL), RESULT_UNKNOWN); 368 } 369 } // namespace UserAuth 370 } // namespace UserIam 371 } // namespace OHOS 372