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 <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
26 namespace OHOS {
27 namespace UserIam {
28 namespace UserAuth {
29 using namespace testing;
30 using namespace testing::ext;
31
32 using HdiAuthResultInfo = OHOS::HDI::UserAuth::V1_0::AuthResultInfo;
33 using HdiAuthSolution = OHOS::HDI::UserAuth::V1_0::AuthSolution;
34 using HdiExecutorSendMsg = OHOS::HDI::UserAuth::V1_0::ExecutorSendMsg;
35 using HdiAuthType = OHOS::HDI::UserAuth::V1_0::AuthType;
36 using HdiEnrollParam = OHOS::HDI::UserAuth::V1_0::EnrollParam;
37 using HdiExecutorInfo = OHOS::HDI::UserAuth::V1_0::ExecutorInfo;
38 using HdiScheduleInfo = OHOS::HDI::UserAuth::V1_0::ScheduleInfo;
39 using HdiExecutorRole = OHOS::HDI::UserAuth::V1_0::ExecutorRole;
40 using HdiScheduleMode = OHOS::HDI::UserAuth::V1_0::ScheduleMode;
41 using HdiExecutorSecureLevel = OHOS::HDI::UserAuth::V1_0::ExecutorSecureLevel;
42
43 class AuthenticationImplTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46
47 static void TearDownTestCase();
48
49 void SetUp() override;
50
51 void TearDown() override;
52 };
53
SetUpTestCase()54 void AuthenticationImplTest::SetUpTestCase()
55 {
56 }
57
TearDownTestCase()58 void AuthenticationImplTest::TearDownTestCase()
59 {
60 }
61
SetUp()62 void AuthenticationImplTest::SetUp()
63 {
64 MockIUserAuthInterface::Holder::GetInstance().Reset();
65 }
66
TearDown()67 void AuthenticationImplTest::TearDown()
68 {
69 MockIUserAuthInterface::Holder::GetInstance().Reset();
70 }
71
72 HWTEST_F(AuthenticationImplTest, AuthenticationHdiError, TestSize.Level0)
73 {
74 constexpr uint64_t contextId = 0x1234567;
75 constexpr int32_t userId = 0x11;
76
77 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
78 EXPECT_CALL(*mock, BeginAuthentication(contextId, _, _)).WillRepeatedly(Return(1));
79
80 auto authentication = std::make_shared<AuthenticationImpl>(contextId, userId, FACE, ATL3);
81 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
82 EXPECT_FALSE(authentication->Start(scheduleList, nullptr));
83 }
84
85 HWTEST_F(AuthenticationImplTest, AuthenticationHdiEmpty, TestSize.Level0)
86 {
87 constexpr uint64_t contextId = 0x1234567;
88 constexpr int32_t userId = 0x11;
89
90 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
91 EXPECT_CALL(*mock, BeginAuthentication(contextId, _, _)).WillRepeatedly(Return(0));
92
93 auto authentication = std::make_shared<AuthenticationImpl>(contextId, userId, FACE, ATL3);
94 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
95 EXPECT_FALSE(authentication->Start(scheduleList, nullptr));
96 }
97
98 HWTEST_F(AuthenticationImplTest, AuthenticationInvalidExecutor, TestSize.Level0)
99 {
100 using ScheduleInfo = OHOS::HDI::UserAuth::V1_0::ScheduleInfo;
101 using ExecutorInfo = OHOS::HDI::UserAuth::V1_0::ExecutorInfo;
102
103 constexpr uint64_t contextId = 0x1234567;
104 constexpr int32_t userId = 0x11;
105 constexpr int32_t executorInfoIndex = 0x100;
106 constexpr int32_t scheduleId = 0x1122;
107
__anon532dcded0102(std::vector<ScheduleInfo> &scheduleInfos) 108 auto fillInfoList = [](std::vector<ScheduleInfo> &scheduleInfos) {
109 ExecutorInfo executorInfo;
110 executorInfo.executorIndex = executorInfoIndex;
111
112 ScheduleInfo scheduleInfo;
113
114 scheduleInfo.scheduleId = scheduleId;
115 scheduleInfo.templateIds = {0, 1, 2};
116 scheduleInfo.authType = OHOS::HDI::UserAuth::V1_0::FACE;
117 scheduleInfo.executorMatcher = 0;
118 scheduleInfo.scheduleMode = OHOS::HDI::UserAuth::V1_0::ENROLL;
119 scheduleInfo.executors.push_back(executorInfo);
120
121 std::vector<ScheduleInfo> list;
122 list.emplace_back(scheduleInfo);
123
124 scheduleInfos.swap(list);
125 };
126
127 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
128 EXPECT_CALL(*mock, BeginAuthentication(contextId, _, _)).WillRepeatedly(DoAll(WithArg<2>(fillInfoList), Return(0)));
129
130 auto authentication = std::make_shared<AuthenticationImpl>(contextId, userId, FACE, ATL3);
131 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
132 EXPECT_FALSE(authentication->Start(scheduleList, nullptr));
133 }
134
135 HWTEST_F(AuthenticationImplTest, AuthenticationImplTestUpdate001, TestSize.Level0)
136 {
137 constexpr uint64_t contextId = 54871;
138 constexpr int32_t userId = 1534;
139
140 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
141 EXPECT_NE(mockHdi, nullptr);
142 EXPECT_CALL(*mockHdi, UpdateAuthenticationResult(_, _, _)).Times(1);
143 ON_CALL(*mockHdi, UpdateAuthenticationResult)
144 .WillByDefault(
__anon532dcded0202(uint64_t contextId, const std::vector<uint8_t> &scheduleResult, HdiAuthResultInfo &info) 145 [](uint64_t contextId, const std::vector<uint8_t> &scheduleResult, HdiAuthResultInfo &info) {
146 info.result = HDF_SUCCESS;
147 return HDF_SUCCESS;
148 }
149 );
150
151 auto authentication = std::make_shared<AuthenticationImpl>(contextId, userId, FACE, ATL3);
152 EXPECT_NE(authentication, nullptr);
153 std::vector<uint8_t> scheduleResult;
154 Authentication::AuthResultInfo info = {};
155 EXPECT_TRUE(authentication->Update(scheduleResult, info));
156 }
157
158 HWTEST_F(AuthenticationImplTest, AuthenticationImplTestUpdate002, TestSize.Level0)
159 {
160 constexpr uint64_t contextId = 54871;
161 constexpr int32_t userId = 1534;
162
163 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
164 EXPECT_NE(mockHdi, nullptr);
165 EXPECT_CALL(*mockHdi, UpdateAuthenticationResult(_, _, _)).Times(1);
166 ON_CALL(*mockHdi, UpdateAuthenticationResult)
167 .WillByDefault(
__anon532dcded0302(uint64_t contextId, const std::vector<uint8_t> &scheduleResult, HdiAuthResultInfo &info) 168 [](uint64_t contextId, const std::vector<uint8_t> &scheduleResult, HdiAuthResultInfo &info) {
169 info.result = HDF_FAILURE;
170 HdiExecutorSendMsg msg = {};
171 msg.commandId = 10;
172 msg.executorIndex = 20;
173 info.msgs.push_back(msg);
174 return HDF_FAILURE;
175 }
176 );
177
178 auto authentication = std::make_shared<AuthenticationImpl>(contextId, userId, FACE, ATL3);
179 EXPECT_NE(authentication, nullptr);
180 std::vector<uint8_t> scheduleResult;
181 Authentication::AuthResultInfo info = {};
182 EXPECT_FALSE(authentication->Update(scheduleResult, info));
183 }
184
185 HWTEST_F(AuthenticationImplTest, AuthenticationImplTestStart, TestSize.Level0)
186 {
187 constexpr uint64_t contextId = 34567;
188 constexpr uint64_t userId = 25781;
189 constexpr uint64_t executorIndex = 60;
190
191 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
192 EXPECT_NE(mockHdi, nullptr);
193 EXPECT_CALL(*mockHdi, CancelAuthentication(_))
194 .Times(2)
195 .WillOnce(Return(HDF_SUCCESS))
196 .WillOnce(Return(HDF_FAILURE));
197 EXPECT_CALL(*mockHdi, BeginAuthentication(_, _, _))
198 .WillRepeatedly(
__anon532dcded0402(uint64_t contextId, const HdiAuthSolution ¶m, std::vector<HdiScheduleInfo> &scheduleInfos) 199 [](uint64_t contextId, const HdiAuthSolution ¶m, std::vector<HdiScheduleInfo> &scheduleInfos) {
200 HdiScheduleInfo scheduleInfo = {};
201 scheduleInfo.authType = HdiAuthType::FACE;
202 scheduleInfo.executorMatcher = 10;
203 HdiExecutorInfo executorInfo = {};
204 executorInfo.executorIndex = 60;
205 executorInfo.info.authType = HdiAuthType::FACE;
206 executorInfo.info.esl = HdiExecutorSecureLevel::ESL1;
207 executorInfo.info.executorMatcher = 10;
208 executorInfo.info.executorRole = HdiExecutorRole::ALL_IN_ONE;
209 executorInfo.info.executorSensorHint = 90;
210 executorInfo.info.publicKey = {1, 2, 3, 4};
211 scheduleInfo.executors.push_back(executorInfo);
212 scheduleInfo.scheduleId = 20;
213 scheduleInfo.scheduleMode = HdiScheduleMode::AUTH;
214 scheduleInfo.templateIds.push_back(30);
215 scheduleInfos.push_back(scheduleInfo);
216 return HDF_SUCCESS;
217 }
218 );
219 auto resourceNode = MockResourceNode::CreateWithExecuteIndex(executorIndex, FACE, ALL_IN_ONE);
220 EXPECT_NE(resourceNode, nullptr);
221 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
222 auto authentication = std::make_shared<AuthenticationImpl>(contextId, userId, FACE, ATL3);
223 EXPECT_NE(authentication, nullptr);
224 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
225 auto callback = Common::MakeShared<MockScheduleNodeCallback>();
226 EXPECT_NE(callback, nullptr);
227 EXPECT_TRUE(authentication->Start(scheduleList, callback));
228 EXPECT_TRUE(authentication->Cancel());
229
230 EXPECT_TRUE(authentication->Start(scheduleList, callback));
231 EXPECT_FALSE(authentication->Cancel());
232
233 EXPECT_TRUE(ResourceNodePool::Instance().Delete(executorIndex));
234 }
235 } // namespace UserAuth
236 } // namespace UserIam
237 } // namespace OHOS