/* * Copyright (c) 2023 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include "accesstoken_kit.h" #include "camera_input.h" #include "camera_input_fuzzer.h" #include "camera_log.h" #include "input/camera_device.h" #include "input/camera_manager.h" #include "message_parcel.h" #include "nativetoken_kit.h" #include "test_token.h" #include "token_setproc.h" namespace OHOS { namespace CameraStandard { namespace CameraInputFuzzer { const int32_t LIMITSIZE = 169; const int32_t CAM_NUM = 2; bool g_isCameraDevicePermission = false; static pid_t g_pid = 0; size_t max_length = 64; void Test(uint8_t* data, size_t size) { FuzzedDataProvider fdp(data, size); if (fdp.remaining_bytes() < LIMITSIZE) { return; } CHECK_RETURN_ELOG(!TestToken().GetAllCameraPermission(), "GetPermission error"); auto manager = CameraManager::GetInstance(); CHECK_RETURN_ELOG(!manager, "CameraInputFuzzer: Get CameraManager instance Error"); auto cameras = manager->GetSupportedCameras(); CHECK_RETURN_ELOG(cameras.size() < CAM_NUM, "CameraInputFuzzer: GetSupportedCameras Error"); auto camera = cameras[fdp.ConsumeIntegral() % cameras.size()]; CHECK_RETURN_ELOG(!camera, "CameraInputFuzzer: Camera is null Error"); auto input = manager->CreateCameraInput(camera); CHECK_RETURN_ELOG(!input, "CameraInputFuzzer: CreateCameraInput Error"); std::shared_ptr cameraOcclusionDetectCallback = std::make_shared(); input->SetOcclusionDetectCallback(cameraOcclusionDetectCallback); std::shared_ptr cameraDeviceServiceCallback = std::make_shared(input); uint64_t timestamp = 10; std::shared_ptr metadata = nullptr; const int32_t defaultItems = 10; const int32_t defaultDataLength = 100; int32_t count = 1; int32_t isOcclusionDetected = 1; int32_t isLensDirtyDetected = 1; metadata = std::make_shared(defaultItems, defaultDataLength); metadata->addEntry(OHOS_STATUS_CAMERA_OCCLUSION_DETECTION, &isOcclusionDetected, count); cameraDeviceServiceCallback->OnResult(timestamp, metadata); metadata = std::make_shared(defaultItems, defaultDataLength); metadata->addEntry(OHOS_STATUS_CAMERA_LENS_DIRTY_DETECTION, &isLensDirtyDetected, count); cameraDeviceServiceCallback->OnResult(timestamp, metadata); metadata = std::make_shared(defaultItems, defaultDataLength); metadata->addEntry(OHOS_STATUS_CAMERA_OCCLUSION_DETECTION, &isOcclusionDetected, count); metadata->addEntry(OHOS_STATUS_CAMERA_LENS_DIRTY_DETECTION, &isLensDirtyDetected, count); cameraDeviceServiceCallback->OnResult(timestamp, metadata); TestInput(input, fdp); } void TestInput(sptr input, FuzzedDataProvider& fdp) { MEDIA_INFO_LOG("CameraInputFuzzer: ENTER"); input->Open(); input->SetErrorCallback(make_shared()); input->SetResultCallback(make_shared()); input->GetCameraId(); input->GetCameraDevice(); input->GetErrorCallback(); input->GetResultCallback(); shared_ptr result; input->ProcessCallbackUpdates(fdp.ConsumeIntegral(), result); input->GetCameraSettings(); input->SetCameraSettings(fdp.ConsumeRandomLengthString(max_length)); input->GetMetaSetting(fdp.ConsumeIntegral()); std::vector infos; input->GetCameraAllVendorTags(infos); input->Release(); input->Close(); uint64_t secureSeqId; input->Open(fdp.ConsumeBool(), &secureSeqId); input->Release(); CameraDeviceServiceCallback callback; auto meta = make_shared(10, 100); callback.OnError(fdp.ConsumeIntegral(), fdp.ConsumeIntegral()); callback.OnResult(fdp.ConsumeIntegral(), meta); input->SetInputUsedAsPosition(CAMERA_POSITION_UNSPECIFIED); class CameraOcclusionDetectCallbackMock : public CameraOcclusionDetectCallback { public: void OnCameraOcclusionDetected(const uint8_t isCameraOcclusion, const uint8_t isCameraLensDirty) const override {} }; input->SetOcclusionDetectCallback(make_shared()); input->GetOcclusionDetectCallback(); input->UpdateSetting(meta); input->MergeMetadata(meta, meta); input->closeDelayed(fdp.ConsumeIntegral()); input->CameraServerDied(g_pid); } } // namespace StreamRepeatStubFuzzer } // namespace CameraStandard } // namespace OHOS /* Fuzzer entry point */ extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) { /* Run your code on data */ OHOS::CameraStandard::CameraInputFuzzer::Test(data, size); return 0; }