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 enforced by applicable law, 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 "subscribelongpressevent_fuzzer.h"
17
18 #include "multimodal_input_connect_stub.h"
19 #include "mmi_service.h"
20 #include "mmi_log.h"
21 #include "long_press_event.h"
22 #include <fuzzer/FuzzedDataProvider.h>
23
24 #undef LOG_TAG
25 #define LOG_TAG "SubscribeLongPressEventFuzzTest"
26
27 namespace OHOS {
28 namespace MMI {
29
30 namespace {
31 const std::u16string kInterfaceToken{u"ohos.multimodalinput.IConnectManager"};
32 constexpr int32_t kMinSubscribeId = std::numeric_limits<int32_t>::min();
33 constexpr int32_t kMaxSubscribeId = std::numeric_limits<int32_t>::max();
34 constexpr int32_t kMinFingerCount = 1;
35 constexpr int32_t kMaxFingerCount = 10;
36 constexpr int32_t kMinDurationMs = 0;
37 constexpr int32_t kMaxDurationMs = 30000;
38 } // namespace
39
SubscribeLongPressEventFuzzTest(FuzzedDataProvider & provider)40 bool SubscribeLongPressEventFuzzTest(FuzzedDataProvider &provider)
41 {
42 const int32_t subscribeId = provider.ConsumeIntegralInRange<int32_t>(kMinSubscribeId, kMaxSubscribeId);
43 LongPressRequest req;
44 req.fingerCount = provider.ConsumeIntegralInRange<int32_t>(kMinFingerCount, kMaxFingerCount);
45 req.duration = provider.ConsumeIntegralInRange<int32_t>(kMinDurationMs, kMaxDurationMs);
46
47 MessageParcel datas;
48 if (!datas.WriteInterfaceToken(kInterfaceToken)) {
49 return false;
50 }
51 if (!datas.WriteInt32(subscribeId)) {
52 return false;
53 }
54 if (!datas.WriteParcelable(&req)) {
55 return false;
56 }
57 if (!datas.RewindRead(0)) {
58 return false;
59 }
60 (void)provider.ConsumeRemainingBytes<uint8_t>();
61
62 MessageParcel reply;
63 MessageOption option;
64 MMIService::GetInstance()->state_ = ServiceRunningState::STATE_RUNNING;
65
66 (void)MMIService::GetInstance()->OnRemoteRequest(
67 static_cast<uint32_t>(IMultimodalInputConnectIpcCode::COMMAND_SUBSCRIBE_LONG_PRESS_EVENT),
68 datas, reply, option);
69 return true;
70 }
71
72 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)73 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
74 {
75 if (!data || size == 0) {
76 return 0;
77 }
78 FuzzedDataProvider provider(data, size);
79 (void)OHOS::MMI::SubscribeLongPressEventFuzzTest(provider);
80 return 0;
81 }
82 } // namespace MMI
83 } // namespace OHOS