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     if (saveSubscriptionTaskPtr != nullptr) {
59         saveSubscriptionTaskPtr->SetRunnableBundleNames(bundleNamesPtr);
60         saveSubscriptionTaskPtr->RunTask(userId);
61         saveSubscriptionTaskPtr->RunTaskForBundle(userId, bundleName);
62     }
63 
64     shared_ptr<CycleTask> optimizeStorageTaskPtr = make_shared<OptimizeStorageTask>(dataSyncManager);
65     if (optimizeStorageTaskPtr != nullptr) {
66         optimizeStorageTaskPtr->SetRunnableBundleNames(bundleNamesPtr);
67         optimizeStorageTaskPtr->RunTask(userId);
68         optimizeStorageTaskPtr->RunTaskForBundle(userId, bundleName);
69     }
70 
71     shared_ptr<CycleTask> reportStatisticsTaskPtr = make_shared<ReportStatisticsTask>(dataSyncManager);
72     if (reportStatisticsTaskPtr != nullptr) {
73         reportStatisticsTaskPtr->SetRunnableBundleNames(bundleNamesPtr);
74         reportStatisticsTaskPtr->RunTask(userId);
75         reportStatisticsTaskPtr->RunTaskForBundle(userId, bundleName);
76     }
77 
78     shared_ptr<CycleTask> periodicCheckTaskPtr = make_shared<PeriodicCheckTask>(dataSyncManager);
79     if (periodicCheckTaskPtr != nullptr) {
80         periodicCheckTaskPtr->SetRunnableBundleNames(bundleNamesPtr);
81         periodicCheckTaskPtr->RunTask(userId);
82         periodicCheckTaskPtr->RunTaskForBundle(userId, bundleName);
83         periodicCheckTaskPtr->GetTaskName();
84         periodicCheckTaskPtr->GetDataSyncManager();
85     }
86 }
87 } // namespace OHOS
88 
89 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)90 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
91 {
92     /* Run your code on data */
93     if (data == nullptr || (size <= OHOS::U32_AT_SIZE << OHOS::SPLITE_SIZE)) {
94         return 0;
95     }
96 
97     OHOS::CycleTaskDerivedFuzzTest(data, size);
98     return 0;
99 }
100