Dylan Katz | c247e4a | 2020-06-10 16:21:39 -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 | #include <functional> |
| 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include <utils/String8.h> |
| 22 | |
| 23 | #include "fuzzer/FuzzedDataProvider.h" |
| 24 | #include "mediautils/BatteryNotifier.h" |
| 25 | |
| 26 | static constexpr int kMaxOperations = 30; |
| 27 | static constexpr int kMaxStringLength = 500; |
| 28 | using android::BatteryNotifier; |
| 29 | |
| 30 | std::vector<std::function<void(std::string /*flashlight_name*/, std::string /*camera_name*/, |
| 31 | uid_t /*video_id*/, uid_t /*audio_id*/, uid_t /*light_id*/, |
| 32 | uid_t /*camera_id*/)>> |
| 33 | operations = { |
| 34 | [](std::string, std::string, uid_t, uid_t, uid_t, uid_t) -> void { |
| 35 | BatteryNotifier::getInstance().noteResetVideo(); |
| 36 | }, |
| 37 | [](std::string, std::string, uid_t, uid_t, uid_t, uid_t) -> void { |
| 38 | BatteryNotifier::getInstance().noteResetAudio(); |
| 39 | }, |
| 40 | [](std::string, std::string, uid_t, uid_t, uid_t, uid_t) -> void { |
| 41 | BatteryNotifier::getInstance().noteResetFlashlight(); |
| 42 | }, |
| 43 | [](std::string, std::string, uid_t, uid_t, uid_t, uid_t) -> void { |
| 44 | BatteryNotifier::getInstance().noteResetCamera(); |
| 45 | }, |
| 46 | [](std::string, std::string, uid_t video_id, uid_t, uid_t, uid_t) -> void { |
| 47 | BatteryNotifier::getInstance().noteStartVideo(video_id); |
| 48 | }, |
| 49 | [](std::string, std::string, uid_t video_id, uid_t, uid_t, uid_t) -> void { |
| 50 | BatteryNotifier::getInstance().noteStopVideo(video_id); |
| 51 | }, |
| 52 | [](std::string, std::string, uid_t, uid_t audio_id, uid_t, uid_t) -> void { |
| 53 | BatteryNotifier::getInstance().noteStartAudio(audio_id); |
| 54 | }, |
| 55 | [](std::string, std::string, uid_t, uid_t audio_id, uid_t, uid_t) -> void { |
| 56 | BatteryNotifier::getInstance().noteStopAudio(audio_id); |
| 57 | }, |
| 58 | [](std::string flashlight_name, std::string, uid_t, uid_t, uid_t light_id, uid_t) -> void { |
| 59 | android::String8 name(flashlight_name.c_str()); |
| 60 | BatteryNotifier::getInstance().noteFlashlightOn(name, light_id); |
| 61 | }, |
| 62 | [](std::string flashlight_name, std::string, uid_t, uid_t, uid_t light_id, uid_t) -> void { |
| 63 | android::String8 name(flashlight_name.c_str()); |
| 64 | BatteryNotifier::getInstance().noteFlashlightOff(name, light_id); |
| 65 | }, |
| 66 | [](std::string, std::string camera_name, uid_t, uid_t, uid_t, uid_t camera_id) -> void { |
| 67 | android::String8 name(camera_name.c_str()); |
| 68 | BatteryNotifier::getInstance().noteStartCamera(name, camera_id); |
| 69 | }, |
| 70 | [](std::string, std::string camera_name, uid_t, uid_t, uid_t, uid_t camera_id) -> void { |
| 71 | android::String8 name(camera_name.c_str()); |
| 72 | BatteryNotifier::getInstance().noteStopCamera(name, camera_id); |
| 73 | }, |
| 74 | }; |
| 75 | |
| 76 | extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { |
| 77 | FuzzedDataProvider data_provider(data, size); |
| 78 | std::string camera_name = data_provider.ConsumeRandomLengthString(kMaxStringLength); |
| 79 | std::string flashlight_name = data_provider.ConsumeRandomLengthString(kMaxStringLength); |
| 80 | uid_t video_id = data_provider.ConsumeIntegral<uid_t>(); |
| 81 | uid_t audio_id = data_provider.ConsumeIntegral<uid_t>(); |
| 82 | uid_t light_id = data_provider.ConsumeIntegral<uid_t>(); |
| 83 | uid_t camera_id = data_provider.ConsumeIntegral<uid_t>(); |
| 84 | size_t ops_run = 0; |
| 85 | while (data_provider.remaining_bytes() > 0 && ops_run++ < kMaxOperations) { |
| 86 | uint8_t op = data_provider.ConsumeIntegralInRange<uint8_t>(0, operations.size() - 1); |
| 87 | operations[op](flashlight_name, camera_name, video_id, audio_id, light_id, camera_id); |
| 88 | } |
| 89 | return 0; |
| 90 | } |