|
| 1 | +// This file is part of Substrate. |
| 2 | + |
| 3 | +// Copyright (C) 2021 Parity Technologies (UK) Ltd. |
| 4 | +// SPDX-License-Identifier: Apache-2.0 |
| 5 | + |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | + |
| 18 | +//! # Running |
| 19 | +//! Running this fuzzer can be done with `cargo hfuzz run bags-list`. `honggfuzz` CLI options can |
| 20 | +//! be used by setting `HFUZZ_RUN_ARGS`, such as `-n 4` to use 4 threads. |
| 21 | +//! |
| 22 | +//! # Debugging a panic |
| 23 | +//! Once a panic is found, it can be debugged with |
| 24 | +//! `cargo hfuzz run-debug fixed_point hfuzz_workspace/bags_list/*.fuzz`. |
| 25 | +//! |
| 26 | +//! # More information |
| 27 | +//! More information about `honggfuzz` can be found |
| 28 | +//! [here](https://docs.rs/honggfuzz/). |
| 29 | +
|
| 30 | +use frame_election_provider_support::{SortedListProvider, VoteWeight}; |
| 31 | +use honggfuzz::fuzz; |
| 32 | +use pallet_bags_list::mock::{AccountId, BagsList, ExtBuilder}; |
| 33 | +use std::convert::From; |
| 34 | + |
| 35 | +const ID_RANGE: AccountId = 25_000; |
| 36 | + |
| 37 | +/// Actions of a `SortedListProvider` that we fuzz. |
| 38 | +enum Action { |
| 39 | + Insert, |
| 40 | + Update, |
| 41 | + Remove, |
| 42 | +} |
| 43 | + |
| 44 | +impl From<u32> for Action { |
| 45 | + fn from(v: u32) -> Self { |
| 46 | + let num_variants = Self::Remove as u32 + 1; |
| 47 | + match v % num_variants { |
| 48 | + _x if _x == Action::Insert as u32 => Action::Insert, |
| 49 | + _x if _x == Action::Update as u32 => Action::Update, |
| 50 | + _x if _x == Action::Remove as u32 => Action::Remove, |
| 51 | + _ => unreachable!(), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn main() { |
| 57 | + ExtBuilder::default().build_and_execute(|| loop { |
| 58 | + fuzz!(|data: (AccountId, VoteWeight, u32)| { |
| 59 | + let (account_id_seed, vote_weight, action_seed) = data; |
| 60 | + |
| 61 | + let id = account_id_seed % ID_RANGE; |
| 62 | + let action = Action::from(action_seed); |
| 63 | + |
| 64 | + match action { |
| 65 | + Action::Insert => { |
| 66 | + if BagsList::on_insert(id.clone(), vote_weight).is_err() { |
| 67 | + // this was a duplicate id, which is ok. We can just update it. |
| 68 | + BagsList::on_update(&id, vote_weight); |
| 69 | + } |
| 70 | + assert!(BagsList::contains(&id)); |
| 71 | + }, |
| 72 | + Action::Update => { |
| 73 | + let already_contains = BagsList::contains(&id); |
| 74 | + BagsList::on_update(&id, vote_weight); |
| 75 | + if already_contains { |
| 76 | + assert!(BagsList::contains(&id)); |
| 77 | + } |
| 78 | + }, |
| 79 | + Action::Remove => { |
| 80 | + BagsList::on_remove(&id); |
| 81 | + assert!(!BagsList::contains(&id)); |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + assert!(BagsList::sanity_check().is_ok()); |
| 86 | + }) |
| 87 | + }); |
| 88 | +} |
0 commit comments