1094332d3Sopenharmony_ci/* 2094332d3Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License. 5094332d3Sopenharmony_ci * You may obtain a copy of the License at 6094332d3Sopenharmony_ci * 7094332d3Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8094332d3Sopenharmony_ci * 9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and 13094332d3Sopenharmony_ci * limitations under the License. 14094332d3Sopenharmony_ci */ 15094332d3Sopenharmony_ci 16094332d3Sopenharmony_ci#include <gtest/gtest.h> 17094332d3Sopenharmony_ci 18094332d3Sopenharmony_ci#include "securec.h" 19094332d3Sopenharmony_ci 20094332d3Sopenharmony_ci#include "adaptor_memory.h" 21094332d3Sopenharmony_ci#include "buffer.h" 22094332d3Sopenharmony_ci#include "linked_list.h" 23094332d3Sopenharmony_ci#include "pool.h" 24094332d3Sopenharmony_ci 25094332d3Sopenharmony_ciextern "C" { 26094332d3Sopenharmony_ci extern LinkedList *g_poolList; 27094332d3Sopenharmony_ci extern void DestroyExecutorInfo(void *data); 28094332d3Sopenharmony_ci extern bool IsExecutorIdMatchById(const void *data, const void *condition); 29094332d3Sopenharmony_ci extern bool IsExecutorNodeMatch(const void *data, const void *condition); 30094332d3Sopenharmony_ci extern bool IsExecutorValid(const ExecutorInfoHal *executorInfo); 31094332d3Sopenharmony_ci extern ResultCode GenerateValidExecutorId(uint64_t *executorIndex); 32094332d3Sopenharmony_ci extern bool IsExecutorMatch(const ExecutorCondition *condition, const ExecutorInfoHal *credentialInfo); 33094332d3Sopenharmony_ci} 34094332d3Sopenharmony_ci 35094332d3Sopenharmony_cinamespace OHOS { 36094332d3Sopenharmony_cinamespace UserIam { 37094332d3Sopenharmony_cinamespace UserAuth { 38094332d3Sopenharmony_ciusing namespace testing; 39094332d3Sopenharmony_ciusing namespace testing::ext; 40094332d3Sopenharmony_ci 41094332d3Sopenharmony_ciclass PoolTest : public testing::Test { 42094332d3Sopenharmony_cipublic: 43094332d3Sopenharmony_ci static void SetUpTestCase() {}; 44094332d3Sopenharmony_ci 45094332d3Sopenharmony_ci static void TearDownTestCase() {}; 46094332d3Sopenharmony_ci 47094332d3Sopenharmony_ci void SetUp() {}; 48094332d3Sopenharmony_ci 49094332d3Sopenharmony_ci void TearDown() {}; 50094332d3Sopenharmony_ci}; 51094332d3Sopenharmony_ci 52094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestDestroyExecutorInfo, TestSize.Level0) 53094332d3Sopenharmony_ci{ 54094332d3Sopenharmony_ci DestroyExecutorInfo(nullptr); 55094332d3Sopenharmony_ci ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)Malloc(sizeof(ExecutorInfoHal)); 56094332d3Sopenharmony_ci EXPECT_NE(executorInfo, nullptr); 57094332d3Sopenharmony_ci ASSERT_NE(executorInfo, nullptr); 58094332d3Sopenharmony_ci (void)memset_s(executorInfo, sizeof(ExecutorInfoHal), 0, sizeof(ExecutorInfoHal)); 59094332d3Sopenharmony_ci DestroyExecutorInfo(executorInfo); 60094332d3Sopenharmony_ci} 61094332d3Sopenharmony_ci 62094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestIsExecutorIdMatchById, TestSize.Level0) 63094332d3Sopenharmony_ci{ 64094332d3Sopenharmony_ci EXPECT_FALSE(IsExecutorIdMatchById(nullptr, nullptr)); 65094332d3Sopenharmony_ci} 66094332d3Sopenharmony_ci 67094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestIsExecutorNodeMatch, TestSize.Level0) 68094332d3Sopenharmony_ci{ 69094332d3Sopenharmony_ci EXPECT_FALSE(IsExecutorNodeMatch(nullptr, nullptr)); 70094332d3Sopenharmony_ci} 71094332d3Sopenharmony_ci 72094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestInitResourcePool, TestSize.Level0) 73094332d3Sopenharmony_ci{ 74094332d3Sopenharmony_ci g_poolList = CreateLinkedList(DestroyExecutorInfo); 75094332d3Sopenharmony_ci EXPECT_NE(g_poolList, nullptr); 76094332d3Sopenharmony_ci EXPECT_EQ(InitResourcePool(), 0); 77094332d3Sopenharmony_ci DestroyResourcePool(); 78094332d3Sopenharmony_ci} 79094332d3Sopenharmony_ci 80094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestIsExecutorValid, TestSize.Level0) 81094332d3Sopenharmony_ci{ 82094332d3Sopenharmony_ci EXPECT_FALSE(IsExecutorValid(nullptr)); 83094332d3Sopenharmony_ci} 84094332d3Sopenharmony_ci 85094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestGenerateValidExecutorId, TestSize.Level0) 86094332d3Sopenharmony_ci{ 87094332d3Sopenharmony_ci g_poolList = nullptr; 88094332d3Sopenharmony_ci EXPECT_EQ(GenerateValidExecutorId(nullptr), 8); 89094332d3Sopenharmony_ci} 90094332d3Sopenharmony_ci 91094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestRegisterExecutorToPool_001, TestSize.Level0) 92094332d3Sopenharmony_ci{ 93094332d3Sopenharmony_ci constexpr uint32_t authType = 4; 94094332d3Sopenharmony_ci g_poolList = nullptr; 95094332d3Sopenharmony_ci EXPECT_EQ(RegisterExecutorToPool(nullptr), RESULT_NEED_INIT); 96094332d3Sopenharmony_ci InitResourcePool(); 97094332d3Sopenharmony_ci EXPECT_EQ(RegisterExecutorToPool(nullptr), RESULT_BAD_PARAM); 98094332d3Sopenharmony_ci ExecutorInfoHal info = {}; 99094332d3Sopenharmony_ci info.authType = authType; 100094332d3Sopenharmony_ci EXPECT_EQ(RegisterExecutorToPool(&info), 0); 101094332d3Sopenharmony_ci g_poolList = nullptr; 102094332d3Sopenharmony_ci} 103094332d3Sopenharmony_ci 104094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestRegisterExecutorToPool_002, TestSize.Level0) 105094332d3Sopenharmony_ci{ 106094332d3Sopenharmony_ci constexpr uint32_t authType = 1; 107094332d3Sopenharmony_ci constexpr uint32_t executorSensorHint = 20; 108094332d3Sopenharmony_ci constexpr uint32_t executorRole = 50; 109094332d3Sopenharmony_ci g_poolList = nullptr; 110094332d3Sopenharmony_ci EXPECT_EQ(RegisterExecutorToPool(nullptr), RESULT_NEED_INIT); 111094332d3Sopenharmony_ci InitResourcePool(); 112094332d3Sopenharmony_ci EXPECT_EQ(RegisterExecutorToPool(nullptr), RESULT_BAD_PARAM); 113094332d3Sopenharmony_ci auto *info1 = static_cast<ExecutorInfoHal *>(Malloc(sizeof(ExecutorInfoHal))); 114094332d3Sopenharmony_ci info1->authType = authType; 115094332d3Sopenharmony_ci info1->executorSensorHint = executorSensorHint; 116094332d3Sopenharmony_ci info1->executorRole = executorRole; 117094332d3Sopenharmony_ci g_poolList->insert(g_poolList, static_cast<void *>(info1)); 118094332d3Sopenharmony_ci auto *info2 = static_cast<ExecutorInfoHal *>(Malloc(sizeof(ExecutorInfoHal))); 119094332d3Sopenharmony_ci info2->authType = authType; 120094332d3Sopenharmony_ci info2->executorSensorHint = executorSensorHint; 121094332d3Sopenharmony_ci info2->executorRole = executorRole; 122094332d3Sopenharmony_ci EXPECT_EQ(RegisterExecutorToPool(info2), 0); 123094332d3Sopenharmony_ci DestroyResourcePool(); 124094332d3Sopenharmony_ci} 125094332d3Sopenharmony_ci 126094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestUnregisterExecutorToPool, TestSize.Level0) 127094332d3Sopenharmony_ci{ 128094332d3Sopenharmony_ci g_poolList = nullptr; 129094332d3Sopenharmony_ci constexpr uint64_t index = 12; 130094332d3Sopenharmony_ci EXPECT_EQ(UnregisterExecutorToPool(index), RESULT_NEED_INIT); 131094332d3Sopenharmony_ci} 132094332d3Sopenharmony_ci 133094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestCopyExecutorInfo, TestSize.Level0) 134094332d3Sopenharmony_ci{ 135094332d3Sopenharmony_ci g_poolList = nullptr; 136094332d3Sopenharmony_ci EXPECT_EQ(CopyExecutorInfo(nullptr), nullptr); 137094332d3Sopenharmony_ci} 138094332d3Sopenharmony_ci 139094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestIsExecutorMatch_001, TestSize.Level0) 140094332d3Sopenharmony_ci{ 141094332d3Sopenharmony_ci constexpr uint64_t index = 245485; 142094332d3Sopenharmony_ci constexpr uint64_t executorIndex = 2634; 143094332d3Sopenharmony_ci ExecutorInfoHal executorInfo = {}; 144094332d3Sopenharmony_ci executorInfo.executorIndex = executorIndex; 145094332d3Sopenharmony_ci ExecutorCondition condition = {}; 146094332d3Sopenharmony_ci EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo)); 147094332d3Sopenharmony_ci SetExecutorConditionExecutorIndex(&condition, index); 148094332d3Sopenharmony_ci EXPECT_FALSE(IsExecutorMatch(&condition, &executorInfo)); 149094332d3Sopenharmony_ci executorInfo.executorIndex = index; 150094332d3Sopenharmony_ci EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo)); 151094332d3Sopenharmony_ci} 152094332d3Sopenharmony_ci 153094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestIsExecutorMatch_002, TestSize.Level0) 154094332d3Sopenharmony_ci{ 155094332d3Sopenharmony_ci constexpr uint64_t hint = 245485; 156094332d3Sopenharmony_ci constexpr uint64_t executorIndex = 2634; 157094332d3Sopenharmony_ci ExecutorInfoHal executorInfo = {}; 158094332d3Sopenharmony_ci executorInfo.executorIndex = executorIndex; 159094332d3Sopenharmony_ci ExecutorCondition condition = {}; 160094332d3Sopenharmony_ci EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo)); 161094332d3Sopenharmony_ci SetExecutorConditionSensorHint(&condition, hint); 162094332d3Sopenharmony_ci EXPECT_FALSE(IsExecutorMatch(&condition, &executorInfo)); 163094332d3Sopenharmony_ci executorInfo.executorSensorHint = hint; 164094332d3Sopenharmony_ci EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo)); 165094332d3Sopenharmony_ci} 166094332d3Sopenharmony_ci 167094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestIsExecutorMatch_003, TestSize.Level0) 168094332d3Sopenharmony_ci{ 169094332d3Sopenharmony_ci constexpr uint64_t matcher = 245485; 170094332d3Sopenharmony_ci ExecutorInfoHal executorInfo = {}; 171094332d3Sopenharmony_ci ExecutorCondition condition = {}; 172094332d3Sopenharmony_ci EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo)); 173094332d3Sopenharmony_ci SetExecutorConditionExecutorMatcher(&condition, matcher); 174094332d3Sopenharmony_ci EXPECT_FALSE(IsExecutorMatch(&condition, &executorInfo)); 175094332d3Sopenharmony_ci executorInfo.executorMatcher = matcher; 176094332d3Sopenharmony_ci EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo)); 177094332d3Sopenharmony_ci} 178094332d3Sopenharmony_ci 179094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestQueryExecutor, TestSize.Level0) 180094332d3Sopenharmony_ci{ 181094332d3Sopenharmony_ci g_poolList = nullptr; 182094332d3Sopenharmony_ci EXPECT_EQ(QueryExecutor(nullptr), nullptr); 183094332d3Sopenharmony_ci} 184094332d3Sopenharmony_ci 185094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestQueryCollecterMatcher, TestSize.Level0) 186094332d3Sopenharmony_ci{ 187094332d3Sopenharmony_ci constexpr uint32_t authType1 = 1; 188094332d3Sopenharmony_ci constexpr uint32_t authType2 = 4; 189094332d3Sopenharmony_ci constexpr uint32_t executorSensorHint = 20; 190094332d3Sopenharmony_ci g_poolList = nullptr; 191094332d3Sopenharmony_ci EXPECT_EQ(QueryCollecterMatcher(authType1, executorSensorHint, nullptr), RESULT_NEED_INIT); 192094332d3Sopenharmony_ci InitResourcePool(); 193094332d3Sopenharmony_ci EXPECT_EQ(QueryCollecterMatcher(authType1, executorSensorHint, nullptr), RESULT_BAD_PARAM); 194094332d3Sopenharmony_ci uint32_t matcher = 1245; 195094332d3Sopenharmony_ci EXPECT_EQ(QueryCollecterMatcher(authType1, executorSensorHint, &matcher), RESULT_NOT_FOUND); 196094332d3Sopenharmony_ci ExecutorInfoHal info1 = {}; 197094332d3Sopenharmony_ci info1.authType = authType1; 198094332d3Sopenharmony_ci info1.executorSensorHint = executorSensorHint; 199094332d3Sopenharmony_ci info1.executorRole = ALL_IN_ONE; 200094332d3Sopenharmony_ci g_poolList->insert(g_poolList, static_cast<void *>(&info1)); 201094332d3Sopenharmony_ci ExecutorInfoHal info2 = {}; 202094332d3Sopenharmony_ci info2.authType = authType1; 203094332d3Sopenharmony_ci info2.executorSensorHint = executorSensorHint; 204094332d3Sopenharmony_ci info2.executorRole = VERIFIER; 205094332d3Sopenharmony_ci g_poolList->insert(g_poolList, static_cast<void *>(&info2)); 206094332d3Sopenharmony_ci ExecutorInfoHal info3 = {}; 207094332d3Sopenharmony_ci info3.authType = authType1; 208094332d3Sopenharmony_ci g_poolList->insert(g_poolList, static_cast<void *>(&info3)); 209094332d3Sopenharmony_ci ExecutorInfoHal info4 = {}; 210094332d3Sopenharmony_ci info4.authType = authType2; 211094332d3Sopenharmony_ci g_poolList->insert(g_poolList, static_cast<void *>(&info4)); 212094332d3Sopenharmony_ci g_poolList->insert(g_poolList, nullptr); 213094332d3Sopenharmony_ci EXPECT_EQ(QueryCollecterMatcher(1, 20, &matcher), RESULT_SUCCESS); 214094332d3Sopenharmony_ci g_poolList = nullptr; 215094332d3Sopenharmony_ci} 216094332d3Sopenharmony_ci 217094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestQueryCredentialExecutorIndex, TestSize.Level0) 218094332d3Sopenharmony_ci{ 219094332d3Sopenharmony_ci constexpr uint32_t authType1 = 1; 220094332d3Sopenharmony_ci constexpr uint32_t authType2 = 4; 221094332d3Sopenharmony_ci constexpr uint32_t executorSensorHint = 20; 222094332d3Sopenharmony_ci constexpr uint32_t executorIndex = 1267; 223094332d3Sopenharmony_ci g_poolList = nullptr; 224094332d3Sopenharmony_ci EXPECT_EQ(QueryCredentialExecutorIndex(authType1, executorSensorHint), INVALID_EXECUTOR_INDEX); 225094332d3Sopenharmony_ci InitResourcePool(); 226094332d3Sopenharmony_ci EXPECT_EQ(QueryCredentialExecutorIndex(authType1, executorSensorHint), INVALID_EXECUTOR_INDEX); 227094332d3Sopenharmony_ci ExecutorInfoHal info1 = {}; 228094332d3Sopenharmony_ci info1.authType = authType1; 229094332d3Sopenharmony_ci info1.executorSensorHint = executorSensorHint; 230094332d3Sopenharmony_ci info1.executorRole = ALL_IN_ONE; 231094332d3Sopenharmony_ci info1.executorIndex = executorIndex; 232094332d3Sopenharmony_ci g_poolList->insert(g_poolList, static_cast<void *>(&info1)); 233094332d3Sopenharmony_ci ExecutorInfoHal info2 = {}; 234094332d3Sopenharmony_ci info2.authType = authType1; 235094332d3Sopenharmony_ci info2.executorSensorHint = executorSensorHint; 236094332d3Sopenharmony_ci info2.executorRole = COLLECTOR; 237094332d3Sopenharmony_ci g_poolList->insert(g_poolList, static_cast<void *>(&info2)); 238094332d3Sopenharmony_ci ExecutorInfoHal info3 = {}; 239094332d3Sopenharmony_ci info3.authType = authType1; 240094332d3Sopenharmony_ci g_poolList->insert(g_poolList, static_cast<void *>(&info3)); 241094332d3Sopenharmony_ci ExecutorInfoHal info4 = {}; 242094332d3Sopenharmony_ci info4.authType = authType2; 243094332d3Sopenharmony_ci g_poolList->insert(g_poolList, static_cast<void *>(&info4)); 244094332d3Sopenharmony_ci g_poolList->insert(g_poolList, nullptr); 245094332d3Sopenharmony_ci EXPECT_EQ(QueryCredentialExecutorIndex(authType1, executorSensorHint), info1.executorIndex); 246094332d3Sopenharmony_ci g_poolList = nullptr; 247094332d3Sopenharmony_ci} 248094332d3Sopenharmony_ci 249094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestSetExecutorConditionExecutorIndex, TestSize.Level0) 250094332d3Sopenharmony_ci{ 251094332d3Sopenharmony_ci constexpr uint32_t executorIndex = 10; 252094332d3Sopenharmony_ci SetExecutorConditionExecutorIndex(nullptr, executorIndex); 253094332d3Sopenharmony_ci ExecutorCondition condition = {}; 254094332d3Sopenharmony_ci SetExecutorConditionExecutorIndex(&condition, executorIndex); 255094332d3Sopenharmony_ci EXPECT_EQ(condition.executorIndex, executorIndex); 256094332d3Sopenharmony_ci EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_INDEX, EXECUTOR_CONDITION_INDEX); 257094332d3Sopenharmony_ci} 258094332d3Sopenharmony_ci 259094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestSetExecutorConditionAuthType, TestSize.Level0) 260094332d3Sopenharmony_ci{ 261094332d3Sopenharmony_ci constexpr uint32_t authType = 1; 262094332d3Sopenharmony_ci SetExecutorConditionAuthType(nullptr, authType); 263094332d3Sopenharmony_ci ExecutorCondition condition = {}; 264094332d3Sopenharmony_ci SetExecutorConditionAuthType(&condition, authType); 265094332d3Sopenharmony_ci EXPECT_EQ(condition.authType, authType); 266094332d3Sopenharmony_ci EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_AUTH_TYPE, EXECUTOR_CONDITION_AUTH_TYPE); 267094332d3Sopenharmony_ci} 268094332d3Sopenharmony_ci 269094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestSetExecutorConditionSensorHint, TestSize.Level0) 270094332d3Sopenharmony_ci{ 271094332d3Sopenharmony_ci constexpr uint32_t executorSensorHint = 20; 272094332d3Sopenharmony_ci SetExecutorConditionSensorHint(nullptr, executorSensorHint); 273094332d3Sopenharmony_ci ExecutorCondition condition = {}; 274094332d3Sopenharmony_ci SetExecutorConditionSensorHint(&condition, executorSensorHint); 275094332d3Sopenharmony_ci EXPECT_EQ(condition.executorSensorHint, executorSensorHint); 276094332d3Sopenharmony_ci EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_SENSOR_HINT, EXECUTOR_CONDITION_SENSOR_HINT); 277094332d3Sopenharmony_ci} 278094332d3Sopenharmony_ci 279094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestSetExecutorConditionExecutorRole, TestSize.Level0) 280094332d3Sopenharmony_ci{ 281094332d3Sopenharmony_ci constexpr uint32_t excutorRole = 2136; 282094332d3Sopenharmony_ci SetExecutorConditionExecutorRole(nullptr, excutorRole); 283094332d3Sopenharmony_ci ExecutorCondition condition = {}; 284094332d3Sopenharmony_ci SetExecutorConditionExecutorRole(&condition, excutorRole); 285094332d3Sopenharmony_ci EXPECT_EQ(condition.executorRole, excutorRole); 286094332d3Sopenharmony_ci EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_ROLE, EXECUTOR_CONDITION_ROLE); 287094332d3Sopenharmony_ci} 288094332d3Sopenharmony_ci 289094332d3Sopenharmony_ciHWTEST_F(PoolTest, TestSetExecutorConditionExecutorMatcher, TestSize.Level0) 290094332d3Sopenharmony_ci{ 291094332d3Sopenharmony_ci constexpr uint32_t executorMatcher = 2363; 292094332d3Sopenharmony_ci SetExecutorConditionExecutorMatcher(nullptr, executorMatcher); 293094332d3Sopenharmony_ci ExecutorCondition condition = {}; 294094332d3Sopenharmony_ci SetExecutorConditionExecutorMatcher(&condition, executorMatcher); 295094332d3Sopenharmony_ci EXPECT_EQ(condition.executorMatcher, executorMatcher); 296094332d3Sopenharmony_ci EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_MATCHER, EXECUTOR_CONDITION_MATCHER); 297094332d3Sopenharmony_ci} 298094332d3Sopenharmony_ci} // namespace UserAuth 299094332d3Sopenharmony_ci} // namespace UserIam 300094332d3Sopenharmony_ci} // namespace OHOS 301