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
16
17 #include "accesstoken_kit.h"
18 #include "access_token.h"
19 #include "camera_log.h"
20 #include "deferred_processingservice_event_monitor_fuzzer.h"
21 #include "metadata_utils.h"
22 #include "nativetoken_kit.h"
23 #include "hap_token_info.h"
24 #include "image_info.h"
25 #include "ipc_skeleton.h"
26 #include "token_setproc.h"
27 #include "camera_util.h"
28
29 #include <utility>
30
31 namespace OHOS {
32 namespace CameraStandard {
33 namespace DeferredProcessing {
34
35 static constexpr int32_t MIN_SIZE_NUM = 4;
36
37 std::shared_ptr<CameraStrategy> EventMonitorFuzzer::camerastrategyfuzz_ = nullptr;
38 std::shared_ptr<BatteryLevelStrategy> EventMonitorFuzzer::batterylevelstrategyfuzz_ = nullptr;
39 std::shared_ptr<BatteryStrategy> EventMonitorFuzzer::batterystrategyfuzz_ = nullptr;
40 std::shared_ptr<ChargingStrategy> EventMonitorFuzzer::chargingstrategyfuzz_ = nullptr;
41 std::shared_ptr<ScreenStrategy> EventMonitorFuzzer::screenstrategyfuzz_ = nullptr;
42 std::shared_ptr<ThermalStrategy> EventMonitorFuzzer::thermalstrategyfuzz_ = nullptr;
43 std::shared_ptr<EventStatusChangeCommand> EventMonitorFuzzer::eventstatuschangecommandfuzz_ = nullptr;
44 std::shared_ptr<EventsInfo> EventMonitorFuzzer::eventsinfofuzz_ = nullptr;
45 std::shared_ptr<EventsMonitor> EventMonitorFuzzer::eventsmonitorfuzz_ = nullptr;
46
EventMonitorCameraStrategyFuzzTest(FuzzedDataProvider & fdp)47 void EventMonitorFuzzer::EventMonitorCameraStrategyFuzzTest(FuzzedDataProvider& fdp)
48 {
49 camerastrategyfuzz_ = std::make_shared<CameraStrategy>();
50 OHOS::AAFwk::Want want;
51 static const std::string EVENT_CAMERA = "CAMERA_BEAUTY_NOTIFICATION";
52 want.SetAction(EVENT_CAMERA);
53 EventFwk::CommonEventData CommonEventData { want };
54 camerastrategyfuzz_->handleEvent(CommonEventData);
55
56 batterylevelstrategyfuzz_ = std::make_shared<BatteryLevelStrategy>();
57 batterylevelstrategyfuzz_->handleEvent(CommonEventData);
58
59 batterystrategyfuzz_ = std::make_shared<BatteryStrategy>();
60 batterystrategyfuzz_->handleEvent(CommonEventData);
61
62 chargingstrategyfuzz_ = std::make_shared<ChargingStrategy>();
63 chargingstrategyfuzz_->handleEvent(CommonEventData);
64
65 screenstrategyfuzz_ = std::make_shared<ScreenStrategy>();
66 screenstrategyfuzz_->handleEvent(CommonEventData);
67
68 thermalstrategyfuzz_ = std::make_shared<ThermalStrategy>();
69 thermalstrategyfuzz_->handleEvent(CommonEventData);
70
71 int32_t value = 1;
72 int32_t enumval = fdp.ConsumeIntegralInRange<int32_t>(1, 13);
73 EventType eventType = static_cast<EventType>(enumval);
74 int val = 0;
75 eventstatuschangecommandfuzz_ = std::make_shared<EventStatusChangeCommand>(eventType, val);
76 eventstatuschangecommandfuzz_->Executing();
77
78 eventsinfofuzz_ = std::make_shared<EventsInfo>();
79 eventsinfofuzz_->GetScreenState();
80 eventsinfofuzz_->GetBatteryState();
81 eventsinfofuzz_->GetChargingState();
82 eventsinfofuzz_->GetBatteryLevel();
83 eventsinfofuzz_->GetThermalLevel();
84 eventsinfofuzz_->GetCameraStatus();
85 enumval = fdp.ConsumeIntegralInRange<int32_t>(0, 3);
86 CameraSessionStatus camerasessionstatus = static_cast<CameraSessionStatus>(enumval);
87 eventsinfofuzz_->SetCameraState(camerasessionstatus);
88 eventsinfofuzz_->IsCameraOpen();
89
90 eventsmonitorfuzz_ = std::make_shared<EventsMonitor>();
91 eventsmonitorfuzz_->Initialize();
92 std::vector<EventType>events;
93 std::weak_ptr<IEventsListener>listener;
94 eventsmonitorfuzz_->RegisterEventsListener(events, listener);
95 int32_t level = fdp.ConsumeIntegral<int32_t>();
96 eventsmonitorfuzz_->NotifyThermalLevel(level);
97 enumval = fdp.ConsumeIntegralInRange<int32_t>(0, 3);
98 camerasessionstatus = static_cast<CameraSessionStatus>(enumval);
99 eventsmonitorfuzz_->NotifyCameraSessionStatus(camerasessionstatus);
100 bool available = fdp.ConsumeBool();
101 eventsmonitorfuzz_->NotifyMediaLibraryStatus(available);
102 int32_t status = fdp.ConsumeIntegral<int32_t>();
103 eventsmonitorfuzz_->NotifyImageEnhanceStatus(status);
104 status = fdp.ConsumeIntegral<int32_t>();
105 eventsmonitorfuzz_->NotifyVideoEnhanceStatus(status);
106 status = fdp.ConsumeIntegral<int32_t>();
107 eventsmonitorfuzz_->NotifyScreenStatus(status);
108 status = fdp.ConsumeIntegral<int32_t>();
109 eventsmonitorfuzz_->NotifyChargingStatus(status);
110 status = fdp.ConsumeIntegral<int32_t>();
111 eventsmonitorfuzz_->NotifyBatteryStatus(status);
112 status = fdp.ConsumeIntegral<int32_t>();
113 eventsmonitorfuzz_->NotifyBatteryLevel(status);
114
115 status = fdp.ConsumeIntegral<int32_t>();
116 eventsmonitorfuzz_->NotifyTrailingStatus(status);
117 enumval = fdp.ConsumeIntegralInRange<int32_t>(1, 13);
118 eventType = static_cast<EventType>(enumval);
119 value = fdp.ConsumeIntegral<int32_t>();
120 eventsmonitorfuzz_->NotifyObserversUnlocked(eventType, value);
121 value = fdp.ConsumeIntegral<int32_t>();
122 eventsmonitorfuzz_->NotifyEventToObervers(eventType, value);
123 eventsmonitorfuzz_->SubscribeSystemAbility();
124 eventsmonitorfuzz_->UnSubscribeSystemAbility();
125 }
126
Test(uint8_t * data,size_t size)127 void Test(uint8_t* data, size_t size)
128 {
129 auto eventMonitor = std::make_unique<EventMonitorFuzzer>();
130 if (eventMonitor == nullptr) {
131 MEDIA_INFO_LOG("eventMonitor is null");
132 return;
133 }
134 FuzzedDataProvider fdp(data, size);
135 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
136 return;
137 }
138 eventMonitor->EventMonitorCameraStrategyFuzzTest(fdp);
139 }
140 }
141 } // namespace CameraStandard
142 } // namespace OHOS
143
144 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)145 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
146 {
147 /* Run your code on data */
148 OHOS::CameraStandard::DeferredProcessing::Test(data, size);
149 return 0;
150 }