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
16 #include <cstddef>
17 #include <cstdint>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <sys/stat.h>
21 #include "securec.h"
22 #include <fuzzer/FuzzedDataProvider.h>
23 #include <iostream>
24 #include <memory>
25 #include "demuxermp4auxl_sample.h"
26
27 #define FUZZ_PROJECT_NAME "demuxer_fuzzer"
28 using namespace std;
29 using namespace OHOS::Media;
30 namespace OHOS {
31 const char *FILE_PATH = "/data/test/fuzz_create.mp4";
32 const int64_t EXPECT_SIZE = 36;
33
CheckDataValidity(const uint8_t * data,size_t size)34 bool CheckDataValidity(const uint8_t *data, size_t size)
35 {
36 if (size <= EXPECT_SIZE) {
37 return false;
38 }
39 int32_t fd = open(FILE_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
40 if (fd < 0) {
41 return false;
42 }
43 int len = write(fd, data, size);
44 if (len <= 0) {
45 close(fd);
46 return false;
47 }
48 close(fd);
49 return true;
50 }
DemuxerFuzzTest(const uint8_t * data,size_t size)51 bool DemuxerFuzzTest(const uint8_t *data, size_t size)
52 {
53 if (!CheckDataValidity(data, size)) {
54 return false;
55 }
56 FuzzedDataProvider fdp(data, size);
57 int64_t time = fdp.ConsumeIntegral<int64_t>();
58 uint32_t createSize = fdp.ConsumeIntegral<uint32_t>();
59 shared_ptr<DemuxerMp4AuxlSample> demuxerSample = make_shared<DemuxerMp4AuxlSample>();
60 demuxerSample->filePath = FILE_PATH;
61 demuxerSample->RunNormalDemuxer(createSize, time);
62 int ret = remove(FILE_PATH);
63 if (ret != 0) {
64 return false;
65 }
66 return true;
67 }
68 } // namespace OHOS
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
72 {
73 /* Run your code on data */
74 OHOS::DemuxerFuzzTest(data, size);
75 return 0;
76 }
77