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 <cmath>
17 #include <cstdlib>
18 #include <iostream>
19 #include "aw_common.h"
20 #include "string_ex.h"
21 #include "media_log.h"
22 #include "media_errors.h"
23 #include "directory_ex.h"
24 #include "screencapturefileformatfile_ndk_fuzzer.h"
25 
26 using namespace std;
27 using namespace OHOS;
28 using namespace Media;
29 
30 namespace {
31 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SCREENCAPTURE,
32                                                "ScreenCaptureFileFormatFileNdkFuzzer"};
33 }
34 
35 namespace OHOS {
36 namespace Media {
ScreenCaptureFileFormatFileNdkFuzzer()37 ScreenCaptureFileFormatFileNdkFuzzer::ScreenCaptureFileFormatFileNdkFuzzer()
38 {
39 }
40 
~ScreenCaptureFileFormatFileNdkFuzzer()41 ScreenCaptureFileFormatFileNdkFuzzer::~ScreenCaptureFileFormatFileNdkFuzzer()
42 {
43 }
44 
SetConfig(OH_AVScreenCaptureConfig & config)45 void SetConfig(OH_AVScreenCaptureConfig &config)
46 {
47     OH_AudioCaptureInfo innerCapInfo = {
48         .audioSampleRate = 48000,
49         .audioChannels = 2,
50         .audioSource = OH_ALL_PLAYBACK
51     };
52 
53     OH_AudioEncInfo audioEncInfo = {
54         .audioBitrate = 48000,
55         .audioCodecformat = OH_AudioCodecFormat::OH_AAC_LC
56     };
57 
58     OH_VideoCaptureInfo videoCapInfo = {
59         .videoFrameWidth = 720,
60         .videoFrameHeight = 1080,
61         .videoSource = OH_VIDEO_SOURCE_SURFACE_RGBA
62     };
63 
64     OH_VideoEncInfo videoEncInfo = {
65         .videoCodec = OH_VideoCodecFormat::OH_MPEG4,
66         .videoBitrate = 2000000,
67         .videoFrameRate = 30
68     };
69 
70     OH_AudioInfo audioInfo = {
71         .innerCapInfo = innerCapInfo,
72         .audioEncInfo = audioEncInfo
73     };
74 
75     OH_VideoInfo videoInfo = {
76         .videoCapInfo = videoCapInfo,
77         .videoEncInfo = videoEncInfo
78     };
79 
80     config = {
81         .captureMode = OH_CAPTURE_HOME_SCREEN,
82         .dataType = OH_CAPTURE_FILE,
83         .audioInfo = audioInfo,
84         .videoInfo = videoInfo,
85     };
86 }
87 
FuzzScreenCaptureFileFormatFileNdk(uint8_t * data,size_t size)88 bool ScreenCaptureFileFormatFileNdkFuzzer::FuzzScreenCaptureFileFormatFileNdk(uint8_t *data, size_t size)
89 {
90     if (data == nullptr || size < sizeof(int32_t)) {
91         return false;
92     }
93     screenCapture = OH_AVScreenCapture_Create();
94 
95     OH_AVScreenCaptureConfig config;
96     SetConfig(config);
97     constexpr int32_t fileformatList = 2;
98     constexpr uint32_t recorderTime = 3;
99     const OH_ContainerFormatType fileformat[fileformatList] {
100         CFT_MPEG_4A,
101         CFT_MPEG_4
102     };
103     int32_t randomNum = abs((*reinterpret_cast<int32_t *>(data)) % (fileformatList));
104     MEDIA_LOGI("FuzzTest ScreenCaptureFileFormatFileNdkFuzzer randomNum: %{public}d ", randomNum);
105 
106     OH_RecorderInfo recorderInfo;
107     const std::string screenCaptureRoot = "/data/test/media/";
108     int32_t outputFd = open((screenCaptureRoot + "screen_capture_fuzz_ndk_fileformat_file_01.mp4").c_str(),
109         O_RDWR | O_CREAT, 0777);
110     std::string fileUrl = "fd://" + to_string(outputFd);
111     recorderInfo.url = const_cast<char *>(fileUrl.c_str());
112     recorderInfo.fileFormat = fileformat[randomNum];
113     config.recorderInfo = recorderInfo;
114 
115     OH_AVScreenCapture_Init(screenCapture, config);
116     OH_AVScreenCapture_StartScreenCapture(screenCapture);
117     sleep(recorderTime);
118     OH_AVScreenCapture_StopScreenCapture(screenCapture);
119     OH_AVScreenCapture_Release(screenCapture);
120     return true;
121 }
122 } // namespace Media
123 
FuzzTestScreenCaptureFileFormatFileNdk(uint8_t * data,size_t size)124 bool FuzzTestScreenCaptureFileFormatFileNdk(uint8_t *data, size_t size)
125 {
126     if (data == nullptr) {
127         return true;
128     }
129 
130     if (size < sizeof(int32_t)) {
131         return true;
132     }
133     ScreenCaptureFileFormatFileNdkFuzzer testScreenCapture;
134     return testScreenCapture.FuzzScreenCaptureFileFormatFileNdk(data, size);
135 }
136 } // namespace OHOS
137 
138 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)139 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
140 {
141     MEDIA_LOGI("FuzzTest ScreenCaptureFileFormatFileNdkFuzzer start");
142     MEDIA_LOGI("FuzzTest ScreenCaptureFileFormatFileNdkFuzzer data: %{public}d ", *data);
143     /* Run your code on data */
144     OHOS::FuzzTestScreenCaptureFileFormatFileNdk(data, size);
145     MEDIA_LOGI("FuzzTest ScreenCaptureFileFormatFileNdkFuzzer end");
146     return 0;
147 }