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 "time_broker_fuzzer.h"
17 #include "camera_log.h"
18 #include "message_parcel.h"
19 #include "securec.h"
20 #include <memory>
21 #include "timer/camera_deferred_timer.h"
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 namespace OHOS {
25 namespace CameraStandard {
26 using namespace DeferredProcessing;
27 static constexpr int32_t MIN_SIZE_NUM = 10;
28 std::shared_ptr<TimeBroker> TimeBrokerFuzzer::fuzz_{nullptr};
29
TimeBrokerFuzzTest(FuzzedDataProvider & fdp)30 void TimeBrokerFuzzer::TimeBrokerFuzzTest(FuzzedDataProvider& fdp)
31 {
32 fuzz_ = TimeBroker::Create("camera_deferred_base");
33 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
34 fuzz_->Initialize();
35 uint32_t handle = fuzz_->GenerateHandle();
36 std::function<void(uint32_t handle)> timerCallback = fuzz_->GetExpiredFunc(handle);
37 fuzz_->GetNextHandle(handle);
38 uint32_t delayTimeMs = fdp.ConsumeIntegralInRange<uint32_t>(0, 10);
39 bool force = fdp.ConsumeBool();
40 fuzz_->RegisterCallback(delayTimeMs, timerCallback, handle);
41 fuzz_->RestartTimer(force);
42 fuzz_->TimerExpired();
43 fuzz_->DeregisterCallback(handle);
44 }
45
Test(uint8_t * data,size_t size)46 void Test(uint8_t* data, size_t size)
47 {
48 auto timeBroker = std::make_unique<TimeBrokerFuzzer>();
49 if (timeBroker == nullptr) {
50 MEDIA_INFO_LOG("TimeBroker is null");
51 return;
52 }
53 FuzzedDataProvider fdp(data, size);
54 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
55 return;
56 }
57 timeBroker->TimeBrokerFuzzTest(fdp);
58 }
59 } // namespace CameraStandard
60 } // namespace OHOS
61
62 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)63 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
64 {
65 OHOS::CameraStandard::Test(data, size);
66 return 0;
67 }