Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 1 | /* |
| 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 Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | class FuzzContainer { |
| 27 | std::shared_ptr<FuzzEventHub> mFuzzEventHub; |
| 28 | sp<FuzzInputReaderPolicy> mFuzzPolicy; |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 29 | FuzzInputListener mFuzzListener; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 30 | std::unique_ptr<FuzzInputReaderContext> mFuzzContext; |
| 31 | std::unique_ptr<InputDevice> mFuzzDevice; |
| 32 | InputReaderConfiguration mPolicyConfig; |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 33 | std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp; |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 34 | |
| 35 | public: |
Aditya Wazir | eea5be0 | 2022-10-31 12:40:10 +0530 | [diff] [blame] | 36 | FuzzContainer(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(fdp) { |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 37 | // 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 Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 46 | mFuzzContext = std::make_unique<FuzzInputReaderContext>(mFuzzEventHub, mFuzzPolicy, |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 47 | mFuzzListener, mFdp); |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 48 | |
| 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 Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 61 | std::list<NotifyArgs> out; |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame^] | 62 | out += mFuzzDevice->configure(arbitraryTime, mPolicyConfig, /*changes=*/{}); |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 63 | out += mFuzzDevice->reset(arbitraryTime); |
| 64 | for (const NotifyArgs& args : out) { |
| 65 | mFuzzListener.notify(args); |
| 66 | } |
Michael Ensing | b8d9326 | 2020-05-12 00:41:30 -0700 | [diff] [blame] | 67 | } |
| 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) { |
| 78 | T& mapper = mFuzzDevice->addMapper<T>(mFdp->ConsumeIntegral<int32_t>(), args...); |
| 79 | configureDevice(); |
| 80 | return mapper; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | } // namespace android |