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 "iam_ptr.h"
17
18 #include "credential_info_impl.h"
19 #include "enrollment_impl.h"
20 #include "resource_node_pool.h"
21 #include "mock_iuser_auth_interface.h"
22 #include "mock_resource_node.h"
23 #include "mock_schedule_node_callback.h"
24
25 namespace OHOS {
26 namespace UserIam {
27 namespace UserAuth {
28 using namespace testing;
29 using namespace testing::ext;
30
31 using HdiAuthType = OHOS::HDI::UserAuth::V1_0::AuthType;
32 using HdiEnrollParam = OHOS::HDI::UserAuth::V1_0::EnrollParam;
33 using HdiExecutorInfo = OHOS::HDI::UserAuth::V1_0::ExecutorInfo;
34 using HdiScheduleInfo = OHOS::HDI::UserAuth::V1_0::ScheduleInfo;
35 using HdiExecutorRole = OHOS::HDI::UserAuth::V1_0::ExecutorRole;
36 using HdiScheduleMode = OHOS::HDI::UserAuth::V1_0::ScheduleMode;
37 using HdiExecutorSecureLevel = OHOS::HDI::UserAuth::V1_0::ExecutorSecureLevel;
38 class EnrollmentImplTest : 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 EnrollmentImplTest::SetUpTestCase()
50 {
51 }
52
TearDownTestCase()53 void EnrollmentImplTest::TearDownTestCase()
54 {
55 }
56
SetUp()57 void EnrollmentImplTest::SetUp()
58 {
59 MockIUserAuthInterface::Holder::GetInstance().Reset();
60 }
61
TearDown()62 void EnrollmentImplTest::TearDown()
63 {
64 MockIUserAuthInterface::Holder::GetInstance().Reset();
65 }
66
67 HWTEST_F(EnrollmentImplTest, EnrollmentHdiError, TestSize.Level0)
68 {
69 constexpr int32_t userId = 0x11;
70 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
71 EXPECT_CALL(*mock, BeginEnrollment(userId, _, _, _)).WillRepeatedly(Return(1));
72
73 auto enrollment = std::make_shared<EnrollmentImpl>(userId, FACE);
74 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
75 EXPECT_FALSE(enrollment->Start(scheduleList, nullptr));
76 }
77
78 HWTEST_F(EnrollmentImplTest, EnrollmentHdiEmpty, TestSize.Level0)
79 {
80 constexpr int32_t userId = 0x11;
81
82 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
83 EXPECT_CALL(*mock, BeginEnrollment(userId, _, _, _)).WillRepeatedly(Return(0));
84
85 auto enroll = std::make_shared<EnrollmentImpl>(userId, FACE);
86 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
87 EXPECT_FALSE(enroll->Start(scheduleList, nullptr));
88 }
89
90 /**
91 * @tc.name: enroll_update
92 * @tc.desc: verify hdi enroll
93 * @tc.type: FUNC
94 * @tc.require: issueI5NXMW
95 */
96 HWTEST_F(EnrollmentImplTest, EnrollmentUpdateHdiError, TestSize.Level0)
97 {
98 constexpr int32_t userId = 0x11;
99
100 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
101 EXPECT_CALL(*mock, UpdateEnrollmentResult(userId, _, _)).WillRepeatedly(Return(1));
102
103 auto enroll = std::make_shared<EnrollmentImpl>(userId, FACE);
104 std::vector<uint8_t> scheduleResult = {1, 2, 3};
105 uint64_t credentialId = 0;
106 std::shared_ptr<CredentialInfo> info = nullptr;
107 std::vector<uint8_t> rootSecret;
108 EXPECT_FALSE(enroll->Update(scheduleResult, credentialId, info, rootSecret));
109 }
110
111 HWTEST_F(EnrollmentImplTest, EnrollmentUpdateHdiSuccessful, TestSize.Level0)
112 {
113 using HdiCredentialInfo = OHOS::HDI::UserAuth::V1_0::CredentialInfo;
114 using HdiEnrollResultInfo = OHOS::HDI::UserAuth::V1_0::EnrollResultInfo;
115 constexpr int32_t userId = 0x11;
116 constexpr uint64_t credentialIdRet = 0x12;
117 std::vector<uint8_t> scheduleResult = {1, 2, 3};
118
119 auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
__anonbac0b80e0102(HdiEnrollResultInfo &infoRet) 120 auto fillUpInfos = [](HdiEnrollResultInfo &infoRet) {
121 HdiCredentialInfo oldInfo = {
122 .credentialId = 1,
123 .executorIndex = 2,
124 .templateId = 3,
125 .authType = static_cast<OHOS::HDI::UserAuth::V1_0::AuthType>(0),
126 .executorMatcher = 5,
127 .executorSensorHint = 6,
128 };
129 HdiEnrollResultInfo info = {
130 .credentialId = credentialIdRet,
131 .oldInfo = oldInfo,
132 };
133 infoRet = info;
134 };
135 EXPECT_CALL(*mock, UpdateEnrollmentResult(userId, _, _)).WillRepeatedly(DoAll(WithArg<2>(fillUpInfos), Return(0)));
136
137 auto enroll = std::make_shared<EnrollmentImpl>(userId, FACE);
138 HdiCredentialInfo oldInfo = {};
139 std::shared_ptr<CredentialInfo> info = std::make_shared<CredentialInfoImpl>(userId, oldInfo);
140 uint64_t credentialId = 0;
141 std::vector<uint8_t> rootSecret;
142 EXPECT_TRUE(enroll->Update(scheduleResult, credentialId, info, rootSecret));
143
144 // test return values
145 EXPECT_EQ(credentialId, credentialIdRet);
146 EXPECT_EQ(info->GetCredentialId(), 1U);
147 EXPECT_EQ(info->GetAuthType(), static_cast<AuthType>(0));
148 EXPECT_EQ(info->GetExecutorIndex(), 2U);
149 EXPECT_EQ(info->GetUserId(), 0x11);
150 EXPECT_EQ(info->GetTemplateId(), 3U);
151 EXPECT_EQ(info->GetExecutorMatcher(), 5U);
152 EXPECT_EQ(info->GetExecutorSensorHint(), 6U);
153 }
154
155 HWTEST_F(EnrollmentImplTest, EnrollmentImplTestStart, TestSize.Level0)
156 {
157 constexpr uint64_t userId = 34567;
158 constexpr uint64_t executorIndex = 60;
159
160 auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
161 EXPECT_NE(mockHdi, nullptr);
162 EXPECT_CALL(*mockHdi, CancelEnrollment(_))
163 .Times(2)
164 .WillOnce(Return(HDF_SUCCESS))
165 .WillOnce(Return(HDF_FAILURE));
166 EXPECT_CALL(*mockHdi, BeginEnrollment(_, _, _, _))
167 .WillRepeatedly(
168 [](int32_t userId, const std::vector<uint8_t> &authToken, const HdiEnrollParam ¶m,
__anonbac0b80e0202(int32_t userId, const std::vector<uint8_t> &authToken, const HdiEnrollParam ¶m, HdiScheduleInfo &info) 169 HdiScheduleInfo &info) {
170 info.authType = HdiAuthType::FACE;
171 info.executorMatcher = 10;
172 HdiExecutorInfo executorInfo = {};
173 executorInfo.executorIndex = 60;
174 executorInfo.info.authType = HdiAuthType::FACE;
175 executorInfo.info.esl = HdiExecutorSecureLevel::ESL1;
176 executorInfo.info.executorMatcher = 10;
177 executorInfo.info.executorRole = HdiExecutorRole::ALL_IN_ONE;
178 executorInfo.info.executorSensorHint = 90;
179 executorInfo.info.publicKey = {1, 2, 3, 4};
180 info.executors.push_back(executorInfo);
181 info.scheduleId = 20;
182 info.scheduleMode = HdiScheduleMode::IDENTIFY;
183 info.templateIds.push_back(30);
184 return HDF_SUCCESS;
185 }
186 );
187 auto resourceNode = MockResourceNode::CreateWithExecuteIndex(executorIndex, FACE, ALL_IN_ONE);
188 EXPECT_NE(resourceNode, nullptr);
189 EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
190 auto enroll = std::make_shared<EnrollmentImpl>(userId, FACE);
191 EXPECT_NE(enroll, nullptr);
192 std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
193 auto callback = Common::MakeShared<MockScheduleNodeCallback>();
194 EXPECT_NE(callback, nullptr);
195 EXPECT_TRUE(enroll->Start(scheduleList, callback));
196 EXPECT_TRUE(enroll->Cancel());
197
198 EXPECT_TRUE(enroll->Start(scheduleList, callback));
199 EXPECT_FALSE(enroll->Cancel());
200
201 EXPECT_TRUE(ResourceNodePool::Instance().Delete(executorIndex));
202 }
203 } // namespace UserAuth
204 } // namespace UserIam
205 } // namespace OHOS