15ccb8f90Sopenharmony_ci/*
25ccb8f90Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
35ccb8f90Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
45ccb8f90Sopenharmony_ci * you may not use this file except in compliance with the License.
55ccb8f90Sopenharmony_ci * You may obtain a copy of the License at
65ccb8f90Sopenharmony_ci *
75ccb8f90Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
85ccb8f90Sopenharmony_ci *
95ccb8f90Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
105ccb8f90Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
115ccb8f90Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
125ccb8f90Sopenharmony_ci * See the License for the specific language governing permissions and
135ccb8f90Sopenharmony_ci * limitations under the License.
145ccb8f90Sopenharmony_ci */
155ccb8f90Sopenharmony_ci
165ccb8f90Sopenharmony_ci/* This files contains faultlog fuzzer test modules. */
175ccb8f90Sopenharmony_ci
185ccb8f90Sopenharmony_ci#include "power_fuzzer.h"
195ccb8f90Sopenharmony_ci
205ccb8f90Sopenharmony_ci#include <cstddef>
215ccb8f90Sopenharmony_ci#include <cstdlib>
225ccb8f90Sopenharmony_ci
235ccb8f90Sopenharmony_ci#include "actions/idevice_power_action.h"
245ccb8f90Sopenharmony_ci#include "actions/idevice_state_action.h"
255ccb8f90Sopenharmony_ci#include "message_parcel.h"
265ccb8f90Sopenharmony_ci#include "running_lock_action.h"
275ccb8f90Sopenharmony_ci#include "securec.h"
285ccb8f90Sopenharmony_ci#include "shutdown/shutdown_client.h"
295ccb8f90Sopenharmony_ci
305ccb8f90Sopenharmony_ciusing namespace OHOS;
315ccb8f90Sopenharmony_ciusing namespace OHOS::PowerMgr;
325ccb8f90Sopenharmony_ciusing namespace std;
335ccb8f90Sopenharmony_ci
345ccb8f90Sopenharmony_cinamespace OHOS {
355ccb8f90Sopenharmony_cinamespace PowerMgr {
365ccb8f90Sopenharmony_ciclass FuzzPowerAction : public IDevicePowerAction {
375ccb8f90Sopenharmony_cipublic:
385ccb8f90Sopenharmony_ci    FuzzPowerAction() = default;
395ccb8f90Sopenharmony_ci    virtual ~FuzzPowerAction() = default;
405ccb8f90Sopenharmony_ci    virtual void Reboot([[maybe_unused]] const std::string& reason) {};
415ccb8f90Sopenharmony_ci    virtual void Shutdown([[maybe_unused]] const std::string& reason) {};
425ccb8f90Sopenharmony_ci};
435ccb8f90Sopenharmony_ci
445ccb8f90Sopenharmony_ciclass FuzzShutdownAction : public ShutdownController {
455ccb8f90Sopenharmony_cipublic:
465ccb8f90Sopenharmony_ci    FuzzShutdownAction() = default;
475ccb8f90Sopenharmony_ci    virtual ~FuzzShutdownAction() = default;
485ccb8f90Sopenharmony_ci    virtual void Reboot([[maybe_unused]] const std::string& reason) {};
495ccb8f90Sopenharmony_ci    virtual void Shutdown([[maybe_unused]] const std::string& reason) {};
505ccb8f90Sopenharmony_ci};
515ccb8f90Sopenharmony_ci
525ccb8f90Sopenharmony_ciclass FuzzStateAction : public IDeviceStateAction {
535ccb8f90Sopenharmony_cipublic:
545ccb8f90Sopenharmony_ci    FuzzStateAction() = default;
555ccb8f90Sopenharmony_ci    virtual ~FuzzStateAction() = default;
565ccb8f90Sopenharmony_ci    virtual void Suspend([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] SuspendDeviceType type,
575ccb8f90Sopenharmony_ci        [[maybe_unused]] uint32_t flags) {};
585ccb8f90Sopenharmony_ci    virtual void ForceSuspend() {};
595ccb8f90Sopenharmony_ci    virtual void Wakeup([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] WakeupDeviceType type,
605ccb8f90Sopenharmony_ci        [[maybe_unused]] const std::string& details, [[maybe_unused]] const std::string& pkgName) {};
615ccb8f90Sopenharmony_ci    virtual void RefreshActivity([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] UserActivityType type,
625ccb8f90Sopenharmony_ci        [[maybe_unused]] uint32_t flags) {};
635ccb8f90Sopenharmony_ci    virtual DisplayState GetDisplayState()
645ccb8f90Sopenharmony_ci    {
655ccb8f90Sopenharmony_ci        return DisplayState::DISPLAY_OFF;
665ccb8f90Sopenharmony_ci    };
675ccb8f90Sopenharmony_ci    virtual uint32_t SetDisplayState([[maybe_unused]] DisplayState state,
685ccb8f90Sopenharmony_ci        [[maybe_unused]] StateChangeReason reason = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN)
695ccb8f90Sopenharmony_ci    {
705ccb8f90Sopenharmony_ci        return 0;
715ccb8f90Sopenharmony_ci    }
725ccb8f90Sopenharmony_ci    virtual bool TryToCancelScreenOff()
735ccb8f90Sopenharmony_ci    {
745ccb8f90Sopenharmony_ci        return false;
755ccb8f90Sopenharmony_ci    }
765ccb8f90Sopenharmony_ci    virtual void BeginPowerkeyScreenOff() {};
775ccb8f90Sopenharmony_ci    virtual void EndPowerkeyScreenOff() {};
785ccb8f90Sopenharmony_ci    virtual void SetCoordinated([[maybe_unused]] bool coordinated) {};
795ccb8f90Sopenharmony_ci    virtual uint32_t GoToSleep([[maybe_unused]] const std::function<void()> onSuspend,
805ccb8f90Sopenharmony_ci        [[maybe_unused]] const std::function<void()> onWakeup, [[maybe_unused]] bool force)
815ccb8f90Sopenharmony_ci    {
825ccb8f90Sopenharmony_ci        return 0;
835ccb8f90Sopenharmony_ci    }
845ccb8f90Sopenharmony_ci    virtual void RegisterCallback([[maybe_unused]] std::function<void(uint32_t)>& callback) {};
855ccb8f90Sopenharmony_ci};
865ccb8f90Sopenharmony_ci} // namespace PowerMgr
875ccb8f90Sopenharmony_ci} // namespace OHOS
885ccb8f90Sopenharmony_ci
895ccb8f90Sopenharmony_cinamespace {
905ccb8f90Sopenharmony_ciconst int32_t REWIND_READ_DATA = 0;
915ccb8f90Sopenharmony_ci} // namespace
925ccb8f90Sopenharmony_ci
935ccb8f90Sopenharmony_ciPowerFuzzerTest::PowerFuzzerTest()
945ccb8f90Sopenharmony_ci{
955ccb8f90Sopenharmony_ci    service_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
965ccb8f90Sopenharmony_ci    service_->OnStart();
975ccb8f90Sopenharmony_ci    auto powerAction = new FuzzPowerAction();
985ccb8f90Sopenharmony_ci    auto powerState = new FuzzStateAction();
995ccb8f90Sopenharmony_ci    auto shutdownState = new FuzzStateAction();
1005ccb8f90Sopenharmony_ci    auto lockAction = new RunningLockAction();
1015ccb8f90Sopenharmony_ci    auto shutdownAction = new FuzzShutdownAction();
1025ccb8f90Sopenharmony_ci    service_->EnableMock(powerState, shutdownState, powerAction, lockAction);
1035ccb8f90Sopenharmony_ci    service_->EnableShutdownMock(shutdownAction);
1045ccb8f90Sopenharmony_ci}
1055ccb8f90Sopenharmony_ci
1065ccb8f90Sopenharmony_ciPowerFuzzerTest::~PowerFuzzerTest()
1075ccb8f90Sopenharmony_ci{
1085ccb8f90Sopenharmony_ci    if (service_ != nullptr) {
1095ccb8f90Sopenharmony_ci        service_->OnStop();
1105ccb8f90Sopenharmony_ci        service_->Reset();
1115ccb8f90Sopenharmony_ci    }
1125ccb8f90Sopenharmony_ci    service_ = nullptr;
1135ccb8f90Sopenharmony_ci}
1145ccb8f90Sopenharmony_ci
1155ccb8f90Sopenharmony_civoid PowerFuzzerTest::TestPowerServiceStub(const uint32_t code, const uint8_t* data, size_t size)
1165ccb8f90Sopenharmony_ci{
1175ccb8f90Sopenharmony_ci    MessageParcel datas;
1185ccb8f90Sopenharmony_ci    datas.WriteInterfaceToken(PowerMgrService::GetDescriptor());
1195ccb8f90Sopenharmony_ci    datas.WriteBuffer(data, size);
1205ccb8f90Sopenharmony_ci    datas.RewindRead(REWIND_READ_DATA);
1215ccb8f90Sopenharmony_ci    MessageParcel reply;
1225ccb8f90Sopenharmony_ci    MessageOption option;
1235ccb8f90Sopenharmony_ci    service_->OnRemoteRequest(code, datas, reply, option);
1245ccb8f90Sopenharmony_ci}
125