1 /*
2 * Copyright (c) 2025 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 "UTTest_auth_manager.h"
17
18 #include "distributed_device_profile_client.h"
19 #include "dm_auth_state.h"
20
21 using namespace testing;
22 namespace OHOS {
23 namespace DistributedHardware {
24
SetUp()25 void AuthManagerTest::SetUp()
26 {
27 InitDeviceAuthService();
28 softbusConnector = std::make_shared<SoftbusConnector>();
29 deviceManagerServicelistener = std::make_shared<DeviceManagerServiceListener>();
30 hiChainAuthConnector = std::make_shared<HiChainAuthConnector>();
31 hiChainConnector = std::make_shared<HiChainConnector>();
32 authManager = std::make_shared<AuthSinkManager>(softbusConnector, hiChainConnector, deviceManagerServicelistener,
33 hiChainAuthConnector);
34 }
35
TearDown()36 void AuthManagerTest::TearDown()
37 {
38 softbusConnector = nullptr;
39 deviceManagerServicelistener = nullptr;
40 hiChainAuthConnector = nullptr;
41 hiChainConnector = nullptr;
42 authManager = nullptr;
43 }
44
SetUpTestCase()45 void AuthManagerTest::SetUpTestCase()
46 {
47 DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient =
48 distributedDeviceProfileClientMock_;
49 }
50
TearDownTestCase()51 void AuthManagerTest::TearDownTestCase()
52 {
53 DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = nullptr;
54 distributedDeviceProfileClientMock_ = nullptr;
55 }
56
57 HWTEST_F(AuthManagerTest, HandleBusinessEvents_001, testing::ext::TestSize.Level1)
58 {
59 std::string businessId = "testBusinessId";
60 int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH;
61 EXPECT_CALL(*distributedDeviceProfileClientMock_,
62 PutBusinessEvent(::testing::_)).WillOnce(::testing::Return(DM_OK));
63
64 int32_t ret = authManager->HandleBusinessEvents(businessId, action);
65 EXPECT_EQ(ret, DM_OK);
66 }
67
68 HWTEST_F(AuthManagerTest, HandleBusinessEvents_002, testing::ext::TestSize.Level1)
69 {
70 std::string businessId = "testBusinessId";
71 int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH;
72
73 EXPECT_CALL(*distributedDeviceProfileClientMock_,
74 PutBusinessEvent(::testing::_)).WillOnce(::testing::Return(ERR_DM_AUTH_NOT_START));
75
76 int32_t ret = authManager->HandleBusinessEvents(businessId, action);
77 EXPECT_EQ(ret, ERR_DM_AUTH_NOT_START);
78 }
79
80 HWTEST_F(AuthManagerTest, HandleBusinessEvents_003, testing::ext::TestSize.Level1)
81 {
82 std::string businessId = "testBusinessId";
83 int32_t action = USER_OPERATION_TYPE_ALLOW_AUTH;
84
85 EXPECT_CALL(*distributedDeviceProfileClientMock_, PutBusinessEvent(::testing::_))
86 .WillOnce(::testing::Return(DM_OK));
87
88 int32_t result = authManager->HandleBusinessEvents(businessId, action);
89
90 EXPECT_EQ(result, DM_OK);
91 }
92
93 HWTEST_F(AuthManagerTest, HandleBusinessEvents_004, testing::ext::TestSize.Level1)
94 {
95 std::string businessId = "";
96 int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH;
97
98 EXPECT_CALL(*distributedDeviceProfileClientMock_, PutBusinessEvent(::testing::_))
99 .WillOnce(::testing::Return(DM_OK));
100
101 int32_t result = authManager->HandleBusinessEvents(businessId, action);
102
103 EXPECT_EQ(result, DM_OK);
104 }
105
106 HWTEST_F(AuthManagerTest, ParseJsonObject_001, testing::ext::TestSize.Level1)
107 {
108 JsonObject jsonObject;
109 jsonObject[DM_BUSINESS_ID] = "testBusinessId";
110
111 authManager->ParseJsonObject(jsonObject);
112 EXPECT_EQ(jsonObject[DM_BUSINESS_ID].Get<std::string>(), "testBusinessId");
113 }
114
115 HWTEST_F(AuthManagerTest, ParseJsonObject_002, testing::ext::TestSize.Level1)
116 {
117 JsonObject jsonObject;
118 authManager->ParseJsonObject(jsonObject);
119 EXPECT_EQ(jsonObject[DM_BUSINESS_ID].Get<std::string>(), "");
120 }
121
122 HWTEST_F(AuthManagerTest, OnUserOperation_001, testing::ext::TestSize.Level1)
123 {
124 int32_t action = UiAction::USER_OPERATION_TYPE_CANCEL_AUTH;
125 std::string params = "testParams";
126 int32_t ret = authManager->OnUserOperation(action, params);
127 EXPECT_EQ(ret, DM_OK);
128 }
129
130 HWTEST_F(AuthManagerTest, OnUserOperation_002, testing::ext::TestSize.Level1)
131 {
132 authManager->SetAuthContext(nullptr);
133
134 int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH;
135 std::string params = "";
136
137 int32_t ret = authManager->OnUserOperation(action, params);
138
139 EXPECT_EQ(ret, ERR_DM_AUTH_NOT_START);
140 }
141
142 HWTEST_F(AuthManagerTest, OnUserOperation_003, testing::ext::TestSize.Level1)
143 {
144 authManager->GetAuthContext()->businessId = "";
145
146 int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH;
147 std::string params = "";
148
149 int32_t ret = authManager->OnUserOperation(action, params);
150
151 EXPECT_EQ(ret, DM_OK);
152 }
153
154 HWTEST_F(AuthManagerTest, OnUserOperation_004, testing::ext::TestSize.Level1)
155 {
156 authManager->GetAuthContext()->businessId = "testBusinessId";
157
158 EXPECT_CALL(*distributedDeviceProfileClientMock_, PutBusinessEvent(::testing::_))
159 .WillOnce(::testing::Return(ERR_DM_FAILED));
160
161 int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH;
162 std::string params = "";
163
164 int32_t ret = authManager->OnUserOperation(action, params);
165
166 EXPECT_EQ(ret, ERR_DM_FAILED);
167 }
168
169 HWTEST_F(AuthManagerTest, OnUserOperation_005, testing::ext::TestSize.Level1)
170 {
171 int32_t action = USER_OPERATION_TYPE_AUTH_CONFIRM_TIMEOUT;
172 std::string params = "";
173
174 int32_t ret = authManager->OnUserOperation(action, params);
175
176 EXPECT_EQ(ret, DM_OK);
177 EXPECT_NE(authManager->GetAuthContext()->reason, DM_OK);
178 }
179
180 HWTEST_F(AuthManagerTest, OnUserOperation_006, testing::ext::TestSize.Level1)
181 {
182 int32_t action = USER_OPERATION_TYPE_CANCEL_PINCODE_DISPLAY;
183 std::string params = "";
184
185 int32_t ret = authManager->OnUserOperation(action, params);
186
187 EXPECT_EQ(ret, DM_OK);
188 EXPECT_EQ(authManager->GetAuthContext()->reason, ERR_DM_BIND_USER_CANCEL_PIN_CODE_DISPLAY);
189 }
190
191 HWTEST_F(AuthManagerTest, OnUserOperation_007, testing::ext::TestSize.Level1)
192 {
193 int32_t action = -1;
194 std::string params = "";
195
196 int32_t ret = authManager->OnUserOperation(action, params);
197
198 EXPECT_EQ(ret, DM_OK);
199 }
200
201 /* *
202 * @tc.name: RegisterUiStateCallback_001
203 * @tc.desc: Test RegisterUiStateCallback
204 * @tc.type: FUNC
205 * @tc.require:
206 */
207 HWTEST_F(AuthManagerTest, RegisterUiStateCallback_001, testing::ext::TestSize.Level1)
208 {
209 std::string pkgName = "ohos_test";
210 authManager->context_->authUiStateMgr = nullptr;
211 int32_t ret = authManager->RegisterUiStateCallback(pkgName);
212 ASSERT_EQ(ret, ERR_DM_FAILED);
213 }
214
215 /* *
216 * @tc.name: RegisterUiStateCallback_002
217 * @tc.desc: Test RegisterUiStateCallback
218 * @tc.type: FUNC
219 * @tc.require:
220 */
221 HWTEST_F(AuthManagerTest, RegisterUiStateCallback_002, testing::ext::TestSize.Level1)
222 {
223 std::string pkgName = "ohos_test";
224 std::shared_ptr<IDeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
225 authManager->context_->authUiStateMgr = std::make_shared<AuthUiStateManager>(listener);
226 int32_t ret = authManager->RegisterUiStateCallback(pkgName);
227 ASSERT_EQ(ret, DM_OK);
228 }
229
230 /* *
231 * @tc.name: UnRegisterUiStateCallback_001
232 * @tc.desc: Test UnRegisterUiStateCallback
233 * @tc.type: FUNC
234 * @tc.require:
235 */
236 HWTEST_F(AuthManagerTest, UnRegisterUiStateCallback_001, testing::ext::TestSize.Level1)
237 {
238 std::string pkgName = "ohos_test";
239 authManager->context_->authUiStateMgr = nullptr;
240 int32_t ret = authManager->UnRegisterUiStateCallback(pkgName);
241 ASSERT_EQ(ret, ERR_DM_FAILED);
242 }
243
244 /* *
245 * @tc.name: UnRegisterUiStateCallback_002
246 * @tc.desc: Test UnRegisterUiStateCallback
247 * @tc.type: FUNC
248 * @tc.require:
249 */
250 HWTEST_F(AuthManagerTest, UnRegisterUiStateCallback_002, testing::ext::TestSize.Level1)
251 {
252 std::string pkgName = "ohos_test";
253 std::shared_ptr<IDeviceManagerServiceListener> listener = std::make_shared<DeviceManagerServiceListener>();
254 authManager->context_->authUiStateMgr = std::make_shared<AuthUiStateManager>(listener);
255 int32_t ret = authManager->UnRegisterUiStateCallback(pkgName);
256 ASSERT_EQ(ret, DM_OK);
257 }
258
259 /* *
260 * @tc.name: ImportAuthCode_001
261 * @tc.desc: Test ImportAuthCode
262 * @tc.type: FUNC
263 * @tc.require:
264 */
265 HWTEST_F(AuthManagerTest, ImportAuthCode_001, testing::ext::TestSize.Level1)
266 {
267 std::string authCode = "";
268 std::string pkgName = "pkgName";
269 int32_t ret = authManager->ImportAuthCode(authCode, pkgName);
270 ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
271 }
272
273 /* *
274 * @tc.name: ImportAuthCode_002
275 * @tc.desc: Test ImportAuthCode
276 * @tc.type: FUNC
277 * @tc.require:
278 */
279 HWTEST_F(AuthManagerTest, ImportAuthCode_002, testing::ext::TestSize.Level1)
280 {
281 std::string authCode = "123456";
282 std::string pkgName = "";
283 int32_t ret = authManager->ImportAuthCode(authCode, pkgName);
284 ASSERT_EQ(ret, ERR_DM_INPUT_PARA_INVALID);
285 }
286
287 /* *
288 * @tc.name: ImportAuthCode_003
289 * @tc.desc: Test ImportAuthCode
290 * @tc.type: FUNC
291 * @tc.require:
292 */
293 HWTEST_F(AuthManagerTest, ImportAuthCode_003, testing::ext::TestSize.Level1)
294 {
295 std::string authCode = "123456";
296 std::string pkgName = "pkgName";
297 int32_t ret = authManager->ImportAuthCode(authCode, pkgName);
298 ASSERT_EQ(ret, DM_OK);
299 }
300
301 /* *
302 * @tc.name: IsAuthCodeReady_001
303 * @tc.desc: Test IsAuthCodeReady
304 * @tc.type: FUNC
305 * @tc.require:
306 */
307 HWTEST_F(AuthManagerTest, IsAuthCodeReady_001, testing::ext::TestSize.Level1)
308 {
309 std::string pkgName;
310 authManager->context_->importAuthCode = "";
311 authManager->context_->importPkgName = "importPkgName";
312 bool ret = authManager->IsAuthCodeReady(pkgName);
313 ASSERT_EQ(ret, false);
314 }
315
316 /* *
317 * @tc.name: IsAuthCodeReady_002
318 * @tc.desc: Test IsAuthCodeReady
319 * @tc.type: FUNC
320 * @tc.require:
321 */
322 HWTEST_F(AuthManagerTest, IsAuthCodeReady_002, testing::ext::TestSize.Level1)
323 {
324 std::string pkgName;
325 authManager->context_->importAuthCode = "importAuthCode";
326 authManager->context_->importPkgName = "";
327 bool ret = authManager->IsAuthCodeReady(pkgName);
328 ASSERT_EQ(ret, false);
329 }
330
331 /* *
332 * @tc.name: IsAuthCodeReady_003
333 * @tc.desc: Test IsAuthCodeReady
334 * @tc.type: FUNC
335 * @tc.require:
336 */
337 HWTEST_F(AuthManagerTest, IsAuthCodeReady_003, testing::ext::TestSize.Level1)
338 {
339 std::string pkgName = "pkgName";
340 authManager->context_->importAuthCode = "importAuthCode";
341 authManager->context_->importPkgName = "importPkgName";
342 bool ret = authManager->IsAuthCodeReady(pkgName);
343 ASSERT_EQ(ret, false);
344 }
345
346 /* *
347 * @tc.name: IsAuthCodeReady_004
348 * @tc.desc: Test IsAuthCodeReady
349 * @tc.type: FUNC
350 * @tc.require:
351 */
352 HWTEST_F(AuthManagerTest, IsAuthCodeReady_004, testing::ext::TestSize.Level1)
353 {
354 std::string pkgName = "ohos_test";
355 authManager->context_->importAuthCode = "importAuthCode";
356 authManager->context_->importPkgName = "ohos_test";
357 bool ret = authManager->IsAuthCodeReady(pkgName);
358 ASSERT_EQ(ret, true);
359 }
360
361 HWTEST_F(AuthManagerTest, ParseUltrasonicSide_001, testing::ext::TestSize.Level1)
362 {
363 JsonObject jsonObject;
364 jsonObject[TAG_ULTRASONIC_SIDE] = "0";
365 authManager->ParseUltrasonicSide(jsonObject);
366 jsonObject[TAG_ULTRASONIC_SIDE] = "1";
367 authManager->ParseUltrasonicSide(jsonObject);
368 EXPECT_NE(authManager->context_, nullptr);
369 }
370
371 HWTEST_F(AuthManagerTest, AuthSrcConfirmState_NegotiateUltrasonic_001, testing::ext::TestSize.Level1)
372 {
373 std::shared_ptr<AuthSrcConfirmState> authState = std::make_shared<AuthSrcConfirmState>();
374 std::shared_ptr<DmAuthContext> context = authManager->GetAuthContext();
375
376 context->authType = DmAuthType::AUTH_TYPE_IMPORT_AUTH_CODE;
377 authState->NegotiateUltrasonic(nullptr);
378 authState->NegotiateUltrasonic(context);
379
380 context->authType = DmAuthType::AUTH_TYPE_PIN_ULTRASONIC;
381 authState->NegotiateUltrasonic(context);
382
383 context->accessee.extraInfo = "123456";
384 authState->NegotiateUltrasonic(context);
385
386 JsonObject json;
387 json["isSupportUltrasonic"] = "123456";
388 context->accessee.extraInfo = json.Dump();
389 authState->NegotiateUltrasonic(context);
390
391 json["isSupportUltrasonic"] = true;
392 context->accessee.extraInfo = json.Dump();
393 authState->NegotiateUltrasonic(context);
394
395 json["isSupportUltrasonic"] = false;
396 context->accessee.extraInfo = json.Dump();
397 authState->NegotiateUltrasonic(context);
398 EXPECT_EQ(context->authType, DmAuthType::AUTH_TYPE_PIN);
399 }
400 } // namespace DistributedHardware
401 } // namespace OHOS
402