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