From d6ebd88ae6bc17e93466c5e0379967edaa78809e Mon Sep 17 00:00:00 2001 From: yangli Date: Mon, 28 Oct 2024 21:21:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BC=96=E8=AF=91=E9=80=89?= =?UTF-8?q?=E9=A1=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: yangli --- distributedfile.gni | 6 ++ services/distributedfiledaemon/BUILD.gn | 4 ++ .../src/network/softbus/softbus_agent.cpp | 18 ++++-- .../src/network/softbus/softbus_handler.cpp | 20 +++++++ .../softbus/softbus_session_dispatcher.cpp | 5 ++ .../softbus/softbus_session_listener.cpp | 5 ++ .../test/unittest/BUILD.gn | 55 +++++++++++++++++++ .../network/softbus/softbus_agent_test.cpp | 2 + .../network/softbus/softbus_handler_test.cpp | 8 ++- 9 files changed, 116 insertions(+), 7 deletions(-) diff --git a/distributedfile.gni b/distributedfile.gni index eb5f5ac4..adafc208 100644 --- a/distributedfile.gni +++ b/distributedfile.gni @@ -29,6 +29,7 @@ image_framework_path = "//foundation/multimedia/image_framework" clouddisk_database_path = "${services_path}/clouddisk_database" declare_args() { + support_same_account = true if (defined(global_parts_info) && defined(global_parts_info.powermgr_battery_manager)) { cloudsync_service_power = true @@ -56,4 +57,9 @@ declare_args() { } else { cloudsync_service_hicollie_enable = false } + + if (!defined(global_parts_info) || !defined( + global_parts_info.distributedhardware_distributed_hardware_adapter)) { + support_same_account = false + } } diff --git a/services/distributedfiledaemon/BUILD.gn b/services/distributedfiledaemon/BUILD.gn index b654aab7..040210d4 100644 --- a/services/distributedfiledaemon/BUILD.gn +++ b/services/distributedfiledaemon/BUILD.gn @@ -104,6 +104,10 @@ ohos_shared_library("libdistributedfiledaemon") { defines = [ "LOG_TAG=\"distributedfile_daemon\"" ] + if (support_same_account) { + defines += [ "SUPPORT_SAME_ACCOUNT" ] + } + part_name = "dfs_service" subsystem_name = "filemanagement" kernel_permission_path = "./encaps.json" diff --git a/services/distributedfiledaemon/src/network/softbus/softbus_agent.cpp b/services/distributedfiledaemon/src/network/softbus/softbus_agent.cpp index 6cf371f9..2520817e 100644 --- a/services/distributedfiledaemon/src/network/softbus/softbus_agent.cpp +++ b/services/distributedfiledaemon/src/network/softbus/softbus_agent.cpp @@ -41,7 +41,9 @@ using namespace std; const int32_t DFS_QOS_TYPE_MIN_BW = 90 * 1024 * 1024; const int32_t DFS_QOS_TYPE_MAX_LATENCY = 10000; const int32_t DFS_QOS_TYPE_MIN_LATENCY = 2000; +#ifdef SUPPORT_SAME_ACCOUNT const uint32_t MAX_ONLINE_DEVICE_SIZE = 10000; +#endif SoftbusAgent::SoftbusAgent(weak_ptr mountPoint) : NetworkAgentTemplate(mountPoint) { auto spt = mountPoint.lock(); @@ -58,6 +60,7 @@ SoftbusAgent::SoftbusAgent(weak_ptr mountPoint) : NetworkAgentTempla bool SoftbusAgent::IsSameAccount(const std::string &networkId) { +#ifdef SUPPORT_SAME_ACCOUNT std::vector deviceList; DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(IDaemon::SERVICE_NAME, "", deviceList); if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) { @@ -70,6 +73,9 @@ bool SoftbusAgent::IsSameAccount(const std::string &networkId) } } return false; +#else + return true; +#endif } int32_t SoftbusAgent::JudgeNetworkTypeIsWifi(const DeviceInfo &info) @@ -159,6 +165,10 @@ int32_t SoftbusAgent::OpenSession(const DeviceInfo &info, const uint8_t &linkTyp { LOGI("Start to OpenSession, cid:%{public}s, linkType:%{public}d", Utils::GetAnonyString(info.GetCid()).c_str(), linkType); + if (!IsSameAccount(info.GetCid())) { + LOGI("The source and sink device is not same account, not support."); + return FileManagement::E_INVAL_ARG; + } ISocketListener sessionListener = { .OnBind = SoftbusSessionDispatcher::OnSessionOpened, .OnShutdown = SoftbusSessionDispatcher::OnSessionClosed, @@ -181,10 +191,6 @@ int32_t SoftbusAgent::OpenSession(const DeviceInfo &info, const uint8_t &linkTyp int32_t socketId = Socket(clientInfo); if (socketId < FileManagement::E_OK) { LOGE("Create OpenSoftbusChannel Socket error"); - RADAR_REPORT(RadarReporter::DFX_SET_DFS, RadarReporter::DFX_BUILD__LINK, RadarReporter::DFX_FAILED, - RadarReporter::BIZ_STATE, RadarReporter::DFX_END, RadarReporter::ERROR_CODE, - RadarReporter::CREAT_SOCKET_ERROR, RadarReporter::PACKAGE_NAME, - RadarReporter::dSoftBus + to_string(socketId)); return FileManagement::E_CONTEXT; } int32_t ret = Bind(socketId, qos, sizeof(qos) / sizeof(qos[0]), &sessionListener); @@ -211,6 +217,10 @@ void SoftbusAgent::OpenApSession(const DeviceInfo &info, const uint8_t &linkType { LOGI("Start to OpenApSession, cid:%{public}s, linkType:%{public}d", Utils::GetAnonyString(info.GetCid()).c_str(), linkType); + if (!IsSameAccount(info.GetCid())) { + LOGI("The source and sink device is not same account, not support."); + return; + } if (JudgeNetworkTypeIsWifi(info)) { LOGI("networktype is not wifi"); return; diff --git a/services/distributedfiledaemon/src/network/softbus/softbus_handler.cpp b/services/distributedfiledaemon/src/network/softbus/softbus_handler.cpp index 02b58645..abfa14ce 100644 --- a/services/distributedfiledaemon/src/network/softbus/softbus_handler.cpp +++ b/services/distributedfiledaemon/src/network/softbus/softbus_handler.cpp @@ -37,7 +37,9 @@ const int32_t DFS_QOS_TYPE_MIN_BW = 90 * 1024 * 1024; const int32_t DFS_QOS_TYPE_MAX_LATENCY = 10000; const int32_t DFS_QOS_TYPE_MIN_LATENCY = 2000; const int32_t INVALID_SESSION_ID = -1; +#ifdef SUPPORT_SAME_ACCOUNT const uint32_t MAX_ONLINE_DEVICE_SIZE = 10000; +#endif constexpr size_t MAX_SIZE = 500; std::mutex SoftBusHandler::clientSessNameMapMutex_; std::map SoftBusHandler::clientSessNameMap_; @@ -47,6 +49,16 @@ std::mutex SoftBusHandler::networkIdMapMutex_; std::map SoftBusHandler::networkIdMap_; void SoftBusHandler::OnSinkSessionOpened(int32_t sessionId, PeerSocketInfo info) { + if (!SoftBusHandler::IsSameAccount(info.networkId)) { + std::lock_guard lock(serverIdMapMutex_); + auto it = serverIdMap_.find(info.name); + if (it != serverIdMap_.end()) { + Shutdown(it->second); + serverIdMap_.erase(it); + LOGI("RemoveSessionServer success."); + } + Shutdown(sessionId); + } AllConnectManager::GetInstance().PublishServiceState(info.networkId, ServiceCollaborationManagerBussinessStatus::SCM_CONNECTED); { @@ -59,6 +71,7 @@ void SoftBusHandler::OnSinkSessionOpened(int32_t sessionId, PeerSocketInfo info) bool SoftBusHandler::IsSameAccount(const std::string &networkId) { +#ifdef SUPPORT_SAME_ACCOUNT std::vector deviceList; DistributedHardware::DeviceManager::GetInstance().GetTrustedDeviceList(SERVICE_NAME, "", deviceList); if (deviceList.size() == 0 || deviceList.size() > MAX_ONLINE_DEVICE_SIZE) { @@ -71,6 +84,9 @@ bool SoftBusHandler::IsSameAccount(const std::string &networkId) } } return false; +#else + return true; +#endif } std::string SoftBusHandler::GetSessionName(int32_t sessionId) @@ -167,6 +183,10 @@ int32_t SoftBusHandler::OpenSession(const std::string &mySessionName, const std: return FileManagement::ERR_BAD_VALUE; } LOGI("OpenSession Enter peerDevId: %{public}s", Utils::GetAnonyString(peerDevId).c_str()); + if (!IsSameAccount(peerDevId)) { + LOGI("The source and sink device is not same account, not support."); + return E_OPEN_SESSION; + } QosTV qos[] = { {.qos = QOS_TYPE_MIN_BW, .value = DFS_QOS_TYPE_MIN_BW}, {.qos = QOS_TYPE_MAX_LATENCY, .value = DFS_QOS_TYPE_MAX_LATENCY}, diff --git a/services/distributedfiledaemon/src/network/softbus/softbus_session_dispatcher.cpp b/services/distributedfiledaemon/src/network/softbus/softbus_session_dispatcher.cpp index 8ead4906..7c3198c1 100644 --- a/services/distributedfiledaemon/src/network/softbus/softbus_session_dispatcher.cpp +++ b/services/distributedfiledaemon/src/network/softbus/softbus_session_dispatcher.cpp @@ -81,6 +81,11 @@ weak_ptr SoftbusSessionDispatcher::GetAgent(int32_t sessionId, std void SoftbusSessionDispatcher::OnSessionOpened(int32_t sessionId, PeerSocketInfo info) { LOGI("OnSessionOpened Enter sessionId = %{public}d", sessionId); + if (!SoftbusAgent::IsSameAccount(info.networkId)) { + LOGI("The source and sink device is not same account, not support."); + Shutdown(sessionId); + return; + } std::string peerSessionName(info.name); std::string peerDevId = info.networkId; { diff --git a/services/distributedfiledaemon/src/network/softbus/softbus_session_listener.cpp b/services/distributedfiledaemon/src/network/softbus/softbus_session_listener.cpp index 3d9d8381..9aa13165 100644 --- a/services/distributedfiledaemon/src/network/softbus/softbus_session_listener.cpp +++ b/services/distributedfiledaemon/src/network/softbus/softbus_session_listener.cpp @@ -73,6 +73,11 @@ std::vector SoftBusSessionListener::GetFileName(const std::vector agent = std::make_shared(wmp); bool flag = agent->IsSameAccount(NETWORKID_ONE); EXPECT_EQ(flag, true); +#ifdef SUPPORT_SAME_ACCOUNT flag = agent->IsSameAccount(NETWORKID_TWO); EXPECT_EQ(flag, false); flag = agent->IsSameAccount(NETWORKID_THREE); @@ -138,6 +139,7 @@ HWTEST_F(SoftbusAgentTest, SoftbusAgentTest_IsSameAccount_0100, TestSize.Level1) g_mockGetTrustedDeviceList = false; flag = agent->IsSameAccount(NETWORKID_ONE); EXPECT_EQ(flag, false); +#endif g_mockGetTrustedDeviceList = true; GTEST_LOG_(INFO) << "SoftbusAgentTest_IsSameAccount_0100 end"; } diff --git a/services/distributedfiledaemon/test/unittest/network/softbus/softbus_handler_test.cpp b/services/distributedfiledaemon/test/unittest/network/softbus/softbus_handler_test.cpp index 9d15fa82..695c25bd 100644 --- a/services/distributedfiledaemon/test/unittest/network/softbus/softbus_handler_test.cpp +++ b/services/distributedfiledaemon/test/unittest/network/softbus/softbus_handler_test.cpp @@ -302,6 +302,7 @@ HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_IsSameAccount_0100, TestSize.Lev SoftBusHandler handler; bool flag = handler.IsSameAccount(TEST_NETWORKID); EXPECT_EQ(flag, true); +#ifdef SUPPORT_SAME_ACCOUNT flag = handler.IsSameAccount(TEST_NETWORKID_TWO); EXPECT_EQ(flag, false); flag = handler.IsSameAccount(TEST_NETWORKID_THREE); @@ -309,6 +310,7 @@ HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_IsSameAccount_0100, TestSize.Lev g_mockGetTrustedDeviceList = false; flag = handler.IsSameAccount(TEST_NETWORKID); EXPECT_EQ(flag, false); +#endif g_mockGetTrustedDeviceList = true; GTEST_LOG_(INFO) << "SoftbusHandlerTest_IsSameAccount_0100 end"; } @@ -355,14 +357,14 @@ HWTEST_F(SoftbusHandlerTest, SoftbusHandlerTest_OnSinkSessionOpened_0100, TestSi EXPECT_EQ(handler.GetSessionName(sessionId1), sessionName1); EXPECT_EQ(handler.GetSessionName(sessionId2), sessionName2); EXPECT_EQ(handler.GetSessionName(sessionId3), sessionName3); +#ifdef SUPPORT_SAME_ACCOUNT auto iter = handler.serverIdMap_.find(sessionName2); - if (iter != handler.serverIdMap_.end()) { - handler.serverIdMap_.clear(); + if (iter == handler.serverIdMap_.end()) { EXPECT_TRUE(true); } else { EXPECT_TRUE(false); } - +#endif handler.clientSessNameMap_.erase(sessionId1); handler.clientSessNameMap_.erase(sessionId2); handler.clientSessNameMap_.erase(sessionId3);