• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cloud_enhance_session_fuzzer.h"
17 #include "camera_input.h"
18 #include "camera_log.h"
19 #include "camera_photo_proxy.h"
20 #include "capture_input.h"
21 #include "capture_output.h"
22 #include "capture_scene_const.h"
23 #include "input/camera_manager.h"
24 #include "message_parcel.h"
25 #include "refbase.h"
26 #include <cstddef>
27 #include <cstdint>
28 #include <memory>
29 #include "token_setproc.h"
30 #include "nativetoken_kit.h"
31 #include "accesstoken_kit.h"
32 #include "picture.h"
33 #include "test_token.h"
34 
35 namespace OHOS {
36 namespace CameraStandard {
37 namespace CloudEnhanceSessionFuzzer {
38 const int32_t LIMITSIZE = 4;
39 const int32_t NUM_TWO = 2;
40 sptr<IBufferProducer> surface;
41 sptr<CameraDevice> camera;
42 Profile profile;
43 CaptureOutput* curOutput;
44 bool g_isSupported;
45 bool g_isCameraDevicePermission = false;
46 SceneMode g_sceneMode;
47 
48 sptr<CameraManager> manager;
49 sptr<SurfaceBuffer> surfaceBuffer;
50 SceneMode sceneMode = SceneMode::TIMELAPSE_PHOTO;
51 
GetCameraInput(uint8_t * rawData,size_t size)52 sptr<CaptureInput> GetCameraInput(uint8_t *rawData, size_t size)
53 {
54     MEDIA_INFO_LOG("CloudEnhanceSessionFuzzer: ENTER");
55     auto manager = CameraManager::GetInstance();
56     auto cameras = manager->GetSupportedCameras();
57     CHECK_RETURN_RET_ELOG(cameras.size() < NUM_TWO,
58         nullptr, "CloudEnhanceSessionFuzzer: GetSupportedCameras Error");
59     MessageParcel data;
60     data.WriteRawData(rawData, size);
61     camera = cameras[data.ReadUint32() % cameras.size()];
62     CHECK_RETURN_RET_ELOG(!camera, nullptr, "CloudEnhanceSessionFuzzer: Camera is null Error");
63     return manager->CreateCameraInput(camera);
64 }
65 
GetCaptureOutput(uint8_t * rawData,size_t size)66 sptr<PhotoOutput> GetCaptureOutput(uint8_t *rawData, size_t size)
67 {
68     MEDIA_INFO_LOG("CloudEnhanceSessionFuzzer: ENTER");
69     auto manager = CameraManager::GetInstance();
70     CHECK_RETURN_RET_ELOG(!manager, nullptr, "CloudEnhanceSessionFuzzer: CameraManager::GetInstance Error");
71     MessageParcel data;
72     data.WriteRawData(rawData, size);
73     CHECK_RETURN_RET_ELOG(!camera, nullptr, "CloudEnhanceSessionFuzzer: Camera is null Error");
74     auto capability = manager->GetSupportedOutputCapability(camera, g_sceneMode);
75     CHECK_RETURN_RET_ELOG(!capability, nullptr, "CloudEnhanceSessionFuzzer: GetSupportedOutputCapability Error");
76     auto profiles = capability->GetPhotoProfiles();
77     CHECK_RETURN_RET_ELOG(profiles.empty(), nullptr, "CloudEnhanceSessionFuzzer: GetPhotoProfiles empty");
78     profile = profiles[data.ReadUint32() % profiles.size()];
79     sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
80     CHECK_RETURN_RET_ELOG(!photoSurface, nullptr, "CloudEnhanceSessionFuzzer: create photoSurface Error");
81     surface = photoSurface->GetProducer();
82     CHECK_RETURN_RET_ELOG(!surface, nullptr, "CloudEnhanceSessionFuzzer: surface GetProducer Error");
83     return manager->CreatePhotoOutput(profile, surface);
84 }
85 
TestSession(sptr<CaptureSession> session,uint8_t * rawData,size_t size)86 void TestSession(sptr<CaptureSession> session, uint8_t *rawData, size_t size)
87 {
88     MEDIA_INFO_LOG("CloudEnhanceSessionFuzzer: ENTER");
89     sptr<CaptureInput> input = GetCameraInput(rawData, size);
90     sptr<CaptureOutput> output = GetCaptureOutput(rawData, size);
91     CHECK_RETURN_ELOG(!input || !output || !session, "CloudEnhanceSessionFuzzer: input/output/session is null");
92     MessageParcel data;
93     data.WriteRawData(rawData, size);
94     session->SetMode(g_sceneMode);
95     session->GetMode();
96     PreconfigType preconfigType = static_cast<PreconfigType>(
97         data.ReadInt32() % (PreconfigType::PRECONFIG_HIGH_QUALITY + NUM_TWO));
98     ProfileSizeRatio preconfigRatio = static_cast<ProfileSizeRatio>(
99         data.ReadInt32() % (ProfileSizeRatio::RATIO_16_9 + NUM_TWO));
100     session->CanPreconfig(preconfigType, preconfigRatio);
101     session->Preconfig(preconfigType, preconfigRatio);
102     session->BeginConfig();
103     session->CanAddInput(input);
104     session->AddInput(input);
105     session->CanAddOutput(output);
106     session->AddOutput(output);
107     session->RemoveInput(input);
108     session->RemoveOutput(output);
109     session->AddInput(input);
110     session->AddOutput(output);
111     session->AddSecureOutput(output);
112     input->Open();
113     session->CommitConfig();
114     session->Start();
115     curOutput = output.GetRefPtr();
116     CaptureOutputType outputType = static_cast<CaptureOutputType>(
117         data.ReadInt32() % (CaptureOutputType::CAPTURE_OUTPUT_TYPE_MAX + NUM_TWO));
118     session->ValidateOutputProfile(profile, outputType);
119 }
120 
Test(uint8_t * rawData,size_t size)121 void Test(uint8_t *rawData, size_t size)
122 {
123     CHECK_RETURN(rawData == nullptr || size < LIMITSIZE);
124     CHECK_RETURN_ELOG(!TestToken().GetAllCameraPermission(), "GetPermission error");
125     MessageParcel data;
126     data.WriteRawData(rawData, size);
127     g_sceneMode = static_cast<SceneMode>(
128         data.ReadInt32() % (SceneMode::APERTURE_VIDEO + NUM_TWO));
129     auto manager = CameraManager::GetInstance();
130     auto session = manager->CreateCaptureSession(g_sceneMode);
131     CHECK_RETURN_ELOG(!manager, "CloudEnhanceSessionFuzzer: CreateCaptureSession Error");
132     TestSession(session, rawData, size);
133     session->EnableAutoCloudImageEnhancement(data.ReadBool());
134     session->Release();
135     session->Stop();
136 }
137 
138 } // namespace StreamRepeatStubFuzzer
139 } // namespace CameraStandard
140 } // namespace OHOS
141 
142 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)143 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
144 {
145     /* Run your code on data */
146     OHOS::CameraStandard::CloudEnhanceSessionFuzzer::Test(data, size);
147     return 0;
148 }