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 "device_manager_service_listener.h"
17 #include "dm_crypto.h"
18 #include "dm_auth_context.h"
19 #include "dm_auth_state.h"
20 #include "UTTest_auth_negotiate.h"
21
22 using namespace testing;
23
24 namespace OHOS {
25 namespace DistributedHardware {
26
27 constexpr const char* TEST_STRING = "test_string";
28 constexpr int32_t TEST_NEGATIVE = -1;
29 constexpr int32_t TEST_POSITIVE = 1;
30
SetUpTestCase()31 void AuthNegotiateTest::SetUpTestCase()
32 {
33 LOGI("AuthNegotiateTest::SetUpTestCase start.");
34
35 DmSoftbusConnector::dmSoftbusConnector = softbusConnectorMock;
36 DmSoftbusSession::dmSoftbusSession = softbusSessionMock;
37 DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient =
38 distributedDeviceProfileClientMock_;
39 }
40
TearDownTestCase()41 void AuthNegotiateTest::TearDownTestCase()
42 {
43 LOGI("AuthNegotiateTest::TearDownTestCase done.");
44 softbusConnectorMock = nullptr;
45 softbusSessionMock = nullptr;
46 DmSoftbusConnector::dmSoftbusConnector = nullptr;
47 DmSoftbusSession::dmSoftbusSession = nullptr;
48 DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = nullptr;
49 distributedDeviceProfileClientMock_ = nullptr;
50 }
51
SetUp()52 void AuthNegotiateTest::SetUp()
53 {
54 LOGI("AuthNegotiateTest::SetUp start.");
55 softbusConnector = std::make_shared<SoftbusConnector>();
56 listener = std::make_shared<DeviceManagerServiceListener>();
57 hiChainAuthConnector = std::make_shared<HiChainAuthConnector>();
58 hiChainConnector = std::make_shared<HiChainConnector>();
59 authManager = std::make_shared<AuthSrcManager>(softbusConnector, hiChainConnector, listener,
60 hiChainAuthConnector);
61 context = authManager->GetAuthContext();
62 softbusSession = std::make_shared<SoftbusSession>();
63
64 auto token = AccessTokenKitInterface::GetOrCreateAccessTokenKit();
65 tokenMock = std::static_pointer_cast<AccessTokenKitMock>(token);
66
67 Mock::VerifyAndClearExpectations(&*softbusConnectorMock);
68 Mock::VerifyAndClearExpectations(&*softbusSessionMock);
69 Mock::VerifyAndClearExpectations(&*distributedDeviceProfileClientMock_);
70 }
71
TearDown()72 void AuthNegotiateTest::TearDown()
73 {
74 LOGI("AuthNegotiateTest::TearDown done.");
75 softbusConnector = nullptr;
76 listener = nullptr;
77 hiChainAuthConnector = nullptr;
78 authManager = nullptr;
79 context = nullptr;
80 softbusSession = nullptr;
81
82 Mock::VerifyAndClearExpectations(&*tokenMock);
83 tokenMock = nullptr;
84 }
85
GetBusinessEventMockTrue(DistributedDeviceProfile::BusinessEvent & event)86 int32_t GetBusinessEventMockTrue(DistributedDeviceProfile::BusinessEvent &event)
87 {
88 event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":true}");
89 return DM_OK;
90 }
91
GetBusinessEventMockEmpty(DistributedDeviceProfile::BusinessEvent & event)92 int32_t GetBusinessEventMockEmpty(DistributedDeviceProfile::BusinessEvent &event)
93 {
94 event.SetBusinessValue("");
95 return DM_OK;
96 }
97
GetBusinessEventMockFalse(DistributedDeviceProfile::BusinessEvent & event)98 int32_t GetBusinessEventMockFalse(DistributedDeviceProfile::BusinessEvent &event)
99 {
100 event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":false}");
101 return DM_OK;
102 }
103
104 HWTEST_F(AuthNegotiateTest, AuthSrcStartState_001, testing::ext::TestSize.Level1)
105 {
106 std::shared_ptr<AuthSrcStartState> authState = std::make_shared<AuthSrcStartState>();
107 EXPECT_EQ(authState->GetStateType(), DmAuthStateType::AUTH_SRC_START_STATE);
108 }
109
110 HWTEST_F(AuthNegotiateTest, AuthSrcStartState_002, testing::ext::TestSize.Level1)
111 {
112 std::shared_ptr<AuthSrcStartState> authState = std::make_shared<AuthSrcStartState>();
113 EXPECT_EQ(authState->Action(context), DM_OK);
114 }
115
116 HWTEST_F(AuthNegotiateTest, AuthSrcNegotiateStateMachine_001, testing::ext::TestSize.Level1)
117 {
118 std::shared_ptr<AuthSrcNegotiateStateMachine> authState = std::make_shared<AuthSrcNegotiateStateMachine>();
119 EXPECT_EQ(authState->GetStateType(), DmAuthStateType::AUTH_SRC_NEGOTIATE_STATE);
120 }
121
122 HWTEST_F(AuthNegotiateTest, AuthSrcNegotiateStateMachine_002, testing::ext::TestSize.Level1)
123 {
124 std::shared_ptr<AuthSrcNegotiateStateMachine> authState = std::make_shared<AuthSrcNegotiateStateMachine>();
125
126 EXPECT_CALL(*softbusConnectorMock, GetSoftbusSession)
127 .WillOnce(Return(softbusSession));
128
129 EXPECT_EQ(authState->Action(context), DM_OK);
130 }
131
132 HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_001, testing::ext::TestSize.Level1)
133 {
134 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
135 EXPECT_EQ(authState->GetStateType(), DmAuthStateType::AUTH_SINK_NEGOTIATE_STATE);
136 }
137
138 HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_002, testing::ext::TestSize.Level1)
139 {
140 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
141 context->accesser.dmVersion = DM_VERSION_5_1_0;
142 context->accessee.displayId = -1;
143 EXPECT_CALL(*softbusConnectorMock, GetSoftbusSession).WillOnce(Return(softbusSession));
144 EXPECT_CALL(*tokenMock, GetNativeTokenId(_)).WillOnce(Return(1));
145 EXPECT_EQ(authState->Action(context), DM_OK);
146 }
147
148 HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_003, testing::ext::TestSize.Level1)
149 {
150 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
151 JsonItemObject credObj;
152 JsonObject aclInfo;
153 aclInfo["pointTopointAcl"] = 1;
154 JsonObject credTypeJson;
155 int32_t credType = 1;
156 std::vector<std::string> deleteCredInfo;
157
158 authState->GetSinkCredTypeForP2P(context, credObj, aclInfo, credTypeJson, credType, deleteCredInfo);
159 EXPECT_EQ(credTypeJson["pointTopointCredType"].Get<int32_t>(), credType);
160 }
161
162 HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_005, testing::ext::TestSize.Level1)
163 {
164 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
165
166 DistributedDeviceProfile::AccessControlProfile profile;
167 DistributedDeviceProfile::Accesser accesser;
168 DistributedDeviceProfile::Accessee accessee;
169 accesser.SetAccesserTokenId(123);
170 accessee.SetAccesseeTokenId(456);
171 profile.SetAccesser(accesser);
172 profile.SetAccessee(accessee);
173 profile.SetBindLevel(1);
174
175 JsonObject credIdJson;
176 credIdJson[FILED_CRED_TYPE] = DM_POINT_TO_POINT;
177 std::vector<std::string> appList = {"123", "456"};
178 credIdJson[FILED_AUTHORIZED_APP_LIST] = appList;
179 credIdJson[FILED_AUTHORIZED_SCOPE] = DM_AUTH_SCOPE_USER;
180 JsonObject credInfo;
181 std::string test_cred_id = "123";
182 credInfo.Insert(test_cred_id, credIdJson);
183
184 bool checkResult = false;
185 authState->CheckCredIdInAclForP2P(context, test_cred_id, profile, credInfo, DM_POINT_TO_POINT, checkResult);
186 GTEST_LOG_(INFO) << "checkResult=" << checkResult;
187 EXPECT_TRUE(checkResult);
188 }
189
190 HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_006, testing::ext::TestSize.Level1)
191 {
192 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
193 context->accessee.deviceId = "1";
194 context->accessee.userId = 0;
195 context->accesser.deviceIdHash = Crypto::Sha256("1");
196
197 DistributedDeviceProfile::Accesser accesser;
198 DistributedDeviceProfile::Accessee accessee;
199 accesser.SetAccesserDeviceId("1");
200 accesser.SetAccesserUserId(0);
201 accessee.SetAccesseeDeviceId("1");
202 accessee.SetAccesseeUserId(0);
203
204 bool result = authState->IdenticalAccountAclCompare(context, accesser, accessee);
205 EXPECT_FALSE(result);
206 }
207
208 HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_007, testing::ext::TestSize.Level1)
209 {
210 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
211 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
212 context->accessee.deviceId = "1";
213 context->accessee.userId = 0;
214 context->accesser.deviceIdHash = Crypto::Sha256("1");
215
216 DistributedDeviceProfile::Accesser accesser;
217 DistributedDeviceProfile::Accessee accessee;
218 accesser.SetAccesserDeviceId("1");
219 accesser.SetAccesserUserId(0);
220 accessee.SetAccesseeDeviceId("1");
221 accessee.SetAccesseeUserId(0);
222
223 bool result = authState->ShareAclCompare(context, accesser, accessee);
224 EXPECT_FALSE(result);
225 }
226
227 HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_008, testing::ext::TestSize.Level1)
228 {
229 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
230 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
231 context->accessee.deviceId = "1";
232 context->accessee.userId = 0;
233 context->accesser.deviceIdHash = Crypto::Sha256("1");
234
235 DistributedDeviceProfile::Accesser accesser;
236 DistributedDeviceProfile::Accessee accessee;
237 accesser.SetAccesserDeviceId("1");
238 accesser.SetAccesserUserId(0);
239 accesser.SetAccesserTokenId(0);
240 accesser.SetAccesserBundleName("");
241 accessee.SetAccesseeDeviceId("1");
242 accessee.SetAccesseeUserId(0);
243 accessee.SetAccesseeTokenId(0);
244 accessee.SetAccesseeBundleName("");
245
246 bool result = authState->LnnAclCompare(context, accesser, accessee);
247
248 EXPECT_FALSE(result);
249 }
250
251 HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_Action_003, testing::ext::TestSize.Level1)
252 {
253 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
254
255 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
256 context->businessId = "test_business_id";
257
258 EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_))
259 .WillOnce(::testing::Invoke(GetBusinessEventMockTrue));
260
261 int32_t result = authState->Action(context);
262
263 EXPECT_EQ(result, ERR_DM_ANTI_DISTURB_MODE);
264 EXPECT_EQ(context->reason, ERR_DM_ANTI_DISTURB_MODE);
265 }
266
267 HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_Action_004, testing::ext::TestSize.Level1)
268 {
269 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
270
271 std::shared_ptr<DmAuthContext> context = std::make_shared<DmAuthContext>();
272 context->businessId = "test_business_id";
273
274 EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_))
275 .WillOnce(::testing::Invoke(GetBusinessEventMockFalse));
276
277 int32_t result = authState->Action(context);
278
279 EXPECT_NE(result, ERR_DM_ANTI_DISTURB_MODE);
280 EXPECT_NE(context->reason, ERR_DM_ANTI_DISTURB_MODE);
281 }
282
283 HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_001, testing::ext::TestSize.Level1)
284 {
285 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
286 std::string businessId = "";
287 businessId.clear();
288 EXPECT_FALSE(authState->IsAntiDisturbanceMode(businessId));
289 }
290
291 HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_002, testing::ext::TestSize.Level1)
292 {
293 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
294 std::string businessId = "test_business_id";
295
296 EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_))
297 .WillOnce(::testing::Return(ERR_DM_FAILED));
298
299 EXPECT_FALSE(authState->IsAntiDisturbanceMode(businessId));
300 }
301
302 HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_003, testing::ext::TestSize.Level1)
303 {
304 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
305 std::string businessId = "test_business_id";
306
307 EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_))
308 .WillOnce(::testing::Invoke(GetBusinessEventMockEmpty));
309
310 EXPECT_FALSE(authState->IsAntiDisturbanceMode(businessId));
311 }
312
313 HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_004, testing::ext::TestSize.Level1)
314 {
315 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
316 std::string businessId = "test_business_id";
317
318 EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_))
319 .WillOnce(::testing::Invoke(GetBusinessEventMockTrue));
320
321 EXPECT_TRUE(authState->IsAntiDisturbanceMode(businessId));
322 }
323
324 HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_001, testing::ext::TestSize.Level1)
325 {
326 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
327 std::string businessId = "test_business_id";
328 std::string businessValue = "invalid_json";
329 EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue));
330 }
331
332 HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_002, testing::ext::TestSize.Level1)
333 {
334 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
335 std::string businessId = "test_business_id";
336 std::string businessValue = "{\"is_in_anti_disturbance_mode\":true}";
337 EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue));
338 }
339
340 HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_003, testing::ext::TestSize.Level1)
341 {
342 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
343 std::string businessId = "test_business_id";
344 std::string businessValue = "{\"business_id\":\"wrong_id\",\"is_in_anti_disturbance_mode\":true}";
345 EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue));
346 }
347
348 HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_004, testing::ext::TestSize.Level1)
349 {
350 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
351 std::string businessId = "test_business_id";
352 std::string businessValue = "{\"business_id\":\"test_business_id\"}";
353 EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue));
354 }
355
356 HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_005, testing::ext::TestSize.Level1)
357 {
358 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
359 std::string businessId = "test_business_id";
360 std::string businessValue = "{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":true}";
361 EXPECT_TRUE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue));
362 }
363
364 HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_006, testing::ext::TestSize.Level1)
365 {
366 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
367 std::string businessId = "test_business_id";
368 std::string businessValue = "{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":false}";
369 EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue));
370 }
371
372 HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_007, testing::ext::TestSize.Level1)
373 {
374 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
375 std::string businessId = "test_business_id";
376 std::string businessValue = "{\"business_id\":123,\"is_in_anti_disturbance_mode\":true}";
377
378 EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue));
379 }
380
381 HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_008, testing::ext::TestSize.Level1)
382 {
383 std::shared_ptr<AuthSinkNegotiateStateMachine> authState = std::make_shared<AuthSinkNegotiateStateMachine>();
384 std::string businessId = "test_business_id";
385 std::string businessValue = "{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":123}";
386
387 EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue));
388 }
389 }
390 }