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 "battery_fuzzer_test.h"
19
20 #include <cstddef>
21 #include <cstdint>
22
23 #include "message_parcel.h"
24 #include "securec.h"
25
26 using namespace OHOS::PowerMgr;
27 using namespace OHOS;
28
29 namespace {
30 const int32_t REWIND_READ_DATA = 0;
31 } // namespace
32
BatteryFuzzerTest()33 BatteryFuzzerTest::BatteryFuzzerTest()
34 {
35 service_ = DelayedSpSingleton<BatteryService>::GetInstance();
36 service_->OnStart();
37 }
38
~BatteryFuzzerTest()39 BatteryFuzzerTest::~BatteryFuzzerTest()
40 {
41 if (service_ != nullptr) {
42 service_->OnStop();
43 }
44 service_ = nullptr;
45 }
46
TestBatteryService()47 void BatteryFuzzerTest::TestBatteryService()
48 {
49 service_->MockUnplugged();
50 service_->IsMockUnplugged();
51 int32_t capacity = 100;
52 service_->MockCapacity(capacity);
53 service_->IsMockCapacity();
54 service_->MockUevent("");
55 service_->Reset();
56 }
57
TestBatteryServiceStub(const uint32_t code,const uint8_t * data,size_t size)58 void BatteryFuzzerTest::TestBatteryServiceStub(const uint32_t code, const uint8_t* data, size_t size)
59 {
60 MessageParcel datas;
61 datas.WriteInterfaceToken(BatteryService::GetDescriptor());
62 datas.WriteBuffer(data, size);
63 datas.RewindRead(REWIND_READ_DATA);
64 MessageParcel reply;
65 MessageOption option;
66 service_->OnRemoteRequest(code, datas, reply, option);
67 }
68