• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "dcameragetdevicebydhbase_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #include "dcamera_host.h"
23 #include "dcamera_provider.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
DcameraGetDeviceByDHBaseFuzzTest(const uint8_t * data,size_t size)27 void DcameraGetDeviceByDHBaseFuzzTest(const uint8_t* data, size_t size)
28 {
29     if ((data == nullptr) || (size == 0)) {
30         return;
31     }
32     std::string deviceId = "1";
33     std::string dhId = "2";
34     DHBase dhBase;
35     dhBase.deviceId_ = deviceId;
36     dhBase.dhId_ = dhId;
37 
38     std::string cameraId = "1__2";
39     std::string sinkAbilityInfo(reinterpret_cast<const char*>(data), size);
40     std::string sourceAbilityInfo(reinterpret_cast<const char*>(data), size);
41     OHOS::sptr<DCameraDevice> dcameraDevice(new (std::nothrow) DCameraDevice(dhBase, sinkAbilityInfo,
42         sourceAbilityInfo));
43     if (dcameraDevice == nullptr) {
44         return;
45     }
46     DCameraHost::GetInstance()->dCameraDeviceMap_.emplace(cameraId, dcameraDevice);
47     DCameraHost::GetInstance()->GetDCameraDeviceByDHBase(dhBase);
48 }
49 
DCameraProviderGetCallbackBydhBaseFuzzTest(const uint8_t * data,size_t size)50 void DCameraProviderGetCallbackBydhBaseFuzzTest(const uint8_t* data, size_t size)
51 {
52     if ((data == nullptr) || (size == 0)) {
53         return;
54     }
55 
56     FuzzedDataProvider fdp(data, size);
57     std::string deviceId = fdp.ConsumeRandomLengthString(size);
58     std::string dhId = fdp.ConsumeRandomLengthString(size);
59 
60     DHBase dhBase;
61     dhBase.deviceId_ = deviceId;
62     dhBase.dhId_ = dhId;
63 
64     std::string sinkAbilityInfo = fdp.ConsumeRandomLengthString(size);
65     std::string sourceAbilityInfo = fdp.ConsumeRandomLengthString(size);
66     OHOS::sptr<DCameraDevice> dcameraDevice(new (std::nothrow) DCameraDevice(dhBase, sinkAbilityInfo,
67         sourceAbilityInfo));
68     if (dcameraDevice == nullptr) {
69         return;
70     }
71 
72     std::string cameraId = deviceId + "__" + dhId;
73     DCameraHost::GetInstance()->dCameraDeviceMap_.emplace(cameraId, dcameraDevice);
74 
75     OHOS::sptr<DCameraProvider> provider = DCameraProvider::GetInstance();
76     if (provider == nullptr) {
77         return;
78     }
79 
80     sptr<IDCameraProviderCallback> callback = provider->GetCallbackBydhBase(dhBase);
81     if (callback != nullptr) {
82         callback->OpenSession(dhBase);
83     }
84 }
85 }
86 }
87 
88 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)89 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
90 {
91     /* Run your code on data */
92     OHOS::DistributedHardware::DcameraGetDeviceByDHBaseFuzzTest(data, size);
93     return 0;
94 }
95 
96