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 
16 #include <iostream>
17 #include "aw_common.h"
18 #include "string_ex.h"
19 #include "media_errors.h"
20 #include "directory_ex.h"
21 #include "window_option.h"
22 #include "avmetadatafile_fuzzer.h"
23 
24 using namespace std;
25 using namespace OHOS;
26 using namespace Media;
27 using namespace PlayerTestParam;
28 
29 namespace OHOS {
30 namespace Media {
AVMetadataFileFuzzer()31 AVMetadataFileFuzzer::AVMetadataFileFuzzer()
32 {
33 }
34 
~AVMetadataFileFuzzer()35 AVMetadataFileFuzzer::~AVMetadataFileFuzzer()
36 {
37 }
38 
FuzzAVMetadataFile(uint8_t * data,size_t size)39 bool AVMetadataFileFuzzer::FuzzAVMetadataFile(uint8_t *data, size_t size)
40 {
41     std::shared_ptr<AVMetadataHelper> avmetadata = OHOS::Media::AVMetadataHelperFactory::CreateAVMetadataHelper();
42     if (avmetadata == nullptr) {
43         return false;
44     }
45 
46     const string path = "/data/test/media/fuzztest.mp4";
47 
48     int32_t retWritefile = WriteDataToFile(path, data, size);
49     if (retWritefile != 0) {
50         avmetadata->Release();
51         return false;
52     }
53 
54     int32_t retMetadatasetsource = MetaDataSetSource(path, avmetadata);
55     if (retMetadatasetsource != 0) {
56         avmetadata->Release();
57         return true;
58     }
59 
60     std::unordered_map<int32_t, std::string> retresolve = avmetadata->ResolveMetadata();
61     if (retresolve.empty()) {
62         avmetadata->Release();
63         return true;
64     }
65 
66     std::shared_ptr<AVSharedMemory> retFetchartpicture = avmetadata->FetchArtPicture();
67     if (retFetchartpicture == nullptr) {
68         avmetadata->Release();
69         return true;
70     }
71 
72     avmetadata->Release();
73     return true;
74 }
75 }
76 
FuzzTestavMetadataFile(uint8_t * data,size_t size)77 bool FuzzTestavMetadataFile(uint8_t *data, size_t size)
78 {
79     if (data == nullptr) {
80         return 0;
81     }
82     AVMetadataFileFuzzer metadata;
83     return metadata.FuzzAVMetadataFile(data, size);
84 }
85 }
86 
87 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)88 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
89 {
90     /* Run your code on data */
91     OHOS::FuzzTestavMetadataFile(data, size);
92     return 0;
93 }