blob: b9929289f1eacd91588ac23b06809387df2c160c [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>
Michael Ensingb8d93262020-05-12 00:41:30 -070023
24namespace android {
25
26class FuzzContainer {
27 std::shared_ptr<FuzzEventHub> mFuzzEventHub;
28 sp<FuzzInputReaderPolicy> mFuzzPolicy;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070029 FuzzInputListener mFuzzListener;
Michael Ensingb8d93262020-05-12 00:41:30 -070030 std::unique_ptr<FuzzInputReaderContext> mFuzzContext;
31 std::unique_ptr<InputDevice> mFuzzDevice;
32 InputReaderConfiguration mPolicyConfig;
Aditya Wazireea5be02022-10-31 12:40:10 +053033 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp;
Michael Ensingb8d93262020-05-12 00:41:30 -070034
35public:
Aditya Wazireea5be02022-10-31 12:40:10 +053036 FuzzContainer(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(fdp) {
Michael Ensingb8d93262020-05-12 00:41:30 -070037 // Setup parameters.
38 std::string deviceName = mFdp->ConsumeRandomLengthString(16);
39 std::string deviceLocation = mFdp->ConsumeRandomLengthString(12);
40 int32_t deviceID = mFdp->ConsumeIntegralInRange<int32_t>(0, 5);
41 int32_t deviceGeneration = mFdp->ConsumeIntegralInRange<int32_t>(/*from*/ 0, /*to*/ 5);
42
43 // Create mocked objects.
44 mFuzzEventHub = std::make_shared<FuzzEventHub>(mFdp);
45 mFuzzPolicy = sp<FuzzInputReaderPolicy>::make(mFdp);
Michael Ensingb8d93262020-05-12 00:41:30 -070046 mFuzzContext = std::make_unique<FuzzInputReaderContext>(mFuzzEventHub, mFuzzPolicy,
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070047 mFuzzListener, mFdp);
Michael Ensingb8d93262020-05-12 00:41:30 -070048
49 InputDeviceIdentifier identifier;
50 identifier.name = deviceName;
51 identifier.location = deviceLocation;
52 mFuzzDevice = std::make_unique<InputDevice>(mFuzzContext.get(), deviceID, deviceGeneration,
53 identifier);
54 mFuzzPolicy->getReaderConfiguration(&mPolicyConfig);
55 }
56
57 ~FuzzContainer() {}
58
59 void configureDevice() {
60 nsecs_t arbitraryTime = mFdp->ConsumeIntegral<nsecs_t>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070061 std::list<NotifyArgs> out;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000062 out += mFuzzDevice->configure(arbitraryTime, mPolicyConfig, /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070063 out += mFuzzDevice->reset(arbitraryTime);
64 for (const NotifyArgs& args : out) {
65 mFuzzListener.notify(args);
66 }
Michael Ensingb8d93262020-05-12 00:41:30 -070067 }
68
69 void addProperty(std::string key, std::string value) {
70 mFuzzEventHub->addProperty(key, value);
71 configureDevice();
72 }
73
74 InputReaderConfiguration& getPolicyConfig() { return mPolicyConfig; }
75
76 template <class T, typename... Args>
77 T& getMapper(Args... args) {
Arpit Singha8c236b2023-04-25 13:56:05 +000078 int32_t eventhubId = mFdp->ConsumeIntegral<int32_t>();
79 // ensure a device entry exists for this eventHubId
80 mFuzzDevice->addEmptyEventHubDevice(eventhubId);
Michael Ensingb8d93262020-05-12 00:41:30 -070081 configureDevice();
Arpit Singha8c236b2023-04-25 13:56:05 +000082
83 return mFuzzDevice->template constructAndAddMapper<T>(eventhubId, args...);
Michael Ensingb8d93262020-05-12 00:41:30 -070084 }
85};
86
87} // namespace android