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 24 namespace android { 25 26 class FuzzContainer { 27 std::shared_ptr<FuzzEventHub> mFuzzEventHub; 28 sp<FuzzInputReaderPolicy> mFuzzPolicy; 29 FuzzInputListener mFuzzListener; 30 std::unique_ptr<FuzzInputReaderContext> mFuzzContext; 31 std::unique_ptr<InputDevice> mFuzzDevice; 32 InputReaderConfiguration mPolicyConfig; 33 std::shared_ptr<ThreadSafeFuzzedDataProvider> mFdp; 34 35 public: FuzzContainer(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp)36 FuzzContainer(std::shared_ptr<ThreadSafeFuzzedDataProvider> fdp) : mFdp(fdp) { 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); 46 mFuzzContext = std::make_unique<FuzzInputReaderContext>(mFuzzEventHub, mFuzzPolicy, 47 mFuzzListener, mFdp); 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 ~FuzzContainer()57 ~FuzzContainer() {} 58 configureDevice()59 void configureDevice() { 60 nsecs_t arbitraryTime = mFdp->ConsumeIntegral<nsecs_t>(); 61 std::list<NotifyArgs> out; 62 out += mFuzzDevice->configure(arbitraryTime, mPolicyConfig, /*changes=*/{}); 63 out += mFuzzDevice->reset(arbitraryTime); 64 for (const NotifyArgs& args : out) { 65 mFuzzListener.notify(args); 66 } 67 } 68 addProperty(std::string key,std::string value)69 void addProperty(std::string key, std::string value) { 70 mFuzzEventHub->addProperty(key, value); 71 configureDevice(); 72 } 73 getPolicyConfig()74 InputReaderConfiguration& getPolicyConfig() { return mPolicyConfig; } 75 76 template <class T, typename... Args> getMapper(Args...args)77 T& getMapper(Args... args) { 78 int32_t eventhubId = mFdp->ConsumeIntegral<int32_t>(); 79 // ensure a device entry exists for this eventHubId 80 mFuzzDevice->addEmptyEventHubDevice(eventhubId); 81 configureDevice(); 82 83 return mFuzzDevice->template constructAndAddMapper<T>(eventhubId, args...); 84 } 85 }; 86 87 } // namespace android 88