1 /*
2 * Copyright (c) 2022 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 "bundlestats_fuzzer.h"
16 #include "bundle_stats.h"
17 #include <cstddef>
18 #include <cstdint>
19
20 namespace OHOS {
21 constexpr size_t NUM_PARA = 3;
22 template<typename T>
TypeCast(const uint8_t * data,int * pos)23 T TypeCast(const uint8_t *data, int *pos)
24 {
25 if (pos) {
26 *pos += sizeof(T);
27 }
28 return *(reinterpret_cast<const T*>(data));
29 }
30
FileUtilFuzzTest(const uint8_t * data,size_t size)31 bool FileUtilFuzzTest(const uint8_t *data, size_t size)
32 {
33 if ((data == nullptr) || (size < sizeof(uint64_t) * NUM_PARA)) {
34 return true;
35 }
36
37 int64_t appSize = TypeCast<int64_t>(data, nullptr);
38 int64_t cacheSize = TypeCast<int64_t>(data + sizeof(int64_t), nullptr);
39 int64_t dataSize = TypeCast<int64_t>(data + sizeof(int64_t) * 2, nullptr);
40
41 Parcel parcel;
42 StorageManager::BundleStats bundlestats(appSize, cacheSize, dataSize);
43 bundlestats.Marshalling(parcel);
44 bundlestats.Unmarshalling(parcel);
45 return true;
46 }
47 } // namespace OHOS
48
49 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)50 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
51 {
52 /* Run your code on data */
53 OHOS::FileUtilFuzzTest(data, size);
54 return 0;
55 }
56