• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #define FUZZ_PROJECT_NAME "powermgrservice_fuzzer"
19 
20 #include "power_fuzzer.h"
21 #include "ipower_mgr.h"
22 #include "powermgrservice_fuzzer_test.h"
23 
24 using namespace OHOS::PowerMgr;
25 
26 namespace OHOS {
OnAsyncShutdown()27 void AsyncShutdownCallback::OnAsyncShutdown()
28 {
29     return;
30 }
31 
OnAsyncShutdownOrReboot(bool isReboot)32 void AsyncShutdownCallback::OnAsyncShutdownOrReboot(bool isReboot)
33 {
34     return;
35 }
36 
OnSyncShutdown()37 void SyncShutdownCallback::OnSyncShutdown()
38 {
39     return;
40 }
41 
OnSyncShutdownOrReboot(bool isReboot)42 void SyncShutdownCallback::OnSyncShutdownOrReboot(bool isReboot)
43 {
44     return;
45 }
46 
OnTakeOverHibernate(const TakeOverInfo & info)47 bool TakeOverShutdownCallback::OnTakeOverHibernate(const TakeOverInfo& info)
48 {
49     return true;
50 }
51 
OnTakeOverShutdown(const TakeOverInfo & info)52 bool TakeOverShutdownCallback::OnTakeOverShutdown(const TakeOverInfo& info)
53 {
54     return true;
55 }
56 
57 
PowerServiceFuzzTest()58 void PowerServiceFuzzTest()
59 {
60     auto g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
61     constexpr int32_t DISPLAY_POWER_MANAGER_ID = 3308;
62     const std::string TEST_DEVICE_ID = "test_device_id";
63     g_service->OnStart();
64 
65     bool clearMemory = false;
66     bool isUsed = false;
67     bool result = false;
68     bool enabled = false;
69     bool enabledLockScreen = false;
70     bool checkLock = false;
71     bool sendScreenOffEvent = false;
72     int64_t callTimeMs  = 0;
73     int32_t timeOutMs  = 0;
74     int32_t powerError  = 0;
75     int32_t lockType  = 0;
76     int32_t reasonValue  = 0;
77     const std::string apiVersion = "-1";
78     sptr<PowerMgrStubAsync> asyncCallback = new PowerMgrStubAsync();
79     sptr<IPowerMgrAsync> powerProxy = iface_cast<IPowerMgrAsync>(asyncCallback);
80     const sptr<IRemoteObject> remoteObj = new RunningLockTokenStub();
81     const sptr<IRemoteObject> token = new RunningLockTokenStub();
82     const std::vector<int32_t> workSources;
83     const RunningLockInfo runningLockInfo("fuzztest", RunningLockType::RUNNINGLOCK_SCREEN);
84 
85     g_service->WakeupDeviceAsyncIpc(callTimeMs, reasonValue, "", apiVersion);
86 
87     g_service->ForceSuspendDeviceIpc(callTimeMs, apiVersion, powerProxy);
88     g_service->HibernateIpc(clearMemory, "", apiVersion, powerProxy);
89     g_service->CreateRunningLockIpc(remoteObj, runningLockInfo, powerError);
90     g_service->ReleaseRunningLockIpc(remoteObj, "");
91 
92     g_service->UpdateWorkSourceIpc(remoteObj, workSources);
93     g_service->LockIpc(remoteObj, timeOutMs, powerError);
94     g_service->UnLockIpc(remoteObj, "", powerError);
95 
96     g_service->IsUsedIpc(remoteObj, isUsed);
97 
98     g_service->SetForceTimingOutIpc(enabled, token, powerError);
99     g_service->LockScreenAfterTimingOutIpc(
100         enabledLockScreen, checkLock, sendScreenOffEvent, token, powerError);
101     g_service->IsRunningLockEnabledIpc(lockType, result, powerError);
102 }
103 
PowerServiceCallbackFuzzTest()104 void PowerServiceCallbackFuzzTest()
105 {
106     auto g_service = DelayedSpSingleton<PowerMgrService>::GetInstance();
107     constexpr int32_t DISPLAY_POWER_MANAGER_ID = 3308;
108     const std::string TEST_DEVICE_ID = "test_device_id";
109     g_service->OnStart();
110 
111     bool isSync = false;
112     int32_t remainTime  = 0;
113     int32_t priorityValue  = 0;
114     const sptr<ITakeOverShutdownCallback> takeOverShutdownCallback = new TakeOverShutdownCallback();
115     const sptr<IAsyncShutdownCallback> asyncShutdownCallback = new AsyncShutdownCallback();
116     const sptr<ISyncShutdownCallback> syncShutdownCallback = new SyncShutdownCallback();
117     const sptr<IPowerRunninglockCallback> runningLockCallback = new PowerRunningLockTestCallback();
118     sptr<IPowerStateCallback> stateCallback = new PowerStateTestCallback();
119     const sptr<ISyncSleepCallback> sleepCallback = new PowerSyncSleepTest1Callback();
120     const sptr<ISyncHibernateCallback> hibernateCallback = new PowerSyncHibernateTest1Callback();
121     sptr<IPowerModeCallback> modeCallback = new PowerModeTestCallback();
122     const sptr<IScreenOffPreCallback> screenOffPreCallback = new ScreenOffPreTestCallback();
123 
124     g_service->RegisterShutdownCallbackIpc(takeOverShutdownCallback, priorityValue);
125     g_service->UnRegisterShutdownCallbackIpc(takeOverShutdownCallback);
126     g_service->RegisterShutdownCallbackIpc(asyncShutdownCallback, priorityValue);
127     g_service->UnRegisterShutdownCallbackIpc(asyncShutdownCallback);
128     g_service->RegisterShutdownCallbackIpc(syncShutdownCallback, priorityValue);
129     g_service->UnRegisterShutdownCallbackIpc(syncShutdownCallback);
130 
131     g_service->RegisterPowerStateCallbackIpc(stateCallback, isSync);
132     g_service->UnRegisterPowerStateCallbackIpc(stateCallback);
133     g_service->RegisterSyncSleepCallbackIpc(sleepCallback, priorityValue);
134     g_service->UnRegisterSyncSleepCallbackIpc(sleepCallback);
135     g_service->RegisterSyncHibernateCallbackIpc(hibernateCallback);
136     g_service->UnRegisterSyncHibernateCallbackIpc(hibernateCallback);
137     g_service->RegisterPowerModeCallbackIpc(modeCallback);
138     g_service->UnRegisterPowerModeCallbackIpc(modeCallback);
139     g_service->RegisterScreenStateCallbackIpc(remainTime, screenOffPreCallback);
140     g_service->UnRegisterScreenStateCallbackIpc(screenOffPreCallback);
141 
142     g_service->RegisterRunningLockCallbackIpc(runningLockCallback);
143     g_service->UnRegisterRunningLockCallbackIpc(runningLockCallback);
144 }
145 }
146 
147 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)148 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
149 {
150     /* Run your code on data */
151     OHOS::PowerServiceFuzzTest();
152     OHOS::PowerServiceCallbackFuzzTest();
153     PowerFuzzerTest g_serviceTest;
154     g_serviceTest.TestPowerServiceStub(static_cast<uint32_t>(IPowerMgrIpcCode::COMMAND_SHELL_DUMP_IPC), data, size);
155     return 0;
156 }
157 
158