• 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 "dcameraconfigurestreams_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 }
DcameraConfigureStreamsFuzzTest(const uint8_t * data,size_t size)39 void DcameraConfigureStreamsFuzzTest(const uint8_t* data, size_t size)
40 {
41     if ((data == nullptr) || (size < sizeof(int))) {
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<DCStreamInfo> streamInfos;
52     DCStreamInfo streamInfo;
53     streamInfo.streamId_ = fdp.ConsumeIntegral<int>();
54     streamInfo.width_ = fdp.ConsumeIntegral<int>();
55     streamInfo.height_ = fdp.ConsumeIntegral<int>();
56     streamInfo.stride_ = fdp.ConsumeIntegral<int>();
57     streamInfo.format_ = fdp.ConsumeIntegral<int>();
58     streamInfo.dataspace_ = fdp.ConsumeIntegral<int>();
59     streamInfo.encodeType_ = encodeType[data[0] % DC_ENCODE_SIZE];
60     streamInfo.type_ = streamType[data[0] % DC_STREAM_SIZE];
61     streamInfos.push_back(streamInfo);
62     DCameraProvider::GetInstance()->ConfigureStreams(dhBase, streamInfos);
63 }
64 }
65 }
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70     /* Run your code on data */
71     OHOS::DistributedHardware::DcameraConfigureStreamsFuzzTest(data, size);
72     return 0;
73 }
74 
75