From 70957b4c9459aed751ab1b66bd0f026006fe2d24 Mon Sep 17 00:00:00 2001
From: l00635678
Date: Wed, 26 Mar 2025 11:56:54 +0800
Subject: [PATCH] =?UTF-8?q?DTFuzz=E7=94=A8=E4=BE=8B=E4=BF=AE=E5=A4=8D?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Signed-off-by: l00635678
---
services/netpolicymanager/BUILD.gn | 1 +
.../src/common/net_policy_event_handler.cpp | 9 ++-
.../src/net_policy_service.cpp | 66 +++++++++++--------
.../src/stub/net_policy_service_stub.cpp | 6 +-
.../net_stats_client_fuzzer.cpp | 4 +-
.../net_policy_service_stub_test.cpp | 5 +-
6 files changed, 55 insertions(+), 36 deletions(-)
diff --git a/services/netpolicymanager/BUILD.gn b/services/netpolicymanager/BUILD.gn
index bc348c9a7..c65327723 100644
--- a/services/netpolicymanager/BUILD.gn
+++ b/services/netpolicymanager/BUILD.gn
@@ -193,6 +193,7 @@ ohos_static_library("net_policy_manager_static") {
defines = [
"NETMGR_LOG_TAG = \"NetPolicyService\"",
"LOG_DOMAIN = 0xD0015B0",
+ "UNITTEST_FORBID_FFRT",
"NETMANAGER_TEST",
]
diff --git a/services/netpolicymanager/src/common/net_policy_event_handler.cpp b/services/netpolicymanager/src/common/net_policy_event_handler.cpp
index cf41b199c..d4d7f5622 100644
--- a/services/netpolicymanager/src/common/net_policy_event_handler.cpp
+++ b/services/netpolicymanager/src/common/net_policy_event_handler.cpp
@@ -38,8 +38,13 @@ void NetPolicyEventHandler::SendEvent(const AppExecFwk::InnerEvent::Pointer &eve
{
auto eventId = static_cast(event->GetInnerEventId());
auto eventData = event->GetSharedObject();
- ffrtQueue_.submit([this, eventId, eventData] { ProcessEvent(eventId, eventData); },
- ffrt::task_attr().delay(static_cast(delayTime)).name("FfrtSendEvent"));
+#ifndef UNITTEST_FORBID_FFRT
+ ffrtQueue_.submit([this, eventId, eventData] {
+#endif
+ ProcessEvent(eventId, eventData);
+#ifndef UNITTEST_FORBID_FFRT
+ }, ffrt::task_attr().delay(static_cast(delayTime)).name("FfrtSendEvent"));
+#endif
}
} // namespace NetManagerStandard
} // namespace OHOS
diff --git a/services/netpolicymanager/src/net_policy_service.cpp b/services/netpolicymanager/src/net_policy_service.cpp
index c714f2f7e..bdca12409 100755
--- a/services/netpolicymanager/src/net_policy_service.cpp
+++ b/services/netpolicymanager/src/net_policy_service.cpp
@@ -138,26 +138,32 @@ void NetPolicyService::Init()
AddSystemAbilityListener(COMM_NET_CONN_MANAGER_SYS_ABILITY_ID);
AddSystemAbilityListener(COMM_NETSYS_NATIVE_SYS_ABILITY_ID);
AddSystemAbilityListener(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
- ffrtQueue_.submit(
- [this]() {
- serviceComm_ = (std::make_unique()).release();
- NetManagerCenter::GetInstance().RegisterPolicyService(serviceComm_);
- netPolicyCore_ = DelayedSingleton::GetInstance();
- netPolicyCallback_ = DelayedSingleton::GetInstance();
- netPolicyTraffic_ = netPolicyCore_->CreateCore();
- netPolicyFirewall_ = netPolicyCore_->CreateCore();
- netPolicyRule_ = netPolicyCore_->CreateCore();
- NetAccessPolicyRDB netAccessPolicy;
- netAccessPolicy.InitRdbStore();
- UpdateNetAccessPolicyToMapFromDB();
+#ifndef UNITTEST_FORBID_FFRT
+ ffrtQueue_.submit([this]() {
+#endif
+ serviceComm_ = (std::make_unique()).release();
+ NetManagerCenter::GetInstance().RegisterPolicyService(serviceComm_);
+ netPolicyCore_ = DelayedSingleton::GetInstance();
+ netPolicyCallback_ = DelayedSingleton::GetInstance();
+ netPolicyTraffic_ = netPolicyCore_->CreateCore();
+ netPolicyFirewall_ = netPolicyCore_->CreateCore();
+ netPolicyRule_ = netPolicyCore_->CreateCore();
+ NetAccessPolicyRDB netAccessPolicy;
+ netAccessPolicy.InitRdbStore();
+ UpdateNetAccessPolicyToMapFromDB();
#ifndef NETMANAGER_TEST
- if (!Publish(DelayedSingleton::GetInstance().get())) {
- NETMGR_LOG_E("Register to sa manager failed");
- }
+ if (!Publish(DelayedSingleton::GetInstance().get())) {
+ NETMGR_LOG_E("Register to sa manager failed");
+ }
+#endif
+#ifndef UNITTEST_FORBID_FFRT
+ }, ffrt::task_attr().name("FfrtNetPolicyServiceInit"));
+ ffrtQueue_.submit([this]() {
+#endif
+ SetBrokerUidAccessPolicyMap(std::nullopt);
+#ifndef UNITTEST_FORBID_FFRT
+ }, ffrt::task_attr().name("InitSetBrokerUidAccessPolicyMapFunc").delay(DELAY_US));
#endif
- }, ffrt::task_attr().name("FfrtNetPolicyServiceInit"));
- ffrtQueue_.submit([this]() { SetBrokerUidAccessPolicyMap(std::nullopt); },
- ffrt::task_attr().name("InitSetBrokerUidAccessPolicyMapFunc").delay(DELAY_US));
}
int32_t NetPolicyService::SetPolicyByUid(uint32_t uid, uint32_t policy)
@@ -403,8 +409,13 @@ void NetPolicyService::OnAddSystemAbility(int32_t systemAbilityId, const std::st
RegisterFactoryResetCallback();
}
if (systemAbilityId == BUNDLE_MGR_SERVICE_SYS_ABILITY_ID) {
- ffrtQueue_.submit([this]() { SetBrokerUidAccessPolicyMap(std::nullopt); },
- ffrt::task_attr().name("SetBrokerUidAccessPolicyMapFunc").delay(DELAY_US));
+#ifndef UNITTEST_FORBID_FFRT
+ ffrtQueue_.submit([this]() {
+#endif
+ SetBrokerUidAccessPolicyMap(std::nullopt);
+#ifndef UNITTEST_FORBID_FFRT
+ }, ffrt::task_attr().name("SetBrokerUidAccessPolicyMapFunc").delay(DELAY_US));
+#endif
EventFwk::MatchingSkills matchingSkills;
matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
@@ -415,13 +426,14 @@ void NetPolicyService::OnAddSystemAbility(int32_t systemAbilityId, const std::st
std::shared_ptr subscriber = std::make_shared(
subscribeInfo, std::static_pointer_cast(shared_from_this()));
EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber);
-
- ffrtQueue_.submit(
- [this]() {
- OverwriteNetAccessPolicyToDBFromConfig();
- UpdateNetAccessPolicyToMapFromDB();
- },
- ffrt::task_attr().name("NetworkAccessPolicyConfigFlush"));
+#ifndef UNITTEST_FORBID_FFRT
+ ffrtQueue_.submit([this]() {
+#endif
+ OverwriteNetAccessPolicyToDBFromConfig();
+ UpdateNetAccessPolicyToMapFromDB();
+#ifndef UNITTEST_FORBID_FFRT
+ }, ffrt::task_attr().name("NetworkAccessPolicyConfigFlush"));
+#endif
}
if (systemAbilityId == COMM_NETSYS_NATIVE_SYS_ABILITY_ID) {
if (hasSARemoved_) {
diff --git a/services/netpolicymanager/src/stub/net_policy_service_stub.cpp b/services/netpolicymanager/src/stub/net_policy_service_stub.cpp
index b21e39a42..125ed07ea 100644
--- a/services/netpolicymanager/src/stub/net_policy_service_stub.cpp
+++ b/services/netpolicymanager/src/stub/net_policy_service_stub.cpp
@@ -160,10 +160,14 @@ int32_t NetPolicyServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data
int32_t result = NETMANAGER_SUCCESS;
auto requestFunc = itFunc->second;
if (requestFunc != nullptr) {
+#ifndef UNITTEST_FORBID_FFRT
auto task = ffrtQueue_.submit_h([this, &data, &reply, &requestFunc, &result]() {
- result = (this->*requestFunc)(data, reply);
+#endif
+ result = (this->*requestFunc)(data, reply);
+#ifndef UNITTEST_FORBID_FFRT
}, ffrt::task_attr().name("FfrtOnRemoteRequest"));
ffrtQueue_.wait(task);
+#endif
NETMGR_LOG_D("stub call end, code = [%{public}d], ret = [%{public}d]", code, result);
return result;
}
diff --git a/test/fuzztest/netstatsclient_fuzzer/net_stats_client_fuzzer.cpp b/test/fuzztest/netstatsclient_fuzzer/net_stats_client_fuzzer.cpp
index 987ec5289..3af903cf6 100644
--- a/test/fuzztest/netstatsclient_fuzzer/net_stats_client_fuzzer.cpp
+++ b/test/fuzztest/netstatsclient_fuzzer/net_stats_client_fuzzer.cpp
@@ -82,9 +82,9 @@ void Init()
{
if (!g_isInited) {
DelayedSingleton::GetInstance()->Init();
- g_isInited = false;
- } else {
g_isInited = true;
+ } else {
+ g_isInited = false;
}
}
diff --git a/test/netpolicymanager/unittest/net_policy_manager_test/net_policy_service_stub_test.cpp b/test/netpolicymanager/unittest/net_policy_manager_test/net_policy_service_stub_test.cpp
index 2c54121b8..72909a9fb 100644
--- a/test/netpolicymanager/unittest/net_policy_manager_test/net_policy_service_stub_test.cpp
+++ b/test/netpolicymanager/unittest/net_policy_manager_test/net_policy_service_stub_test.cpp
@@ -174,10 +174,7 @@ public:
if (itFunc != memberFuncMap_.end()) {
auto requestFunc = itFunc->second;
if (requestFunc != nullptr) {
- auto task = ffrtQueue_.submit_h([this, &data, &reply, &requestFunc, &result]() {
- result = (this->*requestFunc)(data, reply);
- }, ffrt::task_attr().name("FfrtOnRemoteRequest"));
- ffrtQueue_.wait(task);
+ result = (this->*requestFunc)(data, reply);
return result;
}
}
--
Gitee