• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <unistd.h>
17 #include <fcntl.h>
18 #include "native_avsource.h"
19 #include "native_avformat.h"
20 #include "native_avcodec_base.h"
21 
22 #define FUZZ_PROJECT_NAME "demuxer_fuzzer"
23 using namespace std;
24 namespace OHOS {
25 const int64_t EXPECT_SIZE = 64;
26 const char* TEST_FILE_PATH = "/data/test/demuxergetcommentdatafuzztest.mp4";
27 
CheckDataValidity(const uint8_t * data,size_t size)28 bool CheckDataValidity(const uint8_t *data, size_t size)
29 {
30     if (size < EXPECT_SIZE) {
31         return false;
32     }
33     int32_t fd = open(TEST_FILE_PATH, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
34     if (fd < 0) {
35         return false;
36     }
37     int len = write(fd, data, size);
38     if (len <= 0) {
39         close(fd);
40         return false;
41     }
42     close(fd);
43     return true;
44 }
45 
DemuxerGetCommentDataFuzzTest(const uint8_t * data,size_t size)46 void DemuxerGetCommentDataFuzzTest(const uint8_t *data, size_t size)
47 {
48     if (!CheckDataValidity(data, size)) {
49         return;
50     }
51     const char* metaStringValue = nullptr;
52     auto source = OH_AVSource_CreateWithURI(const_cast<char*>(TEST_FILE_PATH));
53     if (source == nullptr) {
54         return;
55     }
56     auto metaFormat = OH_AVSource_GetSourceFormat(source);
57     if (metaFormat == nullptr) {
58         OH_AVSource_Destroy(source);
59         return;
60     }
61     OH_AVFormat_GetStringValue(metaFormat, OH_MD_KEY_COMMENT, &metaStringValue);
62     OH_AVSource_Destroy(source);
63     OH_AVFormat_Destroy(metaFormat);
64     (void)remove(TEST_FILE_PATH);
65     return;
66 }
67 }
68 
69 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
71 {
72     /* Run your code on data */
73     OHOS::DemuxerGetCommentDataFuzzTest(data, size);
74     return 0;
75 }