• 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 #include "display_fuzzer.h"
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "message_parcel.h"
22 #include "securec.h"
23 #include "iremote_object.h"
24 #include "display_brightness_callback_stub.h"
25 #include "idisplay_power_mgr.h"
26 
27 using namespace OHOS::DisplayPowerMgr;
28 using namespace OHOS::PowerMgr;
29 using namespace OHOS;
30 
31 namespace {
32 const int32_t REWIND_READ_DATA = 0;
33 const int32_t SLEEP_TIME = 5;
34 } // namespace
35 
DisplayFuzzerTest()36 DisplayFuzzerTest::DisplayFuzzerTest()
37 {
38     service_ = DelayedSpSingleton<DisplayPowerMgrService>::GetInstance();
39     service_->Init();
40 }
41 
~DisplayFuzzerTest()42 DisplayFuzzerTest::~DisplayFuzzerTest()
43 {
44     if (service_ != nullptr) {
45         sleep(SLEEP_TIME);
46         service_->Deinit();
47         service_->Reset();
48     }
49     service_ = nullptr;
50 }
51 
OnDisplayStateChanged(uint32_t displayId,DisplayPowerMgr::DisplayState state,uint32_t reason)52 void DisplayPowerMgrTestCallback::OnDisplayStateChanged(
53     uint32_t displayId, DisplayPowerMgr::DisplayState state, uint32_t reason)
54 {
55     DISPLAY_HILOGI(LABEL_TEST, "DisplayPowerMgrTestCallback::OnDisplayStateChangedStub");
56 }
57 
TestDisplayServiceStub(const uint32_t code,const uint8_t * data,size_t size)58 void DisplayFuzzerTest::TestDisplayServiceStub(const uint32_t code, const uint8_t* data, size_t size)
59 {
60     MessageParcel datas;
61     datas.WriteInterfaceToken(DisplayPowerMgrService::GetDescriptor());
62     datas.WriteBuffer(data, size);
63     datas.RewindRead(REWIND_READ_DATA);
64     if (code == static_cast<uint32_t>(IDisplayPowerMgrIpcCode::COMMAND_NOTIFY_SCREEN_POWER_STATUS)) {
65         constexpr uint32_t displayId = 0;
66         constexpr uint32_t screenPowerStatus = 3; // 3 represent the POWER_STATUS_OFF
67         datas.WriteUint32(displayId);
68         datas.WriteUint32(screenPowerStatus);
69     }
70     if (code == static_cast<uint32_t>(IDisplayPowerMgrIpcCode::COMMAND_REGISTER_CALLBACK)) {
71         sptr<IDisplayPowerCallback> obj = new DisplayPowerMgrTestCallback();
72         datas.WriteRemoteObject(obj->AsObject());
73     }
74     if (code == static_cast<uint32_t>(IDisplayPowerMgrIpcCode::COMMAND_SET_LIGHT_BRIGHTNESS_THRESHOLD)) {
75         sptr<IDisplayBrightnessCallback> obj = new DisplayBrightnessCallbackStub();
76         datas.WriteRemoteObject(obj->AsObject());
77     }
78     MessageParcel reply;
79     MessageOption option;
80     service_->OnRemoteRequest(code, datas, reply, option);
81 }
82