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 #include <fuzzer/FuzzedDataProvider.h>
21
22 #include "dstream_operator.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 EncodeType encodeType[DC_ENCODE_SIZE] = {
30 EncodeType::ENCODE_TYPE_NULL, EncodeType::ENCODE_TYPE_H264, EncodeType::ENCODE_TYPE_H265,
31 EncodeType::ENCODE_TYPE_JPEG
32 };
33 const uint32_t DC_STREAMINTENT_SIZE = 6;
34 const StreamIntent streamIntentType[DC_STREAMINTENT_SIZE] = {
35 StreamIntent::PREVIEW, StreamIntent::VIDEO, StreamIntent::STILL_CAPTURE, StreamIntent::POST_VIEW,
36 StreamIntent::ANALYZE, StreamIntent::CUSTOM
37 };
38 const uint32_t DC_TUNNELED_MODE = 2;
39 const uint8_t MAX_STRING_LENGTH = 255;
40 }
DcameraCreateStreamsFuzzTest(const uint8_t * data,size_t size)41 void DcameraCreateStreamsFuzzTest(const uint8_t* data, size_t size)
42 {
43 if ((data == nullptr) || (size < sizeof(int32_t))) {
44 return;
45 }
46
47 FuzzedDataProvider fdp(data, size);
48 std::vector<StreamInfo> infos;
49 StreamInfo info;
50 info.streamId_ = fdp.ConsumeIntegral<int>();
51 info.width_ = fdp.ConsumeIntegral<int>();
52 info.height_ = fdp.ConsumeIntegral<int>();
53 info.format_ = fdp.ConsumeIntegral<int>();
54 info.dataspace_ = fdp.ConsumeIntegral<int>();
55 info.intent_ = streamIntentType[data[0] % DC_STREAMINTENT_SIZE];
56 info.tunneledMode_ = fdp.ConsumeIntegral<int>() % DC_TUNNELED_MODE;
57 info.bufferQueue_ = sptr<BufferProducerSequenceable>(new BufferProducerSequenceable());
58 info.encodeType_ = encodeType[data[0] % DC_ENCODE_SIZE];
59 infos.push_back(info);
60
61 std::string sinkAbilityInfo(fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH));
62 std::shared_ptr<DMetadataProcessor> dMetadataProcessor = std::make_shared<DMetadataProcessor>();
63 dMetadataProcessor->InitDCameraAbility(sinkAbilityInfo);
64 OHOS::sptr<DStreamOperator> dCameraStreamOperator(new (std::nothrow) DStreamOperator(dMetadataProcessor));
65
66 dCameraStreamOperator->CreateStreams(infos);
67 }
68 }
69 }
70
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
73 {
74 /* Run your code on data */
75 OHOS::DistributedHardware::DcameraCreateStreamsFuzzTest(data, size);
76 return 0;
77 }
78
79