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 "securec.h"
17 #include <cstdint>
18 #include <cstdlib>
19 #include <memory>
20
21 #include "battery_interface_impl.h"
22 #include "v2_0/battery_interface_proxy.h"
23 #include "v2_0/battery_interface_stub.h"
24 #include "v2_0/ibattery_callback.h"
25 #include "v2_0/types.h"
26
27 using namespace OHOS::HDI::Battery::V2_0;
28 using namespace HDI::Battery;
29 using namespace std;
30
31 namespace OHOS {
32 namespace HDI {
33 namespace Battery {
34 namespace V2_0 {
35 namespace {
36 const int32_t REWIND_READ_DATA = 0;
37 shared_ptr<BatteryInterfaceStub> g_fuzzService = nullptr;
38 } // namespace
39
BatteryStubFuzzTest(const uint8_t * data,size_t size)40 static void BatteryStubFuzzTest(const uint8_t *data, size_t size)
41 {
42 uint32_t code;
43 if (size < sizeof(code)) {
44 return;
45 }
46 if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) {
47 return;
48 }
49
50 MessageParcel datas;
51 datas.WriteInterfaceToken(IBatteryInterface::GetDescriptor());
52 datas.WriteBuffer(data, size);
53 datas.RewindRead(REWIND_READ_DATA);
54 MessageParcel reply;
55 MessageOption option;
56 if (g_fuzzService == nullptr) {
57 sptr<BatteryInterfaceImpl> impl = new BatteryInterfaceImpl();
58 impl->Init();
59 g_fuzzService = make_shared<BatteryInterfaceStub>(impl);
60 }
61 g_fuzzService->OnRemoteRequest(code, datas, reply, option);
62 }
63 } // namespace V2_0
64 } // namespace Battery
65 } // namespace HDI
66 } // namespace OHOS
67
68 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)69 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
70 {
71 /* Run your code on data */
72 OHOS::HDI::Battery::V2_0::BatteryStubFuzzTest(data, size);
73 return 0;
74 }
75