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 const uint32_t BATTERY_INTERFACE_STUB_FUNC_MAX_SIZE = 22;
39 } // namespace
40
BatteryStubFuzzTest(const uint8_t * data,size_t size)41 static void BatteryStubFuzzTest(const uint8_t *data, size_t size)
42 {
43 uint32_t code;
44 if (size < sizeof(code)) {
45 return;
46 }
47 if (memcpy_s(&code, sizeof(code), data, sizeof(code)) != EOK) {
48 return;
49 }
50
51 MessageParcel datas;
52 MessageParcel reply;
53 MessageOption option;
54 if (g_fuzzService == nullptr) {
55 sptr<BatteryInterfaceImpl> impl = new BatteryInterfaceImpl();
56 impl->Init();
57 g_fuzzService = make_shared<BatteryInterfaceStub>(impl);
58 }
59 for (code = CMD_BATTERY_INTERFACE_GET_VERSION; code < BATTERY_INTERFACE_STUB_FUNC_MAX_SIZE; code++) {
60 datas.WriteInterfaceToken(IBatteryInterface::GetDescriptor());
61 datas.WriteBuffer(data, size);
62 datas.RewindRead(REWIND_READ_DATA);
63 g_fuzzService->OnRemoteRequest(code, datas, reply, option);
64 }
65 }
66 } // namespace V2_0
67 } // namespace Battery
68 } // namespace HDI
69 } // namespace OHOS
70
71 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)72 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
73 {
74 /* Run your code on data */
75 OHOS::HDI::Battery::V2_0::BatteryStubFuzzTest(data, size);
76 return 0;
77 }
78