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 "image_type.h"
23 #include "avmetadatasetsource_fuzzer.h"
24 
25 using namespace std;
26 using namespace OHOS;
27 using namespace Media;
28 using namespace PlayerTestParam;
29 
30 namespace OHOS {
31 namespace Media {
AVMetadataSetSourceFuzzer()32 AVMetadataSetSourceFuzzer::AVMetadataSetSourceFuzzer()
33 {
34 }
35 
~AVMetadataSetSourceFuzzer()36 AVMetadataSetSourceFuzzer::~AVMetadataSetSourceFuzzer()
37 {
38 }
39 
FuzzAVMetadataSetSource(uint8_t * data,size_t size)40 bool AVMetadataSetSourceFuzzer::FuzzAVMetadataSetSource(uint8_t *data, size_t size)
41 {
42     constexpr int32_t USAGE_LIST = 2;
43     std::shared_ptr<AVMetadataHelper> avmetadata = AVMetadataHelperFactory::CreateAVMetadataHelper();
44     if (avmetadata == nullptr) {
45         return false;
46     }
47 
48     const string path = "/data/test/media/H264_AAC.mp4";
49     int32_t setsourcefd = open(path.c_str(), O_RDONLY);
50     if (setsourcefd < 0) {
51         (void)close(setsourcefd);
52         avmetadata->Release();
53         return false;
54     }
55 
56     struct stat64 buffer;
57     if (fstat64(setsourcefd, &buffer) != 0) {
58         (void)close(setsourcefd);
59         avmetadata->Release();
60         return false;
61     }
62     int64_t setsourcesize = static_cast<int64_t>(buffer.st_size);
63     AVMetadataUsage usage[USAGE_LIST] {AVMetadataUsage::AV_META_USAGE_META_ONLY,
64                                     AVMetadataUsage::AV_META_USAGE_PIXEL_MAP};
65     int32_t setsourceusage = usage[ProduceRandomNumberCrypt() % USAGE_LIST];
66     int32_t retSetsource = avmetadata->SetSource(setsourcefd,
67         *reinterpret_cast<int64_t *>(data), setsourcesize, setsourceusage);
68     if (retSetsource != 0) {
69         (void)close(setsourcefd);
70         avmetadata->Release();
71         return true;
72     }
73     (void)close(setsourcefd);
74     std::unordered_map<int32_t, std::string> retResolvenetadata = avmetadata->ResolveMetadata();
75 
76     if (retResolvenetadata.empty()) {
77         avmetadata->Release();
78         return true;
79     }
80 
81     avmetadata->Release();
82     return true;
83 }
84 }
85 
FuzzTestAVMetadataSetSource(uint8_t * data,size_t size)86 bool FuzzTestAVMetadataSetSource(uint8_t *data, size_t size)
87 {
88     if (data == nullptr) {
89         return 0;
90     }
91 
92     if (size < sizeof(int64_t)) {
93         return 0;
94     }
95     AVMetadataSetSourceFuzzer metadata;
96     return metadata.FuzzAVMetadataSetSource(data, size);
97 }
98 }
99 
100 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)101 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
102 {
103     /* Run your code on data */
104     OHOS::FuzzTestAVMetadataSetSource(data, size);
105     return 0;
106 }