Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / third_party / betocore / np_adv / fuzz / src / lib.rs
lib.rs38 lines · 1.4 KiBRaw
| 1 | // Copyright 2024 Google LLC |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | use arbitrary::Unstructured; |
| 16 | use np_adv::extended::data_elements::ActionId; |
| 17 | |
| 18 | #[derive(arbitrary::Arbitrary, Clone, Debug)] |
| 19 | pub struct FuzzInput { |
| 20 | #[arbitrary(with = arbitrary_action_ids)] |
| 21 | pub data: [ActionId; 64], |
| 22 | #[arbitrary(with = arbitrary_actions_count)] |
| 23 | pub count: usize, |
| 24 | } |
| 25 | |
| 26 | fn arbitrary_actions_count(u: &mut Unstructured) -> arbitrary::Result<usize> { |
| 27 | u.int_in_range(0..=64).map(|val| usize::try_from(val).unwrap()) |
| 28 | } |
| 29 | |
| 30 | fn arbitrary_action_ids(u: &mut Unstructured) -> arbitrary::Result<[ActionId; 64]> { |
| 31 | Ok(std::array::from_fn(|_| { |
| 32 | let next = u16::try_from( |
| 33 | u.int_in_range(0..=2047).expect("fuzzer should generate enough data for a u16"), |
| 34 | ) |
| 35 | .expect("the range will always be a valid u16"); |
| 36 | ActionId::try_from(next).expect("rang is valid for action_ids") |
| 37 | })) |
| 38 | } |