blob: 1e0764f10ffb57ba4b01a44aea4324191c376815 [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
17#pragma once
18
19#include <InputDevice.h>
20#include <InputMapper.h>
21#include <InputReader.h>
22#include <MapperHelpers.h>
23#include <fuzzer/FuzzedDataProvider.h>
24
25namespace android {
26
27class FuzzContainer {
28 std::shared_ptr<FuzzEventHub> mFuzzEventHub;
29 sp<FuzzInputReaderPolicy> mFuzzPolicy;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070030 FuzzInputListener mFuzzListener;
Michael Ensingb8d93262020-05-12 00:41:30 -070031 std::unique_ptr<FuzzInputReaderContext> mFuzzContext;
32 std::unique_ptr<InputDevice> mFuzzDevice;
33 InputReaderConfiguration mPolicyConfig;
34 std::shared_ptr<FuzzedDataProvider> mFdp;
35
36public:
37 FuzzContainer(std::shared_ptr<FuzzedDataProvider> fdp) : mFdp(fdp) {
38 // Setup parameters.
39 std::string deviceName = mFdp->ConsumeRandomLengthString(16);
40 std::string deviceLocation = mFdp->ConsumeRandomLengthString(12);
41 int32_t deviceID = mFdp->ConsumeIntegralInRange<int32_t>(0, 5);
42 int32_t deviceGeneration = mFdp->ConsumeIntegralInRange<int32_t>(/*from*/ 0, /*to*/ 5);
43
44 // Create mocked objects.
45 mFuzzEventHub = std::make_shared<FuzzEventHub>(mFdp);
46 mFuzzPolicy = sp<FuzzInputReaderPolicy>::make(mFdp);
Michael Ensingb8d93262020-05-12 00:41:30 -070047 mFuzzContext = std::make_unique<FuzzInputReaderContext>(mFuzzEventHub, mFuzzPolicy,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070048 mFuzzListener, mFdp);
Michael Ensingb8d93262020-05-12 00:41:30 -070049
50 InputDeviceIdentifier identifier;
51 identifier.name = deviceName;
52 identifier.location = deviceLocation;
53 mFuzzDevice = std::make_unique<InputDevice>(mFuzzContext.get(), deviceID, deviceGeneration,
54 identifier);
55 mFuzzPolicy->getReaderConfiguration(&mPolicyConfig);
56 }
57
58 ~FuzzContainer() {}
59
60 void configureDevice() {
61 nsecs_t arbitraryTime = mFdp->ConsumeIntegral<nsecs_t>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070062 std::list<NotifyArgs> out;
63 out += mFuzzDevice->configure(arbitraryTime, &mPolicyConfig, 0);
64 out += mFuzzDevice->reset(arbitraryTime);
65 for (const NotifyArgs& args : out) {
66 mFuzzListener.notify(args);
67 }
Michael Ensingb8d93262020-05-12 00:41:30 -070068 }
69
70 void addProperty(std::string key, std::string value) {
71 mFuzzEventHub->addProperty(key, value);
72 configureDevice();
73 }
74
75 InputReaderConfiguration& getPolicyConfig() { return mPolicyConfig; }
76
77 template <class T, typename... Args>
78 T& getMapper(Args... args) {
79 T& mapper = mFuzzDevice->addMapper<T>(mFdp->ConsumeIntegral<int32_t>(), args...);
80 configureDevice();
81 return mapper;
82 }
83};
84
85} // namespace android