1cb7eb8c9Sopenharmony_ci/* 2cb7eb8c9Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3cb7eb8c9Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4cb7eb8c9Sopenharmony_ci * you may not use this file except in compliance with the License. 5cb7eb8c9Sopenharmony_ci * You may obtain a copy of the License at 6cb7eb8c9Sopenharmony_ci * 7cb7eb8c9Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8cb7eb8c9Sopenharmony_ci * 9cb7eb8c9Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10cb7eb8c9Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11cb7eb8c9Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12cb7eb8c9Sopenharmony_ci * See the License for the specific language governing permissions and 13cb7eb8c9Sopenharmony_ci * limitations under the License. 14cb7eb8c9Sopenharmony_ci */ 15cb7eb8c9Sopenharmony_ci#include "cloudsyncmanager_fuzzer.h" 16cb7eb8c9Sopenharmony_ci 17cb7eb8c9Sopenharmony_ci#include <cstddef> 18cb7eb8c9Sopenharmony_ci#include <cstdint> 19cb7eb8c9Sopenharmony_ci#include <memory> 20cb7eb8c9Sopenharmony_ci 21cb7eb8c9Sopenharmony_ci#include "cloud_fuzzer_helper.h" 22cb7eb8c9Sopenharmony_ci#include "cloud_sync_manager.h" 23cb7eb8c9Sopenharmony_ci#include "cloud_sync_manager_impl.h" 24cb7eb8c9Sopenharmony_ci#include "cloud_sync_service_proxy.h" 25cb7eb8c9Sopenharmony_ci#include "i_cloud_download_callback.h" 26cb7eb8c9Sopenharmony_ci#include "i_cloud_sync_callback.h" 27cb7eb8c9Sopenharmony_ci 28cb7eb8c9Sopenharmony_cinamespace OHOS { 29cb7eb8c9Sopenharmony_ciconstexpr size_t U32_AT_SIZE = 4; 30cb7eb8c9Sopenharmony_ciconstexpr size_t BOOL_AT_SIZE = 1; 31cb7eb8c9Sopenharmony_ci 32cb7eb8c9Sopenharmony_ciusing namespace OHOS::FileManagement::CloudSync; 33cb7eb8c9Sopenharmony_ciusing namespace std; 34cb7eb8c9Sopenharmony_ciclass ICloudSyncCallbackTest : public IRemoteStub<ICloudSyncCallback> { 35cb7eb8c9Sopenharmony_cipublic: 36cb7eb8c9Sopenharmony_ci void OnSyncStateChanged(SyncType type, SyncPromptState state) override {} 37cb7eb8c9Sopenharmony_ci void OnSyncStateChanged(CloudSyncState state, ErrorType error) override {} 38cb7eb8c9Sopenharmony_ci}; 39cb7eb8c9Sopenharmony_ci 40cb7eb8c9Sopenharmony_ciclass ICloudDownloadCallbackTest : public IRemoteStub<ICloudDownloadCallback> { 41cb7eb8c9Sopenharmony_cipublic: 42cb7eb8c9Sopenharmony_ci void OnDownloadProcess(const DownloadProgressObj &progress) override {} 43cb7eb8c9Sopenharmony_ci}; 44cb7eb8c9Sopenharmony_ci 45cb7eb8c9Sopenharmony_civoid RegisterCallbackFuzzTest(FuzzData &fuzzData, size_t size) 46cb7eb8c9Sopenharmony_ci{ 47cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 48cb7eb8c9Sopenharmony_ci std::string bundleName = fuzzData.GetStringFromData(static_cast<int>(size)); 49cb7eb8c9Sopenharmony_ci auto cloudSyncCallback = make_shared<ICloudSyncCallbackTest>(); 50cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().RegisterCallback(cloudSyncCallback, bundleName); 51cb7eb8c9Sopenharmony_ci} 52cb7eb8c9Sopenharmony_ci 53cb7eb8c9Sopenharmony_civoid UnRegisterCallbackFuzzTest(FuzzData &fuzzData, size_t size) 54cb7eb8c9Sopenharmony_ci{ 55cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 56cb7eb8c9Sopenharmony_ci std::string bundleName = fuzzData.GetStringFromData(static_cast<int>(size)); 57cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().UnRegisterCallback(bundleName); 58cb7eb8c9Sopenharmony_ci} 59cb7eb8c9Sopenharmony_ci 60cb7eb8c9Sopenharmony_civoid StartSyncFuzzTest(FuzzData &fuzzData, size_t size) 61cb7eb8c9Sopenharmony_ci{ 62cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 63cb7eb8c9Sopenharmony_ci std::string bundleName = fuzzData.GetStringFromData(static_cast<int>(size)); 64cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().StartSync(bundleName); 65cb7eb8c9Sopenharmony_ci} 66cb7eb8c9Sopenharmony_ci 67cb7eb8c9Sopenharmony_civoid GetSyncTimeFuzzTest(FuzzData &fuzzData, size_t size) 68cb7eb8c9Sopenharmony_ci{ 69cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 70cb7eb8c9Sopenharmony_ci int64_t syncTime{0}; 71cb7eb8c9Sopenharmony_ci std::string bundleName = fuzzData.GetStringFromData(static_cast<int>(size)); 72cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().GetSyncTime(syncTime, bundleName); 73cb7eb8c9Sopenharmony_ci} 74cb7eb8c9Sopenharmony_ci 75cb7eb8c9Sopenharmony_civoid StartSyncCallbackFuzzTest(FuzzData &fuzzData, size_t size) 76cb7eb8c9Sopenharmony_ci{ 77cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 78cb7eb8c9Sopenharmony_ci bool forceFlag = fuzzData.GetData<bool>(); 79cb7eb8c9Sopenharmony_ci auto cloudSyncCallback = make_shared<ICloudSyncCallbackTest>(); 80cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().StartSync(forceFlag, cloudSyncCallback); 81cb7eb8c9Sopenharmony_ci} 82cb7eb8c9Sopenharmony_ci 83cb7eb8c9Sopenharmony_civoid TriggerSyncFuzzTest(FuzzData &fuzzData, size_t size) 84cb7eb8c9Sopenharmony_ci{ 85cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 86cb7eb8c9Sopenharmony_ci int32_t userId = 100; 87cb7eb8c9Sopenharmony_ci std::string bundleName = fuzzData.GetStringFromData(static_cast<int>(size)); 88cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().TriggerSync(bundleName, userId); 89cb7eb8c9Sopenharmony_ci 90cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 91cb7eb8c9Sopenharmony_ci userId = fuzzData.GetData<int32_t>(); 92cb7eb8c9Sopenharmony_ci bundleName = fuzzData.GetStringFromData(static_cast<int>(size - U32_AT_SIZE)); 93cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().TriggerSync(bundleName, userId); 94cb7eb8c9Sopenharmony_ci} 95cb7eb8c9Sopenharmony_ci 96cb7eb8c9Sopenharmony_civoid StopSyncFuzzTest(FuzzData &fuzzData, size_t size) 97cb7eb8c9Sopenharmony_ci{ 98cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 99cb7eb8c9Sopenharmony_ci std::string bundleName = fuzzData.GetStringFromData(static_cast<int>(size)); 100cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().StopSync(bundleName); 101cb7eb8c9Sopenharmony_ci} 102cb7eb8c9Sopenharmony_ci 103cb7eb8c9Sopenharmony_civoid ChangeAppSwitchFuzzTest(FuzzData &fuzzData, size_t size) 104cb7eb8c9Sopenharmony_ci{ 105cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 106cb7eb8c9Sopenharmony_ci bool status = fuzzData.GetData<bool>(); 107cb7eb8c9Sopenharmony_ci int len = static_cast<int>((size - BOOL_AT_SIZE) >> 1); 108cb7eb8c9Sopenharmony_ci std::string accoutId = fuzzData.GetStringFromData(len); 109cb7eb8c9Sopenharmony_ci string bundleName = fuzzData.GetStringFromData(static_cast<int>(len)); 110cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().ChangeAppSwitch(accoutId, bundleName, status); 111cb7eb8c9Sopenharmony_ci} 112cb7eb8c9Sopenharmony_ci 113cb7eb8c9Sopenharmony_civoid NotifyDataChangeFuzzTest(FuzzData &fuzzData, size_t size) 114cb7eb8c9Sopenharmony_ci{ 115cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 116cb7eb8c9Sopenharmony_ci int len = static_cast<int>(size >> 1); 117cb7eb8c9Sopenharmony_ci std::string accoutId = fuzzData.GetStringFromData(len); 118cb7eb8c9Sopenharmony_ci std::string bundleName = fuzzData.GetStringFromData(len); 119cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().NotifyDataChange(accoutId, bundleName); 120cb7eb8c9Sopenharmony_ci} 121cb7eb8c9Sopenharmony_ci 122cb7eb8c9Sopenharmony_civoid StartDownloadFileFuzzTest(FuzzData &fuzzData, size_t size) 123cb7eb8c9Sopenharmony_ci{ 124cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 125cb7eb8c9Sopenharmony_ci string uri = fuzzData.GetStringFromData(static_cast<int>(size)); 126cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().StartDownloadFile(uri); 127cb7eb8c9Sopenharmony_ci} 128cb7eb8c9Sopenharmony_ci 129cb7eb8c9Sopenharmony_civoid StartFileCacheFuzzTest(FuzzData &fuzzData, size_t size) 130cb7eb8c9Sopenharmony_ci{ 131cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 132cb7eb8c9Sopenharmony_ci string uri = fuzzData.GetStringFromData(static_cast<int>(size)); 133cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().StartFileCache(uri); 134cb7eb8c9Sopenharmony_ci} 135cb7eb8c9Sopenharmony_ci 136cb7eb8c9Sopenharmony_civoid StopDownloadFileFuzzTest(FuzzData &fuzzData, size_t size) 137cb7eb8c9Sopenharmony_ci{ 138cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 139cb7eb8c9Sopenharmony_ci bool needClean = fuzzData.GetData<bool>(); 140cb7eb8c9Sopenharmony_ci string uri = fuzzData.GetStringFromData(static_cast<int>(size - BOOL_AT_SIZE)); 141cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().StopDownloadFile(uri, needClean); 142cb7eb8c9Sopenharmony_ci} 143cb7eb8c9Sopenharmony_ci 144cb7eb8c9Sopenharmony_civoid RegisterDownloadFileCallbackFuzzTest(FuzzData &fuzzData, size_t size) 145cb7eb8c9Sopenharmony_ci{ 146cb7eb8c9Sopenharmony_ci auto downloadCallback = make_shared<ICloudDownloadCallbackTest>(); 147cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().RegisterDownloadFileCallback(downloadCallback); 148cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().UnregisterDownloadFileCallback(); 149cb7eb8c9Sopenharmony_ci} 150cb7eb8c9Sopenharmony_ci 151cb7eb8c9Sopenharmony_civoid EnableCloudFuzzTest(FuzzData &fuzzData, size_t size) 152cb7eb8c9Sopenharmony_ci{ 153cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 154cb7eb8c9Sopenharmony_ci int len = static_cast<int>(size >> 1); 155cb7eb8c9Sopenharmony_ci std::string accoutId = fuzzData.GetStringFromData(len); 156cb7eb8c9Sopenharmony_ci string itemStr = fuzzData.GetStringFromData(len - static_cast<int>(BOOL_AT_SIZE)); 157cb7eb8c9Sopenharmony_ci bool itemBool = fuzzData.GetData<bool>(); 158cb7eb8c9Sopenharmony_ci SwitchDataObj switchDataObj; 159cb7eb8c9Sopenharmony_ci switchDataObj.switchData.insert({itemStr, itemBool}); 160cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().EnableCloud(accoutId, switchDataObj); 161cb7eb8c9Sopenharmony_ci auto proxy = CloudSyncServiceProxy::GetInstance(); 162cb7eb8c9Sopenharmony_ci CloudSyncManagerImpl::GetInstance().SetDeathRecipient(proxy->AsObject()); 163cb7eb8c9Sopenharmony_ci} 164cb7eb8c9Sopenharmony_ci 165cb7eb8c9Sopenharmony_civoid DisableCloudFuzzTest(FuzzData &fuzzData, size_t size) 166cb7eb8c9Sopenharmony_ci{ 167cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 168cb7eb8c9Sopenharmony_ci std::string accoutId = fuzzData.GetStringFromData(static_cast<int>(size)); 169cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().DisableCloud(accoutId); 170cb7eb8c9Sopenharmony_ci} 171cb7eb8c9Sopenharmony_ci 172cb7eb8c9Sopenharmony_civoid CleanFuzzTest(FuzzData &fuzzData, size_t size) 173cb7eb8c9Sopenharmony_ci{ 174cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 175cb7eb8c9Sopenharmony_ci int len = static_cast<int>(size >> 1); 176cb7eb8c9Sopenharmony_ci std::string accoutId = fuzzData.GetStringFromData(len); 177cb7eb8c9Sopenharmony_ci int32_t itemInt = fuzzData.GetData<int32_t>(); 178cb7eb8c9Sopenharmony_ci auto remainSize = fuzzData.GetRemainSize(); 179cb7eb8c9Sopenharmony_ci string itemStr = fuzzData.GetStringFromData(static_cast<int>(remainSize)); 180cb7eb8c9Sopenharmony_ci CleanOptions cleanOptions; 181cb7eb8c9Sopenharmony_ci cleanOptions.appActionsData.insert({itemStr, itemInt}); 182cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().Clean(accoutId, cleanOptions); 183cb7eb8c9Sopenharmony_ci} 184cb7eb8c9Sopenharmony_ci 185cb7eb8c9Sopenharmony_civoid CleanCacheFuzzTest(FuzzData &fuzzData, size_t size) 186cb7eb8c9Sopenharmony_ci{ 187cb7eb8c9Sopenharmony_ci fuzzData.ResetData(size); 188cb7eb8c9Sopenharmony_ci string uri = fuzzData.GetStringFromData(static_cast<int>(size)); 189cb7eb8c9Sopenharmony_ci CloudSyncManager::GetInstance().CleanCache(uri); 190cb7eb8c9Sopenharmony_ci} 191cb7eb8c9Sopenharmony_ci} // namespace OHOS 192cb7eb8c9Sopenharmony_ci 193cb7eb8c9Sopenharmony_ci/* Fuzzer entry point */ 194cb7eb8c9Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) 195cb7eb8c9Sopenharmony_ci{ 196cb7eb8c9Sopenharmony_ci /* Run your code on data */ 197cb7eb8c9Sopenharmony_ci if (data == nullptr || size <= (OHOS::U32_AT_SIZE << 1)) { 198cb7eb8c9Sopenharmony_ci return 0; 199cb7eb8c9Sopenharmony_ci } 200cb7eb8c9Sopenharmony_ci 201cb7eb8c9Sopenharmony_ci OHOS::FuzzData fuzzData(data, size); 202cb7eb8c9Sopenharmony_ci OHOS::RegisterCallbackFuzzTest(fuzzData, size); 203cb7eb8c9Sopenharmony_ci OHOS::UnRegisterCallbackFuzzTest(fuzzData, size); 204cb7eb8c9Sopenharmony_ci OHOS::StartSyncFuzzTest(fuzzData, size); 205cb7eb8c9Sopenharmony_ci OHOS::GetSyncTimeFuzzTest(fuzzData, size); 206cb7eb8c9Sopenharmony_ci OHOS::StartSyncCallbackFuzzTest(fuzzData, size); 207cb7eb8c9Sopenharmony_ci OHOS::TriggerSyncFuzzTest(fuzzData, size); 208cb7eb8c9Sopenharmony_ci OHOS::StopSyncFuzzTest(fuzzData, size); 209cb7eb8c9Sopenharmony_ci OHOS::ChangeAppSwitchFuzzTest(fuzzData, size); 210cb7eb8c9Sopenharmony_ci OHOS::NotifyDataChangeFuzzTest(fuzzData, size); 211cb7eb8c9Sopenharmony_ci OHOS::StartDownloadFileFuzzTest(fuzzData, size); 212cb7eb8c9Sopenharmony_ci OHOS::StartFileCacheFuzzTest(fuzzData, size); 213cb7eb8c9Sopenharmony_ci OHOS::StopDownloadFileFuzzTest(fuzzData, size); 214cb7eb8c9Sopenharmony_ci OHOS::RegisterDownloadFileCallbackFuzzTest(fuzzData, size); 215cb7eb8c9Sopenharmony_ci OHOS::EnableCloudFuzzTest(fuzzData, size); 216cb7eb8c9Sopenharmony_ci OHOS::DisableCloudFuzzTest(fuzzData, size); 217cb7eb8c9Sopenharmony_ci OHOS::CleanFuzzTest(fuzzData, size); 218cb7eb8c9Sopenharmony_ci OHOS::CleanCacheFuzzTest(fuzzData, size); 219cb7eb8c9Sopenharmony_ci return 0; 220cb7eb8c9Sopenharmony_ci} 221