From 7f8615873a189b7c20e71ba383783ed3f5d193bd Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 11 Jun 2025 18:53:45 +0800 Subject: [PATCH 1/4] add anti Signed-off-by: BrainL --- common/include/dm_constants.h | 7 + common/include/dm_error_type.h | 1 + common/src/dm_constants.cpp | 7 + .../include/authentication_v2/auth_manager.h | 2 + .../authentication_v2/dm_auth_context.h | 2 + .../dm_auth_message_processor.h | 1 + .../include/authentication_v2/dm_auth_state.h | 2 + .../src/authentication_v2/auth_manager.cpp | 44 ++++++ .../auth_stages/auth_negotiate.cpp | 67 +++++++++ .../dm_auth_message_processor.cpp | 45 +++--- test/unittest/UTTest_auth_negotiate.cpp | 136 ++++++++++++++++++ test/unittest/UTTest_auth_negotiate.h | 4 + ...distributed_device_profile_client_mock.cpp | 5 + .../distributed_device_profile_client_mock.h | 2 + 14 files changed, 308 insertions(+), 17 deletions(-) diff --git a/common/include/dm_constants.h b/common/include/dm_constants.h index 0a2645f57..d1a382e8e 100755 --- a/common/include/dm_constants.h +++ b/common/include/dm_constants.h @@ -159,6 +159,13 @@ DM_EXPORT extern const char* CONN_SESSION_TYPE_HML; DM_EXPORT extern const char* CONN_SESSION_TYPE_BLE; DM_EXPORT extern const char* UN_BIND_PARAM_UDID_KEY; +DM_EXPORT extern const char* DM_REJECT_KEY; +DM_EXPORT extern const char* DM_DISTURBANCE_EVENT_KEY; +DM_EXPORT extern const char* DM_BUSINESS_ID; +DM_EXPORT extern const char* DM_TIMESTAMP; +DM_EXPORT extern const char* DM_AUTH_DIALOG_REJECT; +DM_EXPORT extern const char* DM_ANTI_DISTURBANCE_MODE; + // screen state enum ScreenState { DM_SCREEN_UNKNOWN = -1, diff --git a/common/include/dm_error_type.h b/common/include/dm_error_type.h index cd5225aed..6e6552ca3 100644 --- a/common/include/dm_error_type.h +++ b/common/include/dm_error_type.h @@ -142,6 +142,7 @@ enum { ERR_DM_VERIFY_SAME_ACCOUNT_FAILED = 969298353, ERR_DM_DEVICE_FREEZED = 969298355, ERR_DM_SOCKET_IN_USED = 969298356, + ERR_DM_ANTI_DISTURB_MODE = 969298357, }; } // namespace DistributedHardware } // namespace OHOS diff --git a/common/src/dm_constants.cpp b/common/src/dm_constants.cpp index 9be2b9fb7..e9a50d749 100644 --- a/common/src/dm_constants.cpp +++ b/common/src/dm_constants.cpp @@ -149,6 +149,13 @@ const char* CONN_SESSION_TYPE_HML = "HML"; const char* CONN_SESSION_TYPE_BLE = "BLE"; const char* UN_BIND_PARAM_UDID_KEY = "udidKey"; +const char* DM_REJECT_KEY = "business_id_cast+_reject_event"; +const char* DM_DISTURBANCE_EVENT_KEY = "business_id_cast+_disturbance_event"; +const char* DM_BUSINESS_ID = "business_id"; +const char* DM_TIMESTAMP = "timestamp"; +const char* DM_AUTH_DIALOG_REJECT = "is_auth_dialog_reject"; +const char* DM_ANTI_DISTURBANCE_MODE = "is_in_anti_disturbance_mode"; + // errCode map const std::map MAP_ERROR_CODE = { { ERR_DM_TIME_OUT, 96929745 }, { ERR_DM_UNSUPPORTED_AUTH_TYPE, 96929760 }, { ERR_DM_AUTH_BUSINESS_BUSY, 96929761 }, diff --git a/services/implementation/include/authentication_v2/auth_manager.h b/services/implementation/include/authentication_v2/auth_manager.h index 2767c79b4..1c5395dfe 100644 --- a/services/implementation/include/authentication_v2/auth_manager.h +++ b/services/implementation/include/authentication_v2/auth_manager.h @@ -123,6 +123,8 @@ public: void PrepareSoftbusSessionCallback(); void DeleteTimer(); + int32_t HandleBusinessEvents(const std::string &businessId, int32_t action); + protected: std::shared_ptr context_; std::mutex bindParamMutex_; diff --git a/services/implementation/include/authentication_v2/dm_auth_context.h b/services/implementation/include/authentication_v2/dm_auth_context.h index faf47baa2..f7cab34d5 100644 --- a/services/implementation/include/authentication_v2/dm_auth_context.h +++ b/services/implementation/include/authentication_v2/dm_auth_context.h @@ -32,6 +32,7 @@ #include "hichain_connector.h" #include "softbus_connector.h" #include "softbus_session.h" +#include "auth_manager.h" namespace OHOS { namespace DistributedHardware { @@ -202,6 +203,7 @@ struct DmAuthContext { bool successFinished{false}; bool isAppCredentialVerified{false}; // Whether the application credential has been verified bool hmlEnable160M{false}; + std::string businessId; std::string pkgName; // Business-provided identifier, custom-defined by business, carries risk of spoofing std::string pkgLabel; std::string importCodeBundleName; // Bundle name for imported PIN code diff --git a/services/implementation/include/authentication_v2/dm_auth_message_processor.h b/services/implementation/include/authentication_v2/dm_auth_message_processor.h index 0183ac559..0e6987f98 100644 --- a/services/implementation/include/authentication_v2/dm_auth_message_processor.h +++ b/services/implementation/include/authentication_v2/dm_auth_message_processor.h @@ -322,6 +322,7 @@ private: DmAccess &accessTmp); void ParseUltrasonicSide(const JsonObject &jsonObject, std::shared_ptr context); void ParseCert(const JsonObject &jsonObject, std::shared_ptr context); + void ParseAccesserInfo(const JsonObject &jsonObject, std::shared_ptr context); std::shared_ptr cryptoMgr_ = nullptr; std::unordered_map createMessageFuncMap_; std::unordered_map paraseMessageFuncMap_; diff --git a/services/implementation/include/authentication_v2/dm_auth_state.h b/services/implementation/include/authentication_v2/dm_auth_state.h index e729ae5dd..87ebc199c 100644 --- a/services/implementation/include/authentication_v2/dm_auth_state.h +++ b/services/implementation/include/authentication_v2/dm_auth_state.h @@ -477,6 +477,8 @@ private: JsonObject &credTypeJson); void GetSinkCredTypeForP2P(std::shared_ptr context, const JsonItemObject &credObj, JsonObject &aclInfo, JsonObject &credTypeJson, int32_t credType, std::vector &deleteCredInfo); + bool IsAntiDisturbanceMode(const std::string &businessId); + bool ParseAndCheckAntiDisturbanceMode(const std::string &businessId, const std::string &businessValue); }; class AuthSinkDataSyncState : public DmAuthState { diff --git a/services/implementation/src/authentication_v2/auth_manager.cpp b/services/implementation/src/authentication_v2/auth_manager.cpp index cf5a594b2..a9082d772 100644 --- a/services/implementation/src/authentication_v2/auth_manager.cpp +++ b/services/implementation/src/authentication_v2/auth_manager.cpp @@ -14,6 +14,8 @@ */ #include +#include +#include "cJSON.h" #include "app_manager.h" #include "softbus_common.h" @@ -38,6 +40,8 @@ #include "dm_auth_context.h" #include "dm_auth_message_processor.h" #include "dm_auth_state.h" +#include "business_event.h" +#include "distributed_device_profile_client.h" namespace OHOS { namespace DistributedHardware { @@ -48,6 +52,13 @@ constexpr int32_t MAX_PIN_CODE = 999999; constexpr int32_t DM_ULTRASONIC_FORWARD = 0; constexpr int32_t DM_ULTRASONIC_REVERSE = 1; +int64_t GetCurrentTimestamp() +{ + auto now = std::chrono::system_clock::now(); + auto duration = std::chrono::duration_cast(now.time_since_epoch()); + return duration.count(); +} + int32_t GetCloseSessionDelaySeconds(std::string &delaySecondsStr) { if (!IsNumberString(delaySecondsStr)) { @@ -136,6 +147,7 @@ AuthManager::AuthManager(std::shared_ptr softbusConnector, context_->accessee.dmVersion = DM_VERSION_5_1_1; context_->timer = std::make_shared(); context_->authMessageProcessor = std::make_shared(); + context_->businessId = ""; } AuthManager::~AuthManager() @@ -416,6 +428,9 @@ std::string AuthManager::GetBundleName(const JsonObject &jsonObject) void AuthManager::ParseJsonObject(const JsonObject &jsonObject) { + if (IsString(jsonObject, DM_BUSINESS_ID)) { + context_->businessId = jsonObject[DM_BUSINESS_ID].Get(); + } if (jsonObject[APP_OPERATION_KEY].IsString()) { context_->appOperation = jsonObject[APP_OPERATION_KEY].Get(); } @@ -750,6 +765,16 @@ int32_t AuthSinkManager::OnUserOperation(int32_t action, const std::string ¶ return ERR_DM_AUTH_NOT_START; } + std::string businessId = context_->businessId; + if (!businessId.empty()) { + LOGI("AuthSinkManager::OnUserOperation found businessId: %{public}s", businessId.c_str()); + int32_t ret = HandleBusinessEvents(businessId, action); + if (ret != DM_OK) { + LOGE("AuthSinkManager::OnUserOperation failed to handle business events, ret: %{public}d", ret); + return ret; + } + } + switch (action) { case USER_OPERATION_TYPE_CANCEL_AUTH: case USER_OPERATION_TYPE_ALLOW_AUTH: @@ -1103,5 +1128,24 @@ void AuthManager::DeleteTimer() } LOGI("end."); } + +int32_t AuthManager::HandleBusinessEvents(const std::string &businessId, int32_t action) +{ + LOGI("AuthManager::HandleBusinessEvents start."); + DistributedDeviceProfile::BusinessEvent rejectEvent; + rejectEvent.SetBusinessKey(DM_REJECT_KEY); + JsonObject rejectJson; + rejectJson[DM_BUSINESS_ID] = businessId; + rejectJson[DM_AUTH_DIALOG_REJECT] = (action == USER_OPERATION_TYPE_CANCEL_AUTH); + rejectJson[DM_TIMESTAMP] = std::to_string(GetCurrentTimestamp()); + rejectEvent.SetBusinessValue(rejectJson.Dump()); + int32_t ret = DistributedDeviceProfile::DistributedDeviceProfileClient::GetInstance().PutBusinessEvent(rejectEvent); + if (ret != DM_OK) { + LOGE("HandleBusinessEvents failed to store reject_event, ret: %{public}d", ret); + return ret; + } + LOGI("HandleBusinessEvents successfully stored reject_event."); + return DM_OK; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp index 0639821f5..847681d36 100644 --- a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp +++ b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp @@ -43,6 +43,8 @@ #include "multiple_user_connector.h" #include "os_account_manager.h" #include "parameter.h" +#include "business_event.h" +#include "distributed_device_profile_client.h" using namespace OHOS::Security::AccessToken; @@ -226,6 +228,11 @@ int32_t VerifyCertificate(std::shared_ptr context) int32_t AuthSinkNegotiateStateMachine::Action(std::shared_ptr context) { LOGI("AuthSinkNegotiateStateMachine::Action sessionid %{public}d", context->sessionId); + if (IsAntiDisturbanceMode(context->businessId)) { + LOGI("Sink is AntiDisturbMode."); + context->reason = ERR_DM_ANTI_DISTURB_MODE; + return ERR_DM_ANTI_DISTURB_MODE; + } if (FreezeProcess::GetInstance().IsFreezed(context->accessee.bundleName, context->accessee.deviceType)) { LOGE("Device is Freezed"); return ERR_DM_DEVICE_FREEZED; @@ -584,5 +591,65 @@ void AuthSinkNegotiateStateMachine::GetP2PCredentialInfo(std::shared_ptrvaluestring; + if (parsedBusinessId != businessId) { + LOGE("AuthManager::IsAntiDisturbMode failed: businessId mismatch. Expected: %{public}s, Found: %{public}s", + businessId.c_str(), parsedBusinessId.c_str()); + cJSON_Delete(root); + return false; + } + + cJSON* antiDisturbModeItem = cJSON_GetObjectItem(root, DM_ANTI_DISTURBANCE_MODE); + if (antiDisturbModeItem == nullptr || !cJSON_IsBool(antiDisturbModeItem)) { + LOGE("AuthManager::IsAntiDisturbMode failed: 'is_in_anti_disturbance_mode' field is missing or invalid."); + cJSON_Delete(root); + return false; + } + + bool isInAntiDisturbanceMode = cJSON_IsTrue(antiDisturbModeItem); + LOGI("AuthManager::IsAntiDisturbMode result: %{public}s", isInAntiDisturbanceMode ? "true" : "false"); + + cJSON_Delete(root); + return isInAntiDisturbanceMode; +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp index 043302bf8..a7c557672 100644 --- a/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp +++ b/services/implementation/src/authentication_v2/dm_auth_message_processor.cpp @@ -655,6 +655,9 @@ int32_t DmAuthMessageProcessor::CreateNegotiateMessage(std::shared_ptraccessee.pkgName; jsonObject[TAG_HOST_PKGLABEL] = context->pkgLabel; + if (!context->businessId.empty()) { + jsonObject[DM_BUSINESS_ID] = context->businessId; + } return DM_OK; } @@ -976,14 +979,36 @@ int32_t DmAuthMessageProcessor::ParseNegotiateMessage( context->logicalSessionId = jsonObject[DM_TAG_LOGICAL_SESSION_ID].Get(); context->requestId = static_cast(context->logicalSessionId); } + if (jsonObject[TAG_PEER_PKG_NAME].IsString()) { + context->accessee.pkgName = jsonObject[TAG_PEER_PKG_NAME].Get(); + } + if (jsonObject[TAG_PEER_BUNDLE_NAME_V2].IsString()) { + context->accessee.bundleName = jsonObject[TAG_PEER_BUNDLE_NAME_V2].Get(); + } + if (jsonObject[TAG_PEER_DISPLAY_ID].IsNumberInteger()) { + context->accessee.displayId = jsonObject[TAG_PEER_DISPLAY_ID].Get(); + } + if (IsString(jsonObject, TAG_HOST_PKGLABEL)) { + context->pkgLabel = jsonObject[TAG_HOST_PKGLABEL].Get(); + } + if (IsString(jsonObject, DM_BUSINESS_ID)) { + context->businessId = jsonObject[DM_BUSINESS_ID].Get(); + } + ParseAccesserInfo(jsonObject, context); + ParseUltrasonicSide(jsonObject, context); + ParseCert(jsonObject, context); + context->authStateMachine->TransitionTo(std::make_shared()); + return DM_OK; +} + +void DmAuthMessageProcessor::ParseAccesserInfo(const JsonObject &jsonObject, + std::shared_ptr context) +{ if (jsonObject[TAG_PKG_NAME].IsString()) { context->pkgName = jsonObject[TAG_PKG_NAME].Get(); context->accesser.pkgName = context->pkgName; context->accessee.pkgName = context->accesser.pkgName; } - if (jsonObject[TAG_PEER_PKG_NAME].IsString()) { - context->accessee.pkgName = jsonObject[TAG_PEER_PKG_NAME].Get(); - } if (jsonObject[TAG_DM_VERSION_V2].IsString()) { context->accesser.dmVersion = jsonObject[TAG_DM_VERSION_V2].Get(); } @@ -1005,20 +1030,6 @@ int32_t DmAuthMessageProcessor::ParseNegotiateMessage( if (jsonObject[TAG_EXTRA_INFO].IsString()) { context->accesser.extraInfo = jsonObject[TAG_EXTRA_INFO].Get(); } - - if (jsonObject[TAG_PEER_BUNDLE_NAME_V2].IsString()) { - context->accessee.bundleName = jsonObject[TAG_PEER_BUNDLE_NAME_V2].Get(); - } - if (jsonObject[TAG_PEER_DISPLAY_ID].IsNumberInteger()) { - context->accessee.displayId = jsonObject[TAG_PEER_DISPLAY_ID].Get(); - } - if (IsString(jsonObject, TAG_HOST_PKGLABEL)) { - context->pkgLabel = jsonObject[TAG_HOST_PKGLABEL].Get(); - } - ParseUltrasonicSide(jsonObject, context); - ParseCert(jsonObject, context); - context->authStateMachine->TransitionTo(std::make_shared()); - return DM_OK; } void DmAuthMessageProcessor::ParseCert(const JsonObject &jsonObject, diff --git a/test/unittest/UTTest_auth_negotiate.cpp b/test/unittest/UTTest_auth_negotiate.cpp index 02d4a30d3..9fb20157b 100644 --- a/test/unittest/UTTest_auth_negotiate.cpp +++ b/test/unittest/UTTest_auth_negotiate.cpp @@ -33,6 +33,8 @@ void AuthNegotiateTest::SetUpTestCase() DmSoftbusConnector::dmSoftbusConnector = softbusConnectorMock; DmSoftbusSession::dmSoftbusSession = softbusSessionMock; + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = + distributedDeviceProfileClientMock_; } void AuthNegotiateTest::TearDownTestCase() @@ -42,6 +44,7 @@ void AuthNegotiateTest::TearDownTestCase() softbusSessionMock = nullptr; DmSoftbusConnector::dmSoftbusConnector = nullptr; DmSoftbusSession::dmSoftbusSession = nullptr; + distributedDeviceProfileClientMock_ = nullptr; } void AuthNegotiateTest::SetUp() @@ -61,6 +64,7 @@ void AuthNegotiateTest::SetUp() Mock::VerifyAndClearExpectations(&*softbusConnectorMock); Mock::VerifyAndClearExpectations(&*softbusSessionMock); + Mock::VerifyAndClearExpectations(&*distributedDeviceProfileClientMock_); } void AuthNegotiateTest::TearDown() @@ -221,5 +225,137 @@ HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_008, testing::ext::Tes EXPECT_FALSE(result); } +HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_Action_003, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + + std::shared_ptr context = std::make_shared(); + context->businessId = "test_business_id"; + + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_)) + .WillOnce(::testing::Invoke([](DistributedDeviceProfile::BusinessEvent &event) { + event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":true}"); + return DM_OK; + })); + + int32_t result = authState->Action(context); + + EXPECT_EQ(result, ERR_DM_ANTI_DISTURB_MODE); + EXPECT_EQ(context->reason, ERR_DM_ANTI_DISTURB_MODE); +} + +HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_Action_004, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + + std::shared_ptr context = std::make_shared(); + context->businessId = "test_business_id"; + + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_)) + .WillOnce(::testing::Invoke([](DistributedDeviceProfile::BusinessEvent &event) { + event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":false}"); + return DM_OK; + })); + + int32_t result = authState->Action(context); + + EXPECT_NE(result, ERR_DM_ANTI_DISTURB_MODE); + EXPECT_NE(context->reason, ERR_DM_ANTI_DISTURB_MODE); +} + +HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = ""; + businessId.clear(); + EXPECT_FALSE(authState->IsAntiDisturbanceMode(businessId)); +} + +HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_002, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = "test_business_id"; + + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_)) + .WillOnce(::testing::Return(ERR_DM_FAILED)); + + EXPECT_FALSE(authState->IsAntiDisturbanceMode(businessId)); +} + +HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_003, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = "test_business_id"; + + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_)) + .WillOnce(::testing::Invoke([](DistributedDeviceProfile::BusinessEvent &event) { + event.SetBusinessValue(""); + return DM_OK; + })); + + EXPECT_FALSE(authState->IsAntiDisturbanceMode(businessId)); +} + +HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_004, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = "test_business_id"; + + EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_)) + .WillOnce(::testing::Invoke([](DistributedDeviceProfile::BusinessEvent &event) { + event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":true}"); + return DM_OK; + })); + + EXPECT_TRUE(authState->IsAntiDisturbanceMode(businessId)); +} + +HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_001, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = "test_business_id"; + std::string businessValue = "invalid_json"; + EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue)); +} + +HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_002, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = "test_business_id"; + std::string businessValue = "{\"is_in_anti_disturbance_mode\":true}"; + EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue)); +} + +HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_003, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = "test_business_id"; + std::string businessValue = "{\"business_id\":\"wrong_id\",\"is_in_anti_disturbance_mode\":true}"; + EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue)); +} + +HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_004, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = "test_business_id"; + std::string businessValue = "{\"business_id\":\"test_business_id\"}"; + EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue)); +} + +HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_005, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = "test_business_id"; + std::string businessValue = "{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":true}"; + EXPECT_TRUE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue)); +} + +HWTEST_F(AuthNegotiateTest, ParseAndCheckAntiDisturbanceMode_006, testing::ext::TestSize.Level1) +{ + std::shared_ptr authState = std::make_shared(); + std::string businessId = "test_business_id"; + std::string businessValue = "{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":false}"; + EXPECT_FALSE(authState->ParseAndCheckAntiDisturbanceMode(businessId, businessValue)); +} } } \ No newline at end of file diff --git a/test/unittest/UTTest_auth_negotiate.h b/test/unittest/UTTest_auth_negotiate.h index 81608c77d..e3c9ab909 100644 --- a/test/unittest/UTTest_auth_negotiate.h +++ b/test/unittest/UTTest_auth_negotiate.h @@ -21,6 +21,7 @@ #include "accesstoken_kit_mock.h" #include "softbus_connector_mock.h" #include "softbus_session_mock.h" +#include "distributed_device_profile_client_mock.h" namespace OHOS { namespace DistributedHardware { @@ -36,6 +37,9 @@ private: std::make_shared(); static inline std::shared_ptr softbusSessionMock = std::make_shared(); + static inline std::shared_ptr + distributedDeviceProfileClientMock_ = + std::make_shared(); static inline std::shared_ptr tokenMock = nullptr; std::shared_ptr softbusConnector; std::shared_ptr listener; diff --git a/test/unittest/mock/distributed_device_profile_client_mock.cpp b/test/unittest/mock/distributed_device_profile_client_mock.cpp index 89f1b92cd..fc2eb614f 100644 --- a/test/unittest/mock/distributed_device_profile_client_mock.cpp +++ b/test/unittest/mock/distributed_device_profile_client_mock.cpp @@ -86,5 +86,10 @@ int32_t DistributedDeviceProfileClient::GetLocalServiceInfoByBundleAndPinType(co return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->GetLocalServiceInfoByBundleAndPinType( bundleName, pinExchangeType, localServiceInfo); } + +int32_t DistributedDeviceProfileClient::GetBusinessEvent(BusinessEvent &event) +{ + return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->GetBusinessEvent(event); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/distributed_device_profile_client_mock.h b/test/unittest/mock/distributed_device_profile_client_mock.h index 2109ca5db..4c208fbf3 100644 --- a/test/unittest/mock/distributed_device_profile_client_mock.h +++ b/test/unittest/mock/distributed_device_profile_client_mock.h @@ -40,6 +40,7 @@ public: virtual int32_t UpdateLocalServiceInfo(const LocalServiceInfo& localServiceInfo) = 0; virtual int32_t GetLocalServiceInfoByBundleAndPinType(const std::string& bundleName, int32_t pinExchangeType, LocalServiceInfo& localServiceInfo) = 0; + virtual int32_t GetBusinessEvent(BusinessEvent &event) = 0; public: static inline std::shared_ptr dpDistributedDeviceProfileClient = nullptr; }; @@ -58,6 +59,7 @@ public: MOCK_METHOD(int32_t, DeleteLocalServiceInfo, (const std::string&, int32_t)); MOCK_METHOD(int32_t, UpdateLocalServiceInfo, (const LocalServiceInfo&)); MOCK_METHOD(int32_t, GetLocalServiceInfoByBundleAndPinType, (const std::string&, int32_t, LocalServiceInfo&)); + MOCK_METHOD(int32_t, GetBusinessEvent, (BusinessEvent &event)); }; } } -- Gitee From e44e7c9f89a0a7c56c7ed2454151b59adc523983 Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 11 Jun 2025 21:14:03 +0800 Subject: [PATCH 2/4] add UT Signed-off-by: BrainL --- test/unittest/BUILD.gn | 33 +++++++ test/unittest/UTTest_auth_manager.cpp | 94 +++++++++++++++++++ test/unittest/UTTest_auth_manager.h | 48 ++++++++++ test/unittest/UTTest_auth_negotiate.cpp | 38 ++++---- ...distributed_device_profile_client_mock.cpp | 5 + .../distributed_device_profile_client_mock.h | 6 +- 6 files changed, 206 insertions(+), 18 deletions(-) create mode 100644 test/unittest/UTTest_auth_manager.cpp create mode 100644 test/unittest/UTTest_auth_manager.h diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 8e572f9a3..505b89c89 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -26,6 +26,7 @@ group("unittest") { ":UTTest_auth_credential_state", ":UTTest_auth_message_processor", ":UTTest_auth_negotiate", + ":UTTest_auth_manager", ":UTTest_auth_pin_auth_state", ":UTTest_auth_request_state", ":UTTest_auth_response_state", @@ -2180,6 +2181,7 @@ ohos_unittest("UTTest_auth_negotiate") { "${devicemanager_path}/test/unittest/mock/accesstoken_kit_mock.cpp", "${devicemanager_path}/test/unittest/mock/softbus_connector_mock.cpp", "${devicemanager_path}/test/unittest/mock/softbus_session_mock.cpp", + "${devicemanager_path}/test/unittest/mock/distributed_device_profile_client_mock.cpp", ] deps = [ ":device_manager_test_common" ] @@ -2201,6 +2203,37 @@ ohos_unittest("UTTest_auth_negotiate") { ## UnitTest UTTest_auth_negotiate }}} +## UnitTest UTTest_auth_manager {{{ + +ohos_unittest("UTTest_auth_manager") { + module_out_path = module_out_path + + include_dirs = [] + + sources = [ + "${devicemanager_path}/test/unittest/UTTest_auth_manager.cpp", + "${devicemanager_path}/test/unittest/mock/distributed_device_profile_client_mock.cpp", + ] + + deps = [ ":device_manager_test_common" ] + + external_deps = [ + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "device_auth:deviceauth_sdk", + "device_info_manager:distributed_device_profile_common", + "device_info_manager:distributed_device_profile_sdk", + "dsoftbus:softbus_client", + "ffrt:libffrt", + "googletest:gmock", + "googletest:gmock_main", + "hilog:libhilog", + ] +} + +## UnitTest UTTest_auth_manager }}} + ## UnitTest UTTest_auth_confirm {{{ ohos_unittest("UTTest_auth_confirm") { diff --git a/test/unittest/UTTest_auth_manager.cpp b/test/unittest/UTTest_auth_manager.cpp new file mode 100644 index 000000000..0806bdc71 --- /dev/null +++ b/test/unittest/UTTest_auth_manager.cpp @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "UTTest_auth_manager.h" +#include "distributed_device_profile_client.h" +#include "dm_auth_state.h" +using namespace testing; +namespace OHOS { +namespace DistributedHardware { + +void AuthManagerTest::SetUp() +{ + softbusConnector = std::make_shared(); + deviceManagerServicelistener = std::make_shared(); + hiChainAuthConnector = std::make_shared(); + hiChainConnector = std::make_shared(); + authManager = std::make_shared(softbusConnector, hiChainConnector, deviceManagerServicelistener, + hiChainAuthConnector); +} + +void AuthManagerTest::TearDown() +{ + softbusConnector = nullptr; + deviceManagerServicelistener = nullptr; + hiChainAuthConnector = nullptr; + hiChainConnector = nullptr; + authManager = nullptr; +} + +void AuthManagerTest::SetUpTestCase() +{ + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = + distributedDeviceProfileClientMock_; +} + +void AuthManagerTest::TearDownTestCase() +{ + DistributedDeviceProfile::DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient = nullptr; + distributedDeviceProfileClientMock_ = nullptr; +} + +HWTEST_F(AuthManagerTest, HandleBusinessEvents_001, testing::ext::TestSize.Level1) +{ + std::string businessId = "testBusinessId"; + int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH; + EXPECT_CALL(*distributedDeviceProfileClientMock_, + PutBusinessEvent(::testing::_)).WillOnce(::testing::Return(DM_OK)); + + int32_t ret = authManager->HandleBusinessEvents(businessId, action); + EXPECT_EQ(ret, DM_OK); +} + +HWTEST_F(AuthManagerTest, HandleBusinessEvents_002, testing::ext::TestSize.Level1) +{ + std::string businessId = "testBusinessId"; + int32_t action = USER_OPERATION_TYPE_CANCEL_AUTH; + + EXPECT_CALL(*distributedDeviceProfileClientMock_, + PutBusinessEvent(::testing::_)).WillOnce(::testing::Return(ERR_DM_AUTH_NOT_START)); + + int32_t ret = authManager->HandleBusinessEvents(businessId, action); + EXPECT_EQ(ret, ERR_DM_AUTH_NOT_START); +} + +HWTEST_F(AuthManagerTest, ParseJsonObject_001, testing::ext::TestSize.Level1) +{ + JsonObject jsonObject; + jsonObject[DM_BUSINESS_ID] = "testBusinessId"; + + authManager->ParseJsonObject(jsonObject); + EXPECT_EQ(jsonObject[DM_BUSINESS_ID].Get(), "testBusinessId"); +} + +HWTEST_F(AuthManagerTest, OnUserOperation_001, testing::ext::TestSize.Level1) +{ + int32_t action = UiAction::USER_OPERATION_TYPE_CANCEL_AUTH; + std::string params = "testParams"; + int32_t ret = authManager->OnUserOperation(action, params); + EXPECT_EQ(ret, DM_OK); +} +} // namespace DistributedHardware +} // namespace OHOS diff --git a/test/unittest/UTTest_auth_manager.h b/test/unittest/UTTest_auth_manager.h new file mode 100644 index 000000000..3c6f59eea --- /dev/null +++ b/test/unittest/UTTest_auth_manager.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_UTTEST_AUTH_MANAGER_H +#define OHOS_UTTEST_AUTH_MANAGER_H + +#include + +#include "auth_manager.h" +#include "device_manager_service_listener.h" +#include "hichain_connector.h" +#include "softbus_connector.h" +#include "distributed_device_profile_client_mock.h" + +namespace OHOS { +namespace DistributedHardware { +class AuthManagerTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +private: + static inline std::shared_ptr + distributedDeviceProfileClientMock_ = + std::make_shared(); + + std::shared_ptr softbusConnector; + std::shared_ptr deviceManagerServicelistener; + std::shared_ptr hiChainAuthConnector; + std::shared_ptr hiChainConnector; + std::shared_ptr authManager; +}; +} // namespace DistributedHardware +} // namespace OHOS +#endif diff --git a/test/unittest/UTTest_auth_negotiate.cpp b/test/unittest/UTTest_auth_negotiate.cpp index 9fb20157b..2e2bb60f9 100644 --- a/test/unittest/UTTest_auth_negotiate.cpp +++ b/test/unittest/UTTest_auth_negotiate.cpp @@ -81,6 +81,24 @@ void AuthNegotiateTest::TearDown() tokenMock = nullptr; } +int32_t GetBusinessEventMockTrue(DistributedDeviceProfile::BusinessEvent &event) +{ + event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":true}"); + return DM_OK; +} + +int32_t GetBusinessEventMockEmpty(DistributedDeviceProfile::BusinessEvent &event) +{ + event.SetBusinessValue(""); + return DM_OK; +} + +int32_t GetBusinessEventMockFalse(DistributedDeviceProfile::BusinessEvent &event) +{ + event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":false}"); + return DM_OK; +} + HWTEST_F(AuthNegotiateTest, AuthSrcStartState_001, testing::ext::TestSize.Level1) { std::shared_ptr authState = std::make_shared(); @@ -233,10 +251,7 @@ HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_Action_003, testing::e context->businessId = "test_business_id"; EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_)) - .WillOnce(::testing::Invoke([](DistributedDeviceProfile::BusinessEvent &event) { - event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":true}"); - return DM_OK; - })); + .WillOnce(::testing::Invoke(GetBusinessEventMockTrue)); int32_t result = authState->Action(context); @@ -252,10 +267,7 @@ HWTEST_F(AuthNegotiateTest, AuthSinkNegotiateStateMachine_Action_004, testing::e context->businessId = "test_business_id"; EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_)) - .WillOnce(::testing::Invoke([](DistributedDeviceProfile::BusinessEvent &event) { - event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":false}"); - return DM_OK; - })); + .WillOnce(::testing::Invoke(GetBusinessEventMockFalse)); int32_t result = authState->Action(context); @@ -288,10 +300,7 @@ HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_003, testing::ext::TestSize.Le std::string businessId = "test_business_id"; EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_)) - .WillOnce(::testing::Invoke([](DistributedDeviceProfile::BusinessEvent &event) { - event.SetBusinessValue(""); - return DM_OK; - })); + .WillOnce(::testing::Invoke(GetBusinessEventMockEmpty)); EXPECT_FALSE(authState->IsAntiDisturbanceMode(businessId)); } @@ -302,10 +311,7 @@ HWTEST_F(AuthNegotiateTest, IsAntiDisturbanceMode_004, testing::ext::TestSize.Le std::string businessId = "test_business_id"; EXPECT_CALL(*distributedDeviceProfileClientMock_, GetBusinessEvent(::testing::_)) - .WillOnce(::testing::Invoke([](DistributedDeviceProfile::BusinessEvent &event) { - event.SetBusinessValue("{\"business_id\":\"test_business_id\",\"is_in_anti_disturbance_mode\":true}"); - return DM_OK; - })); + .WillOnce(::testing::Invoke(GetBusinessEventMockTrue)); EXPECT_TRUE(authState->IsAntiDisturbanceMode(businessId)); } diff --git a/test/unittest/mock/distributed_device_profile_client_mock.cpp b/test/unittest/mock/distributed_device_profile_client_mock.cpp index fc2eb614f..c67eda3c1 100644 --- a/test/unittest/mock/distributed_device_profile_client_mock.cpp +++ b/test/unittest/mock/distributed_device_profile_client_mock.cpp @@ -91,5 +91,10 @@ int32_t DistributedDeviceProfileClient::GetBusinessEvent(BusinessEvent &event) { return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->GetBusinessEvent(event); } + +int32_t DistributedDeviceProfileClient::PutBusinessEvent(const DistributedDeviceProfile::BusinessEvent &event) +{ + return DpDistributedDeviceProfileClient::dpDistributedDeviceProfileClient->PutBusinessEvent(event); +} } // namespace DistributedHardware } // namespace OHOS \ No newline at end of file diff --git a/test/unittest/mock/distributed_device_profile_client_mock.h b/test/unittest/mock/distributed_device_profile_client_mock.h index 4c208fbf3..13495ff3a 100644 --- a/test/unittest/mock/distributed_device_profile_client_mock.h +++ b/test/unittest/mock/distributed_device_profile_client_mock.h @@ -40,7 +40,8 @@ public: virtual int32_t UpdateLocalServiceInfo(const LocalServiceInfo& localServiceInfo) = 0; virtual int32_t GetLocalServiceInfoByBundleAndPinType(const std::string& bundleName, int32_t pinExchangeType, LocalServiceInfo& localServiceInfo) = 0; - virtual int32_t GetBusinessEvent(BusinessEvent &event) = 0; + virtual int32_t GetBusinessEvent(BusinessEvent &event) = 0; + virtual int32_t PutBusinessEvent(const DistributedDeviceProfile::BusinessEvent &event) = 0; public: static inline std::shared_ptr dpDistributedDeviceProfileClient = nullptr; }; @@ -59,7 +60,8 @@ public: MOCK_METHOD(int32_t, DeleteLocalServiceInfo, (const std::string&, int32_t)); MOCK_METHOD(int32_t, UpdateLocalServiceInfo, (const LocalServiceInfo&)); MOCK_METHOD(int32_t, GetLocalServiceInfoByBundleAndPinType, (const std::string&, int32_t, LocalServiceInfo&)); - MOCK_METHOD(int32_t, GetBusinessEvent, (BusinessEvent &event)); + MOCK_METHOD(int32_t, GetBusinessEvent, (BusinessEvent &event)); + MOCK_METHOD(int32_t, PutBusinessEvent, (const DistributedDeviceProfile::BusinessEvent &event)); }; } } -- Gitee From 3d6f7e9ca7240c33fa161ab3853293dec01ec1c1 Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 11 Jun 2025 21:17:49 +0800 Subject: [PATCH 3/4] modify format Signed-off-by: BrainL --- .../include/authentication_v2/dm_auth_message_processor.h | 2 +- .../src/authentication_v2/auth_stages/auth_negotiate.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/services/implementation/include/authentication_v2/dm_auth_message_processor.h b/services/implementation/include/authentication_v2/dm_auth_message_processor.h index 0e6987f98..a9b348b2f 100644 --- a/services/implementation/include/authentication_v2/dm_auth_message_processor.h +++ b/services/implementation/include/authentication_v2/dm_auth_message_processor.h @@ -322,7 +322,7 @@ private: DmAccess &accessTmp); void ParseUltrasonicSide(const JsonObject &jsonObject, std::shared_ptr context); void ParseCert(const JsonObject &jsonObject, std::shared_ptr context); - void ParseAccesserInfo(const JsonObject &jsonObject, std::shared_ptr context); + void ParseAccesserInfo(const JsonObject &jsonObject, std::shared_ptr context); std::shared_ptr cryptoMgr_ = nullptr; std::unordered_map createMessageFuncMap_; std::unordered_map paraseMessageFuncMap_; diff --git a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp index 847681d36..eaebb8e63 100644 --- a/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp +++ b/services/implementation/src/authentication_v2/auth_stages/auth_negotiate.cpp @@ -228,7 +228,7 @@ int32_t VerifyCertificate(std::shared_ptr context) int32_t AuthSinkNegotiateStateMachine::Action(std::shared_ptr context) { LOGI("AuthSinkNegotiateStateMachine::Action sessionid %{public}d", context->sessionId); - if (IsAntiDisturbanceMode(context->businessId)) { + if (IsAntiDisturbanceMode(context->businessId)) { LOGI("Sink is AntiDisturbMode."); context->reason = ERR_DM_ANTI_DISTURB_MODE; return ERR_DM_ANTI_DISTURB_MODE; -- Gitee From dbf48c587b306a0068ab84c98f7df44740644b62 Mon Sep 17 00:00:00 2001 From: BrainL Date: Wed, 11 Jun 2025 21:29:01 +0800 Subject: [PATCH 4/4] modify gn format Signed-off-by: BrainL --- test/unittest/BUILD.gn | 2 +- test/unittest/UTTest_auth_manager.cpp | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 505b89c89..3438965bc 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -24,9 +24,9 @@ group("unittest") { ":UTTest_auth_acl", ":UTTest_auth_confirm", ":UTTest_auth_credential_state", + ":UTTest_auth_manager", ":UTTest_auth_message_processor", ":UTTest_auth_negotiate", - ":UTTest_auth_manager", ":UTTest_auth_pin_auth_state", ":UTTest_auth_request_state", ":UTTest_auth_response_state", diff --git a/test/unittest/UTTest_auth_manager.cpp b/test/unittest/UTTest_auth_manager.cpp index 0806bdc71..29ed84fc4 100644 --- a/test/unittest/UTTest_auth_manager.cpp +++ b/test/unittest/UTTest_auth_manager.cpp @@ -14,8 +14,10 @@ */ #include "UTTest_auth_manager.h" + #include "distributed_device_profile_client.h" #include "dm_auth_state.h" + using namespace testing; namespace OHOS { namespace DistributedHardware { -- Gitee