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 "camera_window_manager_client_fuzzer.h"
17 #include "camera_log.h"
18 #include "securec.h"
19
20 namespace OHOS {
21 namespace CameraStandard {
22 static constexpr int32_t MIN_SIZE_NUM = 5;
23 const size_t THRESHOLD = 10;
24
25 sptr<CameraWindowManagerClient> CameraWindowManagerClientFuzzer::fuzz_{nullptr};
26 std::shared_ptr<CameraWindowManagerClient::WMSSaStatusChangeCallback>
27 CameraWindowManagerClientFuzzer::callback_{nullptr};
28
29 /*
30 * describe: get data from outside untrusted data(g_data) which size is according to sizeof(T)
31 * tips: only support basic type
32 */
CameraWindowManagerClientFuzzTest(FuzzedDataProvider & fdp)33 void CameraWindowManagerClientFuzzer::CameraWindowManagerClientFuzzTest(FuzzedDataProvider& fdp)
34 {
35 if (fdp.remaining_bytes() < MIN_SIZE_NUM) {
36 return;
37 }
38
39 fuzz_ = CameraWindowManagerClient::GetInstance();
40 CHECK_RETURN_ELOG(!fuzz_, "Create fuzz_ Error");
41 pid_t pid;
42 int32_t systemAbilityId = fdp.ConsumeIntegral<int32_t>();
43 uint8_t randomNum = fdp.ConsumeIntegral<uint8_t>();
44 std::vector<std::string> testStrings = {"test1", "test2"};
45 std::string deviceId(testStrings[randomNum % testStrings.size()]);
46 fuzz_->InitWindowProxy();
47 fuzz_->GetFocusWindowInfo(pid);
48 fuzz_->GetWindowManagerAgent();
49 if (callback_ == nullptr) {
50 callback_ = std::make_shared<CameraWindowManagerClient::WMSSaStatusChangeCallback>();
51 }
52 callback_->OnRemoveSystemAbility(systemAbilityId, deviceId);
53 fuzz_->UnregisterWindowManagerAgent();
54 }
55
Test(uint8_t * data,size_t size)56 void Test(uint8_t* data, size_t size)
57 {
58 auto cameraWindowManagerClient = std::make_unique<CameraWindowManagerClientFuzzer>();
59 if (cameraWindowManagerClient == nullptr) {
60 MEDIA_INFO_LOG("cameraWindowManagerClient is null");
61 return;
62 }
63 FuzzedDataProvider fdp(data, size);
64 cameraWindowManagerClient->CameraWindowManagerClientFuzzTest(fdp);
65 }
66 } // namespace CameraStandard
67 } // namespace OHOS
68
69 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)70 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
71 {
72 if (size < OHOS::CameraStandard::THRESHOLD) {
73 return 0;
74 }
75
76 OHOS::CameraStandard::Test(data, size);
77 return 0;
78 }