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> |
| 23 | #include <fuzzer/FuzzedDataProvider.h> |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | class FuzzContainer { |
| 28 | std::shared_ptr<FuzzEventHub> mFuzzEventHub; |
| 29 | sp<FuzzInputReaderPolicy> mFuzzPolicy; |
| 30 | std::unique_ptr<FuzzInputListener> mFuzzListener; |
| 31 | std::unique_ptr<FuzzInputReaderContext> mFuzzContext; |
| 32 | std::unique_ptr<InputDevice> mFuzzDevice; |
| 33 | InputReaderConfiguration mPolicyConfig; |
| 34 | std::shared_ptr<FuzzedDataProvider> mFdp; |
| 35 | |
| 36 | public: |
| 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); |
| 47 | mFuzzListener = std::make_unique<FuzzInputListener>(); |
| 48 | mFuzzContext = std::make_unique<FuzzInputReaderContext>(mFuzzEventHub, mFuzzPolicy, |
| 49 | *mFuzzListener, mFdp); |
| 50 | |
| 51 | InputDeviceIdentifier identifier; |
| 52 | identifier.name = deviceName; |
| 53 | identifier.location = deviceLocation; |
| 54 | mFuzzDevice = std::make_unique<InputDevice>(mFuzzContext.get(), deviceID, deviceGeneration, |
| 55 | identifier); |
| 56 | mFuzzPolicy->getReaderConfiguration(&mPolicyConfig); |
| 57 | } |
| 58 | |
| 59 | ~FuzzContainer() {} |
| 60 | |
| 61 | void configureDevice() { |
| 62 | nsecs_t arbitraryTime = mFdp->ConsumeIntegral<nsecs_t>(); |
| 63 | mFuzzDevice->configure(arbitraryTime, &mPolicyConfig, 0); |
| 64 | mFuzzDevice->reset(arbitraryTime); |
| 65 | } |
| 66 | |
| 67 | void addProperty(std::string key, std::string value) { |
| 68 | mFuzzEventHub->addProperty(key, value); |
| 69 | configureDevice(); |
| 70 | } |
| 71 | |
| 72 | InputReaderConfiguration& getPolicyConfig() { return mPolicyConfig; } |
| 73 | |
| 74 | template <class T, typename... Args> |
| 75 | T& getMapper(Args... args) { |
| 76 | T& mapper = mFuzzDevice->addMapper<T>(mFdp->ConsumeIntegral<int32_t>(), args...); |
| 77 | configureDevice(); |
| 78 | return mapper; |
| 79 | } |
| 80 | }; |
| 81 | |
| 82 | } // namespace android |