diff --git a/services/distributeddataservice/service/test/fuzztest/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/BUILD.gn index 9e801a6adebfab38c8898add34d65fb1f4f5a629..373d850fd4370a09c7a7d9e103201b7f737c3f17 100644 --- a/services/distributeddataservice/service/test/fuzztest/BUILD.gn +++ b/services/distributeddataservice/service/test/fuzztest/BUILD.gn @@ -49,6 +49,7 @@ group("fuzztest") { deps += [ "datashareservicestub_fuzzer:fuzztest", "datashareserviceimpl_fuzzer:fuzztest", + "datasharesubscriber_fuzzer:fuzztest", ] } diff --git a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/datashareservicestub_fuzzer.cpp b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/datashareservicestub_fuzzer.cpp index 50dd93e577adfbb1d656bcca165f137f045f3091..ca846d3201f7a1230408fb276a4337f4c4b11393 100644 --- a/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/datashareservicestub_fuzzer.cpp +++ b/services/distributeddataservice/service/test/fuzztest/datashareservicestub_fuzzer/datashareservicestub_fuzzer.cpp @@ -30,7 +30,7 @@ using namespace OHOS::DataShare; namespace OHOS { const std::u16string INTERFACE_TOKEN = u"OHOS.DataShare.IDataShareService"; constexpr uint32_t CODE_MIN = 0; -constexpr uint32_t CODE_MAX = IDataShareService::DATA_SHARE_SERVICE_CMD_MAX + 1; +constexpr uint32_t CODE_MAX = static_cast(IDataShareService::DATA_SHARE_SERVICE_CMD_MAX) + 1; constexpr size_t NUM_MIN = 5; constexpr size_t NUM_MAX = 12; @@ -52,6 +52,40 @@ bool OnRemoteRequestFuzz(FuzzedDataProvider &provider) dataShareServiceStub->OnRemoteRequest(code, request, reply); return true; } + +bool OnRemoteRequestFuzz01(uint32_t code, FuzzedDataProvider &provider) +{ + std::shared_ptr dataShareServiceImpl = std::make_shared(); + std::shared_ptr executor = std::make_shared(NUM_MAX, NUM_MIN); + dataShareServiceImpl->OnBind( + { "DataShareServiceStubFuzz", static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor) }); + std::vector remainingData = provider.ConsumeRemainingBytes(); + MessageParcel request; + request.WriteInterfaceToken(INTERFACE_TOKEN); + request.WriteBuffer(static_cast(remainingData.data()), remainingData.size()); + request.RewindRead(0); + MessageParcel reply; + std::shared_ptr dataShareServiceStub = dataShareServiceImpl; + dataShareServiceStub->OnRemoteRequest(code, request, reply); + return true; +} + +bool OnRemoteRequestFuzz02(FuzzedDataProvider &provider) +{ + std::shared_ptr dataShareServiceImpl = std::make_shared(); + std::shared_ptr executor = std::make_shared(NUM_MAX, NUM_MIN); + dataShareServiceImpl->OnBind( + { "DataShareServiceStubFuzz", static_cast(IPCSkeleton::GetSelfTokenID()), std::move(executor) }); + std::vector remainingData = provider.ConsumeRemainingBytes(); + MessageParcel request; + request.WriteInterfaceToken(INTERFACE_TOKEN); + request.WriteBuffer(static_cast(remainingData.data()), remainingData.size()); + request.RewindRead(0); + MessageParcel reply; + std::shared_ptr dataShareServiceStub = dataShareServiceImpl; + dataShareServiceStub->OnRemoteRequest(IDataShareService::DATA_SHARE_CMD_SYSTEM_CODE, request, reply); + return true; +} } // namespace OHOS /* Fuzzer entry point */ @@ -59,5 +93,11 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { FuzzedDataProvider provider(data, size); OHOS::OnRemoteRequestFuzz(provider); + uint32_t codeTest = OHOS::CODE_MIN; + while (codeTest <= OHOS::CODE_MAX) { + OHOS::OnRemoteRequestFuzz01(codeTest, provider); + codeTest++; + } + OHOS::OnRemoteRequestFuzz02(provider); return 0; } \ No newline at end of file diff --git a/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..435cdc38d9a5fe24efaf9937912cd5171c62238a --- /dev/null +++ b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/BUILD.gn @@ -0,0 +1,149 @@ +# 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. +##############################hydra-fuzz######################################## +import("//build/config/features.gni") +import("//build/test.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") + +##############################fuzztest########################################## +ohos_fuzztest("DataShareSubscriberFuzzTest") { + module_out_path = "datamgr_service/datamgr_service" + + include_dirs = [ + "${data_service_path}/adapter/include", + "${data_service_path}/app/src", + "${data_service_path}/framework/include", + "${data_service_path}/service/common", + "${data_service_path}/service/config/include", + "${data_service_path}/service/crypto/include", + "${data_service_path}/service/data_share/common", + "${data_service_path}/service/data_share/data", + "${data_service_path}/service/data_share/dfx", + "${data_service_path}/service/data_share/strategies", + "${data_service_path}/service/data_share/subscriber_managers", + "${data_service_path}/service/data_share", + "${data_service_path}/service/kvdb", + "${data_service_path}/service/permission/include", + "${data_service_path}/adapter/include/communicator", + ] + + fuzz_config_file = + "${data_service_path}/service/test/fuzztest/datasharesubscriber_fuzzer" + + cflags = [ + "-Dprivate=public", + "-Dprotected=public", + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + + sources = [ + "${data_service_path}/service/common/xcollie.cpp", + "${data_service_path}/service/data_share/common/app_connect_manager.cpp", + "${data_service_path}/service/data_share/common/bundle_mgr_proxy.cpp", + "${data_service_path}/service/data_share/common/common_utils.cpp", + "${data_service_path}/service/data_share/common/db_delegate.cpp", + "${data_service_path}/service/data_share/common/div_strategy.cpp", + "${data_service_path}/service/data_share/common/extension_ability_manager.cpp", + "${data_service_path}/service/data_share/common/extension_connect_adaptor.cpp", + "${data_service_path}/service/data_share/common/extension_mgr_proxy.cpp", + "${data_service_path}/service/data_share/common/kv_delegate.cpp", + "${data_service_path}/service/data_share/common/proxy_data_manager.cpp", + "${data_service_path}/service/data_share/common/rdb_delegate.cpp", + "${data_service_path}/service/data_share/common/scheduler_manager.cpp", + "${data_service_path}/service/data_share/common/seq_strategy.cpp", + "${data_service_path}/service/data_share/common/uri_utils.cpp", + "${data_service_path}/service/data_share/data/published_data.cpp", + "${data_service_path}/service/data_share/data/resultset_json_formatter.cpp", + "${data_service_path}/service/data_share/data/template_data.cpp", + "${data_service_path}/service/data_share/data_provider_config.cpp", + "${data_service_path}/service/data_share/data_share_db_config.cpp", + "${data_service_path}/service/data_share/data_share_obs_proxy.cpp", + "${data_service_path}/service/data_share/data_share_profile_config.cpp", + "${data_service_path}/service/data_share/data_share_silent_config.cpp", + "${data_service_path}/service/data_share/data_share_types_util.cpp", + "${data_service_path}/service/data_share/dfx/hiview_adapter.cpp", + "${data_service_path}/service/data_share/dfx/hiview_fault_adapter.cpp", + "${data_service_path}/service/data_share/strategies/data_proxy/load_config_from_data_proxy_node_strategy.cpp", + "${data_service_path}/service/data_share/strategies/data_share/load_config_from_data_share_bundle_info_strategy.cpp", + "${data_service_path}/service/data_share/strategies/general/check_is_data_proxy_strategy.cpp", + "${data_service_path}/service/data_share/strategies/general/check_is_single_app_strategy.cpp", + "${data_service_path}/service/data_share/strategies/general/cross_permission_strategy.cpp", + "${data_service_path}/service/data_share/strategies/general/load_config_common_strategy.cpp", + "${data_service_path}/service/data_share/strategies/general/load_config_data_info_strategy.cpp", + "${data_service_path}/service/data_share/strategies/general/load_config_from_bundle_info_strategy.cpp", + "${data_service_path}/service/data_share/strategies/general/permission_strategy.cpp", + "${data_service_path}/service/data_share/strategies/get_data_strategy.cpp", + "${data_service_path}/service/data_share/strategies/publish_strategy.cpp", + "${data_service_path}/service/data_share/strategies/rdb_notify_strategy.cpp", + "${data_service_path}/service/data_share/strategies/subscribe_strategy.cpp", + "${data_service_path}/service/data_share/strategies/template_strategy.cpp", + "${data_service_path}/service/data_share/subscriber_managers/proxy_data_subscriber_manager.cpp", + "${data_service_path}/service/data_share/subscriber_managers/published_data_subscriber_manager.cpp", + "${data_service_path}/service/data_share/subscriber_managers/rdb_subscriber_manager.cpp", + "${data_service_path}/service/data_share/sys_event_subscriber.cpp", + "${data_service_path}/service/kvdb/user_delegate.cpp", + "${data_service_path}/service/permission/src/permit_delegate.cpp", + "${data_service_path}/service/permission/src/permission_validator.cpp", + "datasharesubscriber_fuzzer.cpp", + ] + + deps = [ + "${data_service_path}/adapter/account:distributeddata_account", + "${data_service_path}/adapter/communicator:distributeddata_communicator", + "${data_service_path}/framework:distributeddatasvcfwk", + "${data_service_path}/service:distributeddatasvc", + ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "ability_base:zuri", + "ability_runtime:dataobs_manager", + "ability_runtime:extension_manager", + "ability_runtime:wantagent_innerkits", + "access_token:libaccesstoken_sdk", + "access_token:libtokenid_sdk", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "common_event_service:cesfwk_innerkits", + "data_share:datashare_common", + "data_share:datashare_consumer", + "device_manager:devicemanagersdk", + "hicollie:libhicollie", + "hilog:libhilog", + "hisysevent:libhisysevent", + "huks:libhukssdk", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + "kv_store:distributeddb", + "qos_manager:concurrent_task_client", + "qos_manager:qos", + "relational_store:native_rdb", + "relational_store:rdb_data_share_adapter", + "resource_management:global_resmgr", + "samgr:samgr_proxy", + "time_service:time_client", + ] +} + +############################################################################### +group("fuzztest") { + testonly = true + + deps = [ ":DataShareSubscriberFuzzTest" ] +} +############################################################################### diff --git a/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/corpus/init b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..7ade8a0faafeaedba7241e7d4a97b8e1f9691932 --- /dev/null +++ b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/corpus/init @@ -0,0 +1,16 @@ +/* + * 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. + */ + +FUZZ \ No newline at end of file diff --git a/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/datasharesubscriber_fuzzer.cpp b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/datasharesubscriber_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b6be365265f3396f049ce35debb2f4d76c27fb9e --- /dev/null +++ b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/datasharesubscriber_fuzzer.cpp @@ -0,0 +1,95 @@ +/* + * 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. + */ +#define LOG_TAG "DataShareSubscriberFuzzTest" +#include + +#include "datasharesubscriber_fuzzer.h" + +#include +#include + +#include "log_print.h" +#include "proxy_data_subscriber_manager.h" +#include "ipc_skeleton.h" +#include "message_parcel.h" +#include "securec.h" + +using namespace OHOS::DataShare; +namespace OHOS { + +void AddFuzz(FuzzedDataProvider &provider) +{ + std::string uri = provider.ConsumeRandomLengthString(); + std::string bundleName = provider.ConsumeRandomLengthString(); + ProxyDataKey key(uri, bundleName); + sptr observer; + std::string callerAppIdentifier = provider.ConsumeRandomLengthString(); + int32_t userId = provider.ConsumeIntegral(); + auto& manager = ProxyDataSubscriberManager::GetInstance(); + manager.Add(key, observer, bundleName, callerAppIdentifier, userId); +} + +void CheckAllowListFuzz(FuzzedDataProvider &provider) +{ + std::string callerAppIdentifier = provider.ConsumeRandomLengthString(); + std::vector allowList; + const std::string allowList1 = provider.ConsumeRandomLengthString(); + const std::string allowList2 = provider.ConsumeRandomLengthString(); + auto& manager = ProxyDataSubscriberManager::GetInstance(); + manager.CheckAllowList(allowList, callerAppIdentifier); +} + +void EmitFuzz(FuzzedDataProvider &provider) +{ + std::vector keys; + std::string uri = provider.ConsumeRandomLengthString(); + std::string bundleName = provider.ConsumeRandomLengthString(); + ProxyDataKey key(uri, bundleName); + std::string uri1 = provider.ConsumeRandomLengthString(); + std::string bundleName1 = provider.ConsumeRandomLengthString(); + ProxyDataKey key1(uri1, bundleName1); + keys.push_back(key); + keys.push_back(key1); + std::map> datas; + std::vector insertData; + DataShareProxyData data1; + insertData.push_back(data1); + datas[DataShareObserver::ChangeType::INSERT] = insertData; + int32_t userId = provider.ConsumeIntegral(); + auto& manager = ProxyDataSubscriberManager::GetInstance(); + manager.Emit(keys, datas, userId); +} + +void ObserverNodeFuzz(FuzzedDataProvider &provider) +{ + sptr observer; + uint32_t tokenId = provider.ConsumeIntegral(); + std::string name = provider.ConsumeRandomLengthString(); + std::string caller = provider.ConsumeRandomLengthString(); + int32_t userId = provider.ConsumeIntegral(); + ProxyDataSubscriberManager::ObserverNode observerNode(observer, tokenId, name, caller, userId); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + FuzzedDataProvider provider(data, size); + OHOS::AddFuzz(provider); + OHOS::CheckAllowListFuzz(provider); + OHOS::EmitFuzz(provider); + OHOS::ObserverNodeFuzz(provider); + return 0; +} \ No newline at end of file diff --git a/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/datasharesubscriber_fuzzer.h b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/datasharesubscriber_fuzzer.h new file mode 100644 index 0000000000000000000000000000000000000000..dfa8298bb8c0fc95a83928cfb18b43afa18daaad --- /dev/null +++ b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/datasharesubscriber_fuzzer.h @@ -0,0 +1,21 @@ +/* + * 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. + */ + +#ifndef DATAMGR_SERVICE_DATA_SHARE_SUBSCRIBER_FUZZER_H +#define DATAMGR_SERVICE_DATA_SHARE_SUBSCRIBER_FUZZER_H + +#define FUZZ_PROJECT_NAME "datasharesubscriber_fuzzer" + +#endif // DATAMGR_SERVICE_DATA_SHARE_SUBSCRIBER_FUZZER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/project.xml b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..a37a23b1533f07ab59744732b0f46b2b5fd958d8 --- /dev/null +++ b/services/distributeddataservice/service/test/fuzztest/datasharesubscriber_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + \ No newline at end of file