Skip to content

Commit

Permalink
token[-2022]: Add proptest for unpacking (#3421)
Browse files Browse the repository at this point in the history
  • Loading branch information
joncinque committed Aug 3, 2022
1 parent 4aa529d commit d7f352b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion token/program-2022/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,7 @@ pub(crate) fn encode_instruction<T: Into<u8>, D: Pod>(

#[cfg(test)]
mod test {
use super::*;
use {super::*, proptest::prelude::*};

#[test]
fn test_instruction_packing() {
Expand Down Expand Up @@ -2284,4 +2284,14 @@ mod test {
ui_amount,
));
}

proptest! {
#![proptest_config(ProptestConfig::with_cases(1024))]
#[test]
fn test_instruction_unpack_panic(
data in prop::collection::vec(any::<u8>(), 0..255)
) {
let _no_panic = TokenInstruction::unpack(&data);
}
}
}
1 change: 1 addition & 0 deletions token/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ thiserror = "1.0"

[dev-dependencies]
lazy_static = "1.4.0"
proptest = "1.0"
serial_test = "0.5.1"
solana-program-test = "1.10.33"
solana-sdk = "1.10.33"
Expand Down
17 changes: 8 additions & 9 deletions token/program/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1431,7 +1431,7 @@ pub fn is_valid_signer_index(index: usize) -> bool {

#[cfg(test)]
mod test {
use super::*;
use {super::*, proptest::prelude::*};

#[test]
fn test_instruction_packing() {
Expand Down Expand Up @@ -1674,14 +1674,13 @@ mod test {
assert_eq!(unpacked, check);
}

#[test]
fn test_instruction_unpack_panic() {
for i in 0..255u8 {
for j in 1..10 {
let mut data = vec![0; j];
data[0] = i;
let _no_panic = TokenInstruction::unpack(&data);
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(1024))]
#[test]
fn test_instruction_unpack_panic(
data in prop::collection::vec(any::<u8>(), 0..255)
) {
let _no_panic = TokenInstruction::unpack(&data);
}
}
}

0 comments on commit d7f352b

Please sign in to comment.