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