Michael Ensing | 7c58e65 | 2020-05-12 00:41:30 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2020 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 | int32_t meventID; |
| 29 | std::shared_ptr<FuzzEventHub> mFuzzEventHub; |
| 30 | sp<FuzzInputReaderPolicy> mFuzzPolicy; |
| 31 | sp<FuzzInputListener> mFuzzListener; |
| 32 | std::unique_ptr<FuzzInputReaderContext> mFuzzContext; |
| 33 | std::unique_ptr<InputDevice> mFuzzDevice; |
| 34 | InputReaderConfiguration mPolicyConfig; |
| 35 | std::shared_ptr<FuzzedDataProvider> fdp; |
| 36 | |
| 37 | public: |
| 38 | FuzzContainer(std::shared_ptr<FuzzedDataProvider> fdp) : fdp(fdp) { |
| 39 | // Setup parameters. |
| 40 | std::string deviceName = fdp->ConsumeRandomLengthString(16); |
| 41 | std::string deviceLocation = fdp->ConsumeRandomLengthString(12); |
| 42 | int32_t deviceID = fdp->ConsumeIntegralInRange<int32_t>(0, 5); |
| 43 | int32_t deviceGeneration = fdp->ConsumeIntegralInRange<int32_t>(0, 5); |
| 44 | meventID = fdp->ConsumeIntegral<int32_t>(); |
| 45 | |
| 46 | // Create mocked objects. |
| 47 | mFuzzEventHub = std::make_shared<FuzzEventHub>(fdp); |
| 48 | mFuzzPolicy = new FuzzInputReaderPolicy(fdp); |
| 49 | mFuzzListener = new FuzzInputListener(); |
| 50 | mFuzzContext = std::make_unique<FuzzInputReaderContext>(mFuzzEventHub, mFuzzPolicy, |
| 51 | mFuzzListener, fdp); |
| 52 | |
| 53 | InputDeviceIdentifier identifier; |
| 54 | identifier.name = deviceName; |
| 55 | identifier.location = deviceLocation; |
| 56 | mFuzzDevice = std::make_unique<InputDevice>(mFuzzContext.get(), deviceID, deviceGeneration, |
| 57 | identifier); |
| 58 | mFuzzPolicy->getReaderConfiguration(&mPolicyConfig); |
| 59 | } |
| 60 | |
| 61 | ~FuzzContainer() {} |
| 62 | |
| 63 | void configureDevice() { |
| 64 | nsecs_t arbitraryTime = fdp->ConsumeIntegral<nsecs_t>(); |
| 65 | mFuzzDevice->configure(arbitraryTime, &mPolicyConfig, 0); |
| 66 | mFuzzDevice->reset(arbitraryTime); |
| 67 | } |
| 68 | |
| 69 | void addProperty(const String8& key, const String8& 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>(fdp->ConsumeIntegral<int32_t>(), args...); |
| 79 | configureDevice(); |
| 80 | return mapper; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | } // namespace android |