• 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 "session_coordinator_fuzzer.h"
17 #include "camera_log.h"
18 #include "deferred_processing_service.h"
19 #include "image_info.h"
20 #include "message_parcel.h"
21 #include <cstddef>
22 #include <cstdint>
23 #include <memory>
24 #include "accesstoken_kit.h"
25 #include "camera_error_code.h"
26 #include "securec.h"
27 #include <fuzzer/FuzzedDataProvider.h>
28 
29 namespace OHOS {
30 namespace CameraStandard {
31 static constexpr int32_t NUM_TRI = 13;
32 static constexpr int32_t MIN_SIZE_NUM = 20;
33 constexpr int VIDEO_REQUEST_FD_ID = 1;
34 
35 std::shared_ptr<DeferredProcessing::SessionCoordinator> SessionCoordinatorFuzzer::fuzz_{nullptr};
36 
SessionCoordinatorFuzzTest(FuzzedDataProvider & fdp)37 void SessionCoordinatorFuzzer::SessionCoordinatorFuzzTest(FuzzedDataProvider& fdp)
38 {
39     fuzz_ = std::make_shared<DeferredProcessing::SessionCoordinator>();
40     CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
41     fuzz_->Initialize();
42     int32_t userId = fdp.ConsumeIntegral<int32_t>();
43     uint8_t randomNum = fdp.ConsumeIntegral<int32_t>();
44     std::vector<std::string> testStrings = {"test1", "test2"};
45     std::string imageId(testStrings[randomNum % testStrings.size()]);
46     int32_t dataSize = fdp.ConsumeIntegralInRange<int32_t>(1, 10);
47     std::shared_ptr<DeferredProcessing::SharedBuffer> sharedBuffer =
48         std::make_shared<DeferredProcessing::SharedBuffer>(dataSize);
49     sharedBuffer->Initialize();
50     DeferredProcessing::DpsError dpsError =
51         static_cast<DeferredProcessing::DpsError>(fdp.ConsumeIntegral<int32_t>() % NUM_TRI);
52     sptr<IPCFileDescriptor> ipcFd = sptr<IPCFileDescriptor>::MakeSptr(VIDEO_REQUEST_FD_ID);
53     fuzz_->videoProcCallbacks_->OnProcessDone(userId, imageId, ipcFd);
54     fuzz_->videoProcCallbacks_->OnError(userId, imageId, dpsError);
55     std::vector<DeferredProcessing::DpsStatus> dpsStatusVec = {
56         DeferredProcessing::DPS_SESSION_STATE_IDLE,
57         DeferredProcessing::DPS_SESSION_STATE_RUNNABLE,
58         DeferredProcessing::DPS_SESSION_STATE_RUNNING,
59         DeferredProcessing::DPS_SESSION_STATE_SUSPENDED,
60     };
61     uint8_t arrcodeNum = randomNum % dpsStatusVec.size();
62     DeferredProcessing::DpsStatus dpsStatus = dpsStatusVec[arrcodeNum];
63     fuzz_->videoProcCallbacks_->OnStateChanged(userId, dpsStatus);
64     fuzz_->Start();
65     fuzz_->OnStateChanged(userId, dpsStatus);
66     fuzz_->OnVideoStateChanged(userId, dpsStatus);
67     fuzz_->GetVideoProcCallbacks();
68     fuzz_->OnVideoProcessDone(userId, imageId, ipcFd);
69     fuzz_->OnVideoError(userId, imageId, dpsError);
70     auto videoCallback = fuzz_->GetRemoteVideoCallback(userId);
71     fuzz_->ProcessVideoResults(videoCallback);
72     fuzz_->DeleteVideoSession(userId);
73     fuzz_->Stop();
74 }
75 
Test(uint8_t * data,size_t size)76 void Test(uint8_t* data, size_t size)
77 {
78     FuzzedDataProvider fdp(data, size);
79     if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
80         return;
81     }
82     auto sessionCoordinatorFuzz = std::make_unique<SessionCoordinatorFuzzer>();
83     if (sessionCoordinatorFuzz == nullptr) {
84         MEDIA_INFO_LOG("sessionCoordinatorFuzz is null");
85         return;
86     }
87     sessionCoordinatorFuzz->SessionCoordinatorFuzzTest(fdp);
88 }
89 } // namespace CameraStandard
90 } // namespace OHOS
91 
92 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)93 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
94 {
95     OHOS::CameraStandard::Test(data, size);
96     return 0;
97 }