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 "clouddisknotify_fuzzer.h"
16
17 #include <cstddef>
18 #include <cstdint>
19 #include <vector>
20
21 #include "cloud_fuzzer_helper.h"
22 #include "clouddisk_notify.h"
23
24 namespace OHOS {
25 constexpr size_t U32_AT_SIZE = 4;
26 constexpr int SPLITE_SIZE = 5;
27 constexpr size_t DIVIDED_NUM = 2;
28 constexpr uint32_t OPS_TYE_SIZE = 11;
29
30 using namespace OHOS::FileManagement::CloudDisk;
31 using namespace std;
32
TryNotifyServiceFuzzTest(FuzzData & fuzzData,size_t size)33 void TryNotifyServiceFuzzTest(FuzzData &fuzzData, size_t size)
34 {
35 fuzzData.ResetData(size);
36 int len = static_cast<int>((size - U32_AT_SIZE)) / SPLITE_SIZE;
37 if (len <= 0) {
38 return;
39 }
40 NotifyParamService paramService;
41 paramService.notifyType = static_cast<NotifyType>(fuzzData.GetData<uint32_t>());
42 CacheNode cacheNode;
43 cacheNode.cloudId = fuzzData.GetStringFromData(len);
44 cacheNode.fileName = fuzzData.GetStringFromData(len);
45 cacheNode.isDir = (size % DIVIDED_NUM == 0) ? "file" : "directory";
46 cacheNode.parentCloudId = fuzzData.GetStringFromData(len);
47 paramService.node = cacheNode;
48 ParamServiceOther paramOthers;
49 paramOthers.userId = fuzzData.GetData<int32_t>();
50 paramOthers.bundleName = fuzzData.GetStringFromData(len);
51 NotifyData notifyData;
52 notifyData.uri = fuzzData.GetStringFromData(len);
53 notifyData.isDir = (size % DIVIDED_NUM == 0);
54 notifyData.type = paramService.notifyType;
55 paramOthers.notifyDataList.emplace_back(notifyData);
56 paramService.opsType = static_cast<NotifyOpsType>(fuzzData.GetData<uint32_t>() % OPS_TYE_SIZE);
57 CloudDiskNotify::GetInstance().TryNotifyService(paramService, paramOthers);
58 }
59
GetDeleteNotifyDataFuzzTest(FuzzData & fuzzData,size_t size)60 void GetDeleteNotifyDataFuzzTest(FuzzData &fuzzData, size_t size)
61 {
62 fuzzData.ResetData(size);
63 ParamServiceOther paramOthers;
64 paramOthers.userId = fuzzData.GetData<int32_t>();
65 vector<NativeRdb::ValueObject> deleteIds;
66 int len = static_cast<int>((size - U32_AT_SIZE) >> 1);
67 string cloudId = fuzzData.GetStringFromData(len);
68 deleteIds.emplace_back(cloudId);
69 paramOthers.bundleName = fuzzData.GetStringFromData(len);
70 vector<NotifyData> notifyDataList;
71 CloudDiskNotify::GetInstance().GetDeleteNotifyData(deleteIds, notifyDataList, paramOthers);
72 }
73
AddNotifyFuzzTest(FuzzData & fuzzData,size_t size)74 void AddNotifyFuzzTest(FuzzData &fuzzData, size_t size)
75 {
76 fuzzData.ResetData(size);
77 NotifyData notifyData;
78 notifyData.type = static_cast<NotifyType>(fuzzData.GetData<uint32_t>());
79 int len = static_cast<int>(size - U32_AT_SIZE);
80 notifyData.uri = fuzzData.GetStringFromData(len);
81 notifyData.isDir = (size % DIVIDED_NUM == 0);
82 CloudDiskNotify::GetInstance().AddNotify(notifyData);
83 }
84 } // namespace OHOS
85
86 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)87 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
88 {
89 /* Run your code on data */
90 if (data == nullptr || size <= (OHOS::U32_AT_SIZE << 1) + static_cast<size_t>(OHOS::SPLITE_SIZE)) {
91 return 0;
92 }
93
94 OHOS::FuzzData fuzzdata(data, size);
95 OHOS::TryNotifyServiceFuzzTest(fuzzdata, size);
96 OHOS::GetDeleteNotifyDataFuzzTest(fuzzdata, size);
97 OHOS::AddNotifyFuzzTest(fuzzdata, size);
98 return 0;
99 }
100