• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "delete_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 class DeleteImplTest : 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 DeleteImplTest::SetUpTestCase()
42 {
43 }
44 
TearDownTestCase()45 void DeleteImplTest::TearDownTestCase()
46 {
47 }
48 
SetUp()49 void DeleteImplTest::SetUp()
50 {
51     MockIUserAuthInterface::Holder::GetInstance().Reset();
52 }
53 
TearDown()54 void DeleteImplTest::TearDown()
55 {
56     MockIUserAuthInterface::Holder::GetInstance().Reset();
57 }
58 
59 HWTEST_F(DeleteImplTest, AbandonHdiError, TestSize.Level0)
60 {
61     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
62     DeleteImpl::DeleteParam para = {};
63     para.userId = 1;
64     para.credentialId = 1;
65     para.tokenId = 1;
66     para.token = testAuthToken;
67     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
68     EXPECT_CALL(*mock, DeleteCredential(_, _, _, _)).WillRepeatedly(Return(1));
69 
70     auto abandon = std::make_shared<DeleteImpl>(para);
71     std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
72     bool isCredentialDelete = false;
73     EXPECT_FALSE(abandon->Start(scheduleList, nullptr, isCredentialDelete));
74 }
75 
76 HWTEST_F(DeleteImplTest, AbandonHdiEmpty, TestSize.Level0)
77 {
78     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
79     DeleteImpl::DeleteParam para = {};
80     para.userId = 1;
81     para.credentialId = 1;
82     para.tokenId = 1;
83     para.token = testAuthToken;
84 
85     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
86     EXPECT_CALL(*mock, DeleteCredential(_, _, _, _)).WillRepeatedly(Return(0));
87 
88     auto abandon = std::make_shared<DeleteImpl>(para);
89     std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
90     bool isCredentialDelete = false;
91     EXPECT_FALSE(abandon->Start(scheduleList, nullptr, isCredentialDelete));
92 }
93 
94 HWTEST_F(DeleteImplTest, AbandonUpdateHdiError, TestSize.Level0)
95 {
96     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
97     DeleteImpl::DeleteParam para = {};
98     para.userId = 1;
99     para.credentialId = 1;
100     para.tokenId = 1;
101     para.token = testAuthToken;
102 
103     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
104     EXPECT_CALL(*mock, UpdateAbandonResult(para.userId, _, _)).WillRepeatedly(Return(1));
105 
106     auto abandon = std::make_shared<DeleteImpl>(para);
107     std::vector<uint8_t> scheduleResult = {1, 2, 3};
108     std::shared_ptr<CredentialInfoInterface> info = nullptr;
109     EXPECT_FALSE(abandon->Update(scheduleResult, info));
110 }
111 
112 HWTEST_F(DeleteImplTest, AbandonUpdateHdiSuccessful_001, TestSize.Level0)
113 {
114     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
115     DeleteImpl::DeleteParam para = {};
116     para.userId = 1;
117     para.credentialId = 1;
118     para.tokenId = 1;
119     para.token = testAuthToken;
120     std::vector<uint8_t> scheduleResult = {1, 2, 3};
121     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
122     EXPECT_CALL(*mock, UpdateAbandonResult(para.userId, _, _))
123         .WillRepeatedly(
124             [](int32_t userId, const std::vector<uint8_t>& scheduleResult,
__anon4f6296220102(int32_t userId, const std::vector<uint8_t>& scheduleResult, std::vector<HdiCredentialInfo>& infos) 125                 std::vector<HdiCredentialInfo>& infos) {
126                 HdiCredentialInfo info = {
127                     .credentialId = 1,
128                     .executorIndex = 2,
129                     .templateId = 3,
130                     .authType = static_cast<HdiAuthType>(0),
131                     .executorMatcher = 5,
132                     .executorSensorHint = 6,
133                 };
134                 infos.push_back(info);
135                 return HDF_SUCCESS;
136             }
137         );
138 
139     auto abandon = std::make_shared<DeleteImpl>(para);
140     HdiCredentialInfo oldInfo = {};
141     std::shared_ptr<CredentialInfoInterface> info = std::make_shared<CredentialInfoImpl>(para.userId, oldInfo);
142     EXPECT_TRUE(abandon->Update(scheduleResult, info));
143 
144     EXPECT_EQ(info->GetCredentialId(), 1U);
145     EXPECT_EQ(info->GetAuthType(), static_cast<AuthType>(0));
146     EXPECT_EQ(info->GetExecutorIndex(), 2U);
147     EXPECT_EQ(info->GetUserId(), 1);
148     EXPECT_EQ(info->GetTemplateId(), 3U);
149     EXPECT_EQ(info->GetExecutorMatcher(), 5U);
150     EXPECT_EQ(info->GetExecutorSensorHint(), 6U);
151 }
152 
153 HWTEST_F(DeleteImplTest, AbandonUpdateHdiSuccessful_002, TestSize.Level0)
154 {
155     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
156     DeleteImpl::DeleteParam para = {};
157     para.userId = 1;
158     para.credentialId = 1;
159     para.tokenId = 1;
160     para.token = testAuthToken;
161     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
162     EXPECT_CALL(*mock, UpdateAbandonResult(_, _, _)).WillRepeatedly(Return(0));
163     auto abandon = std::make_shared<DeleteImpl>(para);
164 
165     std::vector<uint8_t> scheduleResult = {1, 2, 3};
166     std::shared_ptr<CredentialInfoInterface> info = nullptr;
167     EXPECT_TRUE(abandon->Update(scheduleResult, info));
168 }
169 
170 HWTEST_F(DeleteImplTest, DeleteImplTestStart_001, TestSize.Level0)
171 {
172     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
173     DeleteImpl::DeleteParam para = {};
174     para.userId = 1;
175     para.credentialId = 1;
176     para.tokenId = 1;
177     para.token = testAuthToken;
178     constexpr uint64_t executorIndex = 60;
179 
180     auto mockHdi = MockIUserAuthInterface::Holder::GetInstance().Get();
181     EXPECT_NE(mockHdi, nullptr);
182     EXPECT_CALL(*mockHdi, DeleteCredential(_, _, _, _))
183         .WillRepeatedly(
184             [](int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken,
__anon4f6296220202(int32_t userId, uint64_t credentialId, const std::vector<uint8_t> &authToken, HdiCredentialOperateResult &operateResult) 185                 HdiCredentialOperateResult &operateResult) {
186                 operateResult.operateType = HdiCredentialOperateType::CREDENTIAL_ABANDON;
187                 operateResult.scheduleInfo.authType = HdiAuthType::FACE;
188                 operateResult.scheduleInfo.executorMatcher = 10;
189                 operateResult.scheduleInfo.executorIndexes.push_back(60);
190                 std::vector<uint8_t> executorMessages;
191                 executorMessages.resize(1);
192                 operateResult.scheduleInfo.executorMessages.push_back(executorMessages);
193                 operateResult.scheduleInfo.scheduleId = 20;
194                 operateResult.scheduleInfo.scheduleMode = HdiScheduleMode::ABANDON;
195                 operateResult.scheduleInfo.templateIds.push_back(30);
196                 return HDF_SUCCESS;
197             }
198         );
199     auto resourceNode = MockResourceNode::CreateWithExecuteIndex(executorIndex, FACE, ALL_IN_ONE);
200     EXPECT_NE(resourceNode, nullptr);
201     EXPECT_TRUE(ResourceNodePool::Instance().Insert(resourceNode));
202     auto abandon = std::make_shared<DeleteImpl>(para);
203     EXPECT_NE(abandon, nullptr);
204     std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
205     auto callback = Common::MakeShared<MockScheduleNodeCallback>();
206     EXPECT_NE(callback, nullptr);
207     bool isCredentialDelete = false;
208     EXPECT_TRUE(abandon->Start(scheduleList, callback, isCredentialDelete));
209     EXPECT_TRUE(abandon->Cancel());
210     EXPECT_TRUE(ResourceNodePool::Instance().Delete(executorIndex));
211 }
212 
213 HWTEST_F(DeleteImplTest, DeleteImplTestStart_002, TestSize.Level0)
214 {
215     std::vector<uint8_t> testAuthToken = {1, 2, 3, 4};
216     DeleteImpl::DeleteParam para = {};
217     para.userId = 1;
218     para.credentialId = 1;
219     para.tokenId = 1;
220     para.token = testAuthToken;
221     auto mock = MockIUserAuthInterface::Holder::GetInstance().Get();
222     EXPECT_CALL(*mock, DeleteCredential(_, _, _, _)).WillRepeatedly(Return(1));
223     auto abandon = std::make_shared<DeleteImpl>(para);
224     std::vector<std::shared_ptr<ScheduleNode>> scheduleList;
225     auto callback = Common::MakeShared<MockScheduleNodeCallback>();
226     bool isCredentialDelete = false;
227     EXPECT_FALSE(abandon->Start(scheduleList, callback, isCredentialDelete));
228 }
229 } // namespace UserAuth
230 } // namespace UserIam
231 } // namespace OHOS