1 /*
2 * Copyright (c) 2025 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 #include <cstddef>
16 #include <cstdint>
17 #include <memory>
18
19 #include "camera_input.h"
20 #include "camera_log.h"
21 #include "camera_outputcapability_fuzzer.h"
22 #include "camera_photo_proxy.h"
23 #include "capture_input.h"
24 #include "capture_output.h"
25 #include "capture_scene_const.h"
26 #include "input/camera_manager.h"
27 #include "message_parcel.h"
28 #include "refbase.h"
29 #include "token_setproc.h"
30 #include "nativetoken_kit.h"
31 #include "accesstoken_kit.h"
32 #include "securec.h"
33
34 namespace OHOS {
35 namespace CameraStandard {
36 static constexpr int32_t MIN_SIZE_NUM = 4;
37
38 std::shared_ptr<CameraOutputCapability> CameraOutputCapabilityFuzzer::capabilityfuzz_{nullptr};
39 std::shared_ptr<Profile> CameraOutputCapabilityFuzzer::profilefuzz_{nullptr};
40 std::shared_ptr<VideoProfile> CameraOutputCapabilityFuzzer::videoProfilefuzz_{nullptr};
41
CameraOutputCapabilityFuzzTest(FuzzedDataProvider & fdp)42 void CameraOutputCapabilityFuzzer::CameraOutputCapabilityFuzzTest(FuzzedDataProvider& fdp)
43 {
44 int32_t value = fdp.ConsumeIntegralInRange<int32_t>(-2, 5);
45 ProfileSizeRatio sizeRatio = static_cast<ProfileSizeRatio>(value);
46 float unspecifiedValue = fdp.ConsumeFloatingPoint<float>();
47 GetTargetRatio(sizeRatio, unspecifiedValue);
48
49 value = fdp.ConsumeIntegral<int32_t>();
50 CameraFormat format = static_cast<CameraFormat>(value);
51 Size profileSize;
52 value = fdp.ConsumeIntegral<uint32_t>();
53 profileSize.width = value;
54 value = fdp.ConsumeIntegral<uint32_t>();
55 profileSize.height = value;
56 Fps fps;
57 value = fdp.ConsumeIntegral<uint32_t>();
58 fps.fixedFps = value;
59 value = fdp.ConsumeIntegral<uint32_t>();
60 fps.minFps = value;
61 value = fdp.ConsumeIntegral<uint32_t>();
62 fps.maxFps = value;
63 std::vector<uint32_t> abilityId;
64 value = fdp.ConsumeIntegral<uint32_t>();
65 abilityId.push_back(value);
66 value = fdp.ConsumeIntegral<uint32_t>();
67 abilityId.push_back(value);
68 int32_t specId = fdp.ConsumeIntegral<int32_t>();
69 profilefuzz_ = std::make_shared<Profile>(format, profileSize, fps, abilityId, specId);
70
71 profilefuzz_->GetFps();
72
73 std::vector<int32_t> framerates;
74 value = fdp.ConsumeIntegral<int32_t>();
75 framerates.push_back(value);
76 value = fdp.ConsumeIntegral<int32_t>();
77 framerates.push_back(value);
78 videoProfilefuzz_ = std::make_shared<VideoProfile>(format, profileSize, framerates);
79
80 VideoProfile videoProfile1(format, profileSize, framerates);
81 videoProfilefuzz_->IsContains(videoProfile1);
82 format = static_cast<CameraFormat>(value);
83 value = fdp.ConsumeIntegral<uint32_t>();
84 profileSize.width = value;
85 value = fdp.ConsumeIntegral<uint32_t>();
86 profileSize.height = value;
87 framerates.clear();
88 value = fdp.ConsumeIntegral<int32_t>();
89 framerates.push_back(value);
90 value = fdp.ConsumeIntegral<int32_t>();
91 framerates.push_back(value);
92 VideoProfile videoProfile2(format, profileSize, framerates);
93 videoProfilefuzz_->IsContains(videoProfile2);
94
95 capabilityfuzz_ = std::make_shared<CameraOutputCapability>();
96 std::vector<Profile> previewProfiles;
97 value = fdp.ConsumeIntegral<int32_t>();
98 format = static_cast<CameraFormat>(value);
99 Size profileSize1;
100 value = fdp.ConsumeIntegral<uint32_t>();
101 profileSize1.width = value;
102 value = fdp.ConsumeIntegral<uint32_t>();
103 profileSize1.height = value;
104 Profile profile1(format, profileSize1);
105 previewProfiles.push_back(profile1);
106 capabilityfuzz_->previewProfiles_.push_back(profile1);
107 capabilityfuzz_->IsMatchPreviewProfiles(previewProfiles);
108
109 capabilityfuzz_->photoProfiles_.push_back(profile1);
110 capabilityfuzz_->IsMatchPhotoProfiles(previewProfiles);
111
112 capabilityfuzz_->videoProfiles_.push_back(videoProfile1);
113 vector<VideoProfile>videoProfilevec = {videoProfile1};
114 capabilityfuzz_->IsMatchVideoProfiles(videoProfilevec);
115
116 capabilityfuzz_->RemoveDuplicatesProfiles();
117
118 capabilityfuzz_->GetSupportedMetadataObjectType();
119 }
120
Test(uint8_t * data,size_t size)121 void Test(uint8_t* data, size_t size)
122 {
123 auto cameraOutputCapability = std::make_unique<CameraOutputCapabilityFuzzer>();
124 if (cameraOutputCapability == nullptr) {
125 MEDIA_INFO_LOG("cameraOutputCapability is null");
126 return;
127 }
128 FuzzedDataProvider fdp(data, size);
129 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
130 return;
131 }
132 cameraOutputCapability->CameraOutputCapabilityFuzzTest(fdp);
133 }
134 } // namespace CameraStandard
135 } // namespace OHOS
136
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)137 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
138 {
139 OHOS::CameraStandard::Test(data, size);
140 return 0;
141 }