diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn index 5db5261095e068922981221edcb45ed4fb097d21..76bc2ae2cc8a4db47f073118d5f55c41474639bb 100644 --- a/test/fuzztest/BUILD.gn +++ b/test/fuzztest/BUILD.gn @@ -20,5 +20,17 @@ group("dfs_service_fuzztest") { "${distributedfile_path}/test/fuzztest/daemonstub_fuzzer:DaemonStubFuzzTest", "${distributedfile_path}/test/fuzztest/ffrttimer_fuzzer:FfrtTimerFuzzTest", "${distributedfile_path}/test/fuzztest/filetranslistenerstub_fuzzer:FileTransListenerStubFuzzTest", + "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer:CloudSyncManagerFuzzTest", + "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_two:CloudSyncManagerFuzzTestTwo", + "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_three:CloudSyncManagerFuzzTestThree", + "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_four:CloudSyncManagerFuzzTestFour", + "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_five:CloudSyncManagerFuzzTestFive", + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer:CloudSyncServiceStubFuzzTest", + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_two:CloudSyncServiceStubFuzzTestTwo", + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_three:CloudSyncServiceStubFuzzTestThree", + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_four:CloudSyncServiceStubFuzzTestFour", + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_five:CloudSyncServiceStubFuzzTestFive", + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_six:CloudSyncServiceStubFuzzTestSix", + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_seven:CloudSyncServiceStubFuzzTestSeven", ] } diff --git a/test/fuzztest/cloudsyncmanager_fuzzer/cloudsyncmanager_fuzzer.cpp b/test/fuzztest/cloudsyncmanager_fuzzer/cloudsyncmanager_fuzzer.cpp index 6b50c04f0e7ff9dfa6bd025eb769e1433505a361..08199cd5ed1e51eeeaa5c041eefd4dcc337063d0 100644 --- a/test/fuzztest/cloudsyncmanager_fuzzer/cloudsyncmanager_fuzzer.cpp +++ b/test/fuzztest/cloudsyncmanager_fuzzer/cloudsyncmanager_fuzzer.cpp @@ -64,119 +64,6 @@ void StartSyncFuzzTest(FuzzData &fuzzData, size_t size) std::string bundleName = fuzzData.GetStringFromData(static_cast(size)); CloudSyncManager::GetInstance().StartSync(bundleName); } - -void GetSyncTimeFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - int64_t syncTime{0}; - std::string bundleName = fuzzData.GetStringFromData(static_cast(size)); - CloudSyncManager::GetInstance().GetSyncTime(syncTime, bundleName); -} - -void StartSyncCallbackFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - bool forceFlag = fuzzData.GetData(); - auto cloudSyncCallback = make_shared(); - CloudSyncManager::GetInstance().StartSync(forceFlag, cloudSyncCallback); -} - -void TriggerSyncFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - int32_t userId = 100; - std::string bundleName = fuzzData.GetStringFromData(static_cast(size)); - CloudSyncManager::GetInstance().TriggerSync(bundleName, userId); - - fuzzData.ResetData(size); - userId = fuzzData.GetData(); - bundleName = fuzzData.GetStringFromData(static_cast(size - U32_AT_SIZE)); - CloudSyncManager::GetInstance().TriggerSync(bundleName, userId); -} - -void StopSyncFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - std::string bundleName = fuzzData.GetStringFromData(static_cast(size)); - CloudSyncManager::GetInstance().StopSync(bundleName); -} - -void ChangeAppSwitchFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - bool status = fuzzData.GetData(); - int len = static_cast((size - BOOL_AT_SIZE) >> 1); - std::string accoutId = fuzzData.GetStringFromData(len); - string bundleName = fuzzData.GetStringFromData(static_cast(len)); - CloudSyncManager::GetInstance().ChangeAppSwitch(accoutId, bundleName, status); -} - -void NotifyDataChangeFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - int len = static_cast(size >> 1); - std::string accoutId = fuzzData.GetStringFromData(len); - std::string bundleName = fuzzData.GetStringFromData(len); - CloudSyncManager::GetInstance().NotifyDataChange(accoutId, bundleName); -} - -void StartDownloadFileFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - int64_t downloadId = 0; - string uri = fuzzData.GetStringFromData(static_cast(size)); - auto downloadCallback = make_shared(); - CloudSyncManager::GetInstance().StartDownloadFile(uri, downloadCallback, downloadId); -} - -void StopDownloadFileFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - bool needClean = fuzzData.GetData(); - int64_t downloadId = fuzzData.GetData(); - CloudSyncManager::GetInstance().StopDownloadFile(downloadId, needClean); -} - -void EnableCloudFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - int len = static_cast(size >> 1); - std::string accoutId = fuzzData.GetStringFromData(len); - string itemStr = fuzzData.GetStringFromData(len - static_cast(BOOL_AT_SIZE)); - bool itemBool = fuzzData.GetData(); - SwitchDataObj switchDataObj; - switchDataObj.switchData.insert({itemStr, itemBool}); - CloudSyncManager::GetInstance().EnableCloud(accoutId, switchDataObj); - auto proxy = ServiceProxy::GetInstance(); - CloudSyncManagerImpl::GetInstance().SetDeathRecipient(proxy->AsObject()); -} - -void DisableCloudFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - std::string accoutId = fuzzData.GetStringFromData(static_cast(size)); - CloudSyncManager::GetInstance().DisableCloud(accoutId); -} - -void CleanFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - int len = static_cast(size >> 1); - std::string accoutId = fuzzData.GetStringFromData(len); - int32_t itemInt = fuzzData.GetData(); - auto remainSize = fuzzData.GetRemainSize(); - string itemStr = fuzzData.GetStringFromData(static_cast(remainSize)); - CleanOptions cleanOptions; - cleanOptions.appActionsData.insert({itemStr, itemInt}); - CloudSyncManager::GetInstance().Clean(accoutId, cleanOptions); -} - -void CleanCacheFuzzTest(FuzzData &fuzzData, size_t size) -{ - fuzzData.ResetData(size); - string uri = fuzzData.GetStringFromData(static_cast(size)); - CloudSyncManager::GetInstance().CleanCache(uri); -} } // namespace OHOS /* Fuzzer entry point */ @@ -191,17 +78,5 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::RegisterCallbackFuzzTest(fuzzData, size); OHOS::UnRegisterCallbackFuzzTest(fuzzData, size); OHOS::StartSyncFuzzTest(fuzzData, size); - OHOS::GetSyncTimeFuzzTest(fuzzData, size); - OHOS::StartSyncCallbackFuzzTest(fuzzData, size); - OHOS::TriggerSyncFuzzTest(fuzzData, size); - OHOS::StopSyncFuzzTest(fuzzData, size); - OHOS::ChangeAppSwitchFuzzTest(fuzzData, size); - OHOS::NotifyDataChangeFuzzTest(fuzzData, size); - OHOS::StartDownloadFileFuzzTest(fuzzData, size); - OHOS::StopDownloadFileFuzzTest(fuzzData, size); - OHOS::EnableCloudFuzzTest(fuzzData, size); - OHOS::DisableCloudFuzzTest(fuzzData, size); - OHOS::CleanFuzzTest(fuzzData, size); - OHOS::CleanCacheFuzzTest(fuzzData, size); return 0; } diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_five/BUILD.gn b/test/fuzztest/cloudsyncmanager_fuzzer_five/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..5fa4d56b9fca70c113aa89bb6043cbf125ee5b07 --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_five/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncManagerFuzzTestFive") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_five" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_five/cloudsyncmanager_fuzzer_five.cpp" ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${innerkits_native_path}/cloudsync_kit_inner:cloudsync_kit_inner", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "c_utils:utils", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":CloudSyncManagerFuzzTestFive" ] +} diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_five/cloudsyncmanager_fuzzer_five.cpp b/test/fuzztest/cloudsyncmanager_fuzzer_five/cloudsyncmanager_fuzzer_five.cpp new file mode 100644 index 0000000000000000000000000000000000000000..5ee2a459b2b6c23e5c948e27aa6e1158704e5db3 --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_five/cloudsyncmanager_fuzzer_five.cpp @@ -0,0 +1,87 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncmanager_fuzzer_five.h" + +#include +#include +#include + +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_manager.h" +#include "cloud_sync_manager_impl.h" +#include "service_proxy.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +void DisableCloudFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + std::string accoutId = fuzzData.GetStringFromData(static_cast(size)); + CloudSyncManager::GetInstance().DisableCloud(accoutId); +} + +void CleanFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int len = static_cast(size >> 1); + std::string accoutId = fuzzData.GetStringFromData(len); + int32_t itemInt = fuzzData.GetData(); + auto remainSize = fuzzData.GetRemainSize(); + string itemStr = fuzzData.GetStringFromData(static_cast(remainSize)); + CleanOptions cleanOptions; + cleanOptions.appActionsData.insert({itemStr, itemInt}); + CloudSyncManager::GetInstance().Clean(accoutId, cleanOptions); +} + +void CleanCacheFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + string uri = fuzzData.GetStringFromData(static_cast(size)); + CloudSyncManager::GetInstance().CleanCache(uri); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size <= (OHOS::U32_AT_SIZE << 1)) { + return 0; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::DisableCloudFuzzTest(fuzzData, size); + OHOS::CleanFuzzTest(fuzzData, size); + OHOS::CleanCacheFuzzTest(fuzzData, size); + return 0; +} diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_five/cloudsyncmanager_fuzzer_five.h b/test/fuzztest/cloudsyncmanager_fuzzer_five/cloudsyncmanager_fuzzer_five.h new file mode 100644 index 0000000000000000000000000000000000000000..bb54d206b08e51e88f8b1f7f4b6d955af2895bc1 --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_five/cloudsyncmanager_fuzzer_five.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_MANAGER_FUZZER_FIVE_H +#define CLOUD_SYNC_MANAGER_FUZZER_FIVE_H + +#define FUZZ_PROJECT_NAME "cloudsyncmanager_fuzzer_five" + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_five/corpus/init b/test/fuzztest/cloudsyncmanager_fuzzer_five/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_five/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_five/project.xml b/test/fuzztest/cloudsyncmanager_fuzzer_five/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_five/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_four/BUILD.gn b/test/fuzztest/cloudsyncmanager_fuzzer_four/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..9a5d57a23c0336a81c6a215ce9db8436402f6755 --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_four/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncManagerFuzzTestFour") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_four" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_four/cloudsyncmanager_fuzzer_four.cpp" ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${innerkits_native_path}/cloudsync_kit_inner:cloudsync_kit_inner", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "c_utils:utils", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":CloudSyncManagerFuzzTestFour" ] +} diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_four/cloudsyncmanager_fuzzer_four.cpp b/test/fuzztest/cloudsyncmanager_fuzzer_four/cloudsyncmanager_fuzzer_four.cpp new file mode 100644 index 0000000000000000000000000000000000000000..48b7613acc1d93ea54d1fd702efc5fec3639155d --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_four/cloudsyncmanager_fuzzer_four.cpp @@ -0,0 +1,91 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncmanager_fuzzer_four.h" + +#include +#include +#include + +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_manager.h" +#include "cloud_sync_manager_impl.h" +#include "service_proxy.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +void StartDownloadFileFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int64_t downloadId = 0; + string uri = fuzzData.GetStringFromData(static_cast(size)); + auto downloadCallback = make_shared(); + CloudSyncManager::GetInstance().StartDownloadFile(uri, downloadCallback, downloadId); +} + +void StopDownloadFileFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + bool needClean = fuzzData.GetData(); + int64_t downloadId = fuzzData.GetData(); + CloudSyncManager::GetInstance().StopDownloadFile(downloadId, needClean); +} + +void EnableCloudFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int len = static_cast(size >> 1); + std::string accoutId = fuzzData.GetStringFromData(len); + string itemStr = fuzzData.GetStringFromData(len - static_cast(BOOL_AT_SIZE)); + bool itemBool = fuzzData.GetData(); + SwitchDataObj switchDataObj; + switchDataObj.switchData.insert({itemStr, itemBool}); + CloudSyncManager::GetInstance().EnableCloud(accoutId, switchDataObj); + auto proxy = ServiceProxy::GetInstance(); + CloudSyncManagerImpl::GetInstance().SetDeathRecipient(proxy->AsObject()); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size <= (OHOS::U32_AT_SIZE << 1)) { + return 0; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::StartDownloadFileFuzzTest(fuzzData, size); + OHOS::StopDownloadFileFuzzTest(fuzzData, size); + OHOS::EnableCloudFuzzTest(fuzzData, size); + return 0; +} diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_four/cloudsyncmanager_fuzzer_four.h b/test/fuzztest/cloudsyncmanager_fuzzer_four/cloudsyncmanager_fuzzer_four.h new file mode 100644 index 0000000000000000000000000000000000000000..d3f01d76de05f586cf3b5162e137bbd7f67618e9 --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_four/cloudsyncmanager_fuzzer_four.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_MANAGER_FUZZER_FOUR_H +#define CLOUD_SYNC_MANAGER_FUZZER_FOUR_H + +#define FUZZ_PROJECT_NAME "cloudsyncmanager_fuzzer_four" + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_four/corpus/init b/test/fuzztest/cloudsyncmanager_fuzzer_four/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_four/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_four/project.xml b/test/fuzztest/cloudsyncmanager_fuzzer_four/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_four/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_three/BUILD.gn b/test/fuzztest/cloudsyncmanager_fuzzer_three/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..52836adc14f02342a1493aec7c07d62054c65676 --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_three/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncManagerFuzzTestThree") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_three" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_three/cloudsyncmanager_fuzzer_three.cpp" ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${innerkits_native_path}/cloudsync_kit_inner:cloudsync_kit_inner", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "c_utils:utils", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":CloudSyncManagerFuzzTestThree" ] +} diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_three/cloudsyncmanager_fuzzer_three.cpp b/test/fuzztest/cloudsyncmanager_fuzzer_three/cloudsyncmanager_fuzzer_three.cpp new file mode 100644 index 0000000000000000000000000000000000000000..68485f8702f7258f3e1c1400caa3b84b5dac3141 --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_three/cloudsyncmanager_fuzzer_three.cpp @@ -0,0 +1,86 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncmanager_fuzzer_three.h" + +#include +#include +#include + +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_manager.h" +#include "cloud_sync_manager_impl.h" +#include "service_proxy.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +void StopSyncFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + std::string bundleName = fuzzData.GetStringFromData(static_cast(size)); + CloudSyncManager::GetInstance().StopSync(bundleName); +} + +void ChangeAppSwitchFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + bool status = fuzzData.GetData(); + int len = static_cast((size - BOOL_AT_SIZE) >> 1); + std::string accoutId = fuzzData.GetStringFromData(len); + string bundleName = fuzzData.GetStringFromData(static_cast(len)); + CloudSyncManager::GetInstance().ChangeAppSwitch(accoutId, bundleName, status); +} + +void NotifyDataChangeFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int len = static_cast(size >> 1); + std::string accoutId = fuzzData.GetStringFromData(len); + std::string bundleName = fuzzData.GetStringFromData(len); + CloudSyncManager::GetInstance().NotifyDataChange(accoutId, bundleName); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size <= (OHOS::U32_AT_SIZE << 1)) { + return 0; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::StopSyncFuzzTest(fuzzData, size); + OHOS::ChangeAppSwitchFuzzTest(fuzzData, size); + OHOS::NotifyDataChangeFuzzTest(fuzzData, size); + return 0; +} diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_three/cloudsyncmanager_fuzzer_three.h b/test/fuzztest/cloudsyncmanager_fuzzer_three/cloudsyncmanager_fuzzer_three.h new file mode 100644 index 0000000000000000000000000000000000000000..eaf817dac259acad2d2caa2f30bb549441785366 --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_three/cloudsyncmanager_fuzzer_three.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_MANAGER_FUZZER_THREE_H +#define CLOUD_SYNC_MANAGER_FUZZER_THREE_H + +#define FUZZ_PROJECT_NAME "cloudsyncmanager_fuzzer_three" + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_three/corpus/init b/test/fuzztest/cloudsyncmanager_fuzzer_three/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_three/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_three/project.xml b/test/fuzztest/cloudsyncmanager_fuzzer_three/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_three/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_two/BUILD.gn b/test/fuzztest/cloudsyncmanager_fuzzer_two/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..3e8da497dba67429e21acd55a7d63b214bffd86c --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_two/BUILD.gn @@ -0,0 +1,61 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncManagerFuzzTestTwo") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_two" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${distributedfile_path}/frameworks/native/cloudsync_kit_inner/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ "${distributedfile_path}/test/fuzztest/cloudsyncmanager_fuzzer_two/cloudsyncmanager_fuzzer_two.cpp" ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${innerkits_native_path}/cloudsync_kit_inner:cloudsync_kit_inner", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "c_utils:utils", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + ] +} + +group("fuzztest") { + testonly = true + deps = [ ":CloudSyncManagerFuzzTestTwo" ] +} diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_two/cloudsyncmanager_fuzzer_two.cpp b/test/fuzztest/cloudsyncmanager_fuzzer_two/cloudsyncmanager_fuzzer_two.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e4cbd54f5932af537a92bf0b7387e6361f26927a --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_two/cloudsyncmanager_fuzzer_two.cpp @@ -0,0 +1,89 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncmanager_fuzzer_two.h" + +#include +#include +#include + +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_manager.h" +#include "cloud_sync_manager_impl.h" +#include "service_proxy.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +void GetSyncTimeFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int64_t syncTime{0}; + std::string bundleName = fuzzData.GetStringFromData(static_cast(size)); + CloudSyncManager::GetInstance().GetSyncTime(syncTime, bundleName); +} + +void StartSyncCallbackFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + bool forceFlag = fuzzData.GetData(); + auto cloudSyncCallback = make_shared(); + CloudSyncManager::GetInstance().StartSync(forceFlag, cloudSyncCallback); +} + +void TriggerSyncFuzzTest(FuzzData &fuzzData, size_t size) +{ + fuzzData.ResetData(size); + int32_t userId = 100; + std::string bundleName = fuzzData.GetStringFromData(static_cast(size)); + CloudSyncManager::GetInstance().TriggerSync(bundleName, userId); + + fuzzData.ResetData(size); + userId = fuzzData.GetData(); + bundleName = fuzzData.GetStringFromData(static_cast(size - U32_AT_SIZE)); + CloudSyncManager::GetInstance().TriggerSync(bundleName, userId); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size <= (OHOS::U32_AT_SIZE << 1)) { + return 0; + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::GetSyncTimeFuzzTest(fuzzData, size); + OHOS::StartSyncCallbackFuzzTest(fuzzData, size); + OHOS::TriggerSyncFuzzTest(fuzzData, size); + return 0; +} diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_two/cloudsyncmanager_fuzzer_two.h b/test/fuzztest/cloudsyncmanager_fuzzer_two/cloudsyncmanager_fuzzer_two.h new file mode 100644 index 0000000000000000000000000000000000000000..8f30bf89201110a95db4fa9594462f5d5e52a58f --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_two/cloudsyncmanager_fuzzer_two.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_MANAGER_FUZZER_TWO_H +#define CLOUD_SYNC_MANAGER_FUZZER_TWO_H + +#define FUZZ_PROJECT_NAME "cloudsyncmanager_fuzzer_two" + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_two/corpus/init b/test/fuzztest/cloudsyncmanager_fuzzer_two/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_two/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncmanager_fuzzer_two/project.xml b/test/fuzztest/cloudsyncmanager_fuzzer_two/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncmanager_fuzzer_two/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer/BUILD.gn b/test/fuzztest/cloudsyncservicestub_fuzzer/BUILD.gn index 5d8e388597c3562142bdeea02704b180033bc169..6aa88c0e629ab3fd1254e28b8da01cfe325a15bd 100644 --- a/test/fuzztest/cloudsyncservicestub_fuzzer/BUILD.gn +++ b/test/fuzztest/cloudsyncservicestub_fuzzer/BUILD.gn @@ -50,7 +50,6 @@ ohos_fuzztest("CloudSyncServiceStubFuzzTest") { "--coverage", ] sources = [ - "${distributedfile_path}/services/cloudsyncservice/src/ipc/cloud_sync_service_stub.cpp", "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer/cloudsyncservicestub_fuzzer.cpp", ] @@ -77,5 +76,5 @@ ohos_fuzztest("CloudSyncServiceStubFuzzTest") { group("fuzztest") { testonly = true - deps = [] + deps = [":CloudSyncServiceStubFuzzTest"] } diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer/cloudsyncservicestub_fuzzer.cpp b/test/fuzztest/cloudsyncservicestub_fuzzer/cloudsyncservicestub_fuzzer.cpp index d1a74c7dc2ab8d1f4470af97641bdf854692ce88..f59a44ac8dfa938f8995a14dbea4eafce9b548fe 100644 --- a/test/fuzztest/cloudsyncservicestub_fuzzer/cloudsyncservicestub_fuzzer.cpp +++ b/test/fuzztest/cloudsyncservicestub_fuzzer/cloudsyncservicestub_fuzzer.cpp @@ -159,482 +159,6 @@ void HandleDeleteAssetFuzzTest(std::shared_ptr cloudSyncSe cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); } - -void HandleDisableCloudFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - string accountId = fuzzData.GetStringFromData(static_cast(size)); - datas.WriteString(accountId); - datas.RewindRead(0); - // SERVICE_CMD_DISABLE_CLOUD - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DISABLE_CLOUD); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleDownloadFileFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int32_t userId = fuzzData.GetData(); - datas.WriteInt32(userId); - int pos = static_cast(size - U32_AT_SIZE) / (SPLITE_SIZE + 1); - string bundleName = fuzzData.GetStringFromData(pos); - datas.WriteString(bundleName); - AssetInfo assetInfo = {.uri = fuzzData.GetStringFromData(pos), - .recordType = fuzzData.GetStringFromData(pos), - .recordId = fuzzData.GetStringFromData(pos), - .fieldKey = fuzzData.GetStringFromData(pos), - .assetName = fuzzData.GetStringFromData(pos)}; - AssetInfoObj assetInfoObj(assetInfo); - datas.WriteParcelable(&assetInfoObj); - datas.RewindRead(0); - // SERVICE_CMD_DOWNLOAD_FILE - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILE); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleEnableCloudFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int pos = static_cast(size >> 1); - string accountId = fuzzData.GetStringFromData(pos); - datas.WriteString(accountId); - int itemStrLen = pos / SPLITE_SIZE; - if (itemStrLen <= static_cast(BOOL_AT_SIZE)) { - return; - } - std::map switchData; - for (int i = 0; i < SPLITE_SIZE; i++) { - string itemStr = fuzzData.GetStringFromData(itemStrLen - static_cast(BOOL_AT_SIZE)); - bool itemBool = fuzzData.GetData(); - switchData.insert(pair(itemStr, itemBool)); - } - SwitchDataObj switchDataObj; - switchDataObj.switchData = switchData; - datas.WriteParcelable(&switchDataObj); - datas.RewindRead(0); - // SERVICE_CMD_ENABLE_CLOUD - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_ENABLE_CLOUD); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleNotifyDataChangeFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int pos = static_cast(size >> 1); - string accountId = fuzzData.GetStringFromData(pos); - datas.WriteString(accountId); - string bundleName = fuzzData.GetStringFromData(pos); - datas.WriteString(bundleName); - datas.RewindRead(0); - // SERVICE_CMD_NOTIFY_DATA_CHANGE - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_DATA_CHANGE); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleRegisterCallbackInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - sptr callback = new (std::nothrow) ICloudSyncCallbackTest(); - if (callback == nullptr) { - return; - } - datas.WriteRemoteObject(callback->AsObject().GetRefPtr()); - string bundleName = fuzzData.GetStringFromData(static_cast(size)); - datas.WriteString(bundleName); - datas.RewindRead(0); - // SERVICE_CMD_REGISTER_CALLBACK - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_CALLBACK); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleStartDownloadFileFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int64_t downloadId = fuzzData.GetData(); - int len = static_cast(size - U64_AT_SIZE); - string path = fuzzData.GetStringFromData(len); - datas.WriteString(path); - sptr callback = new (std::nothrow) ICloudDownloadCallbackTest(); - datas.WriteRemoteObject(callback->AsObject().GetRefPtr()); - datas.WriteInt64(downloadId); - datas.RewindRead(0); - // SERVICE_CMD_START_DOWNLOAD_FILE - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_DOWNLOAD_FILE); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleStartSyncInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - auto forceFlag = fuzzData.GetData(); - datas.WriteBool(forceFlag); - string bundleName = fuzzData.GetStringFromData(static_cast(size - BOOL_AT_SIZE)); - datas.WriteString(bundleName); - datas.RewindRead(0); - // SERVICE_CMD_START_SYNC - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_SYNC); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleStopDownloadFileFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int64_t downloadId = fuzzData.GetData(); - int len = static_cast(size - U64_AT_SIZE); - bool needClean = fuzzData.GetData(); - datas.WriteInt64(downloadId); - datas.WriteBool(needClean); - datas.RewindRead(0); - // SERVICE_CMD_STOP_DOWNLOAD_FILE - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_DOWNLOAD_FILE); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleStopSyncInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - const uint8_t *data, - size_t size) -{ - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - datas.WriteBuffer(data, size); - datas.RewindRead(0); - // SERVICE_CMD_STOP_SYNC - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_SYNC); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleUnRegisterCallbackInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - string bundleName = fuzzData.GetStringFromData(static_cast(size)); - datas.WriteString(bundleName); - datas.RewindRead(0); - // SERVICE_CMD_UNREGISTER_CALLBACK - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_CALLBACK); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleUploadAssetFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int32_t userId = fuzzData.GetData(); - datas.WriteInt32(userId); - int len = static_cast(size - U32_AT_SIZE); - string request = fuzzData.GetStringFromData(len); - datas.WriteString(request); - datas.RewindRead(0); - // SERVICE_CMD_UPLOAD_ASSET - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UPLOAD_ASSET); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleTriggerSyncInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int len = static_cast(size - U32_AT_SIZE); - string bundleName = fuzzData.GetStringFromData(len); - datas.WriteString(bundleName); - int32_t userId = fuzzData.GetData(); - datas.WriteInt32(userId); - datas.RewindRead(0); - // SERVICE_CMD_TRIGGER_SYNC - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_TRIGGER_SYNC); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleNotifyEventChangeFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int32_t userId = fuzzData.GetData(); - datas.WriteInt32(userId); - - int pos = static_cast((size - U32_AT_SIZE) >> 1); - string eventIdStr = fuzzData.GetStringFromData(pos); - string extraDataStr = fuzzData.GetStringFromData(pos); - datas.WriteString(eventIdStr); - datas.WriteString(extraDataStr); - datas.RewindRead(0); - // SERVICE_CMD_NOTIFY_EVENT_CHANGE - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_EVENT_CHANGE); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleStartFileCacheFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int32_t userId = fuzzData.GetData(); - datas.WriteInt32(userId); - int pos = static_cast((size - U32_AT_SIZE) >> 1); - string eventIdStr = fuzzData.GetStringFromData(pos); - string extraDataStr = fuzzData.GetStringFromData(pos); - datas.WriteString(eventIdStr); - datas.WriteString(extraDataStr); - datas.RewindRead(0); - // SERVICE_CMD_START_FILE_CACHE - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_FILE_CACHE); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleDownloadFilesFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - int32_t userId = fuzzData.GetData(); - datas.WriteInt32(userId); - int32_t vecSize = fuzzData.GetData() % SPLITE_SIZE + 1; - auto remainSize = fuzzData.GetRemainSize(); - if (static_cast(remainSize) <= vecSize * SPLITE_SIZE + 1) { - return; - } - int len = static_cast(remainSize / (vecSize * SPLITE_SIZE + 1)); - string bundleName = fuzzData.GetStringFromData(len); - datas.WriteString(bundleName); - datas.WriteInt32(vecSize); - for (auto i = 0; i < vecSize; i++) { - AssetInfo assetInfo = {.uri = fuzzData.GetStringFromData(len), - .recordType = fuzzData.GetStringFromData(len), - .recordId = fuzzData.GetStringFromData(len), - .fieldKey = fuzzData.GetStringFromData(len), - .assetName = fuzzData.GetStringFromData(len)}; - AssetInfoObj assetInfoObj(assetInfo); - datas.WriteParcelable(&assetInfoObj); - } - datas.RewindRead(0); - // SERVICE_CMD_DOWNLOAD_FILES - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILES); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleDownloadAssetFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - uint64_t taskId = fuzzData.GetData(); - datas.WriteUint64(taskId); - int32_t userId = fuzzData.GetData(); - datas.WriteUint32(userId); - int len = static_cast(fuzzData.GetRemainSize() >> 1); - string bundleName = fuzzData.GetStringFromData(len); - datas.WriteString(bundleName); - string networkId = fuzzData.GetStringFromData(len); - datas.WriteString(networkId); - - fuzzData.ResetData(size); - len = static_cast(size) / SPLITE_SIZE; - AssetInfo assetInfo = {.uri = fuzzData.GetStringFromData(len), - .recordType = fuzzData.GetStringFromData(len), - .recordId = fuzzData.GetStringFromData(len), - .fieldKey = fuzzData.GetStringFromData(len), - .assetName = fuzzData.GetStringFromData(len)}; - AssetInfoObj assetInfoObj(assetInfo); - datas.WriteParcelable(&assetInfoObj); - datas.RewindRead(0); - // SERVICE_CMD_DOWNLOAD_ASSET - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_ASSET); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleRegisterDownloadAssetCallbackFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - sptr callback = new (std::nothrow) IDownloadAssetCallbackTest(); - if (callback == nullptr) { - return; - } - datas.WriteRemoteObject(callback->AsObject().GetRefPtr()); - datas.RewindRead(0); - // SERVICE_CMD_REGISTER_DOWNLOAD_ASSET_CALLBACK - uint32_t code = - static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_ASSET_CALLBACK); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleGetSyncTimeFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - string bundleName = fuzzData.GetStringFromData(static_cast(size)); - datas.WriteString(bundleName); - datas.RewindRead(0); - // SERVICE_CMD_GET_SYNC_TIME - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_TIME); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} - -void HandleCleanCacheFuzzTest(std::shared_ptr cloudSyncServiceStubStr, - FuzzData &fuzzData, - size_t size) -{ - fuzzData.ResetData(size); - MessageParcel datas; - if (!WriteInterfaceToken(datas)) { - return; - } - string uri = fuzzData.GetStringFromData(static_cast(size)); - datas.WriteString(uri); - datas.RewindRead(0); - // SERVICE_CMD_CLEAN_CACHE - uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN_CACHE); - MessageParcel reply; - MessageOption option; - - cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); -} } // namespace OHOS /* Fuzzer entry point */ @@ -657,25 +181,6 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) OHOS::HandleChangeAppSwitchFuzzTest(cloudSyncServiceStubStr, fuzzData, size); OHOS::HandleCleanFuzzTest(cloudSyncServiceStubStr, fuzzData, size); OHOS::HandleDeleteAssetFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleDisableCloudFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleDownloadFileFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleEnableCloudFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleNotifyDataChangeFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleRegisterCallbackInnerFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleStartDownloadFileFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleStartSyncInnerFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleStopDownloadFileFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleStopSyncInnerFuzzTest(cloudSyncServiceStubStr, data, size); - OHOS::HandleUnRegisterCallbackInnerFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleUploadAssetFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleTriggerSyncInnerFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleNotifyEventChangeFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleStartFileCacheFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleDownloadFilesFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleDownloadAssetFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleRegisterDownloadAssetCallbackFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleGetSyncTimeFuzzTest(cloudSyncServiceStubStr, fuzzData, size); - OHOS::HandleCleanCacheFuzzTest(cloudSyncServiceStubStr, fuzzData, size); if (OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_ != nullptr) { OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().queue_.wait( OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_); diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_five/BUILD.gn b/test/fuzztest/cloudsyncservicestub_fuzzer_five/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..76da51677c373f165bb3b1fe050bdb7af43b24a7 --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_five/BUILD.gn @@ -0,0 +1,80 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncServiceStubFuzzTestFive") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_five" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${services_path}/cloudsyncservice/include", + "${services_path}/cloudsyncservice/include/cycle_task", + "${services_path}/cloudsyncservice/include/cycle_task/tasks", + "${services_path}/cloudsyncservice/include/data_sync", + "${services_path}/cloudsyncservice/include/data_sync/cloud_disk_data_sync", + "${services_path}/cloudsyncservice/include/data_sync/gallery_data_sync", + "${services_path}/cloudsyncservice/include/ipc", + "${services_path}/cloudsyncservice/include/sync_rule", + "${services_path}/cloudsyncservice/include/transport", + "${services_path}/cloudsyncservice/include/transport/softbus", + "${utils_path}/dentry/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_five/cloudsyncservicestub_fuzzer_five.cpp", + ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${services_path}/cloudsyncservice:cloudsync_sa_static", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", + "c_utils:utils", + "device_manager:devicemanagersdk", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [":CloudSyncServiceStubFuzzTestFive"] +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_five/cloudsyncservicestub_fuzzer_five.cpp b/test/fuzztest/cloudsyncservicestub_fuzzer_five/cloudsyncservicestub_fuzzer_five.cpp new file mode 100644 index 0000000000000000000000000000000000000000..23fdea0370dbe24506f174f1d755b8ef924c2a2b --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_five/cloudsyncservicestub_fuzzer_five.cpp @@ -0,0 +1,189 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncservicestub_fuzzer_five.h" + +#include +#include +#include +#include + +#include "accesstoken_kit.h" +#include "cloud_file_sync_service_interface_code.h" +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_service.h" +#include "cloud_sync_service_stub.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" +#include "i_download_asset_callback.h" +#include "task_state_manager.h" + +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "utils_log.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; +constexpr int SPLITE_SIZE = 5; +const std::u16string CLOUD_SYNC_SERVICE_TOKEN = u"OHOS.Filemanagement.Dfs.ICloudSyncService"; +constexpr int32_t SERVICE_SA_ID = 5204; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +class IDownloadAssetCallbackTest : public IRemoteStub { +public: + void OnFinished(const TaskId taskId, const std::string &uri, const int32_t result) override {} +}; + +void NativeTokenGet(bool isSystem) +{ + uint64_t tokenId; + static const char *perms[] = {"ohos.permission.CLOUDFILE_SYNC", "ohos.permission.CLOUDFILE_SYNC_MANAGER", + "ohos.permission.PROXY_AUTHORIZATION_URI"}; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 3, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "CloudOnRemoteRequestFuzzerTest"; + tokenId = GetAccessTokenId(&infoInstance); + if (isSystem) { + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + } + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +bool WriteInterfaceToken(MessageParcel &data) +{ + if (!data.WriteInterfaceToken(CLOUD_SYNC_SERVICE_TOKEN)) { + LOGE("Write token failed."); + return false; + } + return true; +} + +void HandleUnRegisterCallbackInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + string bundleName = fuzzData.GetStringFromData(static_cast(size)); + datas.WriteString(bundleName); + datas.RewindRead(0); + // SERVICE_CMD_UNREGISTER_CALLBACK + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UNREGISTER_CALLBACK); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleUploadAssetFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int32_t userId = fuzzData.GetData(); + datas.WriteInt32(userId); + int len = static_cast(size - U32_AT_SIZE); + string request = fuzzData.GetStringFromData(len); + datas.WriteString(request); + datas.RewindRead(0); + // SERVICE_CMD_UPLOAD_ASSET + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_UPLOAD_ASSET); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleTriggerSyncInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int len = static_cast(size - U32_AT_SIZE); + string bundleName = fuzzData.GetStringFromData(len); + datas.WriteString(bundleName); + int32_t userId = fuzzData.GetData(); + datas.WriteInt32(userId); + datas.RewindRead(0); + // SERVICE_CMD_TRIGGER_SYNC + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_TRIGGER_SYNC); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size < OHOS::U32_AT_SIZE * static_cast(OHOS::SPLITE_SIZE)) { + return 0; + } + + OHOS::NativeTokenGet(true); + auto cloudSyncServiceStubStr = + std::make_shared(OHOS::SERVICE_SA_ID); + if (cloudSyncServiceStubStr->dataSyncManager_ == nullptr) { + cloudSyncServiceStubStr->dataSyncManager_ = + std::make_shared(); + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::HandleUnRegisterCallbackInnerFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleUploadAssetFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleTriggerSyncInnerFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + if (OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_ != nullptr) { + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().queue_.wait( + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_); + } + return 0; +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_five/cloudsyncservicestub_fuzzer_five.h b/test/fuzztest/cloudsyncservicestub_fuzzer_five/cloudsyncservicestub_fuzzer_five.h new file mode 100644 index 0000000000000000000000000000000000000000..3d102f070baf213d4ed71aa8366d2926f807c9b4 --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_five/cloudsyncservicestub_fuzzer_five.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_SERVICE_STUB_FUZZER_FIVE_H +#define CLOUD_SYNC_SERVICE_STUB_FUZZER_FIVE_H + + +#define FUZZ_PROJECT_NAME "cloudsyncservicestub_fuzzer_five" + + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_five/corpus/init b/test/fuzztest/cloudsyncservicestub_fuzzer_five/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_five/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_five/project.xml b/test/fuzztest/cloudsyncservicestub_fuzzer_five/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_five/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_four/BUILD.gn b/test/fuzztest/cloudsyncservicestub_fuzzer_four/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..445705aec76bf669524648450fe26dfbc4d98a1c --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_four/BUILD.gn @@ -0,0 +1,80 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncServiceStubFuzzTestFour") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_four" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${services_path}/cloudsyncservice/include", + "${services_path}/cloudsyncservice/include/cycle_task", + "${services_path}/cloudsyncservice/include/cycle_task/tasks", + "${services_path}/cloudsyncservice/include/data_sync", + "${services_path}/cloudsyncservice/include/data_sync/cloud_disk_data_sync", + "${services_path}/cloudsyncservice/include/data_sync/gallery_data_sync", + "${services_path}/cloudsyncservice/include/ipc", + "${services_path}/cloudsyncservice/include/sync_rule", + "${services_path}/cloudsyncservice/include/transport", + "${services_path}/cloudsyncservice/include/transport/softbus", + "${utils_path}/dentry/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_four/cloudsyncservicestub_fuzzer_four.cpp", + ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${services_path}/cloudsyncservice:cloudsync_sa_static", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", + "c_utils:utils", + "device_manager:devicemanagersdk", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [":CloudSyncServiceStubFuzzTestFour"] +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_four/cloudsyncservicestub_fuzzer_four.cpp b/test/fuzztest/cloudsyncservicestub_fuzzer_four/cloudsyncservicestub_fuzzer_four.cpp new file mode 100644 index 0000000000000000000000000000000000000000..0fb1c6703d83230df1de93ee02e420a13e6c2959 --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_four/cloudsyncservicestub_fuzzer_four.cpp @@ -0,0 +1,186 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncservicestub_fuzzer_four.h" + +#include +#include +#include +#include + +#include "accesstoken_kit.h" +#include "cloud_file_sync_service_interface_code.h" +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_service.h" +#include "cloud_sync_service_stub.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" +#include "i_download_asset_callback.h" +#include "task_state_manager.h" + +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "utils_log.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; +constexpr int SPLITE_SIZE = 5; +const std::u16string CLOUD_SYNC_SERVICE_TOKEN = u"OHOS.Filemanagement.Dfs.ICloudSyncService"; +constexpr int32_t SERVICE_SA_ID = 5204; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +class IDownloadAssetCallbackTest : public IRemoteStub { +public: + void OnFinished(const TaskId taskId, const std::string &uri, const int32_t result) override {} +}; + +void NativeTokenGet(bool isSystem) +{ + uint64_t tokenId; + static const char *perms[] = {"ohos.permission.CLOUDFILE_SYNC", "ohos.permission.CLOUDFILE_SYNC_MANAGER", + "ohos.permission.PROXY_AUTHORIZATION_URI"}; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 3, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "CloudOnRemoteRequestFuzzerTest"; + tokenId = GetAccessTokenId(&infoInstance); + if (isSystem) { + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + } + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +bool WriteInterfaceToken(MessageParcel &data) +{ + if (!data.WriteInterfaceToken(CLOUD_SYNC_SERVICE_TOKEN)) { + LOGE("Write token failed."); + return false; + } + return true; +} + +void HandleStartSyncInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + auto forceFlag = fuzzData.GetData(); + datas.WriteBool(forceFlag); + string bundleName = fuzzData.GetStringFromData(static_cast(size - BOOL_AT_SIZE)); + datas.WriteString(bundleName); + datas.RewindRead(0); + // SERVICE_CMD_START_SYNC + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_SYNC); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleStopDownloadFileFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int64_t downloadId = fuzzData.GetData(); + int len = static_cast(size - U64_AT_SIZE); + bool needClean = fuzzData.GetData(); + datas.WriteInt64(downloadId); + datas.WriteBool(needClean); + datas.RewindRead(0); + // SERVICE_CMD_STOP_DOWNLOAD_FILE + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_DOWNLOAD_FILE); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleStopSyncInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + const uint8_t *data, + size_t size) +{ + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + datas.WriteBuffer(data, size); + datas.RewindRead(0); + // SERVICE_CMD_STOP_SYNC + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_STOP_SYNC); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size < OHOS::U32_AT_SIZE * static_cast(OHOS::SPLITE_SIZE)) { + return 0; + } + + OHOS::NativeTokenGet(true); + auto cloudSyncServiceStubStr = + std::make_shared(OHOS::SERVICE_SA_ID); + if (cloudSyncServiceStubStr->dataSyncManager_ == nullptr) { + cloudSyncServiceStubStr->dataSyncManager_ = + std::make_shared(); + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::HandleStartSyncInnerFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleStopDownloadFileFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleStopSyncInnerFuzzTest(cloudSyncServiceStubStr, data, size); + if (OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_ != nullptr) { + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().queue_.wait( + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_); + } + return 0; +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_four/cloudsyncservicestub_fuzzer_four.h b/test/fuzztest/cloudsyncservicestub_fuzzer_four/cloudsyncservicestub_fuzzer_four.h new file mode 100644 index 0000000000000000000000000000000000000000..8338d1b5426de55d036fb83c601fa18c3f48452f --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_four/cloudsyncservicestub_fuzzer_four.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_SERVICE_STUB_FUZZER_FOUR_H +#define CLOUD_SYNC_SERVICE_STUB_FUZZER_FOUR_H + + +#define FUZZ_PROJECT_NAME "cloudsyncservicestub_fuzzer_four" + + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_four/corpus/init b/test/fuzztest/cloudsyncservicestub_fuzzer_four/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_four/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_four/project.xml b/test/fuzztest/cloudsyncservicestub_fuzzer_four/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_four/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_seven/BUILD.gn b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..1203846e195da84d23b329e60ba681392adc6d43 --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/BUILD.gn @@ -0,0 +1,80 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncServiceStubFuzzTestSeven") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_seven" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${services_path}/cloudsyncservice/include", + "${services_path}/cloudsyncservice/include/cycle_task", + "${services_path}/cloudsyncservice/include/cycle_task/tasks", + "${services_path}/cloudsyncservice/include/data_sync", + "${services_path}/cloudsyncservice/include/data_sync/cloud_disk_data_sync", + "${services_path}/cloudsyncservice/include/data_sync/gallery_data_sync", + "${services_path}/cloudsyncservice/include/ipc", + "${services_path}/cloudsyncservice/include/sync_rule", + "${services_path}/cloudsyncservice/include/transport", + "${services_path}/cloudsyncservice/include/transport/softbus", + "${utils_path}/dentry/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_seven/cloudsyncservicestub_fuzzer_seven.cpp", + ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${services_path}/cloudsyncservice:cloudsync_sa_static", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", + "c_utils:utils", + "device_manager:devicemanagersdk", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [":CloudSyncServiceStubFuzzTestSeven"] +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_seven/cloudsyncservicestub_fuzzer_seven.cpp b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/cloudsyncservicestub_fuzzer_seven.cpp new file mode 100644 index 0000000000000000000000000000000000000000..ff3b4dd2f122b69226898e50c9292b320399fdfe --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/cloudsyncservicestub_fuzzer_seven.cpp @@ -0,0 +1,225 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncservicestub_fuzzer_seven.h" + +#include +#include +#include +#include + +#include "accesstoken_kit.h" +#include "cloud_file_sync_service_interface_code.h" +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_service.h" +#include "cloud_sync_service_stub.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" +#include "i_download_asset_callback.h" +#include "task_state_manager.h" + +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "utils_log.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; +constexpr int SPLITE_SIZE = 5; +const std::u16string CLOUD_SYNC_SERVICE_TOKEN = u"OHOS.Filemanagement.Dfs.ICloudSyncService"; +constexpr int32_t SERVICE_SA_ID = 5204; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +class IDownloadAssetCallbackTest : public IRemoteStub { +public: + void OnFinished(const TaskId taskId, const std::string &uri, const int32_t result) override {} +}; + +void NativeTokenGet(bool isSystem) +{ + uint64_t tokenId; + static const char *perms[] = {"ohos.permission.CLOUDFILE_SYNC", "ohos.permission.CLOUDFILE_SYNC_MANAGER", + "ohos.permission.PROXY_AUTHORIZATION_URI"}; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 3, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "CloudOnRemoteRequestFuzzerTest"; + tokenId = GetAccessTokenId(&infoInstance); + if (isSystem) { + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + } + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +bool WriteInterfaceToken(MessageParcel &data) +{ + if (!data.WriteInterfaceToken(CLOUD_SYNC_SERVICE_TOKEN)) { + LOGE("Write token failed."); + return false; + } + return true; +} + +void HandleDownloadAssetFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + uint64_t taskId = fuzzData.GetData(); + datas.WriteUint64(taskId); + int32_t userId = fuzzData.GetData(); + datas.WriteUint32(userId); + int len = static_cast(fuzzData.GetRemainSize() >> 1); + string bundleName = fuzzData.GetStringFromData(len); + datas.WriteString(bundleName); + string networkId = fuzzData.GetStringFromData(len); + datas.WriteString(networkId); + + fuzzData.ResetData(size); + len = static_cast(size) / SPLITE_SIZE; + AssetInfo assetInfo = {.uri = fuzzData.GetStringFromData(len), + .recordType = fuzzData.GetStringFromData(len), + .recordId = fuzzData.GetStringFromData(len), + .fieldKey = fuzzData.GetStringFromData(len), + .assetName = fuzzData.GetStringFromData(len)}; + AssetInfoObj assetInfoObj(assetInfo); + datas.WriteParcelable(&assetInfoObj); + datas.RewindRead(0); + // SERVICE_CMD_DOWNLOAD_ASSET + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_ASSET); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleRegisterDownloadAssetCallbackFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + sptr callback = new (std::nothrow) IDownloadAssetCallbackTest(); + if (callback == nullptr) { + return; + } + datas.WriteRemoteObject(callback->AsObject().GetRefPtr()); + datas.RewindRead(0); + // SERVICE_CMD_REGISTER_DOWNLOAD_ASSET_CALLBACK + uint32_t code = + static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_DOWNLOAD_ASSET_CALLBACK); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleGetSyncTimeFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + string bundleName = fuzzData.GetStringFromData(static_cast(size)); + datas.WriteString(bundleName); + datas.RewindRead(0); + // SERVICE_CMD_GET_SYNC_TIME + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_GET_SYNC_TIME); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleCleanCacheFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + string uri = fuzzData.GetStringFromData(static_cast(size)); + datas.WriteString(uri); + datas.RewindRead(0); + // SERVICE_CMD_CLEAN_CACHE + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_CLEAN_CACHE); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size < OHOS::U32_AT_SIZE * static_cast(OHOS::SPLITE_SIZE)) { + return 0; + } + + OHOS::NativeTokenGet(true); + auto cloudSyncServiceStubStr = + std::make_shared(OHOS::SERVICE_SA_ID); + if (cloudSyncServiceStubStr->dataSyncManager_ == nullptr) { + cloudSyncServiceStubStr->dataSyncManager_ = + std::make_shared(); + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::HandleDownloadAssetFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleRegisterDownloadAssetCallbackFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleGetSyncTimeFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleCleanCacheFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + if (OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_ != nullptr) { + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().queue_.wait( + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_); + } + return 0; +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_seven/cloudsyncservicestub_fuzzer_seven.h b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/cloudsyncservicestub_fuzzer_seven.h new file mode 100644 index 0000000000000000000000000000000000000000..1970b7d45960692d370222ec143775b2ec93579b --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/cloudsyncservicestub_fuzzer_seven.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_SERVICE_STUB_FUZZER_SEVEN_H +#define CLOUD_SYNC_SERVICE_STUB_FUZZER_SEVEN_H + + +#define FUZZ_PROJECT_NAME "cloudsyncservicestub_fuzzer_seven" + + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_seven/corpus/init b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_seven/project.xml b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_seven/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_six/BUILD.gn b/test/fuzztest/cloudsyncservicestub_fuzzer_six/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..e92302efd85f159f51b2fa2016213137e974429a --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_six/BUILD.gn @@ -0,0 +1,80 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncServiceStubFuzzTestSix") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_six" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${services_path}/cloudsyncservice/include", + "${services_path}/cloudsyncservice/include/cycle_task", + "${services_path}/cloudsyncservice/include/cycle_task/tasks", + "${services_path}/cloudsyncservice/include/data_sync", + "${services_path}/cloudsyncservice/include/data_sync/cloud_disk_data_sync", + "${services_path}/cloudsyncservice/include/data_sync/gallery_data_sync", + "${services_path}/cloudsyncservice/include/ipc", + "${services_path}/cloudsyncservice/include/sync_rule", + "${services_path}/cloudsyncservice/include/transport", + "${services_path}/cloudsyncservice/include/transport/softbus", + "${utils_path}/dentry/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_six/cloudsyncservicestub_fuzzer_six.cpp", + ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${services_path}/cloudsyncservice:cloudsync_sa_static", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", + "c_utils:utils", + "device_manager:devicemanagersdk", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [":CloudSyncServiceStubFuzzTestSix"] +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_six/cloudsyncservicestub_fuzzer_six.cpp b/test/fuzztest/cloudsyncservicestub_fuzzer_six/cloudsyncservicestub_fuzzer_six.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b25d6e4d25e7c2d85451d50dc7df0671dd3cead6 --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_six/cloudsyncservicestub_fuzzer_six.cpp @@ -0,0 +1,212 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncservicestub_fuzzer_six.h" + +#include +#include +#include +#include + +#include "accesstoken_kit.h" +#include "cloud_file_sync_service_interface_code.h" +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_service.h" +#include "cloud_sync_service_stub.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" +#include "i_download_asset_callback.h" +#include "task_state_manager.h" + +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "utils_log.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; +constexpr int SPLITE_SIZE = 5; +const std::u16string CLOUD_SYNC_SERVICE_TOKEN = u"OHOS.Filemanagement.Dfs.ICloudSyncService"; +constexpr int32_t SERVICE_SA_ID = 5204; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +class IDownloadAssetCallbackTest : public IRemoteStub { +public: + void OnFinished(const TaskId taskId, const std::string &uri, const int32_t result) override {} +}; + +void NativeTokenGet(bool isSystem) +{ + uint64_t tokenId; + static const char *perms[] = {"ohos.permission.CLOUDFILE_SYNC", "ohos.permission.CLOUDFILE_SYNC_MANAGER", + "ohos.permission.PROXY_AUTHORIZATION_URI"}; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 3, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "CloudOnRemoteRequestFuzzerTest"; + tokenId = GetAccessTokenId(&infoInstance); + if (isSystem) { + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + } + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +bool WriteInterfaceToken(MessageParcel &data) +{ + if (!data.WriteInterfaceToken(CLOUD_SYNC_SERVICE_TOKEN)) { + LOGE("Write token failed."); + return false; + } + return true; +} + +void HandleNotifyEventChangeFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int32_t userId = fuzzData.GetData(); + datas.WriteInt32(userId); + + int pos = static_cast((size - U32_AT_SIZE) >> 1); + string eventIdStr = fuzzData.GetStringFromData(pos); + string extraDataStr = fuzzData.GetStringFromData(pos); + datas.WriteString(eventIdStr); + datas.WriteString(extraDataStr); + datas.RewindRead(0); + // SERVICE_CMD_NOTIFY_EVENT_CHANGE + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_EVENT_CHANGE); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleStartFileCacheFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int32_t userId = fuzzData.GetData(); + datas.WriteInt32(userId); + int pos = static_cast((size - U32_AT_SIZE) >> 1); + string eventIdStr = fuzzData.GetStringFromData(pos); + string extraDataStr = fuzzData.GetStringFromData(pos); + datas.WriteString(eventIdStr); + datas.WriteString(extraDataStr); + datas.RewindRead(0); + // SERVICE_CMD_START_FILE_CACHE + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_FILE_CACHE); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleDownloadFilesFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int32_t userId = fuzzData.GetData(); + datas.WriteInt32(userId); + int32_t vecSize = fuzzData.GetData() % SPLITE_SIZE + 1; + auto remainSize = fuzzData.GetRemainSize(); + if (static_cast(remainSize) <= vecSize * SPLITE_SIZE + 1) { + return; + } + int len = static_cast(remainSize / (vecSize * SPLITE_SIZE + 1)); + string bundleName = fuzzData.GetStringFromData(len); + datas.WriteString(bundleName); + datas.WriteInt32(vecSize); + for (auto i = 0; i < vecSize; i++) { + AssetInfo assetInfo = {.uri = fuzzData.GetStringFromData(len), + .recordType = fuzzData.GetStringFromData(len), + .recordId = fuzzData.GetStringFromData(len), + .fieldKey = fuzzData.GetStringFromData(len), + .assetName = fuzzData.GetStringFromData(len)}; + AssetInfoObj assetInfoObj(assetInfo); + datas.WriteParcelable(&assetInfoObj); + } + datas.RewindRead(0); + // SERVICE_CMD_DOWNLOAD_FILES + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILES); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size < OHOS::U32_AT_SIZE * static_cast(OHOS::SPLITE_SIZE)) { + return 0; + } + + OHOS::NativeTokenGet(true); + auto cloudSyncServiceStubStr = + std::make_shared(OHOS::SERVICE_SA_ID); + if (cloudSyncServiceStubStr->dataSyncManager_ == nullptr) { + cloudSyncServiceStubStr->dataSyncManager_ = + std::make_shared(); + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::HandleNotifyEventChangeFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleStartFileCacheFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleDownloadFilesFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + if (OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_ != nullptr) { + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().queue_.wait( + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_); + } + return 0; +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_six/cloudsyncservicestub_fuzzer_six.h b/test/fuzztest/cloudsyncservicestub_fuzzer_six/cloudsyncservicestub_fuzzer_six.h new file mode 100644 index 0000000000000000000000000000000000000000..8db71252a02d13ba798ccadccf66af2cf56ade56 --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_six/cloudsyncservicestub_fuzzer_six.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_SERVICE_STUB_FUZZER_SIX_H +#define CLOUD_SYNC_SERVICE_STUB_FUZZER_SIX_H + + +#define FUZZ_PROJECT_NAME "cloudsyncservicestub_fuzzer_six" + + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_six/corpus/init b/test/fuzztest/cloudsyncservicestub_fuzzer_six/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_six/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_six/project.xml b/test/fuzztest/cloudsyncservicestub_fuzzer_six/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_six/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_three/BUILD.gn b/test/fuzztest/cloudsyncservicestub_fuzzer_three/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..c2217886a3785525d8a10be1f9948efd4cd64cdb --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_three/BUILD.gn @@ -0,0 +1,80 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncServiceStubFuzzTestThree") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_three" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${services_path}/cloudsyncservice/include", + "${services_path}/cloudsyncservice/include/cycle_task", + "${services_path}/cloudsyncservice/include/cycle_task/tasks", + "${services_path}/cloudsyncservice/include/data_sync", + "${services_path}/cloudsyncservice/include/data_sync/cloud_disk_data_sync", + "${services_path}/cloudsyncservice/include/data_sync/gallery_data_sync", + "${services_path}/cloudsyncservice/include/ipc", + "${services_path}/cloudsyncservice/include/sync_rule", + "${services_path}/cloudsyncservice/include/transport", + "${services_path}/cloudsyncservice/include/transport/softbus", + "${utils_path}/dentry/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_three/cloudsyncservicestub_fuzzer_three.cpp", + ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${services_path}/cloudsyncservice:cloudsync_sa_static", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", + "c_utils:utils", + "device_manager:devicemanagersdk", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [":CloudSyncServiceStubFuzzTestThree"] +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_three/cloudsyncservicestub_fuzzer_three.cpp b/test/fuzztest/cloudsyncservicestub_fuzzer_three/cloudsyncservicestub_fuzzer_three.cpp new file mode 100644 index 0000000000000000000000000000000000000000..63bbd7699153df546b488ab708d54f7a2c577b1f --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_three/cloudsyncservicestub_fuzzer_three.cpp @@ -0,0 +1,196 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncservicestub_fuzzer_three.h" + +#include +#include +#include +#include + +#include "accesstoken_kit.h" +#include "cloud_file_sync_service_interface_code.h" +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_service.h" +#include "cloud_sync_service_stub.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" +#include "i_download_asset_callback.h" +#include "task_state_manager.h" + +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "utils_log.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; +constexpr int SPLITE_SIZE = 5; +const std::u16string CLOUD_SYNC_SERVICE_TOKEN = u"OHOS.Filemanagement.Dfs.ICloudSyncService"; +constexpr int32_t SERVICE_SA_ID = 5204; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +class IDownloadAssetCallbackTest : public IRemoteStub { +public: + void OnFinished(const TaskId taskId, const std::string &uri, const int32_t result) override {} +}; + +void NativeTokenGet(bool isSystem) +{ + uint64_t tokenId; + static const char *perms[] = {"ohos.permission.CLOUDFILE_SYNC", "ohos.permission.CLOUDFILE_SYNC_MANAGER", + "ohos.permission.PROXY_AUTHORIZATION_URI"}; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 3, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "CloudOnRemoteRequestFuzzerTest"; + tokenId = GetAccessTokenId(&infoInstance); + if (isSystem) { + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + } + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +bool WriteInterfaceToken(MessageParcel &data) +{ + if (!data.WriteInterfaceToken(CLOUD_SYNC_SERVICE_TOKEN)) { + LOGE("Write token failed."); + return false; + } + return true; +} + +void HandleNotifyDataChangeFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int pos = static_cast(size >> 1); + string accountId = fuzzData.GetStringFromData(pos); + datas.WriteString(accountId); + string bundleName = fuzzData.GetStringFromData(pos); + datas.WriteString(bundleName); + datas.RewindRead(0); + // SERVICE_CMD_NOTIFY_DATA_CHANGE + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_NOTIFY_DATA_CHANGE); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleRegisterCallbackInnerFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + sptr callback = new (std::nothrow) ICloudSyncCallbackTest(); + if (callback == nullptr) { + return; + } + datas.WriteRemoteObject(callback->AsObject().GetRefPtr()); + string bundleName = fuzzData.GetStringFromData(static_cast(size)); + datas.WriteString(bundleName); + datas.RewindRead(0); + // SERVICE_CMD_REGISTER_CALLBACK + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_REGISTER_CALLBACK); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleStartDownloadFileFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int64_t downloadId = fuzzData.GetData(); + int len = static_cast(size - U64_AT_SIZE); + string path = fuzzData.GetStringFromData(len); + datas.WriteString(path); + sptr callback = new (std::nothrow) ICloudDownloadCallbackTest(); + datas.WriteRemoteObject(callback->AsObject().GetRefPtr()); + datas.WriteInt64(downloadId); + datas.RewindRead(0); + // SERVICE_CMD_START_DOWNLOAD_FILE + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_START_DOWNLOAD_FILE); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size < OHOS::U32_AT_SIZE * static_cast(OHOS::SPLITE_SIZE)) { + return 0; + } + + OHOS::NativeTokenGet(true); + auto cloudSyncServiceStubStr = + std::make_shared(OHOS::SERVICE_SA_ID); + if (cloudSyncServiceStubStr->dataSyncManager_ == nullptr) { + cloudSyncServiceStubStr->dataSyncManager_ = + std::make_shared(); + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::HandleNotifyDataChangeFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleRegisterCallbackInnerFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleStartDownloadFileFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + if (OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_ != nullptr) { + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().queue_.wait( + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_); + } + return 0; +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_three/cloudsyncservicestub_fuzzer_three.h b/test/fuzztest/cloudsyncservicestub_fuzzer_three/cloudsyncservicestub_fuzzer_three.h new file mode 100644 index 0000000000000000000000000000000000000000..e87c6ef06fe959bc7ff242d8f85292610b19ca31 --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_three/cloudsyncservicestub_fuzzer_three.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_SERVICE_STUB_FUZZER_THREE_H +#define CLOUD_SYNC_SERVICE_STUB_FUZZER_THREE_H + + +#define FUZZ_PROJECT_NAME "cloudsyncservicestub_fuzzer_three" + + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_three/corpus/init b/test/fuzztest/cloudsyncservicestub_fuzzer_three/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_three/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_three/project.xml b/test/fuzztest/cloudsyncservicestub_fuzzer_three/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_three/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_two/BUILD.gn b/test/fuzztest/cloudsyncservicestub_fuzzer_two/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d1ec8010908aeb3be0734ab2b878ffa86a9672b7 --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_two/BUILD.gn @@ -0,0 +1,80 @@ +# Copyright (c) 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +import("//foundation/filemanagement/dfs_service/distributedfile.gni") +ohos_fuzztest("CloudSyncServiceStubFuzzTestTwo") { + module_out_path = "dfs_service/dfs_service" + fuzz_config_file = + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_two" + include_dirs = [ + "${distributedfile_path}/test/fuzztest/fuzz_common", + "${innerkits_native_path}/cloudsync_kit_inner", + "${services_path}/cloudsyncservice/include", + "${services_path}/cloudsyncservice/include/cycle_task", + "${services_path}/cloudsyncservice/include/cycle_task/tasks", + "${services_path}/cloudsyncservice/include/data_sync", + "${services_path}/cloudsyncservice/include/data_sync/cloud_disk_data_sync", + "${services_path}/cloudsyncservice/include/data_sync/gallery_data_sync", + "${services_path}/cloudsyncservice/include/ipc", + "${services_path}/cloudsyncservice/include/sync_rule", + "${services_path}/cloudsyncservice/include/transport", + "${services_path}/cloudsyncservice/include/transport/softbus", + "${utils_path}/dentry/include", + ] + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + "-flto", + "-fvisibility=hidden", + "-Dprivate=public", + "--coverage", + ] + ldflags = [ + "-flto", + "--coverage", + ] + sources = [ + "${distributedfile_path}/test/fuzztest/cloudsyncservicestub_fuzzer_two/cloudsyncservicestub_fuzzer_two.cpp", + ] + + deps = [ + "${innerkits_native_path}/cloud_file_kit_inner:cloudfile_kit", + "${services_path}/cloudsyncservice:cloudsync_sa_static", + "${utils_path}:libdistributedfileutils_lite", + ] + + external_deps = [ + "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken_shared", + "access_token:libtokensetproc_shared", + "c_utils:utils", + "device_manager:devicemanagersdk", + "ffrt:libffrt", + "hilog:libhilog", + "ipc:ipc_single", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] +} + +group("fuzztest") { + testonly = true + deps = [":CloudSyncServiceStubFuzzTestTwo"] +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_two/cloudsyncservicestub_fuzzer_two.cpp b/test/fuzztest/cloudsyncservicestub_fuzzer_two/cloudsyncservicestub_fuzzer_two.cpp new file mode 100644 index 0000000000000000000000000000000000000000..61a3669708b7866bc3089baf63fc6c3e0c50f0ee --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_two/cloudsyncservicestub_fuzzer_two.cpp @@ -0,0 +1,207 @@ +/* + * Copyright (c) 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. + */ +#include "cloudsyncservicestub_fuzzer_two.h" + +#include +#include +#include +#include + +#include "accesstoken_kit.h" +#include "cloud_file_sync_service_interface_code.h" +#include "cloud_fuzzer_helper.h" +#include "cloud_sync_service.h" +#include "cloud_sync_service_stub.h" +#include "i_cloud_download_callback.h" +#include "i_cloud_sync_callback.h" +#include "i_download_asset_callback.h" +#include "task_state_manager.h" + +#include "message_parcel.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "utils_log.h" + +namespace OHOS { +constexpr size_t U32_AT_SIZE = 4; +constexpr size_t U64_AT_SIZE = 8; +constexpr size_t BOOL_AT_SIZE = 1; +constexpr int SPLITE_SIZE = 5; +const std::u16string CLOUD_SYNC_SERVICE_TOKEN = u"OHOS.Filemanagement.Dfs.ICloudSyncService"; +constexpr int32_t SERVICE_SA_ID = 5204; + +using namespace OHOS::FileManagement::CloudSync; +using namespace std; +class ICloudSyncCallbackTest : public IRemoteStub { +public: + void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} + void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} +}; + +class ICloudDownloadCallbackTest : public IRemoteStub { +public: + void OnDownloadProcess(const DownloadProgressObj &progress) override {} +}; + +class IDownloadAssetCallbackTest : public IRemoteStub { +public: + void OnFinished(const TaskId taskId, const std::string &uri, const int32_t result) override {} +}; + +void NativeTokenGet(bool isSystem) +{ + uint64_t tokenId; + static const char *perms[] = {"ohos.permission.CLOUDFILE_SYNC", "ohos.permission.CLOUDFILE_SYNC_MANAGER", + "ohos.permission.PROXY_AUTHORIZATION_URI"}; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = 3, + .aclsNum = 0, + .dcaps = nullptr, + .perms = perms, + .acls = nullptr, + .aplStr = "system_core", + }; + + infoInstance.processName = "CloudOnRemoteRequestFuzzerTest"; + tokenId = GetAccessTokenId(&infoInstance); + if (isSystem) { + const uint64_t systemAppMask = (static_cast(1) << 32); + tokenId |= systemAppMask; + } + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +bool WriteInterfaceToken(MessageParcel &data) +{ + if (!data.WriteInterfaceToken(CLOUD_SYNC_SERVICE_TOKEN)) { + LOGE("Write token failed."); + return false; + } + return true; +} + +void HandleDisableCloudFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + string accountId = fuzzData.GetStringFromData(static_cast(size)); + datas.WriteString(accountId); + datas.RewindRead(0); + // SERVICE_CMD_DISABLE_CLOUD + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DISABLE_CLOUD); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleDownloadFileFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int32_t userId = fuzzData.GetData(); + datas.WriteInt32(userId); + int pos = static_cast(size - U32_AT_SIZE) / (SPLITE_SIZE + 1); + string bundleName = fuzzData.GetStringFromData(pos); + datas.WriteString(bundleName); + AssetInfo assetInfo = {.uri = fuzzData.GetStringFromData(pos), + .recordType = fuzzData.GetStringFromData(pos), + .recordId = fuzzData.GetStringFromData(pos), + .fieldKey = fuzzData.GetStringFromData(pos), + .assetName = fuzzData.GetStringFromData(pos)}; + AssetInfoObj assetInfoObj(assetInfo); + datas.WriteParcelable(&assetInfoObj); + datas.RewindRead(0); + // SERVICE_CMD_DOWNLOAD_FILE + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_DOWNLOAD_FILE); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} + +void HandleEnableCloudFuzzTest(std::shared_ptr cloudSyncServiceStubStr, + FuzzData &fuzzData, + size_t size) +{ + fuzzData.ResetData(size); + MessageParcel datas; + if (!WriteInterfaceToken(datas)) { + return; + } + int pos = static_cast(size >> 1); + string accountId = fuzzData.GetStringFromData(pos); + datas.WriteString(accountId); + int itemStrLen = pos / SPLITE_SIZE; + if (itemStrLen <= static_cast(BOOL_AT_SIZE)) { + return; + } + std::map switchData; + for (int i = 0; i < SPLITE_SIZE; i++) { + string itemStr = fuzzData.GetStringFromData(itemStrLen - static_cast(BOOL_AT_SIZE)); + bool itemBool = fuzzData.GetData(); + switchData.insert(pair(itemStr, itemBool)); + } + SwitchDataObj switchDataObj; + switchDataObj.switchData = switchData; + datas.WriteParcelable(&switchDataObj); + datas.RewindRead(0); + // SERVICE_CMD_ENABLE_CLOUD + uint32_t code = static_cast(CloudFileSyncServiceInterfaceCode::SERVICE_CMD_ENABLE_CLOUD); + MessageParcel reply; + MessageOption option; + + cloudSyncServiceStubStr->OnRemoteRequest(code, datas, reply, option); +} +} // namespace OHOS + +/* Fuzzer entry point */ +extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) +{ + /* Run your code on data */ + if (data == nullptr || size < OHOS::U32_AT_SIZE * static_cast(OHOS::SPLITE_SIZE)) { + return 0; + } + + OHOS::NativeTokenGet(true); + auto cloudSyncServiceStubStr = + std::make_shared(OHOS::SERVICE_SA_ID); + if (cloudSyncServiceStubStr->dataSyncManager_ == nullptr) { + cloudSyncServiceStubStr->dataSyncManager_ = + std::make_shared(); + } + + OHOS::FuzzData fuzzData(data, size); + OHOS::HandleDisableCloudFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleDownloadFileFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + OHOS::HandleEnableCloudFuzzTest(cloudSyncServiceStubStr, fuzzData, size); + if (OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_ != nullptr) { + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().queue_.wait( + OHOS::FileManagement::CloudSync::TaskStateManager::GetInstance().unloadTaskHandle_); + } + return 0; +} diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_two/cloudsyncservicestub_fuzzer_two.h b/test/fuzztest/cloudsyncservicestub_fuzzer_two/cloudsyncservicestub_fuzzer_two.h new file mode 100644 index 0000000000000000000000000000000000000000..3116f344d6b79ae104274057422d42ccb560e2ce --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_two/cloudsyncservicestub_fuzzer_two.h @@ -0,0 +1,22 @@ +/* + * Copyright (c) 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 CLOUD_SYNC_SERVICE_STUB_FUZZER_TWO_H +#define CLOUD_SYNC_SERVICE_STUB_FUZZER_TWO_H + + +#define FUZZ_PROJECT_NAME "cloudsyncservicestub_fuzzer_two" + + +#endif \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_two/corpus/init b/test/fuzztest/cloudsyncservicestub_fuzzer_two/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e7c3fecd8d4d4816e40088113a2316bb9eb2e13f --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_two/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/cloudsyncservicestub_fuzzer_two/project.xml b/test/fuzztest/cloudsyncservicestub_fuzzer_two/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..7133b2b92440904a5ed04b838733acea0f97486a --- /dev/null +++ b/test/fuzztest/cloudsyncservicestub_fuzzer_two/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + +