blob: ac2030afd3005f8d1f04fc87b796362e97a9ddd8 [file] [log] [blame]
Michael Ensingb8d93262020-05-12 00:41:30 -07001/*
2 * Copyright 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Harry Cuttsf61c0472023-07-10 14:45:15 +000017#include <InputDevice.h>
Harry Cuttse36d8352023-07-10 13:28:45 +000018#include <InputReaderBase.h>
Harry Cutts656e0ad2023-06-16 16:39:55 +000019#include <MapperHelpers.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070020#include <SwitchInputMapper.h>
Michael Ensingb8d93262020-05-12 00:41:30 -070021
22namespace android {
23
24extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
Aditya Wazireea5be02022-10-31 12:40:10 +053025 std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp =
26 std::make_shared<ThreadSafeFuzzedDataProvider>(data, size);
Michael Ensingb8d93262020-05-12 00:41:30 -070027
Harry Cuttsf61c0472023-07-10 14:45:15 +000028 // Create mocked objects to support the fuzzed input mapper.
29 std::shared_ptr<FuzzEventHub> eventHub = std::make_shared<FuzzEventHub>(fdp);
30 FuzzInputReaderContext context(eventHub, fdp);
31 InputDevice device = getFuzzedInputDevice(*fdp, &context);
32
33 SwitchInputMapper& mapper =
34 getMapperForDevice<ThreadSafeFuzzedDataProvider,
35 SwitchInputMapper>(*fdp.get(), device, InputReaderConfiguration{});
Michael Ensingb8d93262020-05-12 00:41:30 -070036
37 // Loop through mapper operations until randomness is exhausted.
38 while (fdp->remaining_bytes() > 0) {
39 fdp->PickValueInArray<std::function<void()>>({
40 [&]() -> void {
41 std::string dump;
42 mapper.dump(dump);
43 },
44 [&]() -> void { mapper.getSources(); },
45 [&]() -> void {
Harry Cutts656e0ad2023-06-16 16:39:55 +000046 RawEvent rawEvent = getFuzzedRawEvent(*fdp);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070047 std::list<NotifyArgs> unused = mapper.process(&rawEvent);
Michael Ensingb8d93262020-05-12 00:41:30 -070048 },
49 [&]() -> void {
50 mapper.getSwitchState(fdp->ConsumeIntegral<uint32_t>(),
51 fdp->ConsumeIntegral<int32_t>());
52 },
53 })();
54 }
55
56 return 0;
57}
58
59} // namespace android