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 "video_strategy_center_fuzzer.h"
17 #include "camera_log.h"
18 #include "message_parcel.h"
19 #include "ipc_file_descriptor.h"
20 #include "securec.h"
21
22 namespace OHOS {
23 namespace CameraStandard {
24 using namespace DeferredProcessing;
25 std::shared_ptr<VideoStrategyCenter> VideoStrategyCenterFuzzer::fuzz_ { nullptr };
26 const size_t MAX_LENGTH_STRING = 64;
27 const size_t THRESHOLD = MAX_LENGTH_STRING + 24;
28
VideoStrategyCenterFuzzTest(FuzzedDataProvider & fdp)29 void VideoStrategyCenterFuzzer::VideoStrategyCenterFuzzTest(FuzzedDataProvider& fdp)
30 {
31 auto userId = fdp.ConsumeIntegral<int32_t>();
32 auto repository = std::make_shared<VideoJobRepository>(userId);
33 CHECK_RETURN_ELOG(!repository, "Create repository Error");
34 std::string videoId(fdp.ConsumeRandomLengthString(MAX_LENGTH_STRING));
35 repository->SetJobPending(videoId);
36 repository->SetJobRunning(videoId);
37 repository->SetJobCompleted(videoId);
38 repository->SetJobFailed(videoId);
39 repository->SetJobPause(videoId);
40 repository->SetJobError(videoId);
41
42 fuzz_ = std::make_shared<DeferredProcessing::VideoStrategyCenter>(repository);
43 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
44 fuzz_->GetWork();
45 fuzz_->GetJob();
46 fuzz_->GetExecutionMode();
47 {
48 auto value = fdp.ConsumeIntegral<int32_t>();
49 fuzz_->HandleCameraEvent(value);
50 }
51 {
52 auto value = fdp.ConsumeIntegral<int32_t>();
53 fuzz_->HandleMedialLibraryEvent(value);
54 }
55 {
56 auto value = fdp.ConsumeIntegral<int32_t>();
57 fuzz_->HandleScreenEvent(value);
58 }
59 {
60 auto value = fdp.ConsumeIntegral<int32_t>();
61 fuzz_->HandleChargingEvent(value);
62 }
63 {
64 auto value = fdp.ConsumeIntegral<int32_t>();
65 fuzz_->HandleBatteryEvent(value);
66 }
67 }
68
Test(uint8_t * data,size_t size)69 void Test(uint8_t* data, size_t size)
70 {
71 FuzzedDataProvider fdp(data, size);
72 auto VideoStrategyCenter = std::make_unique<VideoStrategyCenterFuzzer>();
73 if (VideoStrategyCenter == nullptr) {
74 MEDIA_INFO_LOG("VideoStrategyCenter is null");
75 return;
76 }
77 VideoStrategyCenter->VideoStrategyCenterFuzzTest(fdp);
78 }
79
80 } // namespace CameraStandard
81 } // namespace OHOS
82
83 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)84 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
85 {
86 if (size < OHOS::CameraStandard::THRESHOLD) {
87 return 0;
88 }
89
90 OHOS::CameraStandard::Test(data, size);
91 return 0;
92 }