1094332d3Sopenharmony_ci/*
2094332d3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
3094332d3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4094332d3Sopenharmony_ci * you may not use this file except in compliance with the License.
5094332d3Sopenharmony_ci * You may obtain a copy of the License at
6094332d3Sopenharmony_ci *
7094332d3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8094332d3Sopenharmony_ci *
9094332d3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10094332d3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11094332d3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12094332d3Sopenharmony_ci * See the License for the specific language governing permissions and
13094332d3Sopenharmony_ci * limitations under the License.
14094332d3Sopenharmony_ci */
15094332d3Sopenharmony_ci
16094332d3Sopenharmony_ci#include "securec.h"
17094332d3Sopenharmony_ci#include <cstdint>
18094332d3Sopenharmony_ci#include <cstdlib>
19094332d3Sopenharmony_ci#include <memory>
20094332d3Sopenharmony_ci
21094332d3Sopenharmony_ci#include "power_interface_impl.h"
22094332d3Sopenharmony_ci#include "v1_2/ipower_interface.h"
23094332d3Sopenharmony_ci#include "v1_2/power_interface_stub.h"
24094332d3Sopenharmony_ci#include "running_lock_impl.h"
25094332d3Sopenharmony_ci#include "refbase.h"
26094332d3Sopenharmony_ci
27094332d3Sopenharmony_ciusing namespace OHOS::HDI;
28094332d3Sopenharmony_ciusing namespace OHOS::HDI::Power::V1_2;
29094332d3Sopenharmony_ciusing namespace std;
30094332d3Sopenharmony_ci
31094332d3Sopenharmony_cinamespace OHOS {
32094332d3Sopenharmony_cinamespace HDI {
33094332d3Sopenharmony_cinamespace Power {
34094332d3Sopenharmony_cinamespace V1_2 {
35094332d3Sopenharmony_ciconstexpr int32_t DEFAULT_TIMEOUT_FOR_TEST_MS = 100;
36094332d3Sopenharmony_ci
37094332d3Sopenharmony_ciclass PowerFuzzTest {
38094332d3Sopenharmony_cipublic:
39094332d3Sopenharmony_ci    PowerFuzzTest()
40094332d3Sopenharmony_ci    {
41094332d3Sopenharmony_ci        impl_ = sptr<PowerInterfaceImpl>::MakeSptr();
42094332d3Sopenharmony_ci        impl_->SuspendBlock("PowerStubFuzzTest"); // Prevent device sleep
43094332d3Sopenharmony_ci    }
44094332d3Sopenharmony_ci    ~PowerFuzzTest()
45094332d3Sopenharmony_ci    {
46094332d3Sopenharmony_ci        impl_->SuspendUnblock("PowerStubFuzzTest");
47094332d3Sopenharmony_ci    }
48094332d3Sopenharmony_ci    sptr<PowerInterfaceImpl> GetImpl() const
49094332d3Sopenharmony_ci    {
50094332d3Sopenharmony_ci        return impl_;
51094332d3Sopenharmony_ci    }
52094332d3Sopenharmony_ci
53094332d3Sopenharmony_ciprivate:
54094332d3Sopenharmony_ci    sptr<PowerInterfaceImpl> impl_ = nullptr;
55094332d3Sopenharmony_ci};
56094332d3Sopenharmony_cinamespace {
57094332d3Sopenharmony_cishared_ptr<PowerInterfaceStub> g_fuzzService = nullptr;
58094332d3Sopenharmony_cishared_ptr<PowerFuzzTest> g_fuzzTest = nullptr;
59094332d3Sopenharmony_ciconst uint32_t POWER_INTERFACE_STUB_FUNC_MAX_SIZE = 18;
60094332d3Sopenharmony_ci} // namespace
61094332d3Sopenharmony_ci
62094332d3Sopenharmony_cistatic void PowerHdiFuzzTest(const uint8_t *data, size_t size)
63094332d3Sopenharmony_ci{
64094332d3Sopenharmony_ci    uint32_t code;
65094332d3Sopenharmony_ci    if (size < sizeof(code)) {
66094332d3Sopenharmony_ci        return;
67094332d3Sopenharmony_ci    }
68094332d3Sopenharmony_ci    if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) {
69094332d3Sopenharmony_ci        return;
70094332d3Sopenharmony_ci    }
71094332d3Sopenharmony_ci    OHOS::HDI::Power::V1_2::IPowerInterface::Get(true);
72094332d3Sopenharmony_ci
73094332d3Sopenharmony_ci    MessageParcel datas;
74094332d3Sopenharmony_ci    MessageParcel reply;
75094332d3Sopenharmony_ci    MessageOption option;
76094332d3Sopenharmony_ci    if (g_fuzzService == nullptr) {
77094332d3Sopenharmony_ci        g_fuzzTest = make_shared<PowerFuzzTest>();
78094332d3Sopenharmony_ci        g_fuzzService = make_shared<PowerInterfaceStub>(g_fuzzTest->GetImpl());
79094332d3Sopenharmony_ci    }
80094332d3Sopenharmony_ci    for (code = CMD_POWER_INTERFACE_GET_VERSION; code < POWER_INTERFACE_STUB_FUNC_MAX_SIZE; code++) {
81094332d3Sopenharmony_ci        g_fuzzService->OnRemoteRequest(code, datas, reply, option);
82094332d3Sopenharmony_ci    }
83094332d3Sopenharmony_ci    RunningLockImpl::SetDefaultTimeOutMs(DEFAULT_TIMEOUT_FOR_TEST_MS);
84094332d3Sopenharmony_ci    RunningLockImpl::GetCount(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
85094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
86094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
87094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
88094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
89094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
90094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTag(RunningLockType::RUNNINGLOCK_BUTT);
91094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_PHONE);
92094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_NOTIFICATION);
93094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_AUDIO);
94094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_SPORT);
95094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_NAVIGATION);
96094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BACKGROUND_TASK);
97094332d3Sopenharmony_ci    RunningLockImpl::GetRunningLockTagInner(RunningLockType::RUNNINGLOCK_BUTT);
98094332d3Sopenharmony_ci    RunningLockImpl::Clean();
99094332d3Sopenharmony_ci}
100094332d3Sopenharmony_ci} // namespace V1_2
101094332d3Sopenharmony_ci} // namespace Power
102094332d3Sopenharmony_ci} // namespace HDI
103094332d3Sopenharmony_ci} // namespace OHOS
104094332d3Sopenharmony_ci
105094332d3Sopenharmony_ci/* Fuzzer entry point */
106094332d3Sopenharmony_ciextern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
107094332d3Sopenharmony_ci{
108094332d3Sopenharmony_ci    /* Run your code on data */
109094332d3Sopenharmony_ci    OHOS::HDI::Power::V1_2::PowerHdiFuzzTest(data, size);
110094332d3Sopenharmony_ci    return 0;
111094332d3Sopenharmony_ci}
112