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 "buffer.h"
22 #include "linked_list.h"
23 #include "pool.h"
24
25 extern "C" {
26 extern LinkedList *g_poolList;
27 extern void DestroyExecutorInfo(void *data);
28 extern bool IsExecutorIdMatchById(const void *data, const void *condition);
29 extern bool IsExecutorNodeMatch(const void *data, const void *condition);
30 extern bool IsExecutorValid(const ExecutorInfoHal *executorInfo);
31 extern ResultCode GenerateValidExecutorId(uint64_t *executorIndex);
32 extern bool IsExecutorMatch(const ExecutorCondition *condition, const ExecutorInfoHal *credentialInfo);
33 }
34
35 namespace OHOS {
36 namespace UserIam {
37 namespace UserAuth {
38 using namespace testing;
39 using namespace testing::ext;
40
41 class PoolTest : public testing::Test {
42 public:
SetUpTestCase()43 static void SetUpTestCase() {};
44
TearDownTestCase()45 static void TearDownTestCase() {};
46
SetUp()47 void SetUp() {};
48
TearDown()49 void TearDown() {};
50 };
51
HWTEST_F(PoolTest, TestDestroyExecutorInfo, TestSize.Level0)52 HWTEST_F(PoolTest, TestDestroyExecutorInfo, TestSize.Level0)
53 {
54 DestroyExecutorInfo(nullptr);
55 ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)Malloc(sizeof(ExecutorInfoHal));
56 EXPECT_NE(executorInfo, nullptr);
57 ASSERT_NE(executorInfo, nullptr);
58 (void)memset_s(executorInfo, sizeof(ExecutorInfoHal), 0, sizeof(ExecutorInfoHal));
59 DestroyExecutorInfo(executorInfo);
60 }
61
HWTEST_F(PoolTest, TestIsExecutorIdMatchById, TestSize.Level0)62 HWTEST_F(PoolTest, TestIsExecutorIdMatchById, TestSize.Level0)
63 {
64 EXPECT_FALSE(IsExecutorIdMatchById(nullptr, nullptr));
65 }
66
HWTEST_F(PoolTest, TestIsExecutorNodeMatch, TestSize.Level0)67 HWTEST_F(PoolTest, TestIsExecutorNodeMatch, TestSize.Level0)
68 {
69 EXPECT_FALSE(IsExecutorNodeMatch(nullptr, nullptr));
70 }
71
HWTEST_F(PoolTest, TestInitResourcePool, TestSize.Level0)72 HWTEST_F(PoolTest, TestInitResourcePool, TestSize.Level0)
73 {
74 g_poolList = CreateLinkedList(DestroyExecutorInfo);
75 EXPECT_NE(g_poolList, nullptr);
76 EXPECT_EQ(InitResourcePool(), 0);
77 DestroyResourcePool();
78 }
79
HWTEST_F(PoolTest, TestIsExecutorValid, TestSize.Level0)80 HWTEST_F(PoolTest, TestIsExecutorValid, TestSize.Level0)
81 {
82 EXPECT_FALSE(IsExecutorValid(nullptr));
83 }
84
HWTEST_F(PoolTest, TestGenerateValidExecutorId, TestSize.Level0)85 HWTEST_F(PoolTest, TestGenerateValidExecutorId, TestSize.Level0)
86 {
87 g_poolList = nullptr;
88 EXPECT_EQ(GenerateValidExecutorId(nullptr), 8);
89 }
90
HWTEST_F(PoolTest, TestRegisterExecutorToPool_001, TestSize.Level0)91 HWTEST_F(PoolTest, TestRegisterExecutorToPool_001, TestSize.Level0)
92 {
93 constexpr uint32_t authType = 4;
94 g_poolList = nullptr;
95 EXPECT_EQ(RegisterExecutorToPool(nullptr), RESULT_NEED_INIT);
96 InitResourcePool();
97 EXPECT_EQ(RegisterExecutorToPool(nullptr), RESULT_BAD_PARAM);
98 ExecutorInfoHal info = {};
99 info.authType = authType;
100 EXPECT_EQ(RegisterExecutorToPool(&info), 0);
101 g_poolList = nullptr;
102 }
103
HWTEST_F(PoolTest, TestRegisterExecutorToPool_002, TestSize.Level0)104 HWTEST_F(PoolTest, TestRegisterExecutorToPool_002, TestSize.Level0)
105 {
106 constexpr uint32_t authType = 1;
107 constexpr uint32_t executorSensorHint = 20;
108 constexpr uint32_t executorRole = 50;
109 g_poolList = nullptr;
110 EXPECT_EQ(RegisterExecutorToPool(nullptr), RESULT_NEED_INIT);
111 InitResourcePool();
112 EXPECT_EQ(RegisterExecutorToPool(nullptr), RESULT_BAD_PARAM);
113 auto *info1 = static_cast<ExecutorInfoHal *>(Malloc(sizeof(ExecutorInfoHal)));
114 info1->authType = authType;
115 info1->executorSensorHint = executorSensorHint;
116 info1->executorRole = executorRole;
117 g_poolList->insert(g_poolList, static_cast<void *>(info1));
118 auto *info2 = static_cast<ExecutorInfoHal *>(Malloc(sizeof(ExecutorInfoHal)));
119 info2->authType = authType;
120 info2->executorSensorHint = executorSensorHint;
121 info2->executorRole = executorRole;
122 EXPECT_EQ(RegisterExecutorToPool(info2), 0);
123 DestroyResourcePool();
124 }
125
HWTEST_F(PoolTest, TestUnregisterExecutorToPool, TestSize.Level0)126 HWTEST_F(PoolTest, TestUnregisterExecutorToPool, TestSize.Level0)
127 {
128 g_poolList = nullptr;
129 constexpr uint64_t index = 12;
130 EXPECT_EQ(UnregisterExecutorToPool(index), RESULT_NEED_INIT);
131 }
132
HWTEST_F(PoolTest, TestCopyExecutorInfo, TestSize.Level0)133 HWTEST_F(PoolTest, TestCopyExecutorInfo, TestSize.Level0)
134 {
135 g_poolList = nullptr;
136 EXPECT_EQ(CopyExecutorInfo(nullptr), nullptr);
137 }
138
HWTEST_F(PoolTest, TestIsExecutorMatch_001, TestSize.Level0)139 HWTEST_F(PoolTest, TestIsExecutorMatch_001, TestSize.Level0)
140 {
141 constexpr uint64_t index = 245485;
142 constexpr uint64_t executorIndex = 2634;
143 ExecutorInfoHal executorInfo = {};
144 executorInfo.executorIndex = executorIndex;
145 ExecutorCondition condition = {};
146 EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo));
147 SetExecutorConditionExecutorIndex(&condition, index);
148 EXPECT_FALSE(IsExecutorMatch(&condition, &executorInfo));
149 executorInfo.executorIndex = index;
150 EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo));
151 }
152
HWTEST_F(PoolTest, TestIsExecutorMatch_002, TestSize.Level0)153 HWTEST_F(PoolTest, TestIsExecutorMatch_002, TestSize.Level0)
154 {
155 constexpr uint64_t hint = 245485;
156 constexpr uint64_t executorIndex = 2634;
157 ExecutorInfoHal executorInfo = {};
158 executorInfo.executorIndex = executorIndex;
159 ExecutorCondition condition = {};
160 EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo));
161 SetExecutorConditionSensorHint(&condition, hint);
162 EXPECT_FALSE(IsExecutorMatch(&condition, &executorInfo));
163 executorInfo.executorSensorHint = hint;
164 EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo));
165 }
166
HWTEST_F(PoolTest, TestIsExecutorMatch_003, TestSize.Level0)167 HWTEST_F(PoolTest, TestIsExecutorMatch_003, TestSize.Level0)
168 {
169 constexpr uint64_t matcher = 245485;
170 ExecutorInfoHal executorInfo = {};
171 ExecutorCondition condition = {};
172 EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo));
173 SetExecutorConditionExecutorMatcher(&condition, matcher);
174 EXPECT_FALSE(IsExecutorMatch(&condition, &executorInfo));
175 executorInfo.executorMatcher = matcher;
176 EXPECT_TRUE(IsExecutorMatch(&condition, &executorInfo));
177 }
178
HWTEST_F(PoolTest, TestQueryExecutor, TestSize.Level0)179 HWTEST_F(PoolTest, TestQueryExecutor, TestSize.Level0)
180 {
181 g_poolList = nullptr;
182 EXPECT_EQ(QueryExecutor(nullptr), nullptr);
183 }
184
HWTEST_F(PoolTest, TestQueryCollecterMatcher, TestSize.Level0)185 HWTEST_F(PoolTest, TestQueryCollecterMatcher, TestSize.Level0)
186 {
187 constexpr uint32_t authType1 = 1;
188 constexpr uint32_t authType2 = 4;
189 constexpr uint32_t executorSensorHint = 20;
190 g_poolList = nullptr;
191 EXPECT_EQ(QueryCollecterMatcher(authType1, executorSensorHint, nullptr), RESULT_NEED_INIT);
192 InitResourcePool();
193 EXPECT_EQ(QueryCollecterMatcher(authType1, executorSensorHint, nullptr), RESULT_BAD_PARAM);
194 uint32_t matcher = 1245;
195 EXPECT_EQ(QueryCollecterMatcher(authType1, executorSensorHint, &matcher), RESULT_NOT_FOUND);
196 ExecutorInfoHal info1 = {};
197 info1.authType = authType1;
198 info1.executorSensorHint = executorSensorHint;
199 info1.executorRole = ALL_IN_ONE;
200 g_poolList->insert(g_poolList, static_cast<void *>(&info1));
201 ExecutorInfoHal info2 = {};
202 info2.authType = authType1;
203 info2.executorSensorHint = executorSensorHint;
204 info2.executorRole = VERIFIER;
205 g_poolList->insert(g_poolList, static_cast<void *>(&info2));
206 ExecutorInfoHal info3 = {};
207 info3.authType = authType1;
208 g_poolList->insert(g_poolList, static_cast<void *>(&info3));
209 ExecutorInfoHal info4 = {};
210 info4.authType = authType2;
211 g_poolList->insert(g_poolList, static_cast<void *>(&info4));
212 g_poolList->insert(g_poolList, nullptr);
213 EXPECT_EQ(QueryCollecterMatcher(1, 20, &matcher), RESULT_SUCCESS);
214 g_poolList = nullptr;
215 }
216
HWTEST_F(PoolTest, TestQueryCredentialExecutorIndex, TestSize.Level0)217 HWTEST_F(PoolTest, TestQueryCredentialExecutorIndex, TestSize.Level0)
218 {
219 constexpr uint32_t authType1 = 1;
220 constexpr uint32_t authType2 = 4;
221 constexpr uint32_t executorSensorHint = 20;
222 constexpr uint32_t executorIndex = 1267;
223 g_poolList = nullptr;
224 EXPECT_EQ(QueryCredentialExecutorIndex(authType1, executorSensorHint), INVALID_EXECUTOR_INDEX);
225 InitResourcePool();
226 EXPECT_EQ(QueryCredentialExecutorIndex(authType1, executorSensorHint), INVALID_EXECUTOR_INDEX);
227 ExecutorInfoHal info1 = {};
228 info1.authType = authType1;
229 info1.executorSensorHint = executorSensorHint;
230 info1.executorRole = ALL_IN_ONE;
231 info1.executorIndex = executorIndex;
232 g_poolList->insert(g_poolList, static_cast<void *>(&info1));
233 ExecutorInfoHal info2 = {};
234 info2.authType = authType1;
235 info2.executorSensorHint = executorSensorHint;
236 info2.executorRole = COLLECTOR;
237 g_poolList->insert(g_poolList, static_cast<void *>(&info2));
238 ExecutorInfoHal info3 = {};
239 info3.authType = authType1;
240 g_poolList->insert(g_poolList, static_cast<void *>(&info3));
241 ExecutorInfoHal info4 = {};
242 info4.authType = authType2;
243 g_poolList->insert(g_poolList, static_cast<void *>(&info4));
244 g_poolList->insert(g_poolList, nullptr);
245 EXPECT_EQ(QueryCredentialExecutorIndex(authType1, executorSensorHint), info1.executorIndex);
246 g_poolList = nullptr;
247 }
248
HWTEST_F(PoolTest, TestSetExecutorConditionExecutorIndex, TestSize.Level0)249 HWTEST_F(PoolTest, TestSetExecutorConditionExecutorIndex, TestSize.Level0)
250 {
251 constexpr uint32_t executorIndex = 10;
252 SetExecutorConditionExecutorIndex(nullptr, executorIndex);
253 ExecutorCondition condition = {};
254 SetExecutorConditionExecutorIndex(&condition, executorIndex);
255 EXPECT_EQ(condition.executorIndex, executorIndex);
256 EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_INDEX, EXECUTOR_CONDITION_INDEX);
257 }
258
HWTEST_F(PoolTest, TestSetExecutorConditionAuthType, TestSize.Level0)259 HWTEST_F(PoolTest, TestSetExecutorConditionAuthType, TestSize.Level0)
260 {
261 constexpr uint32_t authType = 1;
262 SetExecutorConditionAuthType(nullptr, authType);
263 ExecutorCondition condition = {};
264 SetExecutorConditionAuthType(&condition, authType);
265 EXPECT_EQ(condition.authType, authType);
266 EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_AUTH_TYPE, EXECUTOR_CONDITION_AUTH_TYPE);
267 }
268
HWTEST_F(PoolTest, TestSetExecutorConditionSensorHint, TestSize.Level0)269 HWTEST_F(PoolTest, TestSetExecutorConditionSensorHint, TestSize.Level0)
270 {
271 constexpr uint32_t executorSensorHint = 20;
272 SetExecutorConditionSensorHint(nullptr, executorSensorHint);
273 ExecutorCondition condition = {};
274 SetExecutorConditionSensorHint(&condition, executorSensorHint);
275 EXPECT_EQ(condition.executorSensorHint, executorSensorHint);
276 EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_SENSOR_HINT, EXECUTOR_CONDITION_SENSOR_HINT);
277 }
278
HWTEST_F(PoolTest, TestSetExecutorConditionExecutorRole, TestSize.Level0)279 HWTEST_F(PoolTest, TestSetExecutorConditionExecutorRole, TestSize.Level0)
280 {
281 constexpr uint32_t excutorRole = 2136;
282 SetExecutorConditionExecutorRole(nullptr, excutorRole);
283 ExecutorCondition condition = {};
284 SetExecutorConditionExecutorRole(&condition, excutorRole);
285 EXPECT_EQ(condition.executorRole, excutorRole);
286 EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_ROLE, EXECUTOR_CONDITION_ROLE);
287 }
288
HWTEST_F(PoolTest, TestSetExecutorConditionExecutorMatcher, TestSize.Level0)289 HWTEST_F(PoolTest, TestSetExecutorConditionExecutorMatcher, TestSize.Level0)
290 {
291 constexpr uint32_t executorMatcher = 2363;
292 SetExecutorConditionExecutorMatcher(nullptr, executorMatcher);
293 ExecutorCondition condition = {};
294 SetExecutorConditionExecutorMatcher(&condition, executorMatcher);
295 EXPECT_EQ(condition.executorMatcher, executorMatcher);
296 EXPECT_EQ(condition.conditonFactor & EXECUTOR_CONDITION_MATCHER, EXECUTOR_CONDITION_MATCHER);
297 }
298 } // namespace UserAuth
299 } // namespace UserIam
300 } // namespace OHOS
301