Skip to content

Commit

Permalink
Add fuzz test cases for governance structs: TxInfo, SignersInfo, and …
Browse files Browse the repository at this point in the history
…ProposalCore
  • Loading branch information
immrsd committed Jan 10, 2025
1 parent d3d788a commit cddc53c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
3 changes: 3 additions & 0 deletions packages/governance/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ snforge_std.workspace = true
openzeppelin_testing = { path = "../testing" }
openzeppelin_test_common = { path = "../test_common" }

[features]
fuzzing = []

[lib]

[[target.starknet-contract]]
Expand Down
2 changes: 2 additions & 0 deletions packages/governance/src/tests.cairo
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod governor;
#[cfg(feature: 'fuzzing')]
mod test_fuzz_packing;
mod test_multisig;
mod test_timelock;
mod test_utils;
Expand Down
53 changes: 53 additions & 0 deletions packages/governance/src/tests/test_fuzz_packing.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use core::num::traits::Bounded;
use crate::governor::ProposalCore;
use crate::multisig::storage_utils::{SignersInfo, TxInfo};
use starknet::storage_access::StorePacking;

#[test]
fn test_pack_unpack_tx_info(is_executed_val: u8, submitted_block: u64) {
let is_executed = is_executed_val % 2 == 0;
let packed_value = StorePacking::pack(TxInfo { is_executed, submitted_block });
let unpacked_tx_info: TxInfo = StorePacking::unpack(packed_value);

assert_eq!(unpacked_tx_info.is_executed, is_executed);
assert_eq!(unpacked_tx_info.submitted_block, submitted_block);
}

#[test]
fn test_pack_unpack_signers_info(quorum: u32, signers_count: u32) {
// Packing works incorrectly when the number of signers
// equals max u32 value (0xffffffff or 4_294_967_295).
if signers_count == Bounded::MAX {
return;
};
let packed_value = StorePacking::pack(SignersInfo { quorum, signers_count });
let unpacked_signers_info: SignersInfo = StorePacking::unpack(packed_value);

assert_eq!(unpacked_signers_info.quorum, quorum);
assert_eq!(unpacked_signers_info.signers_count, signers_count);
}

#[test]
fn test_pack_unpack_proposal_core(
proposer_val: felt252,
vote_start: u64,
vote_duration: u64,
executed_val: u8,
canceled_val: u8,
eta_seconds: u64,
) {
let proposer = match proposer_val.try_into() {
Option::Some(proposer) => proposer,
Option::None => { return; },
};
let executed = executed_val % 2 == 0;
let canceled = canceled_val % 2 == 0;
let initial_proposal_core = ProposalCore {
proposer, vote_start, vote_duration, executed, canceled, eta_seconds,
};

let packed_value = StorePacking::pack(initial_proposal_core);
let unpacked_proposal_core: ProposalCore = StorePacking::unpack(packed_value);

assert!(unpacked_proposal_core == initial_proposal_core);
}

0 comments on commit cddc53c

Please sign in to comment.