• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_delete_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 using namespace DataShare;
36 static const int32_t NUM_BYTES = 1;
37 FuzzedDataProvider *provider = nullptr;
FuzzVectorInt32()38 static inline vector<int32_t> FuzzVectorInt32()
39 {
40     return {provider->ConsumeIntegral<int32_t>()};
41 }
42 
FuzzVectorInt64()43 static inline vector<int64_t> FuzzVectorInt64()
44 {
45     return {provider->ConsumeIntegral<int64_t>()};
46 }
47 
FuzzVectorDouble()48 static inline vector<double> FuzzVectorDouble()
49 {
50     return {provider->ConsumeFloatingPoint<double>()};
51 }
52 
FuzzVectorString()53 static inline vector<string> FuzzVectorString()
54 {
55     return {provider->ConsumeBytesAsString(NUM_BYTES)};
56 }
57 
FuzzVectorSingleValueType()58 static inline vector<SingleValue::Type> FuzzVectorSingleValueType()
59 {
60     return {provider->ConsumeIntegral<int64_t>()};
61 }
62 
FuzzVectorMultiValueType()63 static inline vector<MutliValue::Type> FuzzVectorMultiValueType()
64 {
65     return {
66         FuzzVectorInt32(),
67         FuzzVectorInt64(),
68         FuzzVectorString(),
69         FuzzVectorDouble()
70     };
71 }
72 
FuzzOperationItem()73 static inline OperationItem FuzzOperationItem()
74 {
75     return {
76         .operation = provider->ConsumeIntegral<int32_t>(),
77         .singleParams = FuzzVectorSingleValueType(),
78         .multiParams = FuzzVectorMultiValueType(),
79     };
80 }
81 
FuzzVectorOperationItem()82 static inline vector<OperationItem> FuzzVectorOperationItem()
83 {
84     return {FuzzOperationItem()};
85 }
86 
FuzzUri()87 static inline Uri FuzzUri()
88 {
89     return Uri(provider->ConsumeBytesAsString(NUM_BYTES));
90 }
91 
FuzzDataSharePredicates()92 static inline DataSharePredicates FuzzDataSharePredicates()
93 {
94     return DataSharePredicates(FuzzVectorOperationItem());
95 }
96 
DeleteFuzzer(MediaDataShareExtAbility & extension)97 static inline void DeleteFuzzer(MediaDataShareExtAbility &extension)
98 {
99     DataSharePredicates predicates;
100     DataShareValuesBucket values;
101     extension.Delete(FuzzUri(), FuzzDataSharePredicates());
102 }
103 
Init()104 static inline MediaDataShareExtAbility Init()
105 {
106     const std::unique_ptr<AbilityRuntime::Runtime> runtime;
107     return {(*runtime)};
108 }
109 } // namespace OHOS
110 
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
113 {
114     /* Run your code on data */
115     FuzzedDataProvider fdp(data, size);
116     OHOS::provider = &fdp;
117     if (data == nullptr) {
118         return 0;
119     }
120     auto extension = OHOS::Init();
121     OHOS::DeleteFuzzer(extension);
122     return 0;
123 }