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 #include <memory>
16
17 #include "iam_ptr.h"
18
19 #include "identification_impl.h"
20 #include "mock_iuser_auth_interface.h"
21 #include "mock_resource_node.h"
22 #include "mock_schedule_node_callback.h"
23 #include "resource_node_pool.h"
24
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
28 using namespace testing;
29 using namespace testing::ext;
30
31 using HdiScheduleInfo = OHOS::HDI::UserAuth::V1_0::ScheduleInfo;
32 using HdiScheduleMode = OHOS::HDI::UserAuth::V1_0::ScheduleMode;
33 using HdiAuthType = OHOS::HDI::UserAuth::V1_0::AuthType;
34 using HdiExecutorInfo = OHOS::HDI::UserAuth::V1_0::ExecutorInfo;
35 using HdiExecutorSecureLevel = OHOS::HDI::UserAuth::V1_0::ExecutorSecureLevel;
36 using HdiExecutorRole = OHOS::HDI::UserAuth::V1_0::ExecutorRole;
37
38 class IdentificationImplTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41
42 static void TearDownTestCase();
43
44 void SetUp() override;
45
46 void TearDown() override;
47 };
48
SetUpTestCase()49 void IdentificationImplTest::SetUpTestCase()
50 {
51 }
52
TearDownTestCase()53 void IdentificationImplTest::TearDownTestCase()
54 {
55 }
56
SetUp()57 void IdentificationImplTest::SetUp()
58 {
59 MockIUserAuthInterface::Holder::GetInstance().Reset();
60 }
61
TearDown()62 void IdentificationImplTest::TearDown()
63 {
64 MockIUserAuthInterface::Holder::GetInstance().Reset();
65 }
66
67 HWTEST_F(IdentificationImplTest, IdentificationHdiError, TestSize.Level0)
68 {
69 constexpr uint64_t contextId = 0x1234567;
70 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
71 EXPECT_CALL(*mock, BeginIdentification(contextId, _, _, _, _)).WillRepeatedly(Return(1));
72
73 auto identification = std::make_shared<IdentificationImpl>(contextId, FACE);
74 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
75 EXPECT_FALSE(identification->Start(scheduleList, nullptr));
76 }
77
78 HWTEST_F(IdentificationImplTest, IdentificationHdiEmpty, TestSize.Level0)
79 {
80 constexpr uint64_t contextId = 0x1234567;
81 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
82 EXPECT_CALL(*mock, BeginIdentification(contextId, _, _, _, _)).WillRepeatedly(Return(0));
83
84 auto enrollment = std::make_shared<IdentificationImpl>(contextId, FACE);
85 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
86 EXPECT_FALSE(enrollment->Start(scheduleList, nullptr));
87 }
88
89 HWTEST_F(IdentificationImplTest, IdentificationUpdateHdiError, TestSize.Level0)
90 {
91 constexpr uint64_t contextId = 0x1234567;
92 using HdiIdentifyResultInfo = OHOS::HDI::UserAuth::V1_0::IdentifyResultInfo;
93 HdiIdentifyResultInfo info;
94 std::vector<uint8_t> scheduleResult = {1, 2, 3};
95 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
96 EXPECT_CALL(*mock, UpdateIdentificationResult(contextId, _, _)).WillRepeatedly(Return(1));
97 auto identification = std::make_shared<IdentificationImpl>(contextId, FACE);
98 Identification::IdentifyResultInfo retInfo = {};
99 EXPECT_FALSE(identification->Update(scheduleResult, retInfo));
100 }
101
102 HWTEST_F(IdentificationImplTest, IdentificationUpdateHdiSuccessful, TestSize.Level0)
103 {
104 constexpr uint64_t contextId = 0x1234567;
105 using HdiIdentifyResultInfo = OHOS::HDI::UserAuth::V1_0::IdentifyResultInfo;
__anon14c404180102(HdiIdentifyResultInfo &infoRet) 106 auto fillUpInfos = [](HdiIdentifyResultInfo &infoRet) {
107 constexpr int32_t userId = 0x11;
108 const std::vector<uint8_t> token = {1, 2, 3, 4, 5, 6};
109 HdiIdentifyResultInfo info = {
110 .result = 0,
111 .userId = userId,
112 .token = token,
113 };
114 infoRet = info;
115 };
116 std::vector<uint8_t> scheduleResult = {1, 2, 3};
117 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
118 EXPECT_CALL(*mock, UpdateIdentificationResult(contextId, _, _))
119 .WillRepeatedly(DoAll(WithArg<2>(fillUpInfos), Return(0)));
120 auto identification = std::make_shared<IdentificationImpl>(contextId, FACE);
121 Identification::IdentifyResultInfo retInfo = {};
122 EXPECT_TRUE(identification->Update(scheduleResult, retInfo));
123
124 // test IdentifyResultInfo
125 EXPECT_EQ(retInfo.result, 0);
126 EXPECT_EQ(retInfo.userId, 0x11);
127 EXPECT_THAT(retInfo.token, ElementsAre(1, 2, 3, 4, 5, 6));
128 }
129
130 HWTEST_F(IdentificationImplTest, IdentificationTestStart, TestSize.Level0)
131 {
132 constexpr uint64_t contextId = 34567;
133 constexpr uint64_t executorIndex = 60;
134
135 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
136 EXPECT_NE(mockHdi, nullptr);
137 EXPECT_CALL(*mockHdi, CancelIdentification(_))
138 .Times(2)
139 .WillOnce(Return(HDF_SUCCESS))
140 .WillOnce(Return(HDF_FAILURE));
141 EXPECT_CALL(*mockHdi, BeginIdentification(_, _, _, _, _))
142 .WillRepeatedly(
143 [](uint64_t contextId, HdiAuthType authType, const std::vector<uint8_t> &challenge, uint32_t executorId,
__anon14c404180202(uint64_t contextId, HdiAuthType authType, const std::vector<uint8_t> &challenge, uint32_t executorId, HdiScheduleInfo &scheduleInfo) 144 HdiScheduleInfo &scheduleInfo) {
145 scheduleInfo.authType = HdiAuthType::FACE;
146 scheduleInfo.executorMatcher = 10;
147 HdiExecutorInfo executorInfo = {};
148 executorInfo.executorIndex = 60;
149 executorInfo.info.authType = HdiAuthType::FACE;
150 executorInfo.info.esl = HdiExecutorSecureLevel::ESL1;
151 executorInfo.info.executorMatcher = 10;
152 executorInfo.info.executorRole = HdiExecutorRole::ALL_IN_ONE;
153 executorInfo.info.executorSensorHint = 90;
154 executorInfo.info.publicKey = {1, 2, 3, 4};
155 scheduleInfo.executors.push_back(executorInfo);
156 scheduleInfo.scheduleId = 20;
157 scheduleInfo.scheduleMode = HdiScheduleMode::IDENTIFY;
158 scheduleInfo.templateIds.push_back(30);
159 return HDF_SUCCESS;
160 }
161 );
162 auto resourceNode = MockResourceNode::CreateWithExecuteIndex(executorIndex, FACE, ALL_IN_ONE);
163 EXPECT_NE(resourceNode, nullptr);
164 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
165 auto identification = std::make_shared<IdentificationImpl>(contextId, FACE);
166 EXPECT_NE(identification, nullptr);
167 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
168 auto callback = Common::MakeShared<MockScheduleNodeCallback>();
169 EXPECT_NE(callback, nullptr);
170 EXPECT_TRUE(identification->Start(scheduleList, callback));
171 EXPECT_TRUE(identification->Cancel());
172
173 EXPECT_TRUE(identification->Start(scheduleList, callback));
174 EXPECT_FALSE(identification->Cancel());
175
176 EXPECT_TRUE(ResourceNodePool::Instance().Delete(executorIndex));
177 }
178 } // namespace UserAuth
179 } // namespace UserIam
180 } // namespace OHOS