1 /*
2 * Copyright (c) 2025 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 <fuzzer/FuzzedDataProvider.h>
18 #include "demuxersetdatasourcewithprobsize_sample.h"
19
20 #define FUZZ_PROJECT_NAME "demuxer_fuzzer"
21 using namespace std;
22 using namespace OHOS::Media;
23 namespace OHOS {
24 const int64_t EXPECT_SIZE = 64;
25 const char* TEST_FILE_PATH = "/data/test/demuxersetdatasourcewithprobsizefuzztest.mp4";
26
CheckDataValidity(const uint8_t * data,size_t size)27 bool CheckDataValidity(const uint8_t *data, size_t size)
28 {
29 if (size < EXPECT_SIZE) {
30 return false;
31 }
32 int32_t fd = open(TEST_FILE_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
33 if (fd < 0) {
34 return false;
35 }
36 int len = write(fd, data, size);
37 if (len <= 0) {
38 close(fd);
39 return false;
40 }
41 close(fd);
42 return true;
43 }
44
DemuxerSetDataSourceWithProbSizeFuzzTest(const uint8_t * data,size_t size)45 void DemuxerSetDataSourceWithProbSizeFuzzTest(const uint8_t *data, size_t size)
46 {
47 if (!CheckDataValidity(data, size)) {
48 return;
49 }
50
51 FuzzedDataProvider provider(data, size);
52 std::string typeName = provider.ConsumeRandomLengthString();
53 int probSize = provider.ConsumeIntegral<int32_t>();
54 DemuxerPluginTest test;
55 test.Run(typeName, TEST_FILE_PATH, probSize);
56 (void)remove(TEST_FILE_PATH);
57 return;
58 }
59 }
60
61 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)62 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
63 {
64 /* Run your code on data */
65 OHOS::DemuxerSetDataSourceWithProbSizeFuzzTest(data, size);
66 return 0;
67 }