1 /*
2 * Copyright (c) 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 <cstddef>
17 #include <cstdint>
18 #include <memory>
19
20 #include "accesstoken_kit.h"
21 #include "camera_ability_fuzzer.h"
22 #include "camera_log.h"
23 #include "message_parcel.h"
24 #include "nativetoken_kit.h"
25 #include "securec.h"
26 #include "test_token.h"
27 #include "token_setproc.h"
28
29 namespace OHOS {
30 namespace CameraStandard {
31 static constexpr int32_t MIN_SIZE_NUM = 24;
32 const int32_t NUM_TWO = 2;
33 std::shared_ptr<CameraAbility> CameraAbilityFuzzer::fuzz_{nullptr};
34
35 /*
36 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
37 * tips: only support basic type
38 */
39
CameraAbilityFuzzTest(FuzzedDataProvider & fdp)40 void CameraAbilityFuzzer::CameraAbilityFuzzTest(FuzzedDataProvider& fdp)
41 {
42 CHECK_RETURN_ELOG(!TestToken().GetAllCameraPermission(), "GetPermission error");
43 fuzz_ = std::make_shared<CameraAbility>();
44 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
45 fuzz_->HasFlash();
46 {
47 int32_t getData = fdp.ConsumeIntegral<int32_t>();
48 FlashMode flashMode = static_cast<FlashMode>(
49 getData % (FlashMode::FLASH_MODE_ALWAYS_OPEN + NUM_TWO));
50 fuzz_->IsFlashModeSupported(flashMode);
51 }
52 fuzz_->GetSupportedFlashModes();
53 {
54 int32_t getData = fdp.ConsumeIntegral<int32_t>();
55 ExposureMode exposureMode = static_cast<ExposureMode>(
56 getData % (ExposureMode::EXPOSURE_MODE_CONTINUOUS_AUTO + NUM_TWO));
57 fuzz_->IsExposureModeSupported(exposureMode);
58 }
59 fuzz_->GetSupportedExposureModes();
60 fuzz_->GetExposureBiasRange();
61 fuzz_->GetSupportedFocusModes();
62 {
63 int32_t getData = fdp.ConsumeIntegral<int32_t>();
64 FocusMode focusMode = static_cast<FocusMode>(
65 getData % (FocusMode::FOCUS_MODE_LOCKED + NUM_TWO));
66 fuzz_->IsFocusModeSupported(focusMode);
67 }
68 fuzz_->GetZoomRatioRange();
69 fuzz_->GetSupportedBeautyTypes();
70 {
71 int32_t getData = fdp.ConsumeIntegral<int32_t>();
72 BeautyType beautyType = static_cast<BeautyType>(
73 getData % (BeautyType::SKIN_TONE + NUM_TWO));
74 fuzz_->GetSupportedBeautyRange(beautyType);
75 }
76 fuzz_->GetSupportedColorEffects();
77 fuzz_->GetSupportedColorSpaces();
78 fuzz_->IsMacroSupported();
79 fuzz_->GetSupportedPortraitEffects();
80 fuzz_->GetSupportedVirtualApertures();
81 fuzz_->GetSupportedPhysicalApertures();
82 fuzz_->GetSupportedStabilizationMode();
83 {
84 int32_t getData = fdp.ConsumeIntegral<int32_t>();
85 VideoStabilizationMode stabilizationMode = static_cast<VideoStabilizationMode>(
86 getData % (VideoStabilizationMode::AUTO + NUM_TWO));
87 fuzz_->IsVideoStabilizationModeSupported(stabilizationMode);
88 }
89 fuzz_->GetSupportedExposureRange();
90 {
91 int32_t getData = fdp.ConsumeIntegral<int32_t>();
92 SceneFeature feature = static_cast<SceneFeature>(
93 getData % (SceneFeature::FEATURE_ENUM_MAX + NUM_TWO));
94 fuzz_->IsFeatureSupported(feature);
95 }
96 fuzz_->IsLcdFlashSupported();
97 }
98
Test(uint8_t * data,size_t size)99 void Test(uint8_t* data, size_t size)
100 {
101 auto cameraAbility = std::make_unique<CameraAbilityFuzzer>();
102 if (cameraAbility == nullptr) {
103 MEDIA_INFO_LOG("cameraAbility is null");
104 return;
105 }
106 FuzzedDataProvider fdp(data, size);
107 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
108 return;
109 }
110 cameraAbility->CameraAbilityFuzzTest(fdp);
111 }
112 } // namespace CameraStandard
113 } // namespace OHOS
114
115 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)116 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
117 {
118 OHOS::CameraStandard::Test(data, size);
119 return 0;
120 }