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 <fcntl.h>
17 #include <iostream>
18 #include "aw_common.h"
19 #include "string_ex.h"
20 #include "media_errors.h"
21 #include "directory_ex.h"
22 #include "window_option.h"
23 #include "image_type.h"
24 #include "avmetadatasetsource_fuzzer.h"
25
26 using namespace std;
27 using namespace OHOS;
28 using namespace Media;
29 using namespace PlayerTestParam;
30
31 namespace OHOS {
32 namespace Media {
AVMetadataSetSourceFuzzer()33 AVMetadataSetSourceFuzzer::AVMetadataSetSourceFuzzer()
34 {
35 }
36
~AVMetadataSetSourceFuzzer()37 AVMetadataSetSourceFuzzer::~AVMetadataSetSourceFuzzer()
38 {
39 }
40
FuzzAVMetadataSetSource(uint8_t * data,size_t size)41 bool AVMetadataSetSourceFuzzer::FuzzAVMetadataSetSource(uint8_t *data, size_t size)
42 {
43 constexpr int32_t USAGE_LIST = 2;
44 std::shared_ptr<AVMetadataHelper> avmetadata = AVMetadataHelperFactory::CreateAVMetadataHelper();
45 if (avmetadata == nullptr) {
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 reproducibleRandom = abs((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | (data[3]));
67 int32_t setsourceusage = usage[reproducibleRandom % USAGE_LIST];
68 int32_t retSetsource = avmetadata->SetSource(setsourcefd,
69 *reinterpret_cast<int64_t *>(data), setsourcesize, setsourceusage);
70 if (retSetsource != 0) {
71 (void)close(setsourcefd);
72 avmetadata->Release();
73 return true;
74 }
75 (void)close(setsourcefd);
76 std::unordered_map<int32_t, std::string> retResolvenetadata = avmetadata->ResolveMetadata();
77
78 if (retResolvenetadata.empty()) {
79 avmetadata->Release();
80 return true;
81 }
82
83 avmetadata->Release();
84 return true;
85 }
86 }
87
FuzzTestAVMetadataSetSource(uint8_t * data,size_t size)88 bool FuzzTestAVMetadataSetSource(uint8_t *data, size_t size)
89 {
90 if (data == nullptr) {
91 return 0;
92 }
93
94 if (size < sizeof(int64_t)) {
95 return 0;
96 }
97 AVMetadataSetSourceFuzzer metadata;
98 return metadata.FuzzAVMetadataSetSource(data, size);
99 }
100 }
101
102 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)103 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
104 {
105 /* Run your code on data */
106 OHOS::FuzzTestAVMetadataSetSource(data, size);
107 return 0;
108 }