1 /*
2 * Copyright (c) 2024 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 "hstream_repeat_fuzzer.h"
17 #include "foundation/multimedia/camera_framework/common/utils/camera_log.h"
18 #include "message_parcel.h"
19 #include "iservice_registry.h"
20 #include <cstddef>
21 #include <cstdint>
22 #include <memory>
23 #include "token_setproc.h"
24 #include "iconsumer_surface.h"
25 #include "nativetoken_kit.h"
26 #include "accesstoken_kit.h"
27 #include "camera_metadata_info.h"
28 #include "metadata_utils.h"
29 #include "camera_device.h"
30 #include "camera_manager.h"
31 #include "ipc_skeleton.h"
32 #include "securec.h"
33
34 namespace OHOS {
35 namespace CameraStandard {
36 using namespace OHOS::HDI::Camera::V1_0;
37 static constexpr int32_t MIN_SIZE_NUM = 256;
38 const std::u16string INTERFACE_TOKEN = u"OHOS.CameraStandard.IStreamRepeatCallback";
39 const size_t MAX_LENGTH = 64;
40 const int32_t ITEMCOUNT = 10;
41 const int32_t DATASIZE = 100;
42 const int32_t PHOTO_WIDTH = 1280;
43 const int32_t PHOTO_HEIGHT = 960;
44 const int32_t PHOTO_FORMAT = 2000;
45
46 std::shared_ptr<HStreamRepeat> HStreamRepeatFuzzer::fuzz_{nullptr};
47
HStreamRepeatFuzzTest1(FuzzedDataProvider & fdp)48 void HStreamRepeatFuzzer::HStreamRepeatFuzzTest1(FuzzedDataProvider &fdp)
49 {
50 std::shared_ptr<OHOS::Camera::CameraMetadata> cameraAbility;
51 sptr<OHOS::HDI::Camera::V1_0::IStreamOperator> streamOperator;
52 fuzz_->LinkInput(streamOperator, cameraAbility);
53 StreamInfo_V1_1 streamInfo;
54 fuzz_->SetVideoStreamInfo(streamInfo);
55 fuzz_->SetStreamInfo(streamInfo);
56 sptr<OHOS::IBufferProducer> metaProducer;
57 fuzz_->SetMetaProducer(metaProducer);
58 SketchStatus status = SketchStatus::STOPED;
59 fuzz_->UpdateSketchStatus(status);
60 std::shared_ptr<OHOS::Camera::CameraMetadata> settings;
61 settings = std::make_shared<OHOS::Camera::CameraMetadata>(ITEMCOUNT, DATASIZE);
62 fuzz_->StartSketchStream(settings);
63 camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_FRONT;
64 fuzz_->SetUsedAsPosition(cameraPosition);
65 fuzz_->Start(settings, fdp.ConsumeBool());
66 fuzz_->Start();
67 fuzz_->OnFrameStarted();
68 fuzz_->OnFrameEnded(fdp.ConsumeIntegral<int32_t>());
69 CaptureEndedInfoExt captureEndedInfo = {1, 100, true, "video123"};
70 fuzz_->OnDeferredVideoEnhancementInfo(captureEndedInfo);
71 fuzz_->OnFrameError(fdp.ConsumeIntegral<int32_t>());
72 fuzz_->OnSketchStatusChanged(status);
73 fuzz_->Stop();
74 fuzz_->Release();
75 fuzz_->ReleaseStream(fdp.ConsumeBool());
76 }
77
HStreamRepeatFuzzTest2(FuzzedDataProvider & fdp)78 void HStreamRepeatFuzzer::HStreamRepeatFuzzTest2(FuzzedDataProvider &fdp)
79 {
80 sptr<Surface> photoSurface = Surface::CreateSurfaceAsConsumer("hstreamrepeat");
81 CHECK_RETURN_ELOG(!photoSurface, "CreateSurfaceAsConsumer Error");
82 sptr<IBufferProducer> producer = photoSurface->GetProducer();
83 fuzz_->AddDeferredSurface(producer);
84 fuzz_->SetFrameRate(fdp.ConsumeIntegral<int32_t>(), fdp.ConsumeIntegral<int32_t>());
85 fuzz_->SetMirror(fdp.ConsumeBool());
86 fuzz_->SetMirrorForLivePhoto(fdp.ConsumeBool(), fdp.ConsumeIntegral<int32_t>());
87 uint8_t randomNum = fdp.ConsumeIntegral<uint8_t>();
88 std::vector<std::int32_t> test = {0, 90, 180, 270, 360};
89 std::int32_t rotation(test[randomNum % test.size()]);
90 fuzz_->SetCameraRotation(fdp.ConsumeBool(), rotation);
91 fuzz_->SetCameraApi(fdp.ConsumeIntegral<uint32_t>());
92 std::string deviceClass;
93 fuzz_->SetPreviewRotation(deviceClass);
94 fuzz_->UpdateSketchRatio(fdp.ConsumeBool());
95 fuzz_->GetSketchStream();
96 fuzz_->GetRepeatStreamType();
97 fuzz_->SyncTransformToSketch();
98 fuzz_->SetStreamTransform(fdp.ConsumeIntegral<int>());
99 camera_position_enum_t cameraPosition = OHOS_CAMERA_POSITION_FRONT;
100 int32_t sensorOrientation = ((fdp.ConsumeIntegral<int32_t>() % 360) + 360) % 360;
101 fuzz_->ProcessVerticalCameraPosition(sensorOrientation, cameraPosition);
102 int32_t streamRotation = fdp.ConsumeIntegral<int32_t>();
103 fuzz_->ProcessCameraPosition(streamRotation, cameraPosition);
104 fuzz_->ProcessFixedTransform(sensorOrientation, cameraPosition);
105 fuzz_->ProcessFixedDiffDeviceTransform(sensorOrientation, cameraPosition);
106 fuzz_->ProcessCameraSetRotation(sensorOrientation, cameraPosition);
107 camera_position_enum_t cameraPosition1 = OHOS_CAMERA_POSITION_BACK;
108 int32_t sensorOrientation1 = ((fdp.ConsumeIntegral<int32_t>() % 360) + 360) % 360;
109 fuzz_->ProcessVerticalCameraPosition(sensorOrientation1, cameraPosition1);
110 int32_t streamRotation1 = fdp.ConsumeIntegral<int32_t>();
111 fuzz_->ProcessCameraPosition(streamRotation1, cameraPosition1);
112 fuzz_->ProcessFixedTransform(sensorOrientation1, cameraPosition1);
113 fuzz_->ProcessFixedDiffDeviceTransform(sensorOrientation1, cameraPosition1);
114 fuzz_->ProcessCameraSetRotation(sensorOrientation1, cameraPosition1);
115 }
116
HStreamRepeatFuzzTest3(FuzzedDataProvider & fdp)117 void HStreamRepeatFuzzer::HStreamRepeatFuzzTest3(FuzzedDataProvider &fdp)
118 {
119 sptr<Surface> photoSurface = Surface::CreateSurfaceAsConsumer("hstreamrepeat");
120 CHECK_RETURN_ELOG(!photoSurface, "CreateSurfaceAsConsumer Error");
121 sptr<IBufferProducer> producer = photoSurface->GetProducer();
122 std::shared_ptr<OHOS::Camera::CameraMetadata> settings;
123 settings = std::make_shared<OHOS::Camera::CameraMetadata>(ITEMCOUNT, DATASIZE);
124 fuzz_->OperatePermissionCheck(fdp.ConsumeIntegral<int>());
125 fuzz_->OpenVideoDfxSwitch(settings);
126 fuzz_->EnableSecure(fdp.ConsumeBool());
127 fuzz_->UpdateVideoSettings(settings);
128 fuzz_->UpdateFrameRateSettings(settings);
129 std::shared_ptr<OHOS::Camera::CameraMetadata> dynamicSetting;
130 fuzz_->UpdateFrameMuteSettings(settings, dynamicSetting);
131 #ifdef NOTIFICATION_ENABLE
132 fuzz_->UpdateBeautySettings(settings);
133 fuzz_->CancelNotification();
134 fuzz_->IsNeedBeautyNotification();
135 #endif
136 sptr<IStreamCapture> photoOutput = nullptr;
137 fuzz_->AttachMetaSurface(producer, fdp.ConsumeIntegral<int32_t>());
138 std::shared_ptr<StreamRepeatCallbackStub> callback = std::make_shared<HStreamRepeatCallbackStubDemo>();
139 MessageParcel data;
140 MessageParcel reply;
141 MessageOption option;
142 data.WriteInterfaceToken(INTERFACE_TOKEN);
143 data.WriteInt32(fdp.ConsumeIntegral<int32_t>());
144 data.WriteInt32(fdp.ConsumeIntegral<int32_t>());
145 data.WriteInt32(fdp.ConsumeBool());
146 data.WriteString16(Str8ToStr16(fdp.ConsumeRandomLengthString(MAX_LENGTH)));
147 data.WriteUint32(fdp.ConsumeIntegral<uint32_t>());
148 callback->OnRemoteRequest(
149 static_cast<uint32_t>(IStreamRepeatCallbackIpcCode::COMMAND_ON_DEFERRED_VIDEO_ENHANCEMENT_INFO),
150 data,
151 reply,
152 option);
153 }
154
Test(uint8_t * data,size_t size)155 void Test(uint8_t *data, size_t size)
156 {
157 FuzzedDataProvider fdp(data, size);
158 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
159 return;
160 }
161 auto hstreamRepeat = std::make_unique<HStreamRepeatFuzzer>();
162 if (hstreamRepeat == nullptr) {
163 MEDIA_INFO_LOG("hstreamRepeat is null");
164 return;
165 }
166 HStreamRepeatFuzzer::fuzz_ =
167 std::make_shared<HStreamRepeat>(nullptr, PHOTO_FORMAT, PHOTO_WIDTH, PHOTO_HEIGHT, RepeatStreamType::PREVIEW);
168 CHECK_RETURN_ELOG(!HStreamRepeatFuzzer::fuzz_, "Create fuzz_ Error");
169 hstreamRepeat->HStreamRepeatFuzzTest1(fdp);
170 hstreamRepeat->HStreamRepeatFuzzTest2(fdp);
171 hstreamRepeat->HStreamRepeatFuzzTest3(fdp);
172 }
173
174 } // namespace CameraStandard
175 } // namespace OHOS
176
177 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)178 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
179 {
180 OHOS::CameraStandard::Test(data, size);
181 return 0;
182 }