From 86055db1872b413693c9f0394f492cdf7ada5da1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Sun, 6 Jul 2025 18:57:47 +0800 Subject: [PATCH 01/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- .../netvpnclient/src/networkvpn_client.cpp | 174 +++++++++++++++--- .../netvpnclient/include/networkvpn_client.h | 40 +++- sa_profile/1155.xml | 2 +- .../networkvpn_client_test.cpp | 2 +- .../networkvpn_client_test.cpp | 47 ++++- 5 files changed, 234 insertions(+), 31 deletions(-) diff --git a/frameworks/native/netvpnclient/src/networkvpn_client.cpp b/frameworks/native/netvpnclient/src/networkvpn_client.cpp index 93eba99c..a733da85 100644 --- a/frameworks/native/netvpnclient/src/networkvpn_client.cpp +++ b/frameworks/native/netvpnclient/src/networkvpn_client.cpp @@ -40,12 +40,106 @@ int32_t VpnSetUpEventCallback::OnVpnMultiUserSetUp() return NETMANAGER_EXT_SUCCESS; } +int32_t VpnEventCallbackCollection::OnVpnStateChanged(bool isConnected) +{ + for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { + (*iter)->OnVpnStateChanged(isConnected); + } + return NETMANAGER_EXT_SUCCESS; +} + +int32_t VpnEventCallbackCollection::OnMultiVpnStateChanged( + bool isConnected, const std::string &bundleName, const std::string &vpnId) +{ + for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { + (*iter)->OnMultiVpnStateChanged(isConnected, bundleName, vpnId); + } + return NETMANAGER_EXT_SUCCESS; +} + +int32_t VpnEventCallbackCollection::OnVpnMultiUserSetUp() +{ + for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { + (*iter)->OnVpnMultiUserSetUp(); + } + return NETMANAGER_EXT_SUCCESS; +} + +int32_t VpnEventCallbackCollection::RegisterCallback(sptr callback) +{ + for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { + if ((*iter)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) { + return NETMANAGER_EXT_ERR_OPERATION_FAILED; + } + } + vpnEventCbList_.push_back(callback); + return NETMANAGER_EXT_SUCCESS; +} + +int32_t VpnEventCallbackCollection::UnregisterCallback(sptr callback) +{ + for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { + if ((*iter)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) { + vpnEventCbList_.erase(iter); + break; + } + } + return NETMANAGER_EXT_SUCCESS; +} + +NetworkVpnClient::NetworkVpnClient() +{ + Subscribe(); +} + +NetworkVpnClient::~NetworkVpnClient() +{ + Unsubscribe(); + UnregisterVpnEventCbCollection(); +#ifdef SUPPORT_SYSVPN + UnregisterMultiVpnEventCbCollection(); +#endif +} + NetworkVpnClient &NetworkVpnClient::GetInstance() { static NetworkVpnClient instance; return instance; } +void NetworkVpnClient::Subscribe() +{ + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy != nullptr) { + saStatusChangeListener_ = sptr::MakeSptr(); + samgrProxy->SubscribeSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID, saStatusChangeListener_); + } +} + +void NetworkVpnClient::Unsubscribe() +{ + if (saStatusChangeListener_ != nullptr) { + auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (samgrProxy != nullptr) { + samgrProxy->UnSubscribeSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID, saStatusChangeListener_); + } + } +} + +void NetworkVpnClient::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) +{ + switch (systemAbilityId) { + case COMM_VPN_MANAGER_SYS_ABILITY_ID: + NetworkVpnClient::GetInstance().RegisterVpnEventCbCollection(); +#ifdef SUPPORT_SYSVPN + NetworkVpnClient::GetInstance().RegisterMultiVpnEventCbCollection(); +#endif + break; + default: + break; + } +} + int32_t NetworkVpnClient::Prepare(bool &isExistVpn, bool &isRun, std::string &pkg) { sptr proxy = GetProxy(); @@ -280,12 +374,10 @@ int32_t NetworkVpnClient::RegisterMultiVpnEvent(sptr callback NETMGR_EXT_LOG_E("RegisterMultiVpnEvent callback is null."); return NETMANAGER_EXT_ERR_PARAMETER_ERROR; } - sptr proxy = GetProxy(); - if (proxy == nullptr) { - NETMGR_EXT_LOG_E("RegisterMultiVpnEvent proxy is nullptr"); - return NETMANAGER_EXT_ERR_GET_PROXY_FAIL; + if (multiVpnEventCbCollection_ == nullptr) { + multiVpnEventCbCollection_ = sptr::MakeSptr(); } - return proxy->RegisterMultiVpnEvent(callback); + return multiVpnEventCbCollection_->RegisterCallback(callback); } int32_t NetworkVpnClient::UnregisterMultiVpnEvent(sptr callback) @@ -294,12 +386,35 @@ int32_t NetworkVpnClient::UnregisterMultiVpnEvent(sptr callba NETMGR_EXT_LOG_E("UnregisterMultiVpnEvent callback is null."); return NETMANAGER_EXT_ERR_PARAMETER_ERROR; } + if (multiVpnEventCbCollection_ != nullptr) { + return multiVpnEventCbCollection_->UnregisterCallback(callback); + } + return NETMANAGER_EXT_SUCCESS; +} + +void NetworkVpnClient::RegisterMultiVpnEventCbCollection() +{ + if (multiVpnEventCbCollection_ == nullptr) { + multiVpnEventCbCollection_ = sptr::MakeSptr(); + } sptr proxy = GetProxy(); if (proxy == nullptr) { - NETMGR_EXT_LOG_E("UnregisterMultiVpnEvent proxy is nullptr"); - return NETMANAGER_EXT_ERR_GET_PROXY_FAIL; + NETMGR_EXT_LOG_E("RegisterMultiVpnEventCbCollection proxy is nullptr"); + return; + } + proxy->RegisterMultiVpnEvent(multiVpnEventCbCollection_); +} + +void NetworkVpnClient::UnregisterMultiVpnEventCbCollection() +{ + if (multiVpnEventCbCollection_ != nullptr) { + sptr proxy = GetProxy(); + if (proxy == nullptr) { + NETMGR_EXT_LOG_E("UnregisterMultiVpnEventCbCollection proxy is nullptr"); + return; + } + proxy->UnregisterMultiVpnEvent(multiVpnEventCbCollection_); } - return proxy->UnregisterMultiVpnEvent(callback); } #endif // SUPPORT_SYSVPN @@ -309,12 +424,10 @@ int32_t NetworkVpnClient::RegisterVpnEvent(sptr callback) NETMGR_EXT_LOG_E("RegisterVpnEvent callback is null."); return NETMANAGER_EXT_ERR_PARAMETER_ERROR; } - sptr proxy = GetProxy(); - if (proxy == nullptr) { - NETMGR_EXT_LOG_E("RegisterVpnEvent proxy is nullptr"); - return NETMANAGER_EXT_ERR_GET_PROXY_FAIL; + if (vpnEventCbCollection_ == nullptr) { + vpnEventCbCollection_ = sptr::MakeSptr(); } - return proxy->RegisterVpnEvent(callback); + return vpnEventCbCollection_->RegisterCallback(callback); } int32_t NetworkVpnClient::UnregisterVpnEvent(sptr callback) @@ -323,12 +436,35 @@ int32_t NetworkVpnClient::UnregisterVpnEvent(sptr callback) NETMGR_EXT_LOG_E("UnregisterVpnEvent callback is null."); return NETMANAGER_EXT_ERR_PARAMETER_ERROR; } + if (vpnEventCbCollection_ != nullptr) { + return vpnEventCbCollection_->UnregisterCallback(callback); + } + return NETMANAGER_EXT_SUCCESS; +} + +void NetworkVpnClient::RegisterVpnEventCbCollection() +{ + if (vpnEventCbCollection_ == nullptr) { + vpnEventCbCollection_ = sptr::MakeSptr(); + } sptr proxy = GetProxy(); if (proxy == nullptr) { - NETMGR_EXT_LOG_E("UnregisterVpnEvent proxy is nullptr"); - return NETMANAGER_EXT_ERR_GET_PROXY_FAIL; + NETMGR_EXT_LOG_E("RegisterVpnEventCbCollection proxy is nullptr"); + return; + } + proxy->RegisterVpnEvent(vpnEventCbCollection_); +} + +void NetworkVpnClient::UnregisterVpnEventCbCollection() +{ + if (vpnEventCbCollection_ != nullptr) { + sptr proxy = GetProxy(); + if (proxy == nullptr) { + NETMGR_EXT_LOG_E("UnregisterVpnEventCbCollection proxy is nullptr"); + return; + } + proxy->UnregisterVpnEvent(vpnEventCbCollection_); } - return proxy->UnregisterVpnEvent(callback); } int32_t NetworkVpnClient::CreateVpnConnection(bool isVpnExtCall) @@ -363,7 +499,7 @@ sptr NetworkVpnClient::GetProxy() NETMGR_EXT_LOG_E("get SystemAbilityManager failed"); return nullptr; } - sptr remote = sam->CheckSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID); + sptr remote = sam->GetSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID); if (remote == nullptr) { NETMGR_EXT_LOG_E("get Remote vpn service failed"); return nullptr; @@ -397,10 +533,6 @@ void NetworkVpnClient::RecoverCallback() proxy->SetUpVpn(*clientVpnConfig_.first, clientVpnConfig_.second); } NETMGR_EXT_LOG_D("Get proxy %{public}s, count: %{public}u", proxy == nullptr ? "failed" : "success", count); - if (proxy != nullptr && vpnEventCallback_ != nullptr) { - int32_t ret = proxy->RegisterVpnEvent(vpnEventCallback_); - NETMGR_EXT_LOG_D("Register result %{public}d", ret); - } } void NetworkVpnClient::OnRemoteDied(const wptr &remote) diff --git a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h index 113457a9..a8b09435 100644 --- a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h +++ b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h @@ -28,6 +28,7 @@ #include "inetwork_vpn_service.h" #include "ivpn_event_callback.h" #include "vpn_event_callback_stub.h" +#include "system_ability_status_change_stub.h" #include "vpn_interface.h" namespace OHOS { @@ -41,12 +42,28 @@ public: int32_t OnVpnMultiUserSetUp() override; }; +class VpnEventCallbackCollection final : public VpnEventCallbackStub { +public: + int32_t OnVpnStateChanged(bool isConnected) override; + int32_t OnMultiVpnStateChanged(bool isConnected, const std::string &bundleName, + const std::string &vpnId) override; + int32_t OnVpnMultiUserSetUp() override; + + int32_t RegisterCallback(sptr callback); + int32_t UnregisterCallback(sptr callback); + +private: + std::list> vpnEventCbList_; +}; + class NetworkVpnClient { private: - NetworkVpnClient() = default; - ~NetworkVpnClient() = default; + NetworkVpnClient(); + ~NetworkVpnClient(); NetworkVpnClient(const NetworkVpnClient &) = delete; NetworkVpnClient &operator=(const NetworkVpnClient &) = delete; + void Subscribe(); + void Unsubscribe(); public: static NetworkVpnClient &GetInstance(); @@ -283,16 +300,35 @@ private: NetworkVpnClient &client_; }; + class SystemAbilityListener : public SystemAbilityStatusChangeStub { + public: + SystemAbilityListener() = default; + ~SystemAbilityListener() = default; + void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override {}; + private: + void RegisterVpnEventCallback(); + } + sptr GetProxy(); void RecoverCallback(); void OnRemoteDied(const wptr &remote); + void RegisterVpnEventCbCollection(); + void UnregisterVpnEventCbCollection(); +#ifdef + void RegisterMuiltiVpnEventCbCollection(); + void UnregisterMultiVpnEventCbCollection(); +#endif private: std::mutex mutex_; VpnInterface vpnInterface_; + sptr saStatusChangeListener_ = nullptr; sptr vpnEventCallback_ = nullptr; sptr networkVpnService_ = nullptr; sptr deathRecipient_ = nullptr; + sptr vpnEventCbCollection_ = nullptr; + sptr multiVpnEventCbCollection_ = nullptr; std::pair, bool> clientVpnConfig_; }; } // namespace NetManagerStandard diff --git a/sa_profile/1155.xml b/sa_profile/1155.xml index b79415b0..00db8863 100644 --- a/sa_profile/1155.xml +++ b/sa_profile/1155.xml @@ -20,7 +20,7 @@ libnet_vpn_manager.z.so - true + false false 1 diff --git a/test/vpnmanager/unittest/sys_vpn_manager_test/networkvpn_client_test.cpp b/test/vpnmanager/unittest/sys_vpn_manager_test/networkvpn_client_test.cpp index dd1c870c..9ff62de5 100644 --- a/test/vpnmanager/unittest/sys_vpn_manager_test/networkvpn_client_test.cpp +++ b/test/vpnmanager/unittest/sys_vpn_manager_test/networkvpn_client_test.cpp @@ -307,7 +307,7 @@ HWTEST_F(NetworkVpnClientTest, RecoverCallback001, TestSize.Level1) HWTEST_F(NetworkVpnClientTest, OnRemoteDied001, TestSize.Level1) { sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - sptr remote = sam->CheckSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID); + sptr remote = sam->GetSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID); networkVpnClient_.networkVpnService_ = iface_cast(remote); networkVpnClient_.vpnEventCallback_ = new (std::nothrow) VpnSetUpEventCallback(); ASSERT_NE(networkVpnClient_.vpnEventCallback_, nullptr); diff --git a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp index 99c363fd..ad6f30c1 100644 --- a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp +++ b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp @@ -119,9 +119,10 @@ HWTEST_F(NetworkVpnClientTest, SetUpVpn002, TestSize.Level1) HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent001, TestSize.Level1) { + networkVpnClient_.vpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); callback_ = new (std::nothrow) IVpnEventCallbackTest(); - EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_ERR_PERMISSION_DENIED); + EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent002, TestSize.Level1) @@ -136,21 +137,44 @@ HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent001, TestSize.Level1) { EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); callback_ = new (std::nothrow) IVpnEventCallbackTest(); - EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(callback_), NETMANAGER_ERR_PERMISSION_DENIED); + EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent002, TestSize.Level1) { - NetManagerExtAccessToken access; + networkVpnClient_.vpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); callback_ = new (std::nothrow) IVpnEventCallbackTest(); - EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(callback_), NETMANAGER_EXT_ERR_OPERATION_FAILED); + EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); +} + +HWTEST_F(NetworkVpnClientTest, RegisterMultiVpnEvent001, TestSize.Level1) +{ + networkVpnClient_.multiVpnEventCbCollection_ = nullptr; + EXPECT_EQ(networkVpnClient_.RegisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); + callback_ = new (std::nothrow) IVpnEventCallbackTest(); + EXPECT_EQ(networkVpnClient_.RegisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); +} + +HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent001, TestSize.Level1) +{ + EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); + callback_ = new (std::nothrow) IVpnEventCallbackTest(); + EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); +} + +HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent002, TestSize.Level1) +{ + networkVpnClient_.multiVpnEventCbCollection_ = nullptr; + EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); + callback_ = new (std::nothrow) IVpnEventCallbackTest(); + EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } HWTEST_F(NetworkVpnClientTest, GetProxy, TestSize.Level1) { sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - sptr remote = sam->CheckSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID); + sptr remote = sam->GetSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID); networkVpnClient_.networkVpnService_ = iface_cast(remote); EXPECT_EQ(networkVpnClient_.GetProxy(), networkVpnClient_.networkVpnService_); networkVpnClient_.networkVpnService_ = nullptr; @@ -162,7 +186,7 @@ HWTEST_F(NetworkVpnClientTest, OnRemoteDied, TestSize.Level1) sptr remote = nullptr; networkVpnClient_.OnRemoteDied(remote); sptr sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - remote = sam->CheckSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID); + remote = sam->GetSystemAbility(COMM_VPN_MANAGER_SYS_ABILITY_ID); networkVpnClient_.networkVpnService_ = nullptr; networkVpnClient_.OnRemoteDied(remote); networkVpnClient_.networkVpnService_ = iface_cast(remote); @@ -216,5 +240,16 @@ HWTEST_F(NetworkVpnClientTest, GetVpnInterfaceFd, TestSize.Level1) int32_t fd = vpnInterface.GetVpnInterfaceFd(); EXPECT_GT(fd, 0); } + +HWTEST_F(NetworkVpnClientTest, VpnEventCallback, TestSize.Level1) +{ + callback_ = new (std::nothrow) IVpnEventCallbackTest(); + auto collection = sptr::MakeSptr(); + collection.push_back(callback_); + + EXPECT_EQ(collection->OnVpnStateChanged(true), NETMANAGER_EXT_SUCCESS); + EXPECT_EQ(collection->OnMultiVpnStateChanged(true, "test", "0"), NETMANAGER_EXT_SUCCESS); + EXPECT_EQ(collection->OnVpnMultiUserSetUp(), NETMANAGER_EXT_SUCCESS); +} } // namespace NetManagerStandard } // namespace OHOS -- Gitee From ae4a4e7b2ac18837f833bb46946ca4a4eee47def Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Mon, 7 Jul 2025 11:43:56 +0800 Subject: [PATCH 02/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- .../unittest/vpn_manager_test/networkvpn_client_test.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp index ad6f30c1..d3d827b2 100644 --- a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp +++ b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp @@ -156,14 +156,14 @@ HWTEST_F(NetworkVpnClientTest, RegisterMultiVpnEvent001, TestSize.Level1) EXPECT_EQ(networkVpnClient_.RegisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } -HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent001, TestSize.Level1) +HWTEST_F(NetworkVpnClientTest, UnregisterMultiVpnEvent001, TestSize.Level1) { EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); callback_ = new (std::nothrow) IVpnEventCallbackTest(); EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } -HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent002, TestSize.Level1) +HWTEST_F(NetworkVpnClientTest, UnregisterMultiVpnEvent002, TestSize.Level1) { networkVpnClient_.multiVpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); @@ -245,7 +245,7 @@ HWTEST_F(NetworkVpnClientTest, VpnEventCallback, TestSize.Level1) { callback_ = new (std::nothrow) IVpnEventCallbackTest(); auto collection = sptr::MakeSptr(); - collection.push_back(callback_); + collection->vpnEventCallback_.push_back(callback_); EXPECT_EQ(collection->OnVpnStateChanged(true), NETMANAGER_EXT_SUCCESS); EXPECT_EQ(collection->OnMultiVpnStateChanged(true, "test", "0"), NETMANAGER_EXT_SUCCESS); -- Gitee From ef4dbea8e3cf77127d90a1388101f9782f5e3147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Mon, 7 Jul 2025 12:18:42 +0800 Subject: [PATCH 03/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- interfaces/innerkits/netvpnclient/include/networkvpn_client.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h index a8b09435..8828e064 100644 --- a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h +++ b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h @@ -308,7 +308,7 @@ private: void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override {}; private: void RegisterVpnEventCallback(); - } + }; sptr GetProxy(); void RecoverCallback(); -- Gitee From 0865a75bde39f44933a8dc5d2b0dddc61b715e5e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Tue, 8 Jul 2025 09:25:23 +0800 Subject: [PATCH 04/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- .../native/netvpnclient/src/networkvpn_client.cpp | 10 ++++++++++ .../netvpnclient/include/networkvpn_client.h | 14 +++----------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/frameworks/native/netvpnclient/src/networkvpn_client.cpp b/frameworks/native/netvpnclient/src/networkvpn_client.cpp index a733da85..0210e478 100644 --- a/frameworks/native/netvpnclient/src/networkvpn_client.cpp +++ b/frameworks/native/netvpnclient/src/networkvpn_client.cpp @@ -33,6 +33,16 @@ namespace NetManagerStandard { static constexpr uint32_t WAIT_FOR_SERVICE_TIME_MS = 500; static constexpr uint32_t MAX_GET_SERVICE_COUNT = 10; +class NetworkVpnClient::SystemAbilityListener : public SystemAbilityStatusChangeStub { +public: + SystemAbilityListener() = default; + ~SystemAbilityListener() = default; + void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override {}; +private: + void RegisterVpnEventCallback(); +}; + int32_t VpnSetUpEventCallback::OnVpnMultiUserSetUp() { NETMGR_EXT_LOG_I("vpn multiple user setup event."); diff --git a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h index 8828e064..5b647787 100644 --- a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h +++ b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h @@ -300,22 +300,14 @@ private: NetworkVpnClient &client_; }; - class SystemAbilityListener : public SystemAbilityStatusChangeStub { - public: - SystemAbilityListener() = default; - ~SystemAbilityListener() = default; - void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; - void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override {}; - private: - void RegisterVpnEventCallback(); - }; + class SystemAbilityListener; sptr GetProxy(); void RecoverCallback(); void OnRemoteDied(const wptr &remote); void RegisterVpnEventCbCollection(); void UnregisterVpnEventCbCollection(); -#ifdef +#ifdef SUPPORT_SYSVPN void RegisterMuiltiVpnEventCbCollection(); void UnregisterMultiVpnEventCbCollection(); #endif @@ -323,7 +315,7 @@ private: private: std::mutex mutex_; VpnInterface vpnInterface_; - sptr saStatusChangeListener_ = nullptr; + sptr saStatusChangeListener_; sptr vpnEventCallback_ = nullptr; sptr networkVpnService_ = nullptr; sptr deathRecipient_ = nullptr; -- Gitee From 7214b8f29ccd78d2fc19e73e7c8880993a58f81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Tue, 8 Jul 2025 09:27:53 +0800 Subject: [PATCH 05/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- frameworks/native/netvpnclient/src/networkvpn_client.cpp | 1 + interfaces/innerkits/netvpnclient/include/networkvpn_client.h | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/netvpnclient/src/networkvpn_client.cpp b/frameworks/native/netvpnclient/src/networkvpn_client.cpp index 0210e478..e2199340 100644 --- a/frameworks/native/netvpnclient/src/networkvpn_client.cpp +++ b/frameworks/native/netvpnclient/src/networkvpn_client.cpp @@ -26,6 +26,7 @@ #include "netmgr_ext_log_wrapper.h" #include "system_ability_definition.h" #include "network_vpn_service_proxy.h" +#include "system_ability_status_change_stub.h" namespace OHOS { namespace NetManagerStandard { diff --git a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h index 5b647787..41ac3d3e 100644 --- a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h +++ b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h @@ -28,7 +28,6 @@ #include "inetwork_vpn_service.h" #include "ivpn_event_callback.h" #include "vpn_event_callback_stub.h" -#include "system_ability_status_change_stub.h" #include "vpn_interface.h" namespace OHOS { -- Gitee From a88d3fb5e3d8f5d3673f24b3191eb1d233474cf4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Tue, 8 Jul 2025 14:34:47 +0800 Subject: [PATCH 06/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- .../netvpnclient/src/networkvpn_client.cpp | 19 ++++++++++++++----- .../netvpnclient/include/networkvpn_client.h | 2 ++ 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/frameworks/native/netvpnclient/src/networkvpn_client.cpp b/frameworks/native/netvpnclient/src/networkvpn_client.cpp index e2199340..690ce879 100644 --- a/frameworks/native/netvpnclient/src/networkvpn_client.cpp +++ b/frameworks/native/netvpnclient/src/networkvpn_client.cpp @@ -40,8 +40,6 @@ public: ~SystemAbilityListener() = default; void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override {}; -private: - void RegisterVpnEventCallback(); }; int32_t VpnSetUpEventCallback::OnVpnMultiUserSetUp() @@ -53,7 +51,10 @@ int32_t VpnSetUpEventCallback::OnVpnMultiUserSetUp() int32_t VpnEventCallbackCollection::OnVpnStateChanged(bool isConnected) { - for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { + std::shared_lock lock(vpnEventCbMutex_); + std::list> tmpList = vpnEventCbList_; + lock.unlock(); + for (auto iter = tmpList.begin(); iter != tmpList.end(); iter++) { (*iter)->OnVpnStateChanged(isConnected); } return NETMANAGER_EXT_SUCCESS; @@ -62,7 +63,10 @@ int32_t VpnEventCallbackCollection::OnVpnStateChanged(bool isConnected) int32_t VpnEventCallbackCollection::OnMultiVpnStateChanged( bool isConnected, const std::string &bundleName, const std::string &vpnId) { - for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { + std::shared_lock lock(vpnEventCbMutex_); + std::list> tmpList = vpnEventCbList_; + lock.unlock(); + for (auto iter = tmpList.begin(); iter != tmpList.end(); iter++) { (*iter)->OnMultiVpnStateChanged(isConnected, bundleName, vpnId); } return NETMANAGER_EXT_SUCCESS; @@ -70,7 +74,10 @@ int32_t VpnEventCallbackCollection::OnMultiVpnStateChanged( int32_t VpnEventCallbackCollection::OnVpnMultiUserSetUp() { - for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { + std::shared_lock lock(vpnEventCbMutex_); + std::list> tmpList = vpnEventCbList_; + lock.unlock(); + for (auto iter = tmpList.begin(); iter != tmpList.end(); iter++) { (*iter)->OnVpnMultiUserSetUp(); } return NETMANAGER_EXT_SUCCESS; @@ -78,6 +85,7 @@ int32_t VpnEventCallbackCollection::OnVpnMultiUserSetUp() int32_t VpnEventCallbackCollection::RegisterCallback(sptr callback) { + std::unique_lock lock(vpnEventCbMutex_); for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { if ((*iter)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) { return NETMANAGER_EXT_ERR_OPERATION_FAILED; @@ -89,6 +97,7 @@ int32_t VpnEventCallbackCollection::RegisterCallback(sptr cal int32_t VpnEventCallbackCollection::UnregisterCallback(sptr callback) { + std::unique_lock lock(vpnEventCbMutex_); for (auto iter = vpnEventCbList_.begin(); iter != vpnEventCbList_.end(); iter++) { if ((*iter)->AsObject().GetRefPtr() == callback->AsObject().GetRefPtr()) { vpnEventCbList_.erase(iter); diff --git a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h index 41ac3d3e..afe639aa 100644 --- a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h +++ b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -52,6 +53,7 @@ public: int32_t UnregisterCallback(sptr callback); private: + std::shared_mutex vpnEventCbMutex_; std::list> vpnEventCbList_; }; -- Gitee From 3b5a375db3a80551690d191fd4b2ff3281f2e27d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Sun, 13 Jul 2025 20:33:30 +0800 Subject: [PATCH 07/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- frameworks/js/napi/vpn/src/networkvpn_service.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/js/napi/vpn/src/networkvpn_service.cpp b/frameworks/js/napi/vpn/src/networkvpn_service.cpp index 94f530cc..6b4072f8 100644 --- a/frameworks/js/napi/vpn/src/networkvpn_service.cpp +++ b/frameworks/js/napi/vpn/src/networkvpn_service.cpp @@ -1955,7 +1955,7 @@ void NetworkVpnService::OnRemoteDied(const wptr &remoteObject) } sptr callback = iface_cast(diedRemoted); UnregisterVpnEvent(callback); - std::lock_guard autoLock(remoteMutex_); + std::lock_guard autoLock(netVpnMutex_); #ifdef SUPPORT_SYSVPN if (vpnObj_ != nullptr && vpnObj_->IsSystemVpn()) { NETMGR_EXT_LOG_W("system vpn client died"); -- Gitee From a5fd32a21d9f484f682aef1c440d2e30fe090b49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Mon, 14 Jul 2025 17:28:20 +0800 Subject: [PATCH 08/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- .../js/napi/vpn/src/networkvpn_service.cpp | 2 +- .../netvpnclient/src/networkvpn_client.cpp | 57 +++++++++++++++++-- .../netvpnclient/include/networkvpn_client.h | 3 + 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/frameworks/js/napi/vpn/src/networkvpn_service.cpp b/frameworks/js/napi/vpn/src/networkvpn_service.cpp index 6b4072f8..94f530cc 100644 --- a/frameworks/js/napi/vpn/src/networkvpn_service.cpp +++ b/frameworks/js/napi/vpn/src/networkvpn_service.cpp @@ -1955,7 +1955,7 @@ void NetworkVpnService::OnRemoteDied(const wptr &remoteObject) } sptr callback = iface_cast(diedRemoted); UnregisterVpnEvent(callback); - std::lock_guard autoLock(netVpnMutex_); + std::lock_guard autoLock(remoteMutex_); #ifdef SUPPORT_SYSVPN if (vpnObj_ != nullptr && vpnObj_->IsSystemVpn()) { NETMGR_EXT_LOG_W("system vpn client died"); diff --git a/frameworks/native/netvpnclient/src/networkvpn_client.cpp b/frameworks/native/netvpnclient/src/networkvpn_client.cpp index 690ce879..ca0bf18d 100644 --- a/frameworks/native/netvpnclient/src/networkvpn_client.cpp +++ b/frameworks/native/netvpnclient/src/networkvpn_client.cpp @@ -39,7 +39,7 @@ public: SystemAbilityListener() = default; ~SystemAbilityListener() = default; void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; - void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override {}; + void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override; }; int32_t VpnSetUpEventCallback::OnVpnMultiUserSetUp() @@ -107,6 +107,12 @@ int32_t VpnEventCallbackCollection::UnregisterCallback(sptr c return NETMANAGER_EXT_SUCCESS; } +int32_t VpnEventCallbackCollection::GetCallbackNum() +{ + std::shared_lock lock(vpnEventCbMutex_); + return vpnEventCbList_.size(); +} + NetworkVpnClient::NetworkVpnClient() { Subscribe(); @@ -149,12 +155,26 @@ void NetworkVpnClient::Unsubscribe() void NetworkVpnClient::SystemAbilityListener::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) { switch (systemAbilityId) { - case COMM_VPN_MANAGER_SYS_ABILITY_ID: + case COMM_VPN_MANAGER_SYS_ABILITY_ID: { + NetworkVpnClient::GetInstance().SetVpnSaState(true); NetworkVpnClient::GetInstance().RegisterVpnEventCbCollection(); #ifdef SUPPORT_SYSVPN NetworkVpnClient::GetInstance().RegisterMultiVpnEventCbCollection(); #endif break; + } + default: + break; + } +} + +void NetworkVpnClient::SystemAbilityListener::OnRemoveSystemAbility( + int32_t systemAbilityId, const std::string &deviceId) +{ + switch (systemAbilityId) { + case COMM_VPN_MANAGER_SYS_ABILITY_ID: + NetworkVpnClient::GetInstance().SetVpnSaState(false); + break; default: break; } @@ -397,7 +417,11 @@ int32_t NetworkVpnClient::RegisterMultiVpnEvent(sptr callback if (multiVpnEventCbCollection_ == nullptr) { multiVpnEventCbCollection_ = sptr::MakeSptr(); } - return multiVpnEventCbCollection_->RegisterCallback(callback); + int ret = multiVpnEventCbCollection_->RegisterCallback(callback); + if (ret == NETMANAGER_EXT_SUCCESS && multiVpnEventCbCollection_->GetCallbackNum() == 1) { + RegisterMultiVpnEventCbCollection(); + } + return ret; } int32_t NetworkVpnClient::UnregisterMultiVpnEvent(sptr callback) @@ -407,7 +431,10 @@ int32_t NetworkVpnClient::UnregisterMultiVpnEvent(sptr callba return NETMANAGER_EXT_ERR_PARAMETER_ERROR; } if (multiVpnEventCbCollection_ != nullptr) { - return multiVpnEventCbCollection_->UnregisterCallback(callback); + int ret = multiVpnEventCbCollection_->UnregisterCallback(callback); + if (ret == NETMANAGER_EXT_SUCCESS && multiVpnEventCbCollection_->GetCallbackNum() == 0) { + UnregisterMultiVpnEventCbCollection(); + } } return NETMANAGER_EXT_SUCCESS; } @@ -417,6 +444,9 @@ void NetworkVpnClient::RegisterMultiVpnEventCbCollection() if (multiVpnEventCbCollection_ == nullptr) { multiVpnEventCbCollection_ = sptr::MakeSptr(); } + if (multiVpnEventCbCollection_->GetCallbackNum() == 0 || !saStart_) { + return; + } sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_EXT_LOG_E("RegisterMultiVpnEventCbCollection proxy is nullptr"); @@ -447,7 +477,11 @@ int32_t NetworkVpnClient::RegisterVpnEvent(sptr callback) if (vpnEventCbCollection_ == nullptr) { vpnEventCbCollection_ = sptr::MakeSptr(); } - return vpnEventCbCollection_->RegisterCallback(callback); + int ret = vpnEventCbCollection_->RegisterCallback(callback); + if (ret == NETMANAGER_EXT_SUCCESS && vpnEventCbCollection_->GetCallbackNum() == 1) { + RegisterVpnEventCbCollection(); + } + return ret; } int32_t NetworkVpnClient::UnregisterVpnEvent(sptr callback) @@ -457,7 +491,10 @@ int32_t NetworkVpnClient::UnregisterVpnEvent(sptr callback) return NETMANAGER_EXT_ERR_PARAMETER_ERROR; } if (vpnEventCbCollection_ != nullptr) { - return vpnEventCbCollection_->UnregisterCallback(callback); + int ret = vpnEventCbCollection_->UnregisterCallback(callback); + if (ret == NETMANAGER_EXT_SUCCESS && vpnEventCbCollection_->GetCallbackNum() == 0) { + UnregisterVpnEventCbCollection(); + } } return NETMANAGER_EXT_SUCCESS; } @@ -467,6 +504,9 @@ void NetworkVpnClient::RegisterVpnEventCbCollection() if (vpnEventCbCollection_ == nullptr) { vpnEventCbCollection_ = sptr::MakeSptr(); } + if (vpnEventCbCollection_->GetCallbackNum() == 0 || !saStart_) { + return; + } sptr proxy = GetProxy(); if (proxy == nullptr) { NETMGR_EXT_LOG_E("RegisterVpnEventCbCollection proxy is nullptr"); @@ -615,5 +655,10 @@ int32_t NetworkVpnClient::SetSelfVpnPid() } return proxy->SetSelfVpnPid(); } + +void NetworkVpnClient::SetVpnSaState(bool state) +{ + saStart_ = state; +} } // namespace NetManagerStandard } // namespace OHOS diff --git a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h index afe639aa..99b83c23 100644 --- a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h +++ b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h @@ -51,6 +51,7 @@ public: int32_t RegisterCallback(sptr callback); int32_t UnregisterCallback(sptr callback); + int32_t GetCallbackNum(); private: std::shared_mutex vpnEventCbMutex_; @@ -286,6 +287,7 @@ public: int32_t GetSelfAppName(std::string &selfAppName, std::string &selfBundleName); int32_t SetSelfVpnPid(); + void SetVpnSaState(bool state); private: class MonitorVpnServiceDead : public IRemoteObject::DeathRecipient { @@ -323,6 +325,7 @@ private: sptr vpnEventCbCollection_ = nullptr; sptr multiVpnEventCbCollection_ = nullptr; std::pair, bool> clientVpnConfig_; + bool saStart_ = false; }; } // namespace NetManagerStandard } // namespace OHOS -- Gitee From 4a393bb6c543ca10cf865c9c1db76270b75ea785 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Mon, 14 Jul 2025 17:45:48 +0800 Subject: [PATCH 09/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- .../networkvpn_client_test.cpp | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp index d3d827b2..bb6f3b30 100644 --- a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp +++ b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp @@ -122,7 +122,9 @@ HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent001, TestSize.Level1) networkVpnClient_.vpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); callback_ = new (std::nothrow) IVpnEventCallbackTest(); + networkVpnClient_.saStart_ = false; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); + EXPECT_EQ(networkVpnClient_.vpnEventCbCollection_.GetCallbackNum(), 1); } HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent002, TestSize.Level1) @@ -133,10 +135,22 @@ HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent002, TestSize.Level1) EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } +HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent003, TestSize.Level1) +{ + networkVpnClient_.vpnEventCbCollection_ = nullptr; + EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); + callback_ = new (std::nothrow) IVpnEventCallbackTest(); + networkVpnClient_.saStart_ = true; + EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); + EXPECT_EQ(networkVpnClient_.vpnEventCbCollection_.GetCallbackNum(), 1); +} + HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent001, TestSize.Level1) { EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); + networkVpnClient_.vpnEventCbCollection_ = sptr::MakeSptr(); callback_ = new (std::nothrow) IVpnEventCallbackTest(); + networkVpnClient_.RegisterVpnEvent(callback_); EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } @@ -153,13 +167,24 @@ HWTEST_F(NetworkVpnClientTest, RegisterMultiVpnEvent001, TestSize.Level1) networkVpnClient_.multiVpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.RegisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); callback_ = new (std::nothrow) IVpnEventCallbackTest(); + networkVpnClient_.saStart_ = true; + EXPECT_EQ(networkVpnClient_.RegisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); +} + +HWTEST_F(NetworkVpnClientTest, RegisterMultiVpnEvent002, TestSize.Level1) +{ + networkVpnClient_.multiVpnEventCbCollection_ = nullptr; + networkVpnClient_.saStart_ = false; + callback_ = new (std::nothrow) IVpnEventCallbackTest(); EXPECT_EQ(networkVpnClient_.RegisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } HWTEST_F(NetworkVpnClientTest, UnregisterMultiVpnEvent001, TestSize.Level1) { EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = new (std::nothrow) IVpnEventCallbackTest(); + networkVpnClient_.multiVpnEventCbCollection_ = sptr::MakeSptr(); + networkVpnClient_.RegisterMultiVpnEvent(callback_); EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } -- Gitee From f1b7ce2bd3e57e142e724ef70301f5983658b8e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Wed, 16 Jul 2025 11:25:33 +0800 Subject: [PATCH 10/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- .../innerkits/netvpnclient/include/networkvpn_client.h | 4 ++-- .../unittest/vpn_manager_test/networkvpn_client_test.cpp | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h index 99b83c23..b5108c57 100644 --- a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h +++ b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h @@ -311,14 +311,14 @@ private: void RegisterVpnEventCbCollection(); void UnregisterVpnEventCbCollection(); #ifdef SUPPORT_SYSVPN - void RegisterMuiltiVpnEventCbCollection(); + void RegisterMultiVpnEventCbCollection(); void UnregisterMultiVpnEventCbCollection(); #endif private: std::mutex mutex_; VpnInterface vpnInterface_; - sptr saStatusChangeListener_; + sptr saStatusChangeListener_ = nullptr; sptr vpnEventCallback_ = nullptr; sptr networkVpnService_ = nullptr; sptr deathRecipient_ = nullptr; diff --git a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp index bb6f3b30..9b4c60f4 100644 --- a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp +++ b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp @@ -124,7 +124,7 @@ HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent001, TestSize.Level1) callback_ = new (std::nothrow) IVpnEventCallbackTest(); networkVpnClient_.saStart_ = false; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); - EXPECT_EQ(networkVpnClient_.vpnEventCbCollection_.GetCallbackNum(), 1); + EXPECT_EQ(networkVpnClient_.vpnEventCbCollection_->GetCallbackNum(), 1); } HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent002, TestSize.Level1) @@ -142,7 +142,7 @@ HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent003, TestSize.Level1) callback_ = new (std::nothrow) IVpnEventCallbackTest(); networkVpnClient_.saStart_ = true; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); - EXPECT_EQ(networkVpnClient_.vpnEventCbCollection_.GetCallbackNum(), 1); + EXPECT_EQ(networkVpnClient_.vpnEventCbCollection_->GetCallbackNum(), 1); } HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent001, TestSize.Level1) @@ -270,7 +270,7 @@ HWTEST_F(NetworkVpnClientTest, VpnEventCallback, TestSize.Level1) { callback_ = new (std::nothrow) IVpnEventCallbackTest(); auto collection = sptr::MakeSptr(); - collection->vpnEventCallback_.push_back(callback_); + collection->vpnEventCbList_.push_back(callback_); EXPECT_EQ(collection->OnVpnStateChanged(true), NETMANAGER_EXT_SUCCESS); EXPECT_EQ(collection->OnMultiVpnStateChanged(true, "test", "0"), NETMANAGER_EXT_SUCCESS); -- Gitee From f8cea969cfd169d6e39b29249239f10f9549732e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Wed, 16 Jul 2025 16:57:59 +0800 Subject: [PATCH 11/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- .../netvpnclient/src/networkvpn_client.cpp | 2 +- .../netvpnclient/include/networkvpn_client.h | 2 +- .../networkvpn_client_test.cpp | 22 +++++++++---------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/frameworks/native/netvpnclient/src/networkvpn_client.cpp b/frameworks/native/netvpnclient/src/networkvpn_client.cpp index ca0bf18d..6867556f 100644 --- a/frameworks/native/netvpnclient/src/networkvpn_client.cpp +++ b/frameworks/native/netvpnclient/src/networkvpn_client.cpp @@ -113,7 +113,7 @@ int32_t VpnEventCallbackCollection::GetCallbackNum() return vpnEventCbList_.size(); } -NetworkVpnClient::NetworkVpnClient() +NetworkVpnClient::NetworkVpnClient() : saStatusChangeListener_(nullptr) { Subscribe(); } diff --git a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h index b5108c57..a636288a 100644 --- a/interfaces/innerkits/netvpnclient/include/networkvpn_client.h +++ b/interfaces/innerkits/netvpnclient/include/networkvpn_client.h @@ -318,7 +318,7 @@ private: private: std::mutex mutex_; VpnInterface vpnInterface_; - sptr saStatusChangeListener_ = nullptr; + sptr saStatusChangeListener_; sptr vpnEventCallback_ = nullptr; sptr networkVpnService_ = nullptr; sptr deathRecipient_ = nullptr; diff --git a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp index 9b4c60f4..300c67d7 100644 --- a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp +++ b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp @@ -121,7 +121,7 @@ HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent001, TestSize.Level1) { networkVpnClient_.vpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); networkVpnClient_.saStart_ = false; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); EXPECT_EQ(networkVpnClient_.vpnEventCbCollection_->GetCallbackNum(), 1); @@ -131,7 +131,7 @@ HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent002, TestSize.Level1) { NetManagerExtAccessToken access; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } @@ -139,7 +139,7 @@ HWTEST_F(NetworkVpnClientTest, RegisterVpnEvent003, TestSize.Level1) { networkVpnClient_.vpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); networkVpnClient_.saStart_ = true; EXPECT_EQ(networkVpnClient_.RegisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); EXPECT_EQ(networkVpnClient_.vpnEventCbCollection_->GetCallbackNum(), 1); @@ -149,7 +149,7 @@ HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent001, TestSize.Level1) { EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); networkVpnClient_.vpnEventCbCollection_ = sptr::MakeSptr(); - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); networkVpnClient_.RegisterVpnEvent(callback_); EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } @@ -158,7 +158,7 @@ HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent002, TestSize.Level1) { networkVpnClient_.vpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } @@ -166,7 +166,7 @@ HWTEST_F(NetworkVpnClientTest, RegisterMultiVpnEvent001, TestSize.Level1) { networkVpnClient_.multiVpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.RegisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); networkVpnClient_.saStart_ = true; EXPECT_EQ(networkVpnClient_.RegisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } @@ -175,14 +175,14 @@ HWTEST_F(NetworkVpnClientTest, RegisterMultiVpnEvent002, TestSize.Level1) { networkVpnClient_.multiVpnEventCbCollection_ = nullptr; networkVpnClient_.saStart_ = false; - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); EXPECT_EQ(networkVpnClient_.RegisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } HWTEST_F(NetworkVpnClientTest, UnregisterMultiVpnEvent001, TestSize.Level1) { EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); networkVpnClient_.multiVpnEventCbCollection_ = sptr::MakeSptr(); networkVpnClient_.RegisterMultiVpnEvent(callback_); EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); @@ -192,7 +192,7 @@ HWTEST_F(NetworkVpnClientTest, UnregisterMultiVpnEvent002, TestSize.Level1) { networkVpnClient_.multiVpnEventCbCollection_ = nullptr; EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(nullptr), NETMANAGER_EXT_ERR_PARAMETER_ERROR); - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } @@ -221,7 +221,7 @@ HWTEST_F(NetworkVpnClientTest, OnRemoteDied, TestSize.Level1) HWTEST_F(NetworkVpnClientTest, NetworkVpnClientBranch001, TestSize.Level1) { - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); callback_->OnVpnMultiUserSetUp(); networkVpnClient_.multiUserSetUpEvent(); @@ -268,7 +268,7 @@ HWTEST_F(NetworkVpnClientTest, GetVpnInterfaceFd, TestSize.Level1) HWTEST_F(NetworkVpnClientTest, VpnEventCallback, TestSize.Level1) { - callback_ = new (std::nothrow) IVpnEventCallbackTest(); + callback_ = sptr::MakeSptr(); auto collection = sptr::MakeSptr(); collection->vpnEventCbList_.push_back(callback_); -- Gitee From ca19be1613397b87ea40c68181f906d5f7158bb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=A2=81=E7=8E=89=E9=91=AB?= Date: Wed, 16 Jul 2025 17:26:47 +0800 Subject: [PATCH 12/12] set vpn run on create to false MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 袁玉鑫 --- .../unittest/vpn_manager_test/networkvpn_client_test.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp index 300c67d7..d3398b02 100644 --- a/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp +++ b/test/vpnmanager/unittest/vpn_manager_test/networkvpn_client_test.cpp @@ -162,6 +162,7 @@ HWTEST_F(NetworkVpnClientTest, UnregisterVpnEvent002, TestSize.Level1) EXPECT_EQ(networkVpnClient_.UnregisterVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } +#ifdef SUPPORT_SYSVPN HWTEST_F(NetworkVpnClientTest, RegisterMultiVpnEvent001, TestSize.Level1) { networkVpnClient_.multiVpnEventCbCollection_ = nullptr; @@ -195,6 +196,7 @@ HWTEST_F(NetworkVpnClientTest, UnregisterMultiVpnEvent002, TestSize.Level1) callback_ = sptr::MakeSptr(); EXPECT_EQ(networkVpnClient_.UnregisterMultiVpnEvent(callback_), NETMANAGER_EXT_SUCCESS); } +#endif HWTEST_F(NetworkVpnClientTest, GetProxy, TestSize.Level1) { -- Gitee