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 "dcameracreatestreams_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 }
DcameraCreateStreamsFuzzTest(const uint8_t * data,size_t size)38 void DcameraCreateStreamsFuzzTest(const uint8_t* data, size_t size)
39 {
40 if ((data == nullptr) || (size < sizeof(int32_t))) {
41 return;
42 }
43
44 std::vector<StreamInfo> infos;
45 StreamInfo info;
46 info.streamId_ = *(reinterpret_cast<const int*>(data));
47 info.width_ = *(reinterpret_cast<const int*>(data));
48 info.height_ = *(reinterpret_cast<const int*>(data));
49 info.format_ = *(reinterpret_cast<const int*>(data));
50 info.dataspace_ = *(reinterpret_cast<const int*>(data));
51 info.intent_ = streamIntentType[data[0] % DC_STREAMINTENT_SIZE];
52 info.tunneledMode_ = *(reinterpret_cast<const int*>(data)) % 2;
53 info.bufferQueue_ = sptr<BufferProducerSequenceable>(new BufferProducerSequenceable());
54 info.encodeType_ = encodeType[data[0] % DC_ENCODE_SIZE];
55 infos.push_back(info);
56
57 std::string sinkAbilityInfo(reinterpret_cast<const char*>(data), size);
58 std::shared_ptr<DMetadataProcessor> dMetadataProcessor = std::make_shared<DMetadataProcessor>();
59 dMetadataProcessor->InitDCameraAbility(sinkAbilityInfo);
60 OHOS::sptr<DStreamOperator> dCameraStreamOperator(new (std::nothrow) DStreamOperator(dMetadataProcessor));
61
62 dCameraStreamOperator->CreateStreams(infos);
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::DcameraCreateStreamsFuzzTest(data, size);
72 return 0;
73 }
74
75