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 "securec.h"
17 #include <cstdint>
18 #include <cstdlib>
19 #include <memory>
20
21 #include "power_interface_impl.h"
22 #include "v1_2/ipower_interface.h"
23 #include "v1_2/power_interface_stub.h"
24 #include "running_lock_impl.h"
25 #include "refbase.h"
26
27 using namespace OHOS::HDI;
28 using namespace OHOS::HDI::Power::V1_2;
29 using namespace std;
30
31 namespace OHOS {
32 namespace HDI {
33 namespace Power {
34 namespace V1_2 {
35 constexpr int32_t DEFAULT_TIMEOUT_FOR_TEST_MS = 100;
36
37 class PowerFuzzTest {
38 public:
PowerFuzzTest()39 PowerFuzzTest()
40 {
41 impl_ = sptr<PowerInterfaceImpl>::MakeSptr();
42 impl_->SuspendBlock("PowerStubFuzzTest"); // Prevent device sleep
43 }
~PowerFuzzTest()44 ~PowerFuzzTest()
45 {
46 impl_->SuspendUnblock("PowerStubFuzzTest");
47 }
GetImpl() const48 sptr<PowerInterfaceImpl> GetImpl() const
49 {
50 return impl_;
51 }
52
53 private:
54 sptr<PowerInterfaceImpl> impl_ = nullptr;
55 };
56 namespace {
57 shared_ptr<PowerInterfaceStub> g_fuzzService = nullptr;
58 shared_ptr<PowerFuzzTest> g_fuzzTest = nullptr;
59 const uint32_t POWER_INTERFACE_STUB_FUNC_MAX_SIZE = 18;
60 } // namespace
61
PowerHdiFuzzTest(const uint8_t * data,size_t size)62 static void PowerHdiFuzzTest(const uint8_t *data, size_t size)
63 {
64 uint32_t code;
65 if (size < sizeof(code)) {
66 return;
67 }
68 if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) {
69 return;
70 }
71 OHOS::HDI::Power::V1_2::IPowerInterface::Get(true);
72
73 MessageParcel datas;
74 MessageParcel reply;
75 MessageOption option;
76 if (g_fuzzService == nullptr) {
77 g_fuzzTest = make_shared<PowerFuzzTest>();
78 g_fuzzService = make_shared<PowerInterfaceStub>(g_fuzzTest->GetImpl());
79 }
80 for (code = CMD_POWER_INTERFACE_GET_VERSION; code < POWER_INTERFACE_STUB_FUNC_MAX_SIZE; code++) {
81 g_fuzzService->OnRemoteRequest(code, datas, reply, option);
82 }
83 RunningLockImpl::SetDefaultTimeOutMs(DEFAULT_TIMEOUT_FOR_TEST_MS);
84 RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
85 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
86 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
87 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
88 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
89 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
90 RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BUTT);
91 RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
92 RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
93 RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
94 RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
95 RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
96 RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
97 RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BUTT);
98 RunningLockImpl::Clean();
99 }
100 } // namespace V1_2
101 } // namespace Power
102 } // namespace HDI
103 } // namespace OHOS
104
105 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)106 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
107 {
108 /* Run your code on data */
109 OHOS::HDI::Power::V1_2::PowerHdiFuzzTest(data, size);
110 return 0;
111 }
112