• 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_open_file_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;
FuzzUri()37 static inline Uri FuzzUri()
38 {
39     return Uri(provider->ConsumeBytesAsString(NUM_BYTES));
40 }
41 
OpenFileFuzzer(MediaDataShareExtAbility & extension)42 static inline void OpenFileFuzzer(MediaDataShareExtAbility &extension)
43 {
44     extension.OpenFile(FuzzUri(), provider->ConsumeBytesAsString(NUM_BYTES));
45 }
46 
Init()47 static inline MediaDataShareExtAbility Init()
48 {
49     const std::unique_ptr<AbilityRuntime::Runtime> runtime;
50     return {(*runtime)};
51 }
52 } // namespace OHOS
53 
54 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)55 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
56 {
57     /* Run your code on data */
58     FuzzedDataProvider fdp(data, size);
59     OHOS::provider = &fdp;
60     if (data == nullptr) {
61         return 0;
62     }
63     auto extension = OHOS::Init();
64     OHOS::OpenFileFuzzer(extension);
65     return 0;
66 }