1 /*
2 * Copyright (c) 2023 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 "medialibrary_extension_update_fuzzer.h"
16
17 #include <cstdint>
18 #include <string>
19 #include <fuzzer/FuzzedDataProvider.h>
20
21 #include "data_ability_observer_interface.h"
22 #include "datashare_business_error.h"
23 #include "datashare_helper.h"
24 #include "datashare_predicates.h"
25 #include "datashare_values_bucket.h"
26 #include "media_datashare_ext_ability.h"
27 #include "media_datashare_stub_impl.h"
28 #include "media_log.h"
29 #include "runtime.h"
30
31 namespace OHOS {
32 using namespace std;
33 using namespace AbilityRuntime;
34 using namespace DataShare;
35 static const int32_t NUM_BYTES = 1;
36 FuzzedDataProvider *provider = nullptr;
FuzzVectorInt32()37 static inline vector<int32_t> FuzzVectorInt32()
38 {
39 return {provider->ConsumeIntegral<int32_t>()};
40 }
41
FuzzVectorInt64()42 static inline vector<int64_t> FuzzVectorInt64()
43 {
44 return {provider->ConsumeIntegral<int64_t>()};
45 }
46
FuzzVectorDouble()47 static inline vector<double> FuzzVectorDouble()
48 {
49 return {provider->ConsumeFloatingPoint<double>()};
50 }
51
FuzzVectorString()52 static inline vector<string> FuzzVectorString()
53 {
54 return {provider->ConsumeBytesAsString(NUM_BYTES)};
55 }
56
FuzzVectorSingleValueType()57 static inline vector<SingleValue::Type> FuzzVectorSingleValueType()
58 {
59 return {provider->ConsumeIntegral<int64_t>()};
60 }
61
FuzzVectorMultiValueType()62 static inline vector<MutliValue::Type> FuzzVectorMultiValueType()
63 {
64 return {
65 FuzzVectorInt32(),
66 FuzzVectorInt64(),
67 FuzzVectorString(),
68 FuzzVectorDouble()
69 };
70 }
71
FuzzOperationItem()72 static inline OperationItem FuzzOperationItem()
73 {
74 return {
75 .operation = provider->ConsumeIntegral<int32_t>(),
76 .singleParams = FuzzVectorSingleValueType(),
77 .multiParams = FuzzVectorMultiValueType(),
78 };
79 }
80
FuzzVectorOperationItem()81 static inline vector<OperationItem> FuzzVectorOperationItem()
82 {
83 return {FuzzOperationItem()};
84 }
85
FuzzUri()86 static inline Uri FuzzUri()
87 {
88 return Uri(provider->ConsumeBytesAsString(NUM_BYTES));
89 }
90
FuzzDataSharePredicates()91 static inline DataSharePredicates FuzzDataSharePredicates()
92 {
93 return DataSharePredicates(FuzzVectorOperationItem());
94 }
95
FuzzDataShareValuesBucket()96 static inline DataShareValuesBucket FuzzDataShareValuesBucket()
97 {
98 return {};
99 }
100
UpdateFuzzer(MediaDataShareExtAbility & extension)101 static inline void UpdateFuzzer(MediaDataShareExtAbility &extension)
102 {
103 DataSharePredicates predicates;
104 extension.Update(FuzzUri(), FuzzDataSharePredicates(), FuzzDataShareValuesBucket());
105 }
106
Init()107 static inline MediaDataShareExtAbility Init()
108 {
109 const std::unique_ptr<AbilityRuntime::Runtime> runtime;
110 return {(*runtime)};
111 }
112 } // namespace OHOS
113
114 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)115 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
116 {
117 /* Run your code on data */
118 FuzzedDataProvider fdp(data, size);
119 OHOS::provider = &fdp;
120 if (data == nullptr) {
121 return 0;
122 }
123 auto extension = OHOS::Init();
124 OHOS::UpdateFuzzer(extension);
125 return 0;
126 }