• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "dcamerastartcapture_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #include "dcamera_provider.h"
23 #include "v1_1/dcamera_types.h"
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 namespace {
28 const uint32_t DC_ENCODE_SIZE = 4;
29 const DCEncodeType encodeType[DC_ENCODE_SIZE] = {
30     DCEncodeType::ENCODE_TYPE_NULL, DCEncodeType::ENCODE_TYPE_H264, DCEncodeType::ENCODE_TYPE_H265,
31     DCEncodeType::ENCODE_TYPE_JPEG
32 };
33 const uint32_t DC_STREAM_SIZE = 2;
34 const DCStreamType streamType[DC_STREAM_SIZE] = {
35     DCStreamType::CONTINUOUS_FRAME, DCStreamType::SNAPSHOT_FRAME
36 };
37 const uint8_t MAX_STRING_LENGTH = 255;
38 }
DcameraStartCaptureFuzzTest(const uint8_t * data,size_t size)39 void DcameraStartCaptureFuzzTest(const uint8_t* data, size_t size)
40 {
41     if ((data == nullptr) || (size < sizeof(int32_t))) {
42         return;
43     }
44 
45     FuzzedDataProvider fdp(data, size);
46     std::string deviceId(fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH));
47     std::string dhId(fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH));
48     DHBase dhBase;
49     dhBase.deviceId_ = deviceId;
50     dhBase.dhId_ = dhId;
51     std::vector<DCCaptureInfo> captureInfos;
52     DCCaptureInfo captureInfo;
53     captureInfo.streamIds_.push_back(fdp.ConsumeIntegral<int32_t>());
54     captureInfo.width_ = fdp.ConsumeIntegral<int32_t>();
55     captureInfo.height_ = fdp.ConsumeIntegral<int32_t>();
56     captureInfo.stride_ = fdp.ConsumeIntegral<int32_t>();
57     captureInfo.format_ = fdp.ConsumeIntegral<int32_t>();
58     captureInfo.dataspace_ = fdp.ConsumeIntegral<int32_t>();
59     captureInfo.isCapture_ = fdp.ConsumeBool();
60     captureInfo.encodeType_ = encodeType[data[0] % DC_ENCODE_SIZE];
61     captureInfo.type_ = streamType[data[0] % DC_STREAM_SIZE];
62     captureInfos.push_back(captureInfo);
63 
64     DCameraProvider::GetInstance()->StartCapture(dhBase, captureInfos);
65 }
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::DistributedHardware::DcameraStartCaptureFuzzTest(data, size);
74     return 0;
75 }
76 
77