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_insert_fuzzer.h"
16
17 #include <cstdint>
18 #include <string>
19
20 #include "data_ability_observer_interface.h"
21 #include "datashare_business_error.h"
22 #include "datashare_helper.h"
23 #include "datashare_predicates.h"
24 #include "datashare_values_bucket.h"
25 #include "media_datashare_ext_ability.h"
26 #include "media_datashare_stub_impl.h"
27 #include "media_log.h"
28 #include "runtime.h"
29
30 namespace OHOS {
31 using namespace std;
32 using namespace AbilityRuntime;
33 using namespace DataShare;
FuzzString(const uint8_t * data,size_t size)34 static inline string FuzzString(const uint8_t *data, size_t size)
35 {
36 return {reinterpret_cast<const char*>(data), size};
37 }
38
FuzzUri(const uint8_t * data,size_t size)39 static inline Uri FuzzUri(const uint8_t *data, size_t size)
40 {
41 return Uri(FuzzString(data, size));
42 }
43
FuzzDataShareValuesBucket()44 static inline DataShareValuesBucket FuzzDataShareValuesBucket()
45 {
46 return {};
47 }
48
InsertFuzzer(MediaDataShareExtAbility & extension,const uint8_t * data,size_t size)49 static inline void InsertFuzzer(MediaDataShareExtAbility &extension, const uint8_t* data, size_t size)
50 {
51 DataSharePredicates predicates;
52 DataShareValuesBucket values;
53 extension.Insert(FuzzUri(data, size), FuzzDataShareValuesBucket());
54 }
55
Init()56 static inline MediaDataShareExtAbility Init()
57 {
58 const std::unique_ptr<AbilityRuntime::Runtime> runtime;
59 return {(*runtime)};
60 }
61 } // namespace OHOS
62
63 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)64 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
65 {
66 /* Run your code on data */
67 auto extension = OHOS::Init();
68 OHOS::InsertFuzzer(extension, data, size);
69 return 0;
70 }