1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #include "cycletaskrunner_fuzzer.h"
16
17 #include <cstddef>
18 #include <cstdint>
19 #include <memory>
20 #include <new>
21 #include <set>
22 #include <string>
23
24 #include "cloud_file_kit.h"
25 #include "cloud_fuzzer_helper.h"
26 #include "cycle_task_runner.h"
27 #include "optimize_storage_task.h"
28 #include "periodic_check_task.h"
29 #include "report_statistics_task.h"
30 #include "save_subscription_task.h"
31
32 namespace OHOS {
33 constexpr size_t U32_AT_SIZE = 4;
34 constexpr int SPLITE_SIZE = 3;
35 using namespace std;
36 using namespace OHOS::FileManagement;
37 using namespace OHOS::FileManagement::CloudSync;
38
39 class CloudFileKitMock : public CloudFile::CloudFileKit {};
40
41 __attribute__((used)) static bool g_isInit =
42 CloudFile::CloudFileKit::RegisterCloudInstance(new (std::nothrow) CloudFileKitMock());
43
CycleTaskDerivedFuzzTest(const uint8_t * data,size_t size)44 void CycleTaskDerivedFuzzTest(const uint8_t *data, size_t size)
45 {
46 FuzzData fuzzData(data, size);
47 auto dataSyncManager = make_shared<CloudFile::DataSyncManager>();
48 CycleTaskRunner cyclerunner(dataSyncManager);
49 cyclerunner.StartTask();
50
51 int32_t userId = fuzzData.GetData<int32_t>();
52 auto remainSize = fuzzData.GetRemainSize();
53 std::string bundleName = fuzzData.GetStringFromData(static_cast<int>(remainSize));
54 std::set<std::string> bundleNames;
55 bundleNames.insert(bundleName);
56 auto bundleNamesPtr = make_shared<std::set<std::string>>(bundleNames);
57 shared_ptr<CycleTask> saveSubscriptionTaskPtr = make_shared<SaveSubscriptionTask>(dataSyncManager);
58 saveSubscriptionTaskPtr->SetRunnableBundleNames(bundleNamesPtr);
59 saveSubscriptionTaskPtr->RunTask(userId);
60 saveSubscriptionTaskPtr->RunTaskForBundle(userId, bundleName);
61
62 shared_ptr<CycleTask> optimizeStorageTaskPtr = make_shared<OptimizeStorageTask>(dataSyncManager);
63 optimizeStorageTaskPtr->SetRunnableBundleNames(bundleNamesPtr);
64 optimizeStorageTaskPtr->RunTask(userId);
65 optimizeStorageTaskPtr->RunTaskForBundle(userId, bundleName);
66
67 shared_ptr<CycleTask> reportStatisticsTaskPtr = make_shared<ReportStatisticsTask>(dataSyncManager);
68 reportStatisticsTaskPtr->SetRunnableBundleNames(bundleNamesPtr);
69 reportStatisticsTaskPtr->RunTask(userId);
70 reportStatisticsTaskPtr->RunTaskForBundle(userId, bundleName);
71
72 shared_ptr<CycleTask> periodicCheckTaskPtr = make_shared<PeriodicCheckTask>(dataSyncManager);
73 periodicCheckTaskPtr->SetRunnableBundleNames(bundleNamesPtr);
74 periodicCheckTaskPtr->RunTask(userId);
75 periodicCheckTaskPtr->RunTaskForBundle(userId, bundleName);
76 periodicCheckTaskPtr->GetTaskName();
77 periodicCheckTaskPtr->GetDataSyncManager();
78 }
79 } // namespace OHOS
80
81 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)82 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
83 {
84 /* Run your code on data */
85 if (data == nullptr || (size <= OHOS::U32_AT_SIZE << OHOS::SPLITE_SIZE)) {
86 return 0;
87 }
88
89 OHOS::CycleTaskDerivedFuzzTest(data, size);
90 return 0;
91 }
92