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 "dstreamcapture_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 const uint8_t MAX_STRING_LENGTH = 255;
28
DstreamCaptureFuzzTest(const uint8_t * data,size_t size)29 void DstreamCaptureFuzzTest(const uint8_t* data, size_t size)
30 {
31 if ((data == nullptr) || (size < sizeof(int32_t))) {
32 return;
33 }
34
35 FuzzedDataProvider fdp(data, size);
36 int32_t captureId = fdp.ConsumeIntegral<int>();
37 int32_t streamId = fdp.ConsumeIntegral<int>();
38 int32_t value = 2;
39 std::string captureSetting(fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH));
40 std::vector<int32_t> streamIds;
41 streamIds.push_back(streamId);
42
43 CaptureInfo info;
44 info.streamIds_.assign(streamIds.begin(), streamIds.end());
45 info.captureSetting_.assign(captureSetting.begin(), captureSetting.end());
46 info.enableShutterCallback_ = data[0] % value;
47 bool isStreaming = data[0] % value;
48 std::string sinkAbilityInfo(fdp.ConsumeRandomLengthString(MAX_STRING_LENGTH));
49 std::shared_ptr<DMetadataProcessor> dMetadataProcessor = std::make_shared<DMetadataProcessor>();
50 dMetadataProcessor->InitDCameraAbility(sinkAbilityInfo);
51 OHOS::sptr<DStreamOperator> dCameraStreamOperator(new (std::nothrow) DStreamOperator(dMetadataProcessor));
52
53 dCameraStreamOperator->Capture(captureId, info, isStreaming);
54 }
55 }
56 }
57
58 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)59 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
60 {
61 /* Run your code on data */
62 OHOS::DistributedHardware::DstreamCaptureFuzzTest(data, size);
63 return 0;
64 }
65
66