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 "dcameraisstreamssupported_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "dstream_operator.h"
22 #include "v1_1/dcamera_types.h"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
26 namespace {
27 const uint32_t DC_ENCODE_SIZE = 4;
28 const EncodeType encodeType[DC_ENCODE_SIZE] = {
29     EncodeType::ENCODE_TYPE_NULL, EncodeType::ENCODE_TYPE_H264, EncodeType::ENCODE_TYPE_H265,
30     EncodeType::ENCODE_TYPE_JPEG
31 };
32 const uint32_t DC_STREAMINTENT_SIZE = 6;
33 const StreamIntent streamIntentType[DC_STREAMINTENT_SIZE] = {
34     StreamIntent::PREVIEW, StreamIntent::VIDEO, StreamIntent::STILL_CAPTURE, StreamIntent::POST_VIEW,
35     StreamIntent::ANALYZE, StreamIntent::CUSTOM
36 };
37 const uint32_t DC_STREAM_TYPE_SIZE = 3;
38 const StreamSupportType dcStreamType[DC_STREAM_TYPE_SIZE] = {
39     StreamSupportType::DYNAMIC_SUPPORTED, StreamSupportType::RE_CONFIGURED_REQUIRED,
40     StreamSupportType::NOT_SUPPORTED
41 };
42 }
DcameraIsStreamsSupportedFuzzTest(const uint8_t * data,size_t size)43 void DcameraIsStreamsSupportedFuzzTest(const uint8_t* data, size_t size)
44 {
45     if ((data == nullptr) || (size < sizeof(int32_t))) {
46         return;
47     }
48 
49     OperationMode mode = NORMAL;
50     std::vector<uint8_t> modeSetting;
51     modeSetting.push_back(*data);
52     std::vector<StreamInfo> infos;
53     StreamInfo info;
54     info.streamId_ = *(reinterpret_cast<const int*>(data));
55     info.width_ = *(reinterpret_cast<const int*>(data));
56     info.height_ = *(reinterpret_cast<const int*>(data));
57     info.format_ = *(reinterpret_cast<const int*>(data));
58     info.dataspace_ = *(reinterpret_cast<const int*>(data));
59     info.intent_ = streamIntentType[data[0] % DC_STREAMINTENT_SIZE];
60     info.tunneledMode_ = *(reinterpret_cast<const int*>(data)) % 2;
61     info.bufferQueue_ = sptr<BufferProducerSequenceable>(new BufferProducerSequenceable());
62     info.encodeType_ = encodeType[data[0] % DC_ENCODE_SIZE];
63     infos.push_back(info);
64     StreamSupportType type = dcStreamType[data[0] % DC_STREAM_TYPE_SIZE];
65 
66     std::string sinkAbilityInfo(reinterpret_cast<const char*>(data), size);
67     std::shared_ptr<DMetadataProcessor> dMetadataProcessor = std::make_shared<DMetadataProcessor>();
68     dMetadataProcessor->InitDCameraAbility(sinkAbilityInfo);
69     OHOS::sptr<DStreamOperator> dCameraStreamOperator(new (std::nothrow) DStreamOperator(dMetadataProcessor));
70 
71     dCameraStreamOperator->IsStreamsSupported(mode, modeSetting, infos, type);
72 }
73 }
74 }
75 
76 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)77 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
78 {
79     /* Run your code on data */
80     OHOS::DistributedHardware::DcameraIsStreamsSupportedFuzzTest(data, size);
81     return 0;
82 }
83 
84