• 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 "dcameraisstreamssupported_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_STREAM_TYPE_SIZE = 3;
39 const StreamSupportType dcStreamType[DC_STREAM_TYPE_SIZE] = {
40     StreamSupportType::DYNAMIC_SUPPORTED, StreamSupportType::RE_CONFIGURED_REQUIRED,
41     StreamSupportType::NOT_SUPPORTED
42 };
43 const uint32_t DC_TUNNELED_MODE = 2;
44 const uint8_t MAX_STRING_LENGTH = 255;
45 }
DcameraIsStreamsSupportedFuzzTest(const uint8_t * data,size_t size)46 void DcameraIsStreamsSupportedFuzzTest(const uint8_t* data, size_t size)
47 {
48     if ((data == nullptr) || (size < sizeof(int32_t))) {
49         return;
50     }
51 
52     FuzzedDataProvider fdp(data, size);
53     OperationMode mode = NORMAL;
54     std::vector<uint8_t> modeSetting;
55     modeSetting.push_back(*data);
56     std::vector<StreamInfo> infos;
57     StreamInfo info;
58     info.streamId_ = fdp.ConsumeIntegral<int>();
59     info.width_ = fdp.ConsumeIntegral<int>();
60     info.height_ = fdp.ConsumeIntegral<int>();
61     info.format_ = fdp.ConsumeIntegral<int>();
62     info.dataspace_ = fdp.ConsumeIntegral<int>();
63     info.intent_ = streamIntentType[data[0] % DC_STREAMINTENT_SIZE];
64     info.tunneledMode_ = fdp.ConsumeIntegral<int>() % DC_TUNNELED_MODE;
65     info.bufferQueue_ = sptr<BufferProducerSequenceable>(new BufferProducerSequenceable());
66     info.encodeType_ = encodeType[data[0] % DC_ENCODE_SIZE];
67     infos.push_back(info);
68     StreamSupportType type = dcStreamType[data[0] % DC_STREAM_TYPE_SIZE];
69 
70     std::string sinkAbilityInfo(fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH));
71     std::shared_ptr<DMetadataProcessor> dMetadataProcessor = std::make_shared<DMetadataProcessor>();
72     dMetadataProcessor->InitDCameraAbility(sinkAbilityInfo);
73     OHOS::sptr<DStreamOperator> dCameraStreamOperator(new (std::nothrow) DStreamOperator(dMetadataProcessor));
74 
75     dCameraStreamOperator->IsStreamsSupported(mode, modeSetting, infos, type);
76 }
77 }
78 }
79 
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83     /* Run your code on data */
84     OHOS::DistributedHardware::DcameraIsStreamsSupportedFuzzTest(data, size);
85     return 0;
86 }
87 
88