1 /*
2 * Copyright (c) 2022-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 "dcameraadddcameradevice_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20
21 #include "dcamera_host.h"
22 #include "v1_1/id_camera_provider_callback.h"
23
24 namespace OHOS {
25 namespace DistributedHardware {
26
27 class MockDCameraProviderCallbackImpl : public IDCameraProviderCallback {
28 public:
MockDCameraProviderCallbackImpl(const std::string & devId,const std::string & dhId)29 MockDCameraProviderCallbackImpl(const std::string& devId, const std::string& dhId) : devId_(devId), dhId_(dhId)
30 {
31 }
32 ~MockDCameraProviderCallbackImpl() = default;
33
OpenSession(const DHBase & dhBase)34 int32_t OpenSession(const DHBase& dhBase)
35 {
36 return DCamRetCode::SUCCESS;
37 }
CloseSession(const DHBase & dhBase)38 int32_t CloseSession(const DHBase& dhBase)
39 {
40 return DCamRetCode::SUCCESS;
41 }
ConfigureStreams(const DHBase & dhBase,const std::vector<DCStreamInfo> & streamInfos)42 int32_t ConfigureStreams(const DHBase& dhBase, const std::vector<DCStreamInfo>& streamInfos)
43 {
44 return DCamRetCode::SUCCESS;
45 }
ReleaseStreams(const DHBase & dhBase,const std::vector<int> & streamIds)46 int32_t ReleaseStreams(const DHBase& dhBase, const std::vector<int>& streamIds)
47 {
48 return DCamRetCode::SUCCESS;
49 }
StartCapture(const DHBase & dhBase,const std::vector<DCCaptureInfo> & captureInfos)50 int32_t StartCapture(const DHBase& dhBase, const std::vector<DCCaptureInfo>& captureInfos)
51 {
52 return DCamRetCode::SUCCESS;
53 }
StopCapture(const DHBase & dhBase,const std::vector<int> & streamIds)54 int32_t StopCapture(const DHBase& dhBase, const std::vector<int>& streamIds)
55 {
56 return DCamRetCode::SUCCESS;
57 }
UpdateSettings(const DHBase & dhBase,const std::vector<DCameraSettings> & settings)58 int32_t UpdateSettings(const DHBase& dhBase, const std::vector<DCameraSettings>& settings)
59 {
60 return DCamRetCode::SUCCESS;
61 }
NotifyEvent(const DHBase & dhBase,const DCameraHDFEvent & event)62 int32_t NotifyEvent(const DHBase& dhBase, const DCameraHDFEvent& event)
63 {
64 return DCamRetCode::SUCCESS;
65 }
66
67 private:
68
69 std::string devId_;
70 std::string dhId_;
71 };
72
DcameraAddDCameraDeviceFuzzTest(const uint8_t * data,size_t size)73 void DcameraAddDCameraDeviceFuzzTest(const uint8_t* data, size_t size)
74 {
75 if ((data == nullptr) || (size == 0)) {
76 return;
77 }
78 std::string deviceId = "1";
79 std::string dhId = "2";
80 std::string sinkAbilityInfo(reinterpret_cast<const char*>(data), size);
81 std::string srcAbilityInfo(reinterpret_cast<const char*>(data), size);
82 DHBase dhBase;
83 dhBase.deviceId_ = deviceId;
84 dhBase.dhId_ = dhId;
85
86 sptr<IDCameraProviderCallback> callback;
87 OHOS::sptr<DCameraDevice> dcameraDevice(new (std::nothrow) DCameraDevice(dhBase, sinkAbilityInfo, srcAbilityInfo));
88 if (dcameraDevice == nullptr) {
89 return;
90 }
91
92 std::string cameraId = dhBase.deviceId_ + "__" + dhBase.dhId_;
93 auto temp = DCameraHost::GetInstance();
94 temp->dCameraDeviceMap_[cameraId] = dcameraDevice;
95 temp->AddDCameraDevice(dhBase, sinkAbilityInfo, srcAbilityInfo, callback);
96 std::string sourceCodecInfo = "";
97 temp->AddDeviceParamCheck(dhBase, sinkAbilityInfo, sourceCodecInfo, callback);
98 sinkAbilityInfo = "";
99 temp->AddDeviceParamCheck(dhBase, sinkAbilityInfo, srcAbilityInfo, callback);
100 dhBase.deviceId_ = "";
101 temp->AddDeviceParamCheck(dhBase, sinkAbilityInfo, srcAbilityInfo, callback);
102 temp->dCameraDeviceMap_.clear();
103 temp->AddDCameraDevice(dhBase, sinkAbilityInfo, srcAbilityInfo, callback);
104 temp->AddDeviceParamCheck(dhBase, sinkAbilityInfo, sourceCodecInfo, callback);
105 temp->AddDeviceParamCheck(dhBase, sinkAbilityInfo, srcAbilityInfo, callback);
106 temp->AddDeviceParamCheck(dhBase, sinkAbilityInfo, srcAbilityInfo, callback);
107 }
108 }
109 }
110
111 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)112 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
113 {
114 /* Run your code on data */
115 OHOS::DistributedHardware::DcameraAddDCameraDeviceFuzzTest(data, size);
116 return 0;
117 }
118
119