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;
40 virtual void Reboot([[maybe_unused]] 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;
48 virtual void Reboot([[maybe_unused]] 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;
56 virtual void Suspend([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] SuspendDeviceType type,
57 [[maybe_unused]] uint32_t flags) {};
ForceSuspend()58 virtual void ForceSuspend() {};
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) {};
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 };
67 virtual uint32_t SetDisplayState([[maybe_unused]] DisplayState state,
68 [[maybe_unused]] StateChangeReason reason = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN)
69 {
70 return 0;
71 }
TryToCancelScreenOff()72 virtual bool TryToCancelScreenOff()
73 {
74 return false;
75 }
BeginPowerkeyScreenOff()76 virtual void BeginPowerkeyScreenOff() {};
EndPowerkeyScreenOff()77 virtual void EndPowerkeyScreenOff() {};
78 virtual void SetCoordinated([[maybe_unused]] bool coordinated) {};
79 virtual uint32_t GoToSleep([[maybe_unused]] const std::function<void()> onSuspend,
80 [[maybe_unused]] const std::function<void()> onWakeup, [[maybe_unused]] bool force)
81 {
82 return 0;
83 }
84 virtual void RegisterCallback([[maybe_unused]] std::function<void(uint32_t)>& callback) {};
85 };
86 } // namespace PowerMgr
87 } // namespace OHOS
88
89 namespace {
90 const int32_t REWIND_READ_DATA = 0;
91 } // namespace
92
PowerFuzzerTest()93 PowerFuzzerTest::PowerFuzzerTest()
94 {
95 service_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
96 service_->OnStart();
97 auto powerAction = new FuzzPowerAction();
98 auto powerState = new FuzzStateAction();
99 auto shutdownState = new FuzzStateAction();
100 auto lockAction = new RunningLockAction();
101 auto shutdownAction = new FuzzShutdownAction();
102 service_->EnableMock(powerState, shutdownState, powerAction, lockAction);
103 service_->EnableShutdownMock(shutdownAction);
104 }
105
~PowerFuzzerTest()106 PowerFuzzerTest::~PowerFuzzerTest()
107 {
108 if (service_ != nullptr) {
109 service_->OnStop();
110 service_->Reset();
111 }
112 service_ = nullptr;
113 }
114
TestPowerServiceStub(const uint32_t code,const uint8_t * data,size_t size)115 void PowerFuzzerTest::TestPowerServiceStub(const uint32_t code, const uint8_t* data, size_t size)
116 {
117 MessageParcel datas;
118 datas.WriteInterfaceToken(PowerMgrService::GetDescriptor());
119 datas.WriteBuffer(data, size);
120 datas.RewindRead(REWIND_READ_DATA);
121 MessageParcel reply;
122 MessageOption option;
123 service_->OnRemoteRequest(code, datas, reply, option);
124 }
125