blob: e76bd728b817e63a91802263581d2cda4686ae53 [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#include <FuzzContainer.h>
18#include <SwitchInputMapper.h>
19#include <fuzzer/FuzzedDataProvider.h>
20
21namespace android {
22
23extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size) {
24 std::shared_ptr<FuzzedDataProvider> fdp = std::make_shared<FuzzedDataProvider>(data, size);
25 FuzzContainer fuzzer(fdp);
26
27 SwitchInputMapper& mapper = fuzzer.getMapper<SwitchInputMapper>();
28 auto policyConfig = fuzzer.getPolicyConfig();
29
30 // Loop through mapper operations until randomness is exhausted.
31 while (fdp->remaining_bytes() > 0) {
32 fdp->PickValueInArray<std::function<void()>>({
33 [&]() -> void {
34 std::string dump;
35 mapper.dump(dump);
36 },
37 [&]() -> void { mapper.getSources(); },
38 [&]() -> void {
39 int32_t type = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidTypes)
40 : fdp->ConsumeIntegral<int32_t>();
41 int32_t code = fdp->ConsumeBool() ? fdp->PickValueInArray(kValidCodes)
42 : fdp->ConsumeIntegral<int32_t>();
43 RawEvent rawEvent{fdp->ConsumeIntegral<nsecs_t>(),
44 fdp->ConsumeIntegral<nsecs_t>(),
45 fdp->ConsumeIntegral<int32_t>(),
46 type,
47 code,
48 fdp->ConsumeIntegral<int32_t>()};
49 mapper.process(&rawEvent);
50 },
51 [&]() -> void {
52 mapper.getSwitchState(fdp->ConsumeIntegral<uint32_t>(),
53 fdp->ConsumeIntegral<int32_t>());
54 },
55 })();
56 }
57
58 return 0;
59}
60
61} // namespace android