1 /*
2 * Copyright (c) 2023 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 /* This files contains faultlog fuzzer test modules. */
17
18 #include "power_fuzzer.h"
19
20 #include <cstddef>
21 #include <cstdlib>
22
23 #include "actions/idevice_power_action.h"
24 #include "actions/idevice_state_action.h"
25 #include "message_parcel.h"
26 #include "running_lock_action.h"
27 #include "securec.h"
28 #include "shutdown/shutdown_client.h"
29
30 using namespace OHOS;
31 using namespace OHOS::PowerMgr;
32 using namespace std;
33
34 namespace OHOS {
35 namespace PowerMgr {
36 class FuzzPowerAction : public IDevicePowerAction {
37 public:
38 FuzzPowerAction() = default;
39 virtual ~FuzzPowerAction() = default;
Reboot(const std::string & reason)40 virtual void Reboot([[maybe_unused]] const std::string& reason) {};
Shutdown(const std::string & reason)41 virtual void Shutdown([[maybe_unused]] const std::string& reason) {};
42 };
43
44 class FuzzShutdownAction : public ShutdownController {
45 public:
46 FuzzShutdownAction() = default;
47 virtual ~FuzzShutdownAction() = default;
Reboot(const std::string & reason)48 virtual void Reboot([[maybe_unused]] const std::string& reason) {};
Shutdown(const std::string & reason)49 virtual void Shutdown([[maybe_unused]] const std::string& reason) {};
50 };
51
52 class FuzzStateAction : public IDeviceStateAction {
53 public:
54 FuzzStateAction() = default;
55 virtual ~FuzzStateAction() = default;
Suspend(int64_t callTimeMs,SuspendDeviceType type,uint32_t flags)56 virtual void Suspend([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] SuspendDeviceType type,
57 [[maybe_unused]] uint32_t flags) {};
ForceSuspend()58 virtual void ForceSuspend() {};
Wakeup(int64_t callTimeMs,WakeupDeviceType type,const std::string & details,const std::string & pkgName)59 virtual void Wakeup([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] WakeupDeviceType type,
60 [[maybe_unused]] const std::string& details, [[maybe_unused]] const std::string& pkgName) {};
RefreshActivity(int64_t callTimeMs,UserActivityType type,uint32_t flags)61 virtual void RefreshActivity([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] UserActivityType type,
62 [[maybe_unused]] uint32_t flags) {};
GetDisplayState()63 virtual DisplayState GetDisplayState()
64 {
65 return DisplayState::DISPLAY_OFF;
66 };
SetDisplayState(DisplayState state,StateChangeReason reason=StateChangeReason::STATE_CHANGE_REASON_UNKNOWN)67 virtual uint32_t SetDisplayState([[maybe_unused]] DisplayState state,
68 [[maybe_unused]] StateChangeReason reason = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN)
69 {
70 return 0;
71 }
SetInternalScreenDisplayPower(DisplayState state,StateChangeReason reason)72 virtual void SetInternalScreenDisplayPower(
73 [[maybe_unused]] DisplayState state, [[maybe_unused]] StateChangeReason reason)
74 {
75 return;
76 }
SetInternalScreenBrightness()77 virtual void SetInternalScreenBrightness()
78 {
79 return;
80 }
TryToCancelScreenOff()81 virtual bool TryToCancelScreenOff()
82 {
83 return false;
84 }
BeginPowerkeyScreenOff()85 virtual void BeginPowerkeyScreenOff() {};
EndPowerkeyScreenOff()86 virtual void EndPowerkeyScreenOff() {};
SetCoordinated(bool coordinated)87 virtual void SetCoordinated([[maybe_unused]] bool coordinated) {};
GoToSleep(const std::function<void ()> onSuspend,const std::function<void ()> onWakeup,bool force)88 virtual uint32_t GoToSleep([[maybe_unused]] const std::function<void()> onSuspend,
89 [[maybe_unused]] const std::function<void()> onWakeup, [[maybe_unused]] bool force)
90 {
91 return 0;
92 }
RegisterCallback(std::function<void (uint32_t)> & callback)93 virtual void RegisterCallback([[maybe_unused]] std::function<void(uint32_t)>& callback) {};
94 };
95 } // namespace PowerMgr
96 } // namespace OHOS
97
98 namespace {
99 const int32_t REWIND_READ_DATA = 0;
100 } // namespace
101
PowerFuzzerTest()102 PowerFuzzerTest::PowerFuzzerTest()
103 {
104 service_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
105 service_->OnStart();
106 auto powerAction = new FuzzPowerAction();
107 auto powerState = new FuzzStateAction();
108 auto shutdownState = new FuzzStateAction();
109 auto lockAction = new RunningLockAction();
110 auto shutdownAction = new FuzzShutdownAction();
111 service_->EnableMock(powerState, shutdownState, powerAction, lockAction);
112 service_->EnableShutdownMock(shutdownAction);
113 }
114
~PowerFuzzerTest()115 PowerFuzzerTest::~PowerFuzzerTest()
116 {
117 if (service_ != nullptr) {
118 service_->OnStop();
119 service_->Reset();
120 }
121 service_ = nullptr;
122 }
123
TestPowerServiceStub(const uint32_t code,const uint8_t * data,size_t size)124 void PowerFuzzerTest::TestPowerServiceStub(const uint32_t code, const uint8_t* data, size_t size)
125 {
126 MessageParcel datas;
127 datas.WriteInterfaceToken(PowerMgrService::GetDescriptor());
128 datas.WriteBuffer(data, size);
129 datas.RewindRead(REWIND_READ_DATA);
130 MessageParcel reply;
131 MessageOption option;
132 service_->OnRemoteRequest(code, datas, reply, option);
133 }
134