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 <cstddef>
17 #include <cstdint>
18 #include <memory>
19
20 #include "accesstoken_kit.h"
21 #include "camera_error_code.h"
22 #include "camera_log.h"
23 #include "deferredvideoprocsession_fuzzer.h"
24 #include "message_parcel.h"
25 #include "nativetoken_kit.h"
26 #include "securec.h"
27 #include "test_token.h"
28 #include "token_setproc.h"
29
30 namespace OHOS {
31 namespace CameraStandard {
32 static constexpr int32_t MIN_SIZE_NUM = 128;
33 int g_userId = 0;
34 const size_t MAX_LENGTH_STRING = 64;
35
36 std::shared_ptr<DeferredVideoProcessingSessionCallback> callback_ =
37 std::make_shared<DeferredVideoProcessingSessionCallback>();
38 std::shared_ptr<IDeferredVideoProcSessionCallbackFuzz> sessionCallback_ =
39 std::make_shared<IDeferredVideoProcSessionCallbackFuzz>();
40 std::shared_ptr<DeferredVideoProcSession> deferredVideoProcSession_ =
41 std::make_shared<DeferredVideoProcSession>(g_userId, sessionCallback_);
42
createSession(int userId,std::shared_ptr<IDeferredVideoProcSessionCallback> callback,sptr<DeferredProcessing::IDeferredVideoProcessingSession> & session)43 auto createSession(int userId,
44 std::shared_ptr<IDeferredVideoProcSessionCallback> callback,
45 sptr<DeferredProcessing::IDeferredVideoProcessingSession>& session)
46 {
47 auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
48 CHECK_RETURN_RET_ELOG(samgr == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
49 "CreateDeferredVideoProcessingSession Failed to get System ability manager");
50 sptr<IRemoteObject> object = samgr->GetSystemAbility(CAMERA_SERVICE_ID);
51 CHECK_RETURN_RET_ELOG(object == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
52 "CreateDeferredVideoProcessingSession object is null");
53 sptr<ICameraService> serviceProxy = iface_cast<ICameraService>(object);
54 CHECK_RETURN_RET_ELOG(serviceProxy == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
55 "CreateDeferredVideoProcessingSession serviceProxy is null");
56
57 sptr<DeferredProcessing::IDeferredVideoProcessingSessionCallback> remoteCallback =
58 new(std::nothrow) DeferredVideoProcessingSessionCallback();
59 CHECK_RETURN_RET_ELOG(remoteCallback == nullptr, CameraErrorCode::SERVICE_FATL_ERROR,
60 "CreateDeferredVideoProcessingSession failed to new remoteCallback!");
61 serviceProxy->CreateDeferredVideoProcessingSession(userId, remoteCallback, session);
62 return CameraErrorCode::SUCCESS;
63 }
64
DeferredVideoProcSessionFuzzTest(FuzzedDataProvider & fdp)65 void DeferredVideoProcSessionFuzzer::DeferredVideoProcSessionFuzzTest(FuzzedDataProvider& fdp)
66 {
67 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
68 return;
69 }
70 CHECK_RETURN_ELOG(!TestToken().GetAllCameraPermission(), "GetPermission error");
71 std::string videoId(fdp.ConsumeRandomLengthString(MAX_LENGTH_STRING));
72 sptr<IPCFileDescriptor> ipcFileDescriptor = nullptr;
73 callback_->OnProcessVideoDone(videoId, ipcFileDescriptor);
74 callback_->OnError(videoId, DpsErrorCode::ERROR_SESSION_SYNC_NEEDED);
75 int32_t status = fdp.ConsumeIntegral<int32_t>();
76 callback_->OnStateChanged(status);
77
78 deferredVideoProcSession_->BeginSynchronize();
79 deferredVideoProcSession_->EndSynchronize();
80 sptr<IPCFileDescriptor> srcFd = sptr<IPCFileDescriptor>::MakeSptr(fdp.ConsumeIntegral<int>());
81 sptr<IPCFileDescriptor> dstFd = sptr<IPCFileDescriptor>::MakeSptr(fdp.ConsumeIntegral<int>());
82 deferredVideoProcSession_->AddVideo(videoId, srcFd, dstFd);
83 bool restorable = fdp.ConsumeBool();
84 deferredVideoProcSession_->RemoveVideo(videoId, restorable);
85 deferredVideoProcSession_->RestoreVideo(videoId);
86 sptr<DeferredProcessing::IDeferredVideoProcessingSession> session = nullptr;
87 createSession(g_userId, sessionCallback_, session);
88 CHECK_RETURN_ELOG(session == nullptr, "session is null!");
89 deferredVideoProcSession_->SetDeferredVideoSession(session);
90 int32_t pid = fdp.ConsumeIntegral<int32_t>();
91 deferredVideoProcSession_->ReconnectDeferredProcessingSession();
92 deferredVideoProcSession_->ConnectDeferredProcessingSession();
93 deferredVideoProcSession_->GetCallback();
94 deferredVideoProcSession_->remoteSession_ = nullptr;
95 deferredVideoProcSession_->callback_ = nullptr;
96 deferredVideoProcSession_->CameraServerDied(pid);
97 }
98
Test(uint8_t * data,size_t size)99 void Test(uint8_t* data, size_t size)
100 {
101 auto deferredVideoProcSessionFuzz = std::make_unique<DeferredVideoProcSessionFuzzer>();
102 if (deferredVideoProcSessionFuzz == nullptr) {
103 MEDIA_INFO_LOG("deferredVideoProcSessionFuzz is null");
104 return;
105 }
106 FuzzedDataProvider fdp(data, size);
107 deferredVideoProcSessionFuzz->DeferredVideoProcSessionFuzzTest(fdp);
108 }
109
110 } // namespace CameraStandard
111 } // namespace OHOS
112
113 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)114 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
115 {
116 OHOS::CameraStandard::Test(data, size);
117 return 0;
118 }