• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 FuzzStateAction : public IDeviceStateAction {
45 public:
46     FuzzStateAction() = default;
47     virtual ~FuzzStateAction() = default;
Suspend(int64_t callTimeMs,SuspendDeviceType type,uint32_t flags)48     virtual void Suspend([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] SuspendDeviceType type,
49         [[maybe_unused]] uint32_t flags) {};
ForceSuspend()50     virtual void ForceSuspend() {};
Wakeup(int64_t callTimeMs,WakeupDeviceType type,const std::string & details,const std::string & pkgName)51     virtual void Wakeup([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] WakeupDeviceType type,
52         [[maybe_unused]] const std::string& details, [[maybe_unused]] const std::string& pkgName) {};
RefreshActivity(int64_t callTimeMs,UserActivityType type,uint32_t flags)53     virtual void RefreshActivity([[maybe_unused]] int64_t callTimeMs, [[maybe_unused]] UserActivityType type,
54         [[maybe_unused]] uint32_t flags) {};
GetDisplayState()55     virtual DisplayState GetDisplayState()
56     {
57         return DisplayState::DISPLAY_OFF;
58     };
SetDisplayState(DisplayState state,StateChangeReason reason=StateChangeReason::STATE_CHANGE_REASON_UNKNOWN)59     virtual uint32_t SetDisplayState([[maybe_unused]] DisplayState state,
60         [[maybe_unused]] StateChangeReason reason = StateChangeReason::STATE_CHANGE_REASON_UNKNOWN)
61     {
62         return 0;
63     }
SetCoordinated(bool coordinated)64     virtual void SetCoordinated([[maybe_unused]] bool coordinated) {};
GoToSleep(const std::function<void ()> onSuspend,const std::function<void ()> onWakeup,bool force)65     virtual uint32_t GoToSleep([[maybe_unused]] const std::function<void()> onSuspend,
66         [[maybe_unused]] const std::function<void()> onWakeup, [[maybe_unused]] bool force)
67     {
68         return 0;
69     }
RegisterCallback(std::function<void (uint32_t)> & callback)70     virtual void RegisterCallback([[maybe_unused]] std::function<void(uint32_t)>& callback) {};
71 };
72 } // namespace PowerMgr
73 } // namespace OHOS
74 
75 namespace {
76 const int32_t REWIND_READ_DATA = 0;
77 } // namespace
78 
PowerFuzzerTest()79 PowerFuzzerTest::PowerFuzzerTest()
80 {
81     service_ = DelayedSpSingleton<PowerMgrService>::GetInstance();
82     service_->OnStart();
83     auto powerAction = new FuzzPowerAction();
84     auto powerState = new FuzzStateAction();
85     auto shutdownState = new FuzzStateAction();
86     auto lockAction = new RunningLockAction();
87     service_->EnableMock(powerState, shutdownState, powerAction, lockAction);
88 }
89 
~PowerFuzzerTest()90 PowerFuzzerTest::~PowerFuzzerTest()
91 {
92     if (service_ != nullptr) {
93         service_->OnStop();
94         service_->Reset();
95     }
96     service_ = nullptr;
97 }
98 
TestPowerServiceStub(const uint32_t code,const uint8_t * data,size_t size)99 void PowerFuzzerTest::TestPowerServiceStub(const uint32_t code, const uint8_t* data, size_t size)
100 {
101     MessageParcel datas;
102     datas.WriteInterfaceToken(PowerMgrService::GetDescriptor());
103     datas.WriteBuffer(data, size);
104     datas.RewindRead(REWIND_READ_DATA);
105     MessageParcel reply;
106     MessageOption option;
107     service_->OnRemoteRequest(code, datas, reply, option);
108 }
109