1 /*
2 * Copyright (c) 2024-2024 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 "hdf_base.h"
19 #include "iam_logger.h"
20 #include "iam_ptr.h"
21
22 #include "pin_auth_collector_hdi.h"
23 #include "iam_common_defines.h"
24 #include "mock_iexecute_callback.h"
25 #include "mock_icollector_executor.h"
26
27 #define LOG_TAG "PIN_AUTH_SA"
28
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace OHOS::UserIam::Common;
32
33 namespace OHOS {
34 namespace UserIam {
35 namespace PinAuth {
36 using IamResultCode = OHOS::UserIam::UserAuth::ResultCode;
37 using IamExecutorRole = OHOS::UserIam::UserAuth::ExecutorRole;
38 using IamExecutorInfo = OHOS::UserIam::UserAuth::ExecutorInfo;
39 using IamAuthType = OHOS::UserIam::UserAuth::AuthType;
40 using IamExecutorSecureLevel = OHOS::UserIam::UserAuth::ExecutorSecureLevel;
41 using IamPropertyMode = OHOS::UserIam::UserAuth::PropertyMode;
42 namespace {
43 static const std::map<HDF_STATUS, IamResultCode> RESULT_CODE_MAP = {
44 {HDF_SUCCESS, UserAuth::ResultCode::SUCCESS},
45 {HDF_FAILURE, UserAuth::ResultCode::GENERAL_ERROR},
46 {HDF_ERR_TIMEOUT, UserAuth::ResultCode::TIMEOUT},
47 {HDF_ERR_QUEUE_FULL, UserAuth::ResultCode::BUSY},
48 {HDF_ERR_DEVICE_BUSY, UserAuth::ResultCode::BUSY},
49 {HDF_ERR_INVALID_PARAM, UserAuth::ResultCode::INVALID_PARAMETERS},
50 };
51 }
52
53 class PinAuthCollectorHdiUnitTest : public testing::Test {
54 public:
55 static void SetUpTestCase();
56 static void TearDownTestCase();
57 void SetUp();
58 void TearDown();
59 };
60
SetUpTestCase()61 void PinAuthCollectorHdiUnitTest::SetUpTestCase()
62 {
63 }
64
TearDownTestCase()65 void PinAuthCollectorHdiUnitTest::TearDownTestCase()
66 {
67 }
68
SetUp()69 void PinAuthCollectorHdiUnitTest::SetUp()
70 {
71 }
72
TearDown()73 void PinAuthCollectorHdiUnitTest::TearDown()
74 {
75 }
76
77 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_GetExecutorInfo_001, TestSize.Level0)
78 {
79 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
80 ASSERT_TRUE(executorProxy != nullptr);
__anon15fe304d0202(ExecutorInfo &info) 81 EXPECT_CALL(*executorProxy, GetExecutorInfo(_)).Times(Exactly(1)).WillOnce([](ExecutorInfo &info) {
82 info = {
83 .executorRole = ExecutorRole::COLLECTOR,
84 .authType = AuthType::PIN,
85 .esl = ExecutorSecureLevel::ESL0,
86 };
87 return HDF_SUCCESS;
88 });
89 PinAuthCollectorHdi collectorHdi(executorProxy);
90 IamExecutorInfo info = {};
91 auto ret = collectorHdi.GetExecutorInfo(info);
92 EXPECT_TRUE(info.executorRole == IamExecutorRole::COLLECTOR);
93 EXPECT_TRUE(info.authType == IamAuthType::PIN);
94 EXPECT_TRUE(info.esl == IamExecutorSecureLevel::ESL0);
95 EXPECT_TRUE(ret == IamResultCode::SUCCESS);
96 }
97
98 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_GetExecutorInfo_002, TestSize.Level0)
99 {
100 for (const auto &pair : RESULT_CODE_MAP) {
101 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
102 ASSERT_TRUE(executorProxy != nullptr);
103 EXPECT_CALL(*executorProxy, GetExecutorInfo(_))
104 .Times(Exactly(1))
__anon15fe304d0302(ExecutorInfo &info) 105 .WillOnce([&pair](ExecutorInfo &info) {
106 info = {
107 .executorRole = ExecutorRole::COLLECTOR,
108 .authType = AuthType::PIN,
109 .esl = ExecutorSecureLevel::ESL0,
110 };
111 return static_cast<int32_t>(pair.first);
112 });
113 PinAuthCollectorHdi collectorHdi(executorProxy);
114 IamExecutorInfo info = {};
115 auto ret = collectorHdi.GetExecutorInfo(info);
116 EXPECT_TRUE(ret == pair.second);
117 }
118 }
119
120 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_GetExecutorInfo_003, TestSize.Level0)
121 {
122 static const std::map<AuthType, pair<IamAuthType, IamResultCode>> data = {
123 {AuthType::PIN, {IamAuthType::PIN, IamResultCode::SUCCESS}},
124 {static_cast<AuthType>(AuthType::PIN + 1),
125 {IamAuthType::PIN, IamResultCode::GENERAL_ERROR}},
126 {static_cast<AuthType>(AuthType::PIN - 1),
127 {IamAuthType::PIN, IamResultCode::GENERAL_ERROR}},
128 };
129 for (const auto &pair : data) {
130 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
131 ASSERT_TRUE(executorProxy != nullptr);
132 EXPECT_CALL(*executorProxy, GetExecutorInfo(_))
133 .Times(Exactly(1))
__anon15fe304d0402(ExecutorInfo &info) 134 .WillOnce([&pair](ExecutorInfo &info) {
135 info = {
136 .executorRole = ExecutorRole::COLLECTOR,
137 .authType = pair.first,
138 .esl = ExecutorSecureLevel::ESL0,
139 };
140 return HDF_SUCCESS;
141 });
142 PinAuthCollectorHdi collectorHdi(executorProxy);
143 IamExecutorInfo info = {};
144 auto ret = collectorHdi.GetExecutorInfo(info);
145 EXPECT_TRUE(ret == pair.second.second);
146 if (ret == IamResultCode::SUCCESS) {
147 EXPECT_TRUE(info.authType == pair.second.first);
148 }
149 }
150 }
151
152 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_GetExecutorInfo_004, TestSize.Level0)
153 {
154 static const std::map<ExecutorRole, pair<IamExecutorRole, IamResultCode>> data = {
155 {ExecutorRole::COLLECTOR, {IamExecutorRole::COLLECTOR, IamResultCode::SUCCESS}},
156 {ExecutorRole::VERIFIER, {IamExecutorRole::VERIFIER, IamResultCode::SUCCESS}},
157 {ExecutorRole::ALL_IN_ONE, {IamExecutorRole::ALL_IN_ONE, IamResultCode::SUCCESS}},
158 {static_cast<ExecutorRole>(ExecutorRole::COLLECTOR - 1),
159 {IamExecutorRole::COLLECTOR, IamResultCode::GENERAL_ERROR}},
160 {static_cast<ExecutorRole>(ExecutorRole::COLLECTOR + 1),
161 {IamExecutorRole::COLLECTOR, IamResultCode::GENERAL_ERROR}},
162 };
163 for (const auto &pair : data) {
164 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
165 ASSERT_TRUE(executorProxy != nullptr);
166 EXPECT_CALL(*executorProxy, GetExecutorInfo(_))
167 .Times(Exactly(1))
__anon15fe304d0502(ExecutorInfo &info) 168 .WillOnce([&pair](ExecutorInfo &info) {
169 info = {
170 .executorRole = pair.first,
171 .authType = AuthType::PIN,
172 .esl = ExecutorSecureLevel::ESL0,
173 };
174 return HDF_SUCCESS;
175 });
176 PinAuthCollectorHdi collectorHdi(executorProxy);
177 IamExecutorInfo info = {};
178 auto ret = collectorHdi.GetExecutorInfo(info);
179 EXPECT_TRUE(ret == pair.second.second);
180 if (ret == IamResultCode::SUCCESS) {
181 EXPECT_TRUE(info.executorRole == pair.second.first);
182 }
183 }
184 }
185
186 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_GetExecutorInfo_005, TestSize.Level0)
187 {
188 static const std::map<ExecutorSecureLevel, pair<IamExecutorSecureLevel, IamResultCode>> data =
189 {
190 {ExecutorSecureLevel::ESL0, {IamExecutorSecureLevel::ESL0, IamResultCode::SUCCESS}},
191 {ExecutorSecureLevel::ESL1, {IamExecutorSecureLevel::ESL1, IamResultCode::SUCCESS}},
192 {ExecutorSecureLevel::ESL2, {IamExecutorSecureLevel::ESL2, IamResultCode::SUCCESS}},
193 {ExecutorSecureLevel::ESL3, {IamExecutorSecureLevel::ESL3, IamResultCode::SUCCESS}},
194 {static_cast<ExecutorSecureLevel>(ExecutorSecureLevel::ESL0 - 1),
195 {IamExecutorSecureLevel::ESL3, IamResultCode::GENERAL_ERROR}},
196 {static_cast<ExecutorSecureLevel>(ExecutorSecureLevel::ESL3 + 1),
197 {IamExecutorSecureLevel::ESL3, IamResultCode::GENERAL_ERROR}},
198 };
199 for (const auto &pair : data) {
200 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
201 ASSERT_TRUE(executorProxy != nullptr);
202 EXPECT_CALL(*executorProxy, GetExecutorInfo(_))
203 .Times(Exactly(1))
__anon15fe304d0602(ExecutorInfo &info) 204 .WillOnce([&pair](ExecutorInfo &info) {
205 info = {
206 .executorRole = ExecutorRole::COLLECTOR,
207 .authType = AuthType::PIN,
208 .esl = pair.first,
209 };
210 return HDF_SUCCESS;
211 });
212 PinAuthCollectorHdi collectorHdi(executorProxy);
213 IamExecutorInfo info = {};
214 auto ret = collectorHdi.GetExecutorInfo(info);
215 EXPECT_TRUE(ret == pair.second.second);
216 if (ret == IamResultCode::SUCCESS) {
217 EXPECT_TRUE(info.esl == pair.second.first);
218 }
219 }
220 }
221
222 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_GetExecutorInfo_006, TestSize.Level0)
223 {
224 PinAuthCollectorHdi collectorHdi(nullptr);
225 IamExecutorInfo info = {};
226 auto ret = collectorHdi.GetExecutorInfo(info);
227 EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
228 }
229
230 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_OnRegisterFinish_001, TestSize.Level0)
231 {
232 for (const auto &pair : RESULT_CODE_MAP) {
233 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
234 ASSERT_TRUE(executorProxy != nullptr);
235 EXPECT_CALL(*executorProxy, OnRegisterFinish(_, _, _))
236 .Times(Exactly(1))
237 .WillOnce(
238 [&pair](const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey,
__anon15fe304d0702(const std::vector<uint64_t> &templateIdList, const std::vector<uint8_t> &frameworkPublicKey, const std::vector<uint8_t> &extraInfo) 239 const std::vector<uint8_t> &extraInfo) { return pair.first; });
240 PinAuthCollectorHdi collectorHdi(executorProxy);
241 auto ret =
242 collectorHdi.OnRegisterFinish(std::vector<uint64_t>(), std::vector<uint8_t>(), std::vector<uint8_t>());
243 EXPECT_TRUE(ret == pair.second);
244 }
245 }
246
247 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_OnRegisterFinish_002, TestSize.Level0)
248 {
249 PinAuthCollectorHdi collectorHdi(nullptr);
250 auto ret = collectorHdi.OnRegisterFinish(std::vector<uint64_t>(), std::vector<uint8_t>(), std::vector<uint8_t>());
251 EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
252 }
253
254 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_OnSetData_001, TestSize.Level0)
255 {
256 for (const auto &pair : RESULT_CODE_MAP) {
257 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
258 ASSERT_TRUE(executorProxy != nullptr);
259 EXPECT_CALL(*executorProxy, SetData(_, _, _, _))
260 .Times(Exactly(1))
261 .WillOnce([&pair](uint64_t scheduleId, uint64_t authSubType, const std::vector<uint8_t>& data,
262 int32_t resultCode)
__anon15fe304d0802(uint64_t scheduleId, uint64_t authSubType, const std::vector<uint8_t>& data, int32_t resultCode) 263 { return pair.first; });
264 PinAuthCollectorHdi collectorHdi(executorProxy);
265 auto ret = collectorHdi.OnSetData(0, 0, std::vector<uint8_t>(), 0);
266 EXPECT_TRUE(ret == pair.second);
267 }
268 }
269
270 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_OnSetData_002, TestSize.Level0)
271 {
272 PinAuthCollectorHdi collectorHdi(nullptr);
273 auto ret = collectorHdi.OnSetData(0, 0, std::vector<uint8_t>(), 0);
274 EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
275 }
276
277 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_Cancel_001, TestSize.Level0)
278 {
279 for (const auto &pair : RESULT_CODE_MAP) {
280 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
281 ASSERT_TRUE(executorProxy != nullptr);
__anon15fe304d0902(uint64_t scheduleId) 282 EXPECT_CALL(*executorProxy, Cancel(_)).Times(Exactly(1)).WillOnce([&pair](uint64_t scheduleId) {
283 return pair.first;
284 });
285 PinAuthCollectorHdi collectorHdi(executorProxy);
286 auto ret = collectorHdi.Cancel(0);
287 EXPECT_TRUE(ret == pair.second);
288 }
289 }
290
291 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_Cancel_002, TestSize.Level0)
292 {
293 PinAuthCollectorHdi collectorHdi(nullptr);
294 auto ret = collectorHdi.Cancel(0);
295 EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
296 }
297
298 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_SendMessage_001, TestSize.Level0)
299 {
300 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
301 ASSERT_TRUE(executorProxy != nullptr);
302 PinAuthCollectorHdi collectorHdi(executorProxy);
303 std::vector<uint8_t> data;
304 auto ret = collectorHdi.SendMessage(1, 1, data);
305 EXPECT_TRUE(ret == IamResultCode::SUCCESS);
306 }
307
308 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_SendMessage_002, TestSize.Level0)
309 {
310 PinAuthCollectorHdi collectorHdi(nullptr);
311 std::vector<uint8_t> data;
312 auto ret = collectorHdi.SendMessage(1, 1, data);
313 EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
314 }
315
316 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_Collect_001, TestSize.Level0)
317 {
318 for (const auto &pair : RESULT_CODE_MAP) {
319 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
320 ASSERT_TRUE(executorProxy != nullptr);
321 EXPECT_CALL(*executorProxy, Collect(_, _, _))
322 .Times(Exactly(1))
323 .WillOnce([&pair](uint64_t scheduleId, const std::vector<uint8_t> &extraInfo,
__anon15fe304d0a02(uint64_t scheduleId, const std::vector<uint8_t> &extraInfo, const sptr<IExecutorCallback> &callbackObj) 324 const sptr<IExecutorCallback> &callbackObj) { return pair.first; });
325 auto collectorHdi = MakeShared<PinAuthCollectorHdi>(executorProxy);
326 ASSERT_TRUE(collectorHdi != nullptr);
327 auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
328 ASSERT_TRUE(executeCallback != nullptr);
329 auto ret = collectorHdi->Collect(0, UserAuth::CollectParam{0, 0, std::vector<uint8_t>()}, executeCallback);
330 EXPECT_TRUE(ret == pair.second);
331 }
332 }
333
334 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_Collect_002, TestSize.Level0)
335 {
336 auto executorProxy = new (std::nothrow) MockICollectorExecutor();
337 ASSERT_TRUE(executorProxy != nullptr);
338 auto collectorHdi = MakeShared<PinAuthCollectorHdi>(executorProxy);
339 ASSERT_TRUE(collectorHdi != nullptr);
340 auto ret = collectorHdi->Collect(0, UserAuth::CollectParam{0, 0, std::vector<uint8_t>()}, nullptr);
341 EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
342 }
343
344 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_Collect_003, TestSize.Level0)
345 {
346 auto collectorHdi = MakeShared<PinAuthCollectorHdi>(nullptr);
347 ASSERT_TRUE(collectorHdi != nullptr);
348 auto executeCallback = MakeShared<UserIam::UserAuth::MockIExecuteCallback>();
349 ASSERT_TRUE(executeCallback != nullptr);
350 auto ret = collectorHdi->Collect(0, UserAuth::CollectParam{0, 0, std::vector<uint8_t>()}, executeCallback);
351 EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
352 }
353
354 HWTEST_F(PinAuthCollectorHdiUnitTest, PinAuthCollectorExecutorHdi_Collect_004, TestSize.Level0)
355 {
356 PinAuthCollectorHdi collectorHdi(nullptr);
357 auto ret = collectorHdi.Collect(0, UserAuth::CollectParam{0, 0, std::vector<uint8_t>()}, nullptr);
358 EXPECT_TRUE(ret == IamResultCode::GENERAL_ERROR);
359 }
360 } // namespace PinAuth
361 } // namespace UserIam
362 } // namespace OHOS
363