1 /*
2 * Copyright (C) 2022-2023 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 <memory>
17
18 #include "iam_ptr.h"
19
20 #include "authentication_impl.h"
21 #include "resource_node_pool.h"
22 #include "mock_iuser_auth_interface.h"
23 #include "mock_resource_node.h"
24 #include "mock_schedule_node_callback.h"
25 #include "mock_authentication.h"
26
27 constexpr int32_t TEST_USER_ID = 101;
28
29 namespace OHOS {
30 namespace UserIam {
31 namespace UserAuth {
32 using namespace testing;
33 using namespace testing::ext;
34
35 class AuthenticationImplTest : public testing::Test {
36 public:
37 static void SetUpTestCase();
38
39 static void TearDownTestCase();
40
41 void SetUp() override;
42
43 void TearDown() override;
44 };
45
SetUpTestCase()46 void AuthenticationImplTest::SetUpTestCase()
47 {
48 }
49
TearDownTestCase()50 void AuthenticationImplTest::TearDownTestCase()
51 {
52 }
53
SetUp()54 void AuthenticationImplTest::SetUp()
55 {
56 MockIUserAuthInterface::Holder::GetInstance().Reset();
57 }
58
TearDown()59 void AuthenticationImplTest::TearDown()
60 {
61 MockIUserAuthInterface::Holder::GetInstance().Reset();
62 }
63
64 HWTEST_F(AuthenticationImplTest, AuthenticationHdiError, TestSize.Level0)
65 {
66 constexpr uint64_t contextId = 0x1234567;
67 Authentication::AuthenticationPara para = {};
68 para.userId = 0x11;
69 para.callerName = "com.ohos.test";
70 para.sdkVersion = 11;
71 para.authType = FACE;
72 para.atl = ATL3;
73 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
74 EXPECT_CALL(*mock, BeginAuthentication(contextId, _, _)).WillRepeatedly(Return(1));
75
76 auto authentication = std::make_shared<AuthenticationImpl>(contextId, para);
77 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
78 EXPECT_FALSE(authentication->Start(scheduleList, nullptr));
79 }
80
81 HWTEST_F(AuthenticationImplTest, AuthenticationHdiEmpty, TestSize.Level0)
82 {
83 constexpr uint64_t contextId = 0x1234567;
84 Authentication::AuthenticationPara para = {};
85 para.userId = 0x11;
86 para.callerName = "com.ohos.test";
87 para.sdkVersion = 11;
88 para.authType = FACE;
89 para.atl = ATL3;
90
91 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
92 EXPECT_CALL(*mock, BeginAuthentication(contextId, _, _)).WillRepeatedly(Return(0));
93
94 auto authentication = std::make_shared<AuthenticationImpl>(contextId, para);
95 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
96 EXPECT_FALSE(authentication->Start(scheduleList, nullptr));
97 }
98
99 HWTEST_F(AuthenticationImplTest, AuthenticationInvalidExecutor, TestSize.Level0)
100 {
101 constexpr uint64_t contextId = 0x1234567;
102 Authentication::AuthenticationPara para = {};
103 para.userId = 0x11;
104 para.callerName = "com.ohos.test";
105 para.sdkVersion = 11;
106 para.authType = FACE;
107 para.atl = ATL3;
108 constexpr int32_t executorInfoIndex = 0x100;
109 constexpr int32_t scheduleId = 0x1122;
110
__anonbae66ea50102(std::vector<HdiScheduleInfo> &scheduleInfos) 111 auto fillInfoList = [](std::vector<HdiScheduleInfo> &scheduleInfos) {
112 HdiScheduleInfo scheduleInfo;
113 scheduleInfo.scheduleId = scheduleId;
114 scheduleInfo.templateIds = {0, 1, 2};
115 scheduleInfo.authType = HdiAuthType::FACE;
116 scheduleInfo.executorMatcher = 0;
117 scheduleInfo.scheduleMode = HdiScheduleMode::ENROLL;
118 scheduleInfo.executorIndexes.push_back(executorInfoIndex);
119 std::vector<uint8_t> executorMessages;
120 executorMessages.resize(1);
121 scheduleInfo.executorMessages.push_back(executorMessages);
122 std::vector<HdiScheduleInfo> list;
123 list.emplace_back(scheduleInfo);
124
125 scheduleInfos.swap(list);
126 };
127
128 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
129 EXPECT_CALL(*mock, BeginAuthentication(contextId, _, _)).WillRepeatedly(DoAll(WithArg<2>(fillInfoList),
130 Return(0)));
131
132 auto authentication = std::make_shared<AuthenticationImpl>(contextId, para);
133 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
134 EXPECT_FALSE(authentication->Start(scheduleList, nullptr));
135 }
136
137 HWTEST_F(AuthenticationImplTest, AuthenticationImplTestUpdate001, TestSize.Level0)
138 {
139 constexpr uint64_t contextId = 54871;
140 Authentication::AuthenticationPara para = {};
141 para.userId = 0x11;
142 para.callerName = "com.ohos.test";
143 para.sdkVersion = 11;
144 para.authType = FACE;
145 para.atl = ATL3;
146
147 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
148 EXPECT_NE(mockHdi, nullptr);
149 EXPECT_CALL(*mockHdi, UpdateAuthenticationResult(_, _, _, _)).Times(1);
150 ON_CALL(*mockHdi, UpdateAuthenticationResult)
151 .WillByDefault(
152 [](uint64_t contextId, const std::vector<uint8_t> &scheduleResult, HdiAuthResultInfo &info,
__anonbae66ea50202(uint64_t contextId, const std::vector<uint8_t> &scheduleResult, HdiAuthResultInfo &info, HdiEnrolledState &enrolledState) 153 HdiEnrolledState &enrolledState) {
154 info.result = HDF_SUCCESS;
155 info.userId = TEST_USER_ID;
156 info.credentialId = 1;
157 return HDF_SUCCESS;
158 }
159 );
160
161 auto authentication = std::make_shared<AuthenticationImpl>(contextId, para);
162 EXPECT_NE(authentication, nullptr);
163 std::vector<uint8_t> scheduleResult;
164 Authentication::AuthResultInfo info = {};
165 EXPECT_TRUE(authentication->Update(scheduleResult, info));
166 EXPECT_EQ(info.userId, TEST_USER_ID);
167 EXPECT_EQ(info.credentialId, 1);
168 }
169
170 HWTEST_F(AuthenticationImplTest, AuthenticationImplTestUpdate002, TestSize.Level0)
171 {
172 constexpr uint64_t contextId = 54871;
173 Authentication::AuthenticationPara para = {};
174 para.userId = 0x11;
175 para.callerName = "com.ohos.test";
176 para.sdkVersion = 11;
177 para.authType = FACE;
178 para.atl = ATL3;
179
180 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
181 EXPECT_NE(mockHdi, nullptr);
182 EXPECT_CALL(*mockHdi, UpdateAuthenticationResult(_, _, _, _)).Times(1);
183 ON_CALL(*mockHdi, UpdateAuthenticationResult)
184 .WillByDefault(
185 [](uint64_t contextId, const std::vector<uint8_t> &scheduleResult, HdiAuthResultInfo &info,
__anonbae66ea50302(uint64_t contextId, const std::vector<uint8_t> &scheduleResult, HdiAuthResultInfo &info, HdiEnrolledState &enrolledState) 186 HdiEnrolledState &enrolledState) {
187 info.result = HDF_FAILURE;
188 HdiExecutorSendMsg msg = {};
189 msg.commandId = 10;
190 msg.executorIndex = 20;
191 info.msgs.push_back(msg);
192 return HDF_FAILURE;
193 }
194 );
195
196 auto authentication = std::make_shared<AuthenticationImpl>(contextId, para);
197 EXPECT_NE(authentication, nullptr);
198 std::vector<uint8_t> scheduleResult;
199 Authentication::AuthResultInfo info = {};
200 EXPECT_FALSE(authentication->Update(scheduleResult, info));
201 }
202
203 HWTEST_F(AuthenticationImplTest, AuthenticationImplTestSetEndAfterFirstFail, TestSize.Level0)
204 {
205 constexpr uint64_t contextId = 1234;
206 Authentication::AuthenticationPara para = {};
207 para.userId = 0x11;
208 para.callerName = "com.ohos.test";
209 para.sdkVersion = 11;
210 para.authType = FACE;
211 para.atl = ATL3;
212
213 auto authentication = std::make_shared<AuthenticationImpl>(contextId, para);
214 EXPECT_NE(authentication, nullptr);
215 bool endAfterFirstFail = true;
216 authentication->SetEndAfterFirstFail(endAfterFirstFail);
217 }
218
219 HWTEST_F(AuthenticationImplTest, AuthenticationImplTestGetLatestError, TestSize.Level0)
220 {
221 constexpr uint64_t contextId = 1234;
222 Authentication::AuthenticationPara para = {};
223 para.userId = 0x11;
224 para.callerName = "com.ohos.test";
225 para.sdkVersion = 11;
226 para.authType = FACE;
227 para.atl = ATL3;
228
229 auto authentication = std::make_shared<AuthenticationImpl>(contextId, para);
230 ASSERT_NE(authentication, nullptr);
231 int32_t lastError = authentication->GetLatestError();
232 EXPECT_EQ(ResultCode::GENERAL_ERROR, lastError);
233 }
234
235 HWTEST_F(AuthenticationImplTest, AuthenticationImplTestGetAuthExecutorMsgs, TestSize.Level0)
236 {
237 constexpr uint64_t contextId = 1234;
238 Authentication::AuthenticationPara para = {};
239 para.userId = 0x11;
240 para.callerName = "com.ohos.test";
241 para.sdkVersion = 11;
242 para.authType = FACE;
243 para.atl = ATL3;
244
245 auto authentication = std::make_shared<AuthenticationImpl>(contextId, para);
246 ASSERT_NE(authentication, nullptr);
247 auto authExecutorMsgs = authentication->GetAuthExecutorMsgs();
248 EXPECT_EQ(authExecutorMsgs.size(), 0);
249 }
250
251 HWTEST_F(AuthenticationImplTest, AuthenticationImplTestStart, TestSize.Level0)
252 {
253 constexpr uint64_t contextId = 34567;
254 constexpr uint64_t executorIndex = 60;
255 Authentication::AuthenticationPara para = {};
256 para.userId = 0x11;
257 para.callerName = "com.ohos.test";
258 para.sdkVersion = 11;
259 para.authType = FACE;
260 para.atl = ATL3;
261
262 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
263 EXPECT_NE(mockHdi, nullptr);
264 EXPECT_CALL(*mockHdi, CancelAuthentication(_)).Times(0)
265 .WillOnce(Return(HDF_SUCCESS)).WillOnce(Return(HDF_FAILURE));
266 EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _))
267 .WillRepeatedly(
__anonbae66ea50402(uint64_t contextId, const HdiAuthParam ¶m, std::vector<HdiScheduleInfo> &scheduleInfos) 268 [](uint64_t contextId, const HdiAuthParam ¶m, std::vector<HdiScheduleInfo> &scheduleInfos) {
269 HdiScheduleInfo scheduleInfo = {};
270 scheduleInfo.authType = HdiAuthType::FACE;
271 scheduleInfo.executorMatcher = 10;
272 scheduleInfo.executorIndexes.push_back(60);
273 scheduleInfo.scheduleId = 20;
274 scheduleInfo.scheduleMode = HdiScheduleMode::AUTH;
275 scheduleInfo.templateIds.push_back(30);
276 scheduleInfos.push_back(scheduleInfo);
277 return HDF_SUCCESS;
278 }
279 );
280 auto resourceNode = MockResourceNode::CreateWithExecuteIndex(executorIndex, FACE, ALL_IN_ONE);
281 EXPECT_NE(resourceNode, nullptr);
282 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
283 auto authentication = std::make_shared<AuthenticationImpl>(contextId, para);
284 EXPECT_NE(authentication, nullptr);
285 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
286 auto callback = Common::MakeShared<MockScheduleNodeCallback>();
287 EXPECT_NE(callback, nullptr);
288 EXPECT_FALSE(authentication->Start(scheduleList, callback));
289 EXPECT_FALSE(authentication->Cancel());
290
291 EXPECT_FALSE(authentication->Start(scheduleList, callback));
292 EXPECT_FALSE(authentication->Cancel());
293
294 EXPECT_TRUE(ResourceNodePool::Instance().Delete(executorIndex));
295 }
296 } // namespace UserAuth
297 } // namespace UserIam
298 } // namespace OHOS