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
34 namespace OHOS {
35 namespace CameraStandard {
36 namespace CloudEnhanceSessionFuzzer {
37 const int32_t LIMITSIZE = 4;
38 const int32_t NUM_TWO = 2;
39 sptr<IBufferProducer> surface;
40 sptr<CameraDevice> camera;
41 Profile profile;
42 CaptureOutput* curOutput;
43 bool g_isSupported;
44 bool g_isCameraDevicePermission = false;
45 SceneMode g_sceneMode;
46
GetPermission()47 void GetPermission()
48 {
49 uint64_t tokenId;
50 const char* perms[2];
51 perms[0] = "ohos.permission.DISTRIBUTED_DATASYNC";
52 perms[1] = "ohos.permission.CAMERA";
53 NativeTokenInfoParams infoInstance = {
54 .dcapsNum = 0,
55 .permsNum = 2,
56 .aclsNum = 0,
57 .dcaps = NULL,
58 .perms = perms,
59 .acls = NULL,
60 .processName = "native_camera_tdd",
61 .aplStr = "system_basic",
62 };
63 tokenId = GetAccessTokenId(&infoInstance);
64 SetSelfTokenID(tokenId);
65 OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
66 }
67
68 sptr<CameraManager> manager;
69 sptr<SurfaceBuffer> surfaceBuffer;
70 SceneMode sceneMode = SceneMode::TIMELAPSE_PHOTO;
71
GetCameraInput(uint8_t * rawData,size_t size)72 sptr<CaptureInput> GetCameraInput(uint8_t *rawData, size_t size)
73 {
74 MEDIA_INFO_LOG("CloudEnhanceSessionFuzzer: ENTER");
75 auto manager = CameraManager::GetInstance();
76 auto cameras = manager->GetSupportedCameras();
77 CHECK_ERROR_RETURN_RET_LOG(cameras.size() < NUM_TWO,
78 nullptr, "CloudEnhanceSessionFuzzer: GetSupportedCameras Error");
79 MessageParcel data;
80 data.WriteRawData(rawData, size);
81 camera = cameras[data.ReadUint32() % cameras.size()];
82 CHECK_ERROR_RETURN_RET_LOG(!camera, nullptr, "CloudEnhanceSessionFuzzer: Camera is null Error");
83 return manager->CreateCameraInput(camera);
84 }
85
GetCaptureOutput(uint8_t * rawData,size_t size)86 sptr<PhotoOutput> GetCaptureOutput(uint8_t *rawData, size_t size)
87 {
88 MEDIA_INFO_LOG("CloudEnhanceSessionFuzzer: ENTER");
89 auto manager = CameraManager::GetInstance();
90 CHECK_ERROR_RETURN_RET_LOG(!manager, nullptr, "CloudEnhanceSessionFuzzer: CameraManager::GetInstance Error");
91 MessageParcel data;
92 data.WriteRawData(rawData, size);
93 CHECK_ERROR_RETURN_RET_LOG(!camera, nullptr, "CloudEnhanceSessionFuzzer: Camera is null Error");
94 auto capability = manager->GetSupportedOutputCapability(camera, g_sceneMode);
95 CHECK_ERROR_RETURN_RET_LOG(!capability, nullptr, "CloudEnhanceSessionFuzzer: GetSupportedOutputCapability Error");
96 auto profiles = capability->GetPhotoProfiles();
97 CHECK_ERROR_RETURN_RET_LOG(profiles.empty(), nullptr, "CloudEnhanceSessionFuzzer: GetPhotoProfiles empty");
98 profile = profiles[data.ReadUint32() % profiles.size()];
99 sptr<IConsumerSurface> photoSurface = IConsumerSurface::Create();
100 CHECK_ERROR_RETURN_RET_LOG(!photoSurface, nullptr, "CloudEnhanceSessionFuzzer: create photoSurface Error");
101 surface = photoSurface->GetProducer();
102 CHECK_ERROR_RETURN_RET_LOG(!surface, nullptr, "CloudEnhanceSessionFuzzer: surface GetProducer Error");
103 return manager->CreatePhotoOutput(profile, surface);
104 }
105
TestSession(sptr<CaptureSession> session,uint8_t * rawData,size_t size)106 void TestSession(sptr<CaptureSession> session, uint8_t *rawData, size_t size)
107 {
108 MEDIA_INFO_LOG("CloudEnhanceSessionFuzzer: ENTER");
109 sptr<CaptureInput> input = GetCameraInput(rawData, size);
110 sptr<CaptureOutput> output = GetCaptureOutput(rawData, size);
111 CHECK_ERROR_RETURN_LOG(!input || !output || !session, "CloudEnhanceSessionFuzzer: input/output/session is null");
112 MessageParcel data;
113 data.WriteRawData(rawData, size);
114 session->SetMode(g_sceneMode);
115 session->GetMode();
116 PreconfigType preconfigType = static_cast<PreconfigType>(
117 data.ReadInt32() % (PreconfigType::PRECONFIG_HIGH_QUALITY + NUM_TWO));
118 ProfileSizeRatio preconfigRatio = static_cast<ProfileSizeRatio>(
119 data.ReadInt32() % (ProfileSizeRatio::RATIO_16_9 + NUM_TWO));
120 session->CanPreconfig(preconfigType, preconfigRatio);
121 session->Preconfig(preconfigType, preconfigRatio);
122 session->BeginConfig();
123 session->CanAddInput(input);
124 session->AddInput(input);
125 session->CanAddOutput(output);
126 session->AddOutput(output);
127 session->RemoveInput(input);
128 session->RemoveOutput(output);
129 session->AddInput(input);
130 session->AddOutput(output);
131 session->AddSecureOutput(output);
132 input->Open();
133 session->CommitConfig();
134 session->Start();
135 curOutput = output.GetRefPtr();
136 CaptureOutputType outputType = static_cast<CaptureOutputType>(
137 data.ReadInt32() % (CaptureOutputType::CAPTURE_OUTPUT_TYPE_MAX + NUM_TWO));
138 session->ValidateOutputProfile(profile, outputType);
139 }
140
Test(uint8_t * rawData,size_t size)141 void Test(uint8_t *rawData, size_t size)
142 {
143 CHECK_ERROR_RETURN(rawData == nullptr || size < LIMITSIZE);
144 GetPermission();
145 MessageParcel data;
146 data.WriteRawData(rawData, size);
147 g_sceneMode = static_cast<SceneMode>(
148 data.ReadInt32() % (SceneMode::APERTURE_VIDEO + NUM_TWO));
149 auto manager = CameraManager::GetInstance();
150 auto session = manager->CreateCaptureSession(g_sceneMode);
151 CHECK_ERROR_RETURN_LOG(!manager, "CloudEnhanceSessionFuzzer: CreateCaptureSession Error");
152 TestSession(session, rawData, size);
153 session->EnableAutoCloudImageEnhancement(data.ReadBool());
154 session->Release();
155 session->Stop();
156 }
157
158 } // namespace StreamRepeatStubFuzzer
159 } // namespace CameraStandard
160 } // namespace OHOS
161
162 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)163 extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size)
164 {
165 /* Run your code on data */
166 OHOS::CameraStandard::CloudEnhanceSessionFuzzer::Test(data, size);
167 return 0;
168 }