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