From 5113a4cc381c8b88b940dad1f25e122ddbef311b Mon Sep 17 00:00:00 2001 From: Illia Kripaka Date: Thu, 5 Nov 2026 14:34:45 +0200 Subject: [PATCH 1/2] add asserts and op_return fuzz tests --- tests/stdlib/asserts.rs | 539 +++++++++++++++++++++++++++++++++++++- tests/stdlib/op_return.rs | 0 2 files changed, 533 insertions(+), 6 deletions(-) create mode 100644 tests/stdlib/op_return.rs diff --git a/tests/stdlib/asserts.rs b/tests/stdlib/asserts.rs index b3ca487..63eef70 100644 --- a/tests/stdlib/asserts.rs +++ b/tests/stdlib/asserts.rs @@ -11,6 +11,7 @@ use simplicityhl_std::artifacts::tests::asserts::derived_asserts::{ // simf/tests/asserts.simf. use FunctionToTest::*; +#[derive(Clone, Copy)] enum FunctionToTest { AssertEq1, AssertEq8, @@ -63,11 +64,9 @@ pub fn generate_uints_in_one_range(same: bool, min_val: u128, max_val: u128) -> (some_u, other_u) } -/// Builds the witness for one assert call. `same` controls the two `assert_eq` -/// args; `none` makes the single `assert_none` arg `None`. -fn build_witness(function: FunctionToTest, same: bool, none: bool) -> AssertsTestWitness { - let mut witness = AssertsTestWitness { - function_index: 0, +fn default_witness(function_index: u8) -> AssertsTestWitness { + AssertsTestWitness { + function_index, first_arg_u1: DEFAULT_SOME_U8, // u1 in Simplicity is represented as u8 second_arg_u1: DEFAULT_SOME_U8, first_arg_u8: DEFAULT_SOME_U8, @@ -82,7 +81,13 @@ fn build_witness(function: FunctionToTest, same: bool, none: bool) -> AssertsTes second_arg_u128: DEFAULT_SOME_U128, first_arg_u256: DEFAULT_SOME_U256, second_arg_u256: DEFAULT_SOME_U256, - }; + } +} + +/// Builds the witness for one assert call. `same` controls the two `assert_eq` +/// args; `none` makes the single `assert_none` arg `None`. +fn build_witness(function: FunctionToTest, same: bool, none: bool) -> AssertsTestWitness { + let mut witness = default_witness(0); match function { FunctionToTest::AssertEq1 => { @@ -350,3 +355,525 @@ fn assert_none_256_happy_path(context: simplex::TestContext) -> anyhow::Result<( fn assert_none_256_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { case(AssertNone256).expecting(&context, Expect::AssertFailed) } + +mod asserts_test_fuzz { + use super::*; + + use std::fmt::Debug; + + use simplex::simplicityhl::{Arguments, WitnessValues}; + + use simplex::fuzz::Expect; + use simplex::fuzz::proptest::prelude::any; + use simplex::fuzz::proptest::strategy::{BoxedStrategy, Strategy}; + use simplex::{FuzzMode, TestContext}; + + use crate::common::fuzz; + + fn arb_u1() -> impl Strategy { + any::().prop_map(u8::from) + } + + fn arb_bool() -> impl Strategy { + any::() + } + + fn arb_u8() -> impl Strategy { + any::() + } + + fn arb_u16() -> impl Strategy { + any::() + } + + fn arb_u32() -> impl Strategy { + any::() + } + + fn arb_u64() -> impl Strategy { + any::() + } + + fn arb_u128() -> impl Strategy { + any::() + } + + fn arb_u256() -> impl Strategy { + any::<[u8; 32]>() + } + + type ArgumentFields = fn(&mut AssertsTestWitness) -> (&mut Option, &mut Option); + + fn equal_values_strategy( + function_index: u8, + values: impl Strategy + 'static, + fields: ArgumentFields, + ) -> BoxedStrategy + where + T: Clone + Debug + Into + 'static, + V: 'static, + { + values + .prop_map(move |value| { + let mut witness = default_witness(function_index); + let first: V = value.clone().into(); + let second: V = value.into(); + + Case::set_arguments(fields(&mut witness), first, second); + witness + }) + .boxed() + } + + fn distinct_values_strategy( + function_index: u8, + values: impl Strategy + 'static, + fields: ArgumentFields, + ) -> BoxedStrategy + where + T: PartialEq + Debug + Into + 'static, + V: 'static, + { + let values = values.boxed(); + + (values.clone(), values) + .prop_filter("assert_eq arguments must be distinct", |(first, second)| { + first != second + }) + .prop_map(move |(first, second)| { + let mut witness = default_witness(function_index); + let first: V = first.into(); + let second: V = second.into(); + + Case::set_arguments(fields(&mut witness), first, second); + witness + }) + .boxed() + } + + fn none_strategy( + none: bool, + function_index: u8, + values: impl Strategy + 'static, + fields: ArgumentFields, + ) -> BoxedStrategy { + values + .prop_map(move |value| { + let mut witness = default_witness(function_index); + + let (first, second) = if none { + (None, Some(value)) + } else { + (Some(value), Some(T::default())) + }; + + Case::set_arguments(fields(&mut witness), first, second); + witness + }) + .boxed() + } + + impl Case { + fn set_arguments( + fields: (&mut Option, &mut Option), + first: impl Into>, + second: impl Into>, + ) { + *fields.0 = first.into(); + *fields.1 = second.into(); + } + + fn u1_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { + (&mut witness.first_arg_u1, &mut witness.second_arg_u1) + } + + fn u8_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { + (&mut witness.first_arg_u8, &mut witness.second_arg_u8) + } + + fn u16_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { + (&mut witness.first_arg_u16, &mut witness.second_arg_u16) + } + + fn u32_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { + (&mut witness.first_arg_u32, &mut witness.second_arg_u32) + } + + fn u64_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { + (&mut witness.first_arg_u64, &mut witness.second_arg_u64) + } + + fn u128_arguments( + witness: &mut AssertsTestWitness, + ) -> (&mut Option, &mut Option) { + (&mut witness.first_arg_u128, &mut witness.second_arg_u128) + } + + fn u256_arguments( + witness: &mut AssertsTestWitness, + ) -> (&mut Option<[u8; 32]>, &mut Option<[u8; 32]>) { + (&mut witness.first_arg_u256, &mut witness.second_arg_u256) + } + + fn strategy(self) -> BoxedStrategy { + match (self.function, self.same) { + ( + AssertNone1 | AssertNone8 | AssertNone16 | AssertNone32 | AssertNone64 + | AssertNone128 | AssertNone256, + _, + ) => self.strategy_with_none_values(), + (_, true) => self.strategy_with_same_values(), + (_, false) => self.strategy_with_distinct_values(), + } + } + + fn strategy_with_none_values(&self) -> BoxedStrategy { + let function_index = self.function as u8; + + match self.function { + AssertNone1 => { + none_strategy(self.none, function_index, arb_u1(), Self::u1_arguments) + } + AssertNone8 => { + none_strategy(self.none, function_index, arb_u8(), Self::u8_arguments) + } + AssertNone16 => { + none_strategy(self.none, function_index, arb_u16(), Self::u16_arguments) + } + AssertNone32 => { + none_strategy(self.none, function_index, arb_u32(), Self::u32_arguments) + } + AssertNone64 => { + none_strategy(self.none, function_index, arb_u64(), Self::u64_arguments) + } + AssertNone128 => { + none_strategy(self.none, function_index, arb_u128(), Self::u128_arguments) + } + AssertNone256 => { + none_strategy(self.none, function_index, arb_u256(), Self::u256_arguments) + } + _ => unreachable!("assert_eq cases aren't handled"), + } + } + + fn strategy_with_same_values(&self) -> BoxedStrategy { + let function_index = self.function as u8; + + match self.function { + AssertEq1 => equal_values_strategy(function_index, arb_u1(), Self::u1_arguments), + AssertEq8 => equal_values_strategy(function_index, arb_u8(), Self::u8_arguments), + AssertEq16 => equal_values_strategy(function_index, arb_u16(), Self::u16_arguments), + AssertEq32 => equal_values_strategy(function_index, arb_u32(), Self::u32_arguments), + AssertEq64 => equal_values_strategy(function_index, arb_u64(), Self::u64_arguments), + AssertEq128 => { + equal_values_strategy(function_index, arb_u128(), Self::u128_arguments) + } + AssertEq256 => { + equal_values_strategy(function_index, arb_u256(), Self::u256_arguments) + } + AssertEqBool => { + equal_values_strategy(function_index, arb_bool(), Self::u1_arguments) + } + _ => unreachable!("assert_none cases aren't handled"), + } + } + + fn strategy_with_distinct_values(&self) -> BoxedStrategy { + let function_index = self.function as u8; + + match self.function { + AssertEq1 => distinct_values_strategy(function_index, arb_u1(), Self::u1_arguments), + AssertEq8 => distinct_values_strategy(function_index, arb_u8(), Self::u8_arguments), + AssertEq16 => { + distinct_values_strategy(function_index, arb_u16(), Self::u16_arguments) + } + AssertEq32 => { + distinct_values_strategy(function_index, arb_u32(), Self::u32_arguments) + } + AssertEq64 => { + distinct_values_strategy(function_index, arb_u64(), Self::u64_arguments) + } + AssertEq128 => { + distinct_values_strategy(function_index, arb_u128(), Self::u128_arguments) + } + AssertEq256 => { + distinct_values_strategy(function_index, arb_u256(), Self::u256_arguments) + } + AssertEqBool => { + distinct_values_strategy(function_index, arb_bool(), Self::u1_arguments) + } + _ => unreachable!("assert_none cases aren't handled"), + } + } + } + + struct CaseFuzz { + case: Case, + context: TestContext, + test_name: &'static str, + expect: Expect, + } + + fn case_fuzz( + function: FunctionToTest, + context: TestContext, + test_name: &'static str, + ) -> CaseFuzz { + CaseFuzz { + case: case(function), + context, + test_name, + expect: Expect::Accept, + } + } + + impl CaseFuzz { + fn equal(mut self) -> Self { + self.case = self.case.equal(); + self + } + + fn none(mut self) -> Self { + self.case = self.case.none(); + self + } + + fn expect(mut self, expect: Expect) -> Self { + self.expect = expect; + self + } + + fn run(self) -> anyhow::Result<()> { + let strategy = self + .case + .strategy() + .prop_map(|witness| { + let arguments: Arguments = AssertsTestArguments {}.into(); + let witness: WitnessValues = witness.into(); + + (arguments, witness) + }) + .boxed(); + + fuzz::run::( + &self.context, + strategy, + self.test_name, + self.expect, + None, + ); + + Ok(()) + } + } + + #[simplex::fuzz] + fn assert_eq_1_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq1, context, "assert_eq_1_happy_path") + .equal() + .run() + } + + #[simplex::fuzz] + fn assert_eq_1_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq1, context, "assert_eq_1_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_eq_8_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq8, context, "assert_eq_8_happy_path") + .equal() + .run() + } + + #[simplex::fuzz] + fn assert_eq_8_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq8, context, "assert_eq_8_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_eq_16_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq16, context, "assert_eq_16_happy_path") + .equal() + .run() + } + + #[simplex::fuzz] + fn assert_eq_16_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq16, context, "assert_eq_16_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_eq_32_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq32, context, "assert_eq_32_happy_path") + .equal() + .run() + } + + #[simplex::fuzz] + fn assert_eq_32_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq32, context, "assert_eq_32_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_eq_64_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq64, context, "assert_eq_64_happy_path") + .equal() + .run() + } + + #[simplex::fuzz] + fn assert_eq_64_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq64, context, "assert_eq_64_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_eq_128_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq128, context, "assert_eq_128_happy_path") + .equal() + .run() + } + + #[simplex::fuzz] + fn assert_eq_128_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq128, context, "assert_eq_128_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_eq_256_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq256, context, "assert_eq_256_happy_path") + .equal() + .run() + } + + #[simplex::fuzz] + fn assert_eq_256_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEq256, context, "assert_eq_256_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_eq_bool_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEqBool, context, "assert_eq_bool_happy_path") + .equal() + .run() + } + + #[simplex::fuzz] + fn assert_eq_bool_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertEqBool, context, "assert_eq_bool_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_none_1_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone1, context, "assert_none_1_happy_path") + .none() + .run() + } + + #[simplex::fuzz] + fn assert_none_1_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone1, context, "assert_none_1_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_none_8_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone8, context, "assert_none_8_happy_path") + .none() + .run() + } + + #[simplex::fuzz] + fn assert_none_8_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone8, context, "assert_none_8_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_none_16_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone16, context, "assert_none_16_happy_path") + .none() + .run() + } + + #[simplex::fuzz] + fn assert_none_16_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone16, context, "assert_none_16_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_none_32_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone32, context, "assert_none_32_happy_path") + .none() + .run() + } + + #[simplex::fuzz] + fn assert_none_32_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone32, context, "assert_none_32_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_none_64_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone64, context, "assert_none_64_happy_path") + .none() + .run() + } + + #[simplex::fuzz] + fn assert_none_64_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone64, context, "assert_none_64_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_none_128_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone128, context, "assert_none_128_happy_path") + .none() + .run() + } + + #[simplex::fuzz] + fn assert_none_128_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone128, context, "assert_none_128_unhappy_path") + .expect(Expect::Reject) + .run() + } + + #[simplex::fuzz] + fn assert_none_256_happy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone256, context, "assert_none_256_happy_path") + .none() + .run() + } + + #[simplex::fuzz] + fn assert_none_256_unhappy_path(context: TestContext) -> anyhow::Result<()> { + case_fuzz(AssertNone256, context, "assert_none_256_unhappy_path") + .expect(Expect::Reject) + .run() + } +} diff --git a/tests/stdlib/op_return.rs b/tests/stdlib/op_return.rs new file mode 100644 index 0000000..e69de29 From 61f273723c9005b2451794885d09186eb950fd14 Mon Sep 17 00:00:00 2001 From: Illia Kripaka Date: Mon, 23 Nov 2026 14:43:14 +0200 Subject: [PATCH 2/2] refactor asserts, binary and OP_RETURN tests * add unit and fuzz tests --- CHANGELOG.md | 2 + tests/stdlib/asserts.rs | 879 ---------------------------- tests/stdlib/asserts/case.rs | 172 ++++++ tests/stdlib/asserts/fuzz.rs | 195 ++++++ tests/stdlib/asserts/mod.rs | 3 + tests/stdlib/asserts/unit.rs | 205 +++++++ tests/stdlib/binary.rs | 12 +- tests/stdlib/common/case.rs | 23 +- tests/stdlib/common/core.rs | 2 +- tests/stdlib/common/fuzz.rs | 17 +- tests/stdlib/common/unit.rs | 37 +- tests/stdlib/op_return.rs | 0 tests/stdlib/utxo/op_return.rs | 761 ------------------------ tests/stdlib/utxo/op_return/case.rs | 204 +++++++ tests/stdlib/utxo/op_return/fuzz.rs | 113 ++++ tests/stdlib/utxo/op_return/mod.rs | 3 + tests/stdlib/utxo/op_return/unit.rs | 431 ++++++++++++++ 17 files changed, 1400 insertions(+), 1659 deletions(-) delete mode 100644 tests/stdlib/asserts.rs create mode 100644 tests/stdlib/asserts/case.rs create mode 100644 tests/stdlib/asserts/fuzz.rs create mode 100644 tests/stdlib/asserts/mod.rs create mode 100644 tests/stdlib/asserts/unit.rs delete mode 100644 tests/stdlib/op_return.rs delete mode 100644 tests/stdlib/utxo/op_return.rs create mode 100644 tests/stdlib/utxo/op_return/case.rs create mode 100644 tests/stdlib/utxo/op_return/fuzz.rs create mode 100644 tests/stdlib/utxo/op_return/mod.rs create mode 100644 tests/stdlib/utxo/op_return/unit.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f31db0..794c7ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,12 +7,14 @@ - Conversion and bit functions for `u2` and `u4`. - Functions for asserting script hashes. - Fuzz tests for `secp256k1` operations. +- Fuzz tests for asserts and `OP_RETURN` functions. - `enforce_relative_distance` and `enforce_relative_duration`, replacements for the deprecated `jet::check_lock_distance` and `jet::check_lock_duration`. ### Changed - Bumped Simplex to 0.2.0. - Aligned existing testing API with changes in Simplex. - `secp256k1` unit tests run without a node, on fixed values. +- Asserts, binary and `OP_RETURN` unit tests run without a node, on fixed values. ## [0.2.0] diff --git a/tests/stdlib/asserts.rs b/tests/stdlib/asserts.rs deleted file mode 100644 index 63eef70..0000000 --- a/tests/stdlib/asserts.rs +++ /dev/null @@ -1,879 +0,0 @@ -use rand::Rng; - -use crate::common::core::{Expect, run}; - -use simplicityhl_std::artifacts::tests::asserts::AssertsProgram as AssertsTestProgram; -use simplicityhl_std::artifacts::tests::asserts::derived_asserts::{ - AssertsArguments as AssertsTestArguments, AssertsWitness as AssertsTestWitness, -}; - -// Dispatch indices which must match the `is_selected(N, ..)` arms in -// simf/tests/asserts.simf. -use FunctionToTest::*; - -#[derive(Clone, Copy)] -enum FunctionToTest { - AssertEq1, - AssertEq8, - AssertEq16, - AssertEq32, - AssertEq64, - AssertEq128, - AssertEq256, - AssertEqBool, - - AssertNone1, - AssertNone8, - AssertNone16, - AssertNone32, - AssertNone64, - AssertNone128, - AssertNone256, -} - -const DEFAULT_SOME_U8: Option = Some(0); -const DEFAULT_SOME_U16: Option = Some(0); -const DEFAULT_SOME_U32: Option = Some(0); -const DEFAULT_SOME_U64: Option = Some(0); -const DEFAULT_SOME_U128: Option = Some(0); -const DEFAULT_SOME_U256: Option<[u8; 32]> = Some([0; 32]); - -fn program() -> AssertsTestProgram { - AssertsTestProgram::new(&AssertsTestArguments {}) -} - -/// Returns two values in `[min, max]` that are equal when `same`, distinct otherwise. -pub fn generate_uints_in_one_range(same: bool, min_val: u128, max_val: u128) -> (u128, u128) { - let some_u = rand::thread_rng().gen_range(min_val..=max_val); - - if same { - return (some_u, some_u); - } - - assert!( - min_val != max_val, - "cannot generate distinct values in a single-value range" - ); - - let mut other_u = rand::thread_rng().gen_range(min_val..=max_val); - - while other_u == some_u { - other_u = rand::thread_rng().gen_range(min_val..=max_val); - } - - (some_u, other_u) -} - -fn default_witness(function_index: u8) -> AssertsTestWitness { - AssertsTestWitness { - function_index, - first_arg_u1: DEFAULT_SOME_U8, // u1 in Simplicity is represented as u8 - second_arg_u1: DEFAULT_SOME_U8, - first_arg_u8: DEFAULT_SOME_U8, - second_arg_u8: DEFAULT_SOME_U8, - first_arg_u16: DEFAULT_SOME_U16, - second_arg_u16: DEFAULT_SOME_U16, - first_arg_u32: DEFAULT_SOME_U32, - second_arg_u32: DEFAULT_SOME_U32, - first_arg_u64: DEFAULT_SOME_U64, - second_arg_u64: DEFAULT_SOME_U64, - first_arg_u128: DEFAULT_SOME_U128, - second_arg_u128: DEFAULT_SOME_U128, - first_arg_u256: DEFAULT_SOME_U256, - second_arg_u256: DEFAULT_SOME_U256, - } -} - -/// Builds the witness for one assert call. `same` controls the two `assert_eq` -/// args; `none` makes the single `assert_none` arg `None`. -fn build_witness(function: FunctionToTest, same: bool, none: bool) -> AssertsTestWitness { - let mut witness = default_witness(0); - - match function { - FunctionToTest::AssertEq1 => { - let (a, b) = generate_uints_in_one_range(same, 0, 1u128); - (witness.first_arg_u1, witness.second_arg_u1) = (Some(a as u8), Some(b as u8)); - } - FunctionToTest::AssertEq8 => { - let (a, b) = generate_uints_in_one_range(same, 0, u8::MAX as u128); - (witness.first_arg_u8, witness.second_arg_u8) = (Some(a as u8), Some(b as u8)); - } - FunctionToTest::AssertEq16 => { - let (a, b) = generate_uints_in_one_range(same, 0, u16::MAX as u128); - (witness.first_arg_u16, witness.second_arg_u16) = (Some(a as u16), Some(b as u16)); - } - FunctionToTest::AssertEq32 => { - let (a, b) = generate_uints_in_one_range(same, 0, u32::MAX as u128); - (witness.first_arg_u32, witness.second_arg_u32) = (Some(a as u32), Some(b as u32)); - } - FunctionToTest::AssertEq64 => { - let (a, b) = generate_uints_in_one_range(same, 0, u64::MAX as u128); - (witness.first_arg_u64, witness.second_arg_u64) = (Some(a as u64), Some(b as u64)); - } - FunctionToTest::AssertEq128 => { - let (a, b) = generate_uints_in_one_range(same, 0, u128::MAX); - (witness.first_arg_u128, witness.second_arg_u128) = (Some(a), Some(b)); - } - FunctionToTest::AssertEq256 => { - let (a, b) = generate_uints_in_one_range(same, 0, u8::MAX as u128); - (witness.first_arg_u256, witness.second_arg_u256) = - (Some([a as u8; 32]), Some([b as u8; 32])); - } - FunctionToTest::AssertEqBool => { - let (a, b) = generate_uints_in_one_range(same, 0, 1u128); - (witness.first_arg_u1, witness.second_arg_u1) = (Some(a as u8), Some(b as u8)); - } - FunctionToTest::AssertNone1 => { - if none { - witness.first_arg_u1 = None; - } - } - FunctionToTest::AssertNone8 => { - if none { - witness.first_arg_u8 = None; - } - } - FunctionToTest::AssertNone16 => { - if none { - witness.first_arg_u16 = None; - } - } - FunctionToTest::AssertNone32 => { - if none { - witness.first_arg_u32 = None; - } - } - FunctionToTest::AssertNone64 => { - if none { - witness.first_arg_u64 = None; - } - } - FunctionToTest::AssertNone128 => { - if none { - witness.first_arg_u128 = None; - } - } - FunctionToTest::AssertNone256 => { - if none { - witness.first_arg_u256 = None; - } - } - } - - witness.function_index = function as u8; - witness -} - -/// One assert call, described by what makes it pass. -struct Case { - function: FunctionToTest, - same: bool, - none: bool, -} - -fn case(function: FunctionToTest) -> Case { - Case { - function, - same: false, - none: false, - } -} - -impl Case { - /// Make the two `assert_eq` arguments equal. - fn equal(mut self) -> Self { - self.same = true; - self - } - - /// Make the `assert_none` argument `None`. - fn none(mut self) -> Self { - self.none = true; - self - } - - /// Fund, spend, and expect the spend to succeed. - fn run(self, context: &simplex::TestContext) -> anyhow::Result<()> { - self.expecting(context, Expect::Ok) - } - - /// Fund, spend, and expect `expect`. - fn expecting(self, context: &simplex::TestContext, expect: Expect) -> anyhow::Result<()> { - let witness = build_witness(self.function, self.same, self.none); - run(context, program(), witness, expect) - } -} - -// assert_eq: happy = equal args, unhappy = distinct args -#[simplex::test] -fn assert_eq_1_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq1).equal().run(&context) -} - -#[simplex::test] -fn assert_eq_1_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq1).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_eq_8_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq8).equal().run(&context) -} - -#[simplex::test] -fn assert_eq_8_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq8).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_eq_16_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq16).equal().run(&context) -} - -#[simplex::test] -fn assert_eq_16_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq16).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_eq_32_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq32).equal().run(&context) -} - -#[simplex::test] -fn assert_eq_32_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq32).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_eq_64_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq64).equal().run(&context) -} - -#[simplex::test] -fn assert_eq_64_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq64).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_eq_128_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq128).equal().run(&context) -} - -#[simplex::test] -fn assert_eq_128_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq128).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_eq_256_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq256).equal().run(&context) -} - -#[simplex::test] -fn assert_eq_256_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEq256).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_eq_bool_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEqBool).equal().run(&context) -} - -#[simplex::test] -fn assert_eq_bool_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertEqBool).expecting(&context, Expect::AssertFailed) -} - -// assert_none: happy = None arg, unhappy = Some arg -#[simplex::test] -fn assert_none_1_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone1).none().run(&context) -} - -#[simplex::test] -fn assert_none_1_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone1).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_none_8_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone8).none().run(&context) -} - -#[simplex::test] -fn assert_none_8_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone8).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_none_16_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone16).none().run(&context) -} - -#[simplex::test] -fn assert_none_16_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone16).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_none_32_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone32).none().run(&context) -} - -#[simplex::test] -fn assert_none_32_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone32).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_none_64_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone64).none().run(&context) -} - -#[simplex::test] -fn assert_none_64_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone64).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_none_128_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone128).none().run(&context) -} - -#[simplex::test] -fn assert_none_128_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone128).expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_none_256_happy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone256).none().run(&context) -} - -#[simplex::test] -fn assert_none_256_unhappy_path(context: simplex::TestContext) -> anyhow::Result<()> { - case(AssertNone256).expecting(&context, Expect::AssertFailed) -} - -mod asserts_test_fuzz { - use super::*; - - use std::fmt::Debug; - - use simplex::simplicityhl::{Arguments, WitnessValues}; - - use simplex::fuzz::Expect; - use simplex::fuzz::proptest::prelude::any; - use simplex::fuzz::proptest::strategy::{BoxedStrategy, Strategy}; - use simplex::{FuzzMode, TestContext}; - - use crate::common::fuzz; - - fn arb_u1() -> impl Strategy { - any::().prop_map(u8::from) - } - - fn arb_bool() -> impl Strategy { - any::() - } - - fn arb_u8() -> impl Strategy { - any::() - } - - fn arb_u16() -> impl Strategy { - any::() - } - - fn arb_u32() -> impl Strategy { - any::() - } - - fn arb_u64() -> impl Strategy { - any::() - } - - fn arb_u128() -> impl Strategy { - any::() - } - - fn arb_u256() -> impl Strategy { - any::<[u8; 32]>() - } - - type ArgumentFields = fn(&mut AssertsTestWitness) -> (&mut Option, &mut Option); - - fn equal_values_strategy( - function_index: u8, - values: impl Strategy + 'static, - fields: ArgumentFields, - ) -> BoxedStrategy - where - T: Clone + Debug + Into + 'static, - V: 'static, - { - values - .prop_map(move |value| { - let mut witness = default_witness(function_index); - let first: V = value.clone().into(); - let second: V = value.into(); - - Case::set_arguments(fields(&mut witness), first, second); - witness - }) - .boxed() - } - - fn distinct_values_strategy( - function_index: u8, - values: impl Strategy + 'static, - fields: ArgumentFields, - ) -> BoxedStrategy - where - T: PartialEq + Debug + Into + 'static, - V: 'static, - { - let values = values.boxed(); - - (values.clone(), values) - .prop_filter("assert_eq arguments must be distinct", |(first, second)| { - first != second - }) - .prop_map(move |(first, second)| { - let mut witness = default_witness(function_index); - let first: V = first.into(); - let second: V = second.into(); - - Case::set_arguments(fields(&mut witness), first, second); - witness - }) - .boxed() - } - - fn none_strategy( - none: bool, - function_index: u8, - values: impl Strategy + 'static, - fields: ArgumentFields, - ) -> BoxedStrategy { - values - .prop_map(move |value| { - let mut witness = default_witness(function_index); - - let (first, second) = if none { - (None, Some(value)) - } else { - (Some(value), Some(T::default())) - }; - - Case::set_arguments(fields(&mut witness), first, second); - witness - }) - .boxed() - } - - impl Case { - fn set_arguments( - fields: (&mut Option, &mut Option), - first: impl Into>, - second: impl Into>, - ) { - *fields.0 = first.into(); - *fields.1 = second.into(); - } - - fn u1_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { - (&mut witness.first_arg_u1, &mut witness.second_arg_u1) - } - - fn u8_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { - (&mut witness.first_arg_u8, &mut witness.second_arg_u8) - } - - fn u16_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { - (&mut witness.first_arg_u16, &mut witness.second_arg_u16) - } - - fn u32_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { - (&mut witness.first_arg_u32, &mut witness.second_arg_u32) - } - - fn u64_arguments(witness: &mut AssertsTestWitness) -> (&mut Option, &mut Option) { - (&mut witness.first_arg_u64, &mut witness.second_arg_u64) - } - - fn u128_arguments( - witness: &mut AssertsTestWitness, - ) -> (&mut Option, &mut Option) { - (&mut witness.first_arg_u128, &mut witness.second_arg_u128) - } - - fn u256_arguments( - witness: &mut AssertsTestWitness, - ) -> (&mut Option<[u8; 32]>, &mut Option<[u8; 32]>) { - (&mut witness.first_arg_u256, &mut witness.second_arg_u256) - } - - fn strategy(self) -> BoxedStrategy { - match (self.function, self.same) { - ( - AssertNone1 | AssertNone8 | AssertNone16 | AssertNone32 | AssertNone64 - | AssertNone128 | AssertNone256, - _, - ) => self.strategy_with_none_values(), - (_, true) => self.strategy_with_same_values(), - (_, false) => self.strategy_with_distinct_values(), - } - } - - fn strategy_with_none_values(&self) -> BoxedStrategy { - let function_index = self.function as u8; - - match self.function { - AssertNone1 => { - none_strategy(self.none, function_index, arb_u1(), Self::u1_arguments) - } - AssertNone8 => { - none_strategy(self.none, function_index, arb_u8(), Self::u8_arguments) - } - AssertNone16 => { - none_strategy(self.none, function_index, arb_u16(), Self::u16_arguments) - } - AssertNone32 => { - none_strategy(self.none, function_index, arb_u32(), Self::u32_arguments) - } - AssertNone64 => { - none_strategy(self.none, function_index, arb_u64(), Self::u64_arguments) - } - AssertNone128 => { - none_strategy(self.none, function_index, arb_u128(), Self::u128_arguments) - } - AssertNone256 => { - none_strategy(self.none, function_index, arb_u256(), Self::u256_arguments) - } - _ => unreachable!("assert_eq cases aren't handled"), - } - } - - fn strategy_with_same_values(&self) -> BoxedStrategy { - let function_index = self.function as u8; - - match self.function { - AssertEq1 => equal_values_strategy(function_index, arb_u1(), Self::u1_arguments), - AssertEq8 => equal_values_strategy(function_index, arb_u8(), Self::u8_arguments), - AssertEq16 => equal_values_strategy(function_index, arb_u16(), Self::u16_arguments), - AssertEq32 => equal_values_strategy(function_index, arb_u32(), Self::u32_arguments), - AssertEq64 => equal_values_strategy(function_index, arb_u64(), Self::u64_arguments), - AssertEq128 => { - equal_values_strategy(function_index, arb_u128(), Self::u128_arguments) - } - AssertEq256 => { - equal_values_strategy(function_index, arb_u256(), Self::u256_arguments) - } - AssertEqBool => { - equal_values_strategy(function_index, arb_bool(), Self::u1_arguments) - } - _ => unreachable!("assert_none cases aren't handled"), - } - } - - fn strategy_with_distinct_values(&self) -> BoxedStrategy { - let function_index = self.function as u8; - - match self.function { - AssertEq1 => distinct_values_strategy(function_index, arb_u1(), Self::u1_arguments), - AssertEq8 => distinct_values_strategy(function_index, arb_u8(), Self::u8_arguments), - AssertEq16 => { - distinct_values_strategy(function_index, arb_u16(), Self::u16_arguments) - } - AssertEq32 => { - distinct_values_strategy(function_index, arb_u32(), Self::u32_arguments) - } - AssertEq64 => { - distinct_values_strategy(function_index, arb_u64(), Self::u64_arguments) - } - AssertEq128 => { - distinct_values_strategy(function_index, arb_u128(), Self::u128_arguments) - } - AssertEq256 => { - distinct_values_strategy(function_index, arb_u256(), Self::u256_arguments) - } - AssertEqBool => { - distinct_values_strategy(function_index, arb_bool(), Self::u1_arguments) - } - _ => unreachable!("assert_none cases aren't handled"), - } - } - } - - struct CaseFuzz { - case: Case, - context: TestContext, - test_name: &'static str, - expect: Expect, - } - - fn case_fuzz( - function: FunctionToTest, - context: TestContext, - test_name: &'static str, - ) -> CaseFuzz { - CaseFuzz { - case: case(function), - context, - test_name, - expect: Expect::Accept, - } - } - - impl CaseFuzz { - fn equal(mut self) -> Self { - self.case = self.case.equal(); - self - } - - fn none(mut self) -> Self { - self.case = self.case.none(); - self - } - - fn expect(mut self, expect: Expect) -> Self { - self.expect = expect; - self - } - - fn run(self) -> anyhow::Result<()> { - let strategy = self - .case - .strategy() - .prop_map(|witness| { - let arguments: Arguments = AssertsTestArguments {}.into(); - let witness: WitnessValues = witness.into(); - - (arguments, witness) - }) - .boxed(); - - fuzz::run::( - &self.context, - strategy, - self.test_name, - self.expect, - None, - ); - - Ok(()) - } - } - - #[simplex::fuzz] - fn assert_eq_1_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq1, context, "assert_eq_1_happy_path") - .equal() - .run() - } - - #[simplex::fuzz] - fn assert_eq_1_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq1, context, "assert_eq_1_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_eq_8_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq8, context, "assert_eq_8_happy_path") - .equal() - .run() - } - - #[simplex::fuzz] - fn assert_eq_8_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq8, context, "assert_eq_8_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_eq_16_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq16, context, "assert_eq_16_happy_path") - .equal() - .run() - } - - #[simplex::fuzz] - fn assert_eq_16_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq16, context, "assert_eq_16_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_eq_32_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq32, context, "assert_eq_32_happy_path") - .equal() - .run() - } - - #[simplex::fuzz] - fn assert_eq_32_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq32, context, "assert_eq_32_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_eq_64_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq64, context, "assert_eq_64_happy_path") - .equal() - .run() - } - - #[simplex::fuzz] - fn assert_eq_64_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq64, context, "assert_eq_64_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_eq_128_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq128, context, "assert_eq_128_happy_path") - .equal() - .run() - } - - #[simplex::fuzz] - fn assert_eq_128_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq128, context, "assert_eq_128_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_eq_256_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq256, context, "assert_eq_256_happy_path") - .equal() - .run() - } - - #[simplex::fuzz] - fn assert_eq_256_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEq256, context, "assert_eq_256_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_eq_bool_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEqBool, context, "assert_eq_bool_happy_path") - .equal() - .run() - } - - #[simplex::fuzz] - fn assert_eq_bool_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertEqBool, context, "assert_eq_bool_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_none_1_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone1, context, "assert_none_1_happy_path") - .none() - .run() - } - - #[simplex::fuzz] - fn assert_none_1_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone1, context, "assert_none_1_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_none_8_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone8, context, "assert_none_8_happy_path") - .none() - .run() - } - - #[simplex::fuzz] - fn assert_none_8_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone8, context, "assert_none_8_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_none_16_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone16, context, "assert_none_16_happy_path") - .none() - .run() - } - - #[simplex::fuzz] - fn assert_none_16_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone16, context, "assert_none_16_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_none_32_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone32, context, "assert_none_32_happy_path") - .none() - .run() - } - - #[simplex::fuzz] - fn assert_none_32_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone32, context, "assert_none_32_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_none_64_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone64, context, "assert_none_64_happy_path") - .none() - .run() - } - - #[simplex::fuzz] - fn assert_none_64_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone64, context, "assert_none_64_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_none_128_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone128, context, "assert_none_128_happy_path") - .none() - .run() - } - - #[simplex::fuzz] - fn assert_none_128_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone128, context, "assert_none_128_unhappy_path") - .expect(Expect::Reject) - .run() - } - - #[simplex::fuzz] - fn assert_none_256_happy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone256, context, "assert_none_256_happy_path") - .none() - .run() - } - - #[simplex::fuzz] - fn assert_none_256_unhappy_path(context: TestContext) -> anyhow::Result<()> { - case_fuzz(AssertNone256, context, "assert_none_256_unhappy_path") - .expect(Expect::Reject) - .run() - } -} diff --git a/tests/stdlib/asserts/case.rs b/tests/stdlib/asserts/case.rs new file mode 100644 index 0000000..f3aee9a --- /dev/null +++ b/tests/stdlib/asserts/case.rs @@ -0,0 +1,172 @@ +use simplicityhl_std::artifacts::tests::asserts::AssertsProgram as AssertsTestProgram; +use simplicityhl_std::artifacts::tests::asserts::derived_asserts::{ + AssertsArguments as AssertsTestArguments, AssertsWitness as AssertsTestWitness, +}; + +use crate::common::case::DispatchCase; + +pub type U256 = [u8; 32]; + +#[derive(Clone, Copy)] +enum FunctionToTest { + AssertEq1 = 0, + AssertEq8 = 1, + AssertEq16 = 2, + AssertEq32 = 3, + AssertEq64 = 4, + AssertEq128 = 5, + AssertEq256 = 6, + AssertEqBool = 7, + AssertNone1 = 8, + AssertNone8 = 9, + AssertNone16 = 10, + AssertNone32 = 11, + AssertNone64 = 12, + AssertNone128 = 13, + AssertNone256 = 14, +} + +pub fn assert_eq_1(a: u8, b: u8) -> Case { + Case::new(FunctionToTest::AssertEq1).u1(Some(a), Some(b)) +} + +pub fn assert_eq_8(a: u8, b: u8) -> Case { + Case::new(FunctionToTest::AssertEq8).u8(Some(a), Some(b)) +} + +pub fn assert_eq_16(a: u16, b: u16) -> Case { + Case::new(FunctionToTest::AssertEq16).u16(Some(a), Some(b)) +} + +pub fn assert_eq_32(a: u32, b: u32) -> Case { + Case::new(FunctionToTest::AssertEq32).u32(Some(a), Some(b)) +} + +pub fn assert_eq_64(a: u64, b: u64) -> Case { + Case::new(FunctionToTest::AssertEq64).u64(Some(a), Some(b)) +} + +pub fn assert_eq_128(a: u128, b: u128) -> Case { + Case::new(FunctionToTest::AssertEq128).u128(Some(a), Some(b)) +} + +pub fn assert_eq_256(a: U256, b: U256) -> Case { + Case::new(FunctionToTest::AssertEq256).u256(Some(a), Some(b)) +} + +pub fn assert_eq_bool(a: bool, b: bool) -> Case { + Case::new(FunctionToTest::AssertEqBool).u1(Some(u8::from(a)), Some(u8::from(b))) +} + +pub fn assert_none_1(value: Option) -> Case { + Case::new(FunctionToTest::AssertNone1).u1(value, Some(0)) +} + +pub fn assert_none_8(value: Option) -> Case { + Case::new(FunctionToTest::AssertNone8).u8(value, Some(0)) +} + +pub fn assert_none_16(value: Option) -> Case { + Case::new(FunctionToTest::AssertNone16).u16(value, Some(0)) +} + +pub fn assert_none_32(value: Option) -> Case { + Case::new(FunctionToTest::AssertNone32).u32(value, Some(0)) +} + +pub fn assert_none_64(value: Option) -> Case { + Case::new(FunctionToTest::AssertNone64).u64(value, Some(0)) +} + +pub fn assert_none_128(value: Option) -> Case { + Case::new(FunctionToTest::AssertNone128).u128(value, Some(0)) +} + +pub fn assert_none_256(value: Option) -> Case { + Case::new(FunctionToTest::AssertNone256).u256(value, Some([0; 32])) +} + +#[derive(Clone, Debug)] +pub struct Case { + witness: AssertsTestWitness, +} + +impl Case { + fn new(function: FunctionToTest) -> Self { + Self { + witness: AssertsTestWitness { + function_index: function as u8, + first_arg_u1: Some(0), + second_arg_u1: Some(0), + first_arg_u8: Some(0), + second_arg_u8: Some(0), + first_arg_u16: Some(0), + second_arg_u16: Some(0), + first_arg_u32: Some(0), + second_arg_u32: Some(0), + first_arg_u64: Some(0), + second_arg_u64: Some(0), + first_arg_u128: Some(0), + second_arg_u128: Some(0), + first_arg_u256: Some([0; 32]), + second_arg_u256: Some([0; 32]), + }, + } + } + + fn u1(mut self, first: Option, second: Option) -> Self { + self.witness.first_arg_u1 = first; + self.witness.second_arg_u1 = second; + self + } + + fn u8(mut self, first: Option, second: Option) -> Self { + self.witness.first_arg_u8 = first; + self.witness.second_arg_u8 = second; + self + } + + fn u16(mut self, first: Option, second: Option) -> Self { + self.witness.first_arg_u16 = first; + self.witness.second_arg_u16 = second; + self + } + + fn u32(mut self, first: Option, second: Option) -> Self { + self.witness.first_arg_u32 = first; + self.witness.second_arg_u32 = second; + self + } + + fn u64(mut self, first: Option, second: Option) -> Self { + self.witness.first_arg_u64 = first; + self.witness.second_arg_u64 = second; + self + } + + fn u128(mut self, first: Option, second: Option) -> Self { + self.witness.first_arg_u128 = first; + self.witness.second_arg_u128 = second; + self + } + + fn u256(mut self, first: Option, second: Option) -> Self { + self.witness.first_arg_u256 = first; + self.witness.second_arg_u256 = second; + self + } +} + +impl DispatchCase for Case { + type Program = AssertsTestProgram; + + const PROGRAM_NAME: &'static str = "asserts"; + + fn arguments() -> AssertsTestArguments { + AssertsTestArguments {} + } + + fn into_witness(self) -> AssertsTestWitness { + self.witness + } +} diff --git a/tests/stdlib/asserts/fuzz.rs b/tests/stdlib/asserts/fuzz.rs new file mode 100644 index 0000000..c641a2b --- /dev/null +++ b/tests/stdlib/asserts/fuzz.rs @@ -0,0 +1,195 @@ +use std::fmt::Debug; + +use simplex::fuzz::proptest::prelude::any; +use simplex::fuzz::proptest::strategy::{BoxedStrategy, Strategy}; +use simplex::{FuzzMode, TestContext}; + +use crate::asserts::{case, case::U256}; +use crate::common::fuzz::{accepts, rejects}; + +fn arb_u1() -> impl Strategy { + any::().prop_map(u8::from) +} + +fn arb_bool() -> impl Strategy { + any::() +} + +fn arb_u8() -> impl Strategy { + any::() +} + +fn arb_u16() -> impl Strategy { + any::() +} + +fn arb_u32() -> impl Strategy { + any::() +} + +fn arb_u64() -> impl Strategy { + any::() +} + +fn arb_u128() -> impl Strategy { + any::() +} + +fn arb_u256() -> impl Strategy { + any::() +} + +fn arb_distinct(values: impl Strategy + 'static) -> impl Strategy +where + T: PartialEq + Debug + 'static, +{ + let values: BoxedStrategy = values.boxed(); + + (values.clone(), values).prop_filter("distinct values", |(a, b)| a != b) +} + +// 0. assert_eq_1 +#[simplex::fuzz] +fn assert_eq_1_accepts_equal(context: TestContext) { + accepts(&context, arb_u1(), |a| case::assert_eq_1(a, a)); +} + +#[simplex::fuzz] +fn assert_eq_1_rejects_distinct(context: TestContext) { + rejects(&context, arb_distinct(arb_u1()), |(a, b)| { + case::assert_eq_1(a, b) + }); +} + +// 1. assert_eq_8 +#[simplex::fuzz] +fn assert_eq_8_accepts_equal(context: TestContext) { + accepts(&context, arb_u8(), |a| case::assert_eq_8(a, a)); +} + +#[simplex::fuzz] +fn assert_eq_8_rejects_distinct(context: TestContext) { + rejects(&context, arb_distinct(arb_u8()), |(a, b)| { + case::assert_eq_8(a, b) + }); +} + +// 2. assert_eq_16 +#[simplex::fuzz] +fn assert_eq_16_accepts_equal(context: TestContext) { + accepts(&context, arb_u16(), |a| case::assert_eq_16(a, a)); +} + +#[simplex::fuzz] +fn assert_eq_16_rejects_distinct(context: TestContext) { + rejects(&context, arb_distinct(arb_u16()), |(a, b)| { + case::assert_eq_16(a, b) + }); +} + +// 3. assert_eq_32 +#[simplex::fuzz] +fn assert_eq_32_accepts_equal(context: TestContext) { + accepts(&context, arb_u32(), |a| case::assert_eq_32(a, a)); +} + +#[simplex::fuzz] +fn assert_eq_32_rejects_distinct(context: TestContext) { + rejects(&context, arb_distinct(arb_u32()), |(a, b)| { + case::assert_eq_32(a, b) + }); +} + +// 4. assert_eq_64 +#[simplex::fuzz] +fn assert_eq_64_accepts_equal(context: TestContext) { + accepts(&context, arb_u64(), |a| case::assert_eq_64(a, a)); +} + +#[simplex::fuzz] +fn assert_eq_64_rejects_distinct(context: TestContext) { + rejects(&context, arb_distinct(arb_u64()), |(a, b)| { + case::assert_eq_64(a, b) + }); +} + +// 5. assert_eq_128 +#[simplex::fuzz] +fn assert_eq_128_accepts_equal(context: TestContext) { + accepts(&context, arb_u128(), |a| case::assert_eq_128(a, a)); +} + +#[simplex::fuzz] +fn assert_eq_128_rejects_distinct(context: TestContext) { + rejects(&context, arb_distinct(arb_u128()), |(a, b)| { + case::assert_eq_128(a, b) + }); +} + +// 6. assert_eq_256 +#[simplex::fuzz] +fn assert_eq_256_accepts_equal(context: TestContext) { + accepts(&context, arb_u256(), |a| case::assert_eq_256(a, a)); +} + +#[simplex::fuzz] +fn assert_eq_256_rejects_distinct(context: TestContext) { + rejects(&context, arb_distinct(arb_u256()), |(a, b)| { + case::assert_eq_256(a, b) + }); +} + +// 7. assert_eq_bool +#[simplex::fuzz] +fn assert_eq_bool_accepts_equal(context: TestContext) { + accepts(&context, arb_bool(), |a| case::assert_eq_bool(a, a)); +} + +#[simplex::fuzz] +fn assert_eq_bool_rejects_distinct(context: TestContext) { + rejects(&context, arb_distinct(arb_bool()), |(a, b)| { + case::assert_eq_bool(a, b) + }); +} + +// 8. assert_none_1 +#[simplex::fuzz] +fn assert_none_1_rejects_some(context: TestContext) { + rejects(&context, arb_u1(), |a| case::assert_none_1(Some(a))); +} + +// 9. assert_none_8 +#[simplex::fuzz] +fn assert_none_8_rejects_some(context: TestContext) { + rejects(&context, arb_u8(), |a| case::assert_none_8(Some(a))); +} + +// 10. assert_none_16 +#[simplex::fuzz] +fn assert_none_16_rejects_some(context: TestContext) { + rejects(&context, arb_u16(), |a| case::assert_none_16(Some(a))); +} + +// 11. assert_none_32 +#[simplex::fuzz] +fn assert_none_32_rejects_some(context: TestContext) { + rejects(&context, arb_u32(), |a| case::assert_none_32(Some(a))); +} + +// 12. assert_none_64 +#[simplex::fuzz] +fn assert_none_64_rejects_some(context: TestContext) { + rejects(&context, arb_u64(), |a| case::assert_none_64(Some(a))); +} + +// 13. assert_none_128 +#[simplex::fuzz] +fn assert_none_128_rejects_some(context: TestContext) { + rejects(&context, arb_u128(), |a| case::assert_none_128(Some(a))); +} + +// 14. assert_none_256 +#[simplex::fuzz] +fn assert_none_256_rejects_some(context: TestContext) { + rejects(&context, arb_u256(), |a| case::assert_none_256(Some(a))); +} diff --git a/tests/stdlib/asserts/mod.rs b/tests/stdlib/asserts/mod.rs new file mode 100644 index 0000000..3180816 --- /dev/null +++ b/tests/stdlib/asserts/mod.rs @@ -0,0 +1,3 @@ +mod case; +mod fuzz; +mod unit; diff --git a/tests/stdlib/asserts/unit.rs b/tests/stdlib/asserts/unit.rs new file mode 100644 index 0000000..202560c --- /dev/null +++ b/tests/stdlib/asserts/unit.rs @@ -0,0 +1,205 @@ +use crate::asserts::case; +use crate::asserts::case::U256; +use crate::common::case::DispatchCase; +use crate::common::core::Expect::AssertFailed; + +// 0. assert_eq_1 +#[test] +fn assert_eq_1_accepts_equal() { + case::assert_eq_1(0, 0).accepts(); + case::assert_eq_1(1, 1).accepts(); +} + +#[test] +fn assert_eq_1_rejects_distinct() { + case::assert_eq_1(0, 1).rejects(AssertFailed); + case::assert_eq_1(1, 0).rejects(AssertFailed); +} + +// 1. assert_eq_8 +#[test] +fn assert_eq_8_accepts_equal() { + case::assert_eq_8(0, 0).accepts(); + case::assert_eq_8(0xA5, 0xA5).accepts(); + case::assert_eq_8(u8::MAX, u8::MAX).accepts(); +} + +#[test] +fn assert_eq_8_rejects_distinct() { + case::assert_eq_8(0, 1).rejects(AssertFailed); + case::assert_eq_8(u8::MAX, 0).rejects(AssertFailed); +} + +// 2. assert_eq_16 +#[test] +fn assert_eq_16_accepts_equal() { + case::assert_eq_16(0, 0).accepts(); + case::assert_eq_16(0xA5A5, 0xA5A5).accepts(); + case::assert_eq_16(u16::MAX, u16::MAX).accepts(); +} + +#[test] +fn assert_eq_16_rejects_distinct() { + case::assert_eq_16(0, 1).rejects(AssertFailed); + case::assert_eq_16(0x00FF, 0x01FF).rejects(AssertFailed); +} + +// 3. assert_eq_32 +#[test] +fn assert_eq_32_accepts_equal() { + case::assert_eq_32(0, 0).accepts(); + case::assert_eq_32(0xA5A5_A5A5, 0xA5A5_A5A5).accepts(); + case::assert_eq_32(u32::MAX, u32::MAX).accepts(); +} + +#[test] +fn assert_eq_32_rejects_distinct() { + case::assert_eq_32(0, 1).rejects(AssertFailed); + case::assert_eq_32(u32::MAX, u32::MAX >> 1).rejects(AssertFailed); +} + +// 4. assert_eq_64 +#[test] +fn assert_eq_64_accepts_equal() { + case::assert_eq_64(0, 0).accepts(); + case::assert_eq_64(0xA5A5_A5A5_A5A5_A5A5, 0xA5A5_A5A5_A5A5_A5A5).accepts(); + case::assert_eq_64(u64::MAX, u64::MAX).accepts(); +} + +#[test] +fn assert_eq_64_rejects_distinct() { + case::assert_eq_64(0, 1).rejects(AssertFailed); + case::assert_eq_64(u64::MAX, u64::MAX >> 1).rejects(AssertFailed); +} + +// 5. assert_eq_128 +#[test] +fn assert_eq_128_accepts_equal() { + case::assert_eq_128(0, 0).accepts(); + case::assert_eq_128(u128::MAX, u128::MAX).accepts(); +} + +#[test] +fn assert_eq_128_rejects_distinct() { + case::assert_eq_128(0, 1).rejects(AssertFailed); + case::assert_eq_128(u128::from(u64::MAX), u128::MAX).rejects(AssertFailed); +} + +// 6. assert_eq_256 +#[test] +fn assert_eq_256_accepts_equal() { + let value: U256 = [0xA5; 32]; + + case::assert_eq_256([0; 32], [0; 32]).accepts(); + case::assert_eq_256(value, value).accepts(); + case::assert_eq_256([u8::MAX; 32], [u8::MAX; 32]).accepts(); +} + +#[test] +fn assert_eq_256_rejects_distinct() { + let mut first_byte_differs: U256 = [0; 32]; + first_byte_differs[0] = 1; + + let mut last_byte_differs: U256 = [0; 32]; + last_byte_differs[31] = 1; + + case::assert_eq_256([0; 32], first_byte_differs).rejects(AssertFailed); + case::assert_eq_256([0; 32], last_byte_differs).rejects(AssertFailed); +} + +// 7. assert_eq_bool +#[test] +fn assert_eq_bool_accepts_equal() { + case::assert_eq_bool(false, false).accepts(); + case::assert_eq_bool(true, true).accepts(); +} + +#[test] +fn assert_eq_bool_rejects_distinct() { + case::assert_eq_bool(false, true).rejects(AssertFailed); + case::assert_eq_bool(true, false).rejects(AssertFailed); +} + +// 8. assert_none_1 +#[test] +fn assert_none_1_accepts_none() { + case::assert_none_1(None).accepts(); +} + +#[test] +fn assert_none_1_rejects_some() { + case::assert_none_1(Some(0)).rejects(AssertFailed); + case::assert_none_1(Some(1)).rejects(AssertFailed); +} + +// 9. assert_none_8 +#[test] +fn assert_none_8_accepts_none() { + case::assert_none_8(None).accepts(); +} + +#[test] +fn assert_none_8_rejects_some() { + case::assert_none_8(Some(0)).rejects(AssertFailed); + case::assert_none_8(Some(u8::MAX)).rejects(AssertFailed); +} + +// 10. assert_none_16 +#[test] +fn assert_none_16_accepts_none() { + case::assert_none_16(None).accepts(); +} + +#[test] +fn assert_none_16_rejects_some() { + case::assert_none_16(Some(0)).rejects(AssertFailed); + case::assert_none_16(Some(u16::MAX)).rejects(AssertFailed); +} + +// 11. assert_none_32 +#[test] +fn assert_none_32_accepts_none() { + case::assert_none_32(None).accepts(); +} + +#[test] +fn assert_none_32_rejects_some() { + case::assert_none_32(Some(0)).rejects(AssertFailed); + case::assert_none_32(Some(u32::MAX)).rejects(AssertFailed); +} + +// 12. assert_none_64 +#[test] +fn assert_none_64_accepts_none() { + case::assert_none_64(None).accepts(); +} + +#[test] +fn assert_none_64_rejects_some() { + case::assert_none_64(Some(0)).rejects(AssertFailed); + case::assert_none_64(Some(u64::MAX)).rejects(AssertFailed); +} + +// 13. assert_none_128 +#[test] +fn assert_none_128_accepts_none() { + case::assert_none_128(None).accepts(); +} + +#[test] +fn assert_none_128_rejects_some() { + case::assert_none_128(Some(0)).rejects(AssertFailed); + case::assert_none_128(Some(u128::MAX)).rejects(AssertFailed); +} + +// 14. assert_none_256 +#[test] +fn assert_none_256_accepts_none() { + case::assert_none_256(None).accepts(); +} + +#[test] +fn assert_none_256_rejects_some() { + case::assert_none_256(Some([0; 32])).rejects(AssertFailed); + case::assert_none_256(Some([u8::MAX; 32])).rejects(AssertFailed); +} diff --git a/tests/stdlib/binary.rs b/tests/stdlib/binary.rs index ca00390..eae348a 100644 --- a/tests/stdlib/binary.rs +++ b/tests/stdlib/binary.rs @@ -1,12 +1,14 @@ -use crate::common::core::{Expect, run}; - use simplicityhl_std::artifacts::tests::binary::BinaryProgram as BinaryTestProgram; use simplicityhl_std::artifacts::tests::binary::derived_binary::{ BinaryArguments as BinaryTestArguments, BinaryWitness as BinaryTestWitness, }; -#[simplex::test] -fn all_ops(context: simplex::TestContext) -> anyhow::Result<()> { +use crate::common::core::Expect; +use crate::common::unit; + +#[test] +fn all_ops() { let program = BinaryTestProgram::new(&BinaryTestArguments {}); - run(&context, program, BinaryTestWitness {}, Expect::Ok) + + unit::check(program.as_ref(), BinaryTestWitness {}, Expect::Ok); } diff --git a/tests/stdlib/common/case.rs b/tests/stdlib/common/case.rs index 6849f2c..c95a1d7 100644 --- a/tests/stdlib/common/case.rs +++ b/tests/stdlib/common/case.rs @@ -1,10 +1,10 @@ +use simplex::fuzz::FuzzProgramTrait; use simplex::program::{Program, ProgramSchema}; use simplex::simplicityhl::Arguments; - -use simplex::fuzz::FuzzProgramTrait; +use simplex::transaction::PartialOutput; use crate::common::core::Expect; -use crate::common::unit; +use crate::common::unit::{self, UnitTx}; pub trait DispatchCase: Sized + 'static { type Program: FuzzProgramTrait; @@ -15,6 +15,10 @@ pub trait DispatchCase: Sized + 'static { fn into_witness(self) -> ::Witness; + fn leading_outputs(&self) -> Vec { + Vec::new() + } + fn program() -> Program { let arguments: Arguments = Self::arguments().into(); @@ -24,7 +28,7 @@ pub trait DispatchCase: Sized + 'static { #[track_caller] fn accepts(self) { - unit::check(&Self::program(), self.into_witness(), Expect::Ok); + self.check(Expect::Ok); } #[track_caller] @@ -35,6 +39,15 @@ pub trait DispatchCase: Sized + 'static { "use `accepts` for cases that must succeed" ); - unit::check(&Self::program(), self.into_witness(), reason); + self.check(reason); + } + + #[track_caller] + fn check(self, expect: Expect) { + let tx = UnitTx { + leading_outputs: self.leading_outputs(), + }; + + unit::check_in(&Self::program(), self.into_witness(), tx, expect); } } diff --git a/tests/stdlib/common/core.rs b/tests/stdlib/common/core.rs index f9c9e82..06963cf 100644 --- a/tests/stdlib/common/core.rs +++ b/tests/stdlib/common/core.rs @@ -227,7 +227,7 @@ where /// Create an op return script from bytes, where the first byte indicates whether /// the bytes are an immediate push or raw opcodes -fn create_op_return_script(data: &[u8]) -> Script { +pub fn create_op_return_script(data: &[u8]) -> Script { let mut builder = Builder::new().push_opcode(opcodes::all::OP_RETURN); if data[0] == 0x00 { diff --git a/tests/stdlib/common/fuzz.rs b/tests/stdlib/common/fuzz.rs index 3c60791..a92aa66 100644 --- a/tests/stdlib/common/fuzz.rs +++ b/tests/stdlib/common/fuzz.rs @@ -1,9 +1,8 @@ use std::fmt::Debug; -use simplex::simplicityhl::Arguments; - use simplex::fuzz::proptest::strategy::Strategy; use simplex::fuzz::{FuzzProgramValues, Scenario}; +use simplex::simplicityhl::Arguments; use simplex::{FuzzMode, TestContext}; use simplex::fuzz::Expect; @@ -42,13 +41,25 @@ pub fn check( ) where C: DispatchCase, T: Debug + 'static, +{ + check_scenario(context, Scenario::default(), inputs, build, expect); +} + +pub fn check_scenario( + context: &TestContext, + mut scenario: Scenario, + inputs: impl Strategy + 'static, + build: impl Fn(T) -> C + 'static, + expect: Expect, +) where + C: DispatchCase, + T: Debug + 'static, { let arguments: Arguments = C::arguments().into(); let values = inputs.prop_map(move |input| { FuzzProgramValues::from((arguments.clone(), build(input).into_witness().into())) }); - let mut scenario = Scenario::default(); scenario .add::(C::PROGRAM_NAME) .spends(DEFAULT_SPENT_INPUT) diff --git a/tests/stdlib/common/unit.rs b/tests/stdlib/common/unit.rs index a87a6fe..1cc54d5 100644 --- a/tests/stdlib/common/unit.rs +++ b/tests/stdlib/common/unit.rs @@ -1,19 +1,29 @@ use simplex::program::{Program, ProgramError, ProgramTrait}; use simplex::provider::SimplicityNetwork; -use simplex::simplicityhl::WitnessValues; use simplex::simplicityhl::elements::{OutPoint, Script, Txid}; use simplex::simplicityhl::simplicity::bit_machine::ExecutionError; +use simplex::simplicityhl::{WitnessValues, elements}; use simplex::transaction::{FinalTransaction, PartialInput, PartialOutput, ProgramInput, UTXO}; use crate::common::core::Expect; const AMOUNT: u64 = 1_000; +const OP_PUSHNUM_1: elements::opcodes::All = elements::opcodes::all::OP_PUSHNUM_1; -pub fn run(program: &Program, witness: impl Into) -> Result<(), ProgramError> { +#[derive(Clone, Debug, Default)] +pub struct UnitTx { + pub leading_outputs: Vec, +} + +pub fn run( + program: &Program, + witness: impl Into, + tx: UnitTx, +) -> Result<(), ProgramError> { let network = SimplicityNetwork::default_regtest(); let witness = witness.into(); - let (pst, _) = unit_transaction(program, witness.clone(), network) + let (pst, _) = unit_transaction(program, witness.clone(), tx, network) .extract_pst() .expect("the unit transaction is explicit"); @@ -24,7 +34,14 @@ pub fn run(program: &Program, witness: impl Into) -> Result<(), P /// matching `expect`. #[track_caller] pub fn check(program: &Program, witness: impl Into, expect: Expect) { - match outcome(run(program, witness)) { + check_in(program, witness, UnitTx::default(), expect); +} + +/// A case that fails before the program runs (e.g. a malformed witness) panics instead of +/// matching `expect`. Uses custom `UnitTx` for pattern matching. +#[track_caller] +pub fn check_in(program: &Program, witness: impl Into, tx: UnitTx, expect: Expect) { + match outcome(run(program, witness, tx)) { Ok(actual) => assert_eq!(actual, expect, "unexpected program outcome"), Err(error) => panic!("the case failed before the program ran: {error}"), } @@ -47,6 +64,7 @@ fn outcome(result: Result<(), ProgramError>) -> Result { fn unit_transaction( program: &Program, witness: WitnessValues, + tx: UnitTx, network: SimplicityNetwork, ) -> FinalTransaction { let asset = network.policy_asset(); @@ -63,7 +81,16 @@ fn unit_transaction( ProgramInput::new(Box::new(program.clone()), witness), None, ); - ft.add_output(PartialOutput::new(Script::from(vec![0x51]), AMOUNT, asset)); + + for output in tx.leading_outputs { + ft.add_output(output); + } + + ft.add_output(PartialOutput::new( + Script::from(vec![OP_PUSHNUM_1.into_u8()]), + AMOUNT, + asset, + )); ft } diff --git a/tests/stdlib/op_return.rs b/tests/stdlib/op_return.rs deleted file mode 100644 index e69de29..0000000 diff --git a/tests/stdlib/utxo/op_return.rs b/tests/stdlib/utxo/op_return.rs deleted file mode 100644 index 98e4164..0000000 --- a/tests/stdlib/utxo/op_return.rs +++ /dev/null @@ -1,761 +0,0 @@ -use rand::Rng; - -use simplex::either::Either; -use simplex::simplicityhl::elements::Script; - -use crate::common::core::{Expect, run, run_with_op_return}; - -use simplicityhl_std::artifacts::tests::utxo::op_return::OpReturnProgram as OpReturnTestProgram; -use simplicityhl_std::artifacts::tests::utxo::op_return::derived_op_return::{ - OpReturnArguments as OpReturnTestArguments, OpReturnWitness as OpReturnTestWitness, -}; - -use FunctionToTest::*; - -enum FunctionToTest { - GetEntryOption, - PushedDataFromEntry, - GetPushedData, - OpXMinus1FromEntry, - GetOpXMinus1, - NegateOrReservedFromEntry, - GetNegateOrReserved, - KindOfEntry, - IsOutputOpReturn, - HasEntry, - AssertOutputIsOpReturn, - AssertHasEntry, -} - -type OpReturnEntry = Either<(u8, [u8; 32]), Either>; - -const DEFAULT_DATA: &[u8; 2] = &[0, 1]; -const OP_PUSHNUM_NEG1: &[u8] = &[0x01, 0x4f]; -const OP_RESERVED: &[u8] = &[0x01, 0x50]; - -fn program() -> OpReturnTestProgram { - OpReturnTestProgram::new(&OpReturnTestArguments {}) -} - -/// One dispatch arm of the contract, plus the witness it reads and whether the -/// spend carries an `OP_RETURN` output. -struct Case { - witness: OpReturnTestWitness, - data: Option<&'static [u8]>, -} - -fn case(function: FunctionToTest) -> Case { - Case { - witness: OpReturnTestWitness { - function_index: function as u8, - output_index: 0, - entry_index: 0, - entry: Either::Right(Either::Right(0)), - expected_entry: None, - expected_value: None, - expected_bool: false, - expected_hash: [0; 32], - }, - data: None, - } -} - -impl Case { - /// `output_index`: the output the arm inspects. - fn output_index(mut self, output_index: u32) -> Self { - self.witness.output_index = output_index; - self - } - - /// `entry_index`: the entry index the arm inspects. - fn entry_index(mut self, entry_index: u32) -> Self { - self.witness.entry_index = entry_index; - self - } - - /// `entry`:the entry passed to the arm. - fn entry(mut self, entry: OpReturnEntry) -> Self { - self.witness.entry = entry; - self - } - - /// `expected_entry`: the entry the arm expects. - fn expected_entry(mut self, expected_entry: Option) -> Self { - self.witness.expected_entry = expected_entry; - self - } - - /// `expected_value`: the value the arm expects. - fn expected_value(mut self, expected_value: Option) -> Self { - self.witness.expected_value = expected_value; - self - } - - /// `expected_bool`: the boolean the arm should report. - fn expected_bool(mut self, expected_bool: bool) -> Self { - self.witness.expected_bool = expected_bool; - self - } - - /// `expected_hash`: the hash of the pushed data the arm inspects. - fn expected_hash(mut self, expected_hash: [u8; 32]) -> Self { - self.witness.expected_hash = expected_hash; - self - } - - /// Add an `OP_RETURN` output carrying `data` to the spend. - /// First byte is a build indicator (0 means single data push, 1 means raw opcode push) - fn op_return(mut self, data: &'static [u8]) -> Self { - self.data = Some(data); - self - } - - /// Fund, spend, and expect the spend to succeed. - fn run(self, context: &simplex::TestContext) -> anyhow::Result<()> { - self.expecting(context, Expect::Ok) - } - - /// Fund, spend, and expect `expect`. - fn expecting(self, context: &simplex::TestContext, expect: Expect) -> anyhow::Result<()> { - match self.data { - Some(data) => run_with_op_return(context, program(), self.witness, expect, data), - None => run(context, program(), self.witness, expect), - } - } -} - -pub fn hash_of_pushed_data(data: &[u8]) -> [u8; 32] { - Script::from(data.to_vec()).wscript_hash().to_byte_array() -} - -#[simplex::test] -fn get_entry_option(context: simplex::TestContext) -> anyhow::Result<()> { - /// 0 = push data (0x12, 0x23), 1 = OP_1, 2 = OP_1NEGATE, 3 = OP_RESERVED. - const MIXED_ENTRIES: &[u8] = &[1, 0x02, 0x12, 0x23, 0x51, 0x4f, 0x50]; - const PUSHED_DATA: &[u8] = &[0x12, 0x23]; - - let cases: [(u32, OpReturnEntry); 4] = [ - (0, Either::Left((0, hash_of_pushed_data(PUSHED_DATA)))), - (1, Either::Right(Either::Right(0))), - (2, Either::Right(Either::Left(0))), - (3, Either::Right(Either::Left(1))), - ]; - - for (entry_index, entry) in cases { - case(GetEntryOption) - .entry_index(entry_index) - .expected_entry(Some(entry)) - .op_return(MIXED_ENTRIES) - .run(&context) - .unwrap_or_else(|_| panic!("entry index = {entry_index}")); - } - - Ok(()) -} - -#[simplex::test] -fn get_entry_option_nonexistent_output(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = u32::MAX; - let entry_index = 0; - let expected_entry: Option = None; - - case(GetEntryOption) - .output_index(output_index) - .entry_index(entry_index) - .expected_entry(expected_entry) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn get_entry_option_not_op_return_output(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - let expected_entry: Option = None; - - case(GetEntryOption) - .output_index(output_index) - .entry_index(entry_index) - .expected_entry(expected_entry) - .run(&context) -} - -#[simplex::test] -fn get_entry_option_out_of_range_entry(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = u32::MAX; - let expected_entry: Option = None; - - case(GetEntryOption) - .output_index(output_index) - .entry_index(entry_index) - .expected_entry(expected_entry) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn pushed_data_from_entry(context: simplex::TestContext) -> anyhow::Result<()> { - let indicator = 0_u8; - let hash = hash_of_pushed_data(&DEFAULT_DATA[1..]); - - let entry: OpReturnEntry = Either::Left((indicator, hash)); - - case(PushedDataFromEntry) - .entry(entry) - .expected_value(Some(indicator)) - .expected_hash(hash) - .run(&context) -} - -#[simplex::test] -fn pushed_data_from_entry_op_x_returns_none(context: simplex::TestContext) -> anyhow::Result<()> { - case(PushedDataFromEntry) - .entry(Either::Right(Either::Right(0))) - .expected_value(None) - .run(&context) -} - -#[simplex::test] -fn get_pushdata0(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - let indicator = 0_u8; - let hash = hash_of_pushed_data(&DEFAULT_DATA[1..]); - - case(GetPushedData) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(Some(indicator)) - .expected_hash(hash) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn get_pushdata1(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - const DATA: [u8; 77] = [0; 77]; // a 76-byte push, which needs OP_PUSHDATA1 - - let indicator = 1_u8; - let hash = hash_of_pushed_data(&DATA[1..]); - - case(GetPushedData) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(Some(indicator)) - .expected_hash(hash) - .op_return(&DATA) - .run(&context) -} - -#[simplex::test] -fn get_pushdata2(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - const DATA: [u8; 7] = [1, 0x4d, 0x03, 0x00, 0xde, 0xad, 0xbe]; // the last 3 bytes are the pushed data - - let indicator = 2_u8; - let hash = hash_of_pushed_data(&DATA[4..]); - - case(GetPushedData) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(Some(indicator)) - .expected_hash(hash) - .op_return(&DATA) - .run(&context) -} - -#[simplex::test] -fn get_pushdata4(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - const DATA: [u8; 9] = [1, 0x4e, 0x03, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe]; // the last 3 bytes are the pushed data - - let indicator = 3_u8; - let hash = hash_of_pushed_data(&DATA[6..]); - - case(GetPushedData) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(Some(indicator)) - .expected_hash(hash) - .op_return(&DATA) - .run(&context) -} - -#[simplex::test] -fn get_pushed_data_nonexistent_output(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = u32::MAX; - let entry_index = 0; - - case(GetPushedData) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn get_pushed_data_not_op_return_output(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - case(GetPushedData) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .run(&context) -} - -#[simplex::test] -fn get_pushed_data_out_of_range_entry(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = u32::MAX; - - case(GetPushedData) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn get_pushed_data_neg1_returns_none(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - case(GetPushedData) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .op_return(OP_PUSHNUM_NEG1) - .run(&context) -} - -#[simplex::test] -fn op_x_minus_1_from_entry(context: simplex::TestContext) -> anyhow::Result<()> { - for i in 0..16 { - let entry: OpReturnEntry = Either::Right(Either::Right(i)); - - case(OpXMinus1FromEntry) - .entry(entry) - .expected_value(Some(i)) - .run(&context) - .unwrap_or_else(|_| panic!("entry index = {i}")); - } - - Ok(()) -} - -#[simplex::test] -fn op_x_minus_1_from_entry_returns_none(context: simplex::TestContext) -> anyhow::Result<()> { - let entry: OpReturnEntry = Either::Right(Either::Left(1)); // OP_RESERVED - - case(OpXMinus1FromEntry) - .entry(entry) - .expected_value(None) - .run(&context) -} - -#[simplex::test] -fn get_op_x_minus_1(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - - // the first byte is the build indicator - const OP_1_TO_16: &[u8] = &[ - 0x01, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, - 0x5f, 0x60, - ]; - - for i in 0..16 { - case(GetOpXMinus1) - .output_index(output_index) - .entry_index(i) - .expected_value(Some(i as u8)) - .op_return(OP_1_TO_16) - .run(&context) - .unwrap_or_else(|err| panic!("{err}, entry index = {i}")); - } - - Ok(()) -} - -#[simplex::test] -fn get_op_x_minus_1_nonexistent_output(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = u32::MAX; - let entry_index = 0; - - case(GetOpXMinus1) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn get_op_x_minus_1_not_op_return_output(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - case(GetOpXMinus1) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .run(&context) -} - -#[simplex::test] -fn get_op_x_minus_1_out_of_range_entry(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = u32::MAX; - const OP_1: &[u8] = &[0x01, 0x51]; - - case(GetOpXMinus1) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .op_return(OP_1) - .run(&context) -} - -#[simplex::test] -fn get_op_x_minus_1_returns_none(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - case(GetOpXMinus1) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .op_return(OP_RESERVED) - .run(&context) -} - -#[simplex::test] -fn negate_or_reserved_from_entry_negate(context: simplex::TestContext) -> anyhow::Result<()> { - let indicator = 0_u8; - - let entry: OpReturnEntry = Either::Right(Either::Left(indicator)); - - case(NegateOrReservedFromEntry) - .entry(entry) - .expected_value(Some(indicator)) - .run(&context) -} - -#[simplex::test] -fn negate_or_reserved_from_entry_reserved(context: simplex::TestContext) -> anyhow::Result<()> { - let indicator = 1_u8; - - let entry: OpReturnEntry = Either::Right(Either::Left(indicator)); - - case(NegateOrReservedFromEntry) - .entry(entry) - .expected_value(Some(indicator)) - .run(&context) -} - -#[simplex::test] -fn negate_or_reserved_from_entry_returns_none(context: simplex::TestContext) -> anyhow::Result<()> { - let entry: OpReturnEntry = Either::Left((0, hash_of_pushed_data(&[1]))); - - case(NegateOrReservedFromEntry) - .entry(entry) - .expected_value(None) - .run(&context) -} - -#[simplex::test] -fn get_negate_or_reserved_negate(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - let indicator = 0_u8; - - case(GetNegateOrReserved) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(Some(indicator)) - .op_return(OP_PUSHNUM_NEG1) - .run(&context) -} - -#[simplex::test] -fn get_negate_or_reserved_reserved(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - let indicator = 1_u8; - - case(GetNegateOrReserved) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(Some(indicator)) - .op_return(OP_RESERVED) - .run(&context) -} - -#[simplex::test] -fn get_negate_or_reserved_nonexistent_output(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = u32::MAX; - let entry_index = 0; - - case(GetNegateOrReserved) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn get_negate_or_reserved_not_op_return_output( - context: simplex::TestContext, -) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - case(GetNegateOrReserved) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .run(&context) -} - -#[simplex::test] -fn get_negate_or_reserved_out_of_range_entry(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = u32::MAX; - - case(GetNegateOrReserved) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .op_return(OP_RESERVED) - .run(&context) -} - -#[simplex::test] -fn get_negate_or_reserved_returns_none(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - case(GetNegateOrReserved) - .output_index(output_index) - .entry_index(entry_index) - .expected_value(None) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn kind_of_entry_pushed_data(context: simplex::TestContext) -> anyhow::Result<()> { - let entry: OpReturnEntry = Either::Left((0, hash_of_pushed_data(&[1]))); - - case(KindOfEntry) - .entry(entry) - .expected_value(Some(0)) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn kind_of_entry_op_x_minus_1(context: simplex::TestContext) -> anyhow::Result<()> { - let op_x_minus_1 = rand::thread_rng().gen_range(0..16); - let entry: OpReturnEntry = Either::Right(Either::Right(op_x_minus_1)); - - case(KindOfEntry) - .entry(entry) - .expected_value(Some(1)) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn kind_of_entry_negate_or_reserved_negate(context: simplex::TestContext) -> anyhow::Result<()> { - let indicator = 0_u8; - let entry: OpReturnEntry = Either::Right(Either::Left(indicator)); - - case(KindOfEntry) - .entry(entry) - .expected_value(Some(2)) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn kind_of_entry_negate_or_reserved_reserved(context: simplex::TestContext) -> anyhow::Result<()> { - let indicator = 1_u8; - let entry: OpReturnEntry = Either::Right(Either::Left(indicator)); - - case(KindOfEntry) - .entry(entry) - .expected_value(Some(2)) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn is_output_op_return_true(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - case(IsOutputOpReturn) - .output_index(output_index) - .entry_index(entry_index) - .expected_bool(true) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn is_output_op_returns_nonexistent_output_false( - context: simplex::TestContext, -) -> anyhow::Result<()> { - let output_index = u32::MAX; - - case(IsOutputOpReturn) - .output_index(output_index) - .expected_bool(false) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn is_output_op_return_false(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - - case(IsOutputOpReturn) - .output_index(output_index) - .expected_bool(false) - .run(&context) -} - -#[simplex::test] -fn has_entry_true(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = 0; - - case(HasEntry) - .output_index(output_index) - .entry_index(entry_index) - .expected_bool(true) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn has_entry_nonexistent_output_false(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = u32::MAX; - - case(HasEntry) - .output_index(output_index) - .expected_bool(false) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn has_entry_out_of_range_entry_false(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = u32::MAX; - - case(HasEntry) - .output_index(output_index) - .entry_index(entry_index) - .expected_bool(false) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn has_entry_output_not_op_return_false(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - - case(HasEntry) - .output_index(output_index) - .expected_bool(false) - .run(&context) -} - -#[simplex::test] -fn assert_output_is_op_return_pass(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - - case(AssertOutputIsOpReturn) - .output_index(output_index) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn assert_output_is_op_returns_nonexistent_output_fails( - context: simplex::TestContext, -) -> anyhow::Result<()> { - let output_index = u32::MAX; - - case(AssertOutputIsOpReturn) - .output_index(output_index) - .op_return(DEFAULT_DATA) - .expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_output_is_op_return_fail(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - - case(AssertOutputIsOpReturn) - .output_index(output_index) - .expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_has_entry_pass(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - - case(AssertHasEntry) - .output_index(output_index) - .op_return(DEFAULT_DATA) - .run(&context) -} - -#[simplex::test] -fn assert_has_entry_nonexistent_output_fails(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = u32::MAX; - - case(AssertHasEntry) - .output_index(output_index) - .op_return(DEFAULT_DATA) - .expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_has_entry_output_not_op_return_fails( - context: simplex::TestContext, -) -> anyhow::Result<()> { - let output_index = 0; - - case(AssertHasEntry) - .output_index(output_index) - .expecting(&context, Expect::AssertFailed) -} - -#[simplex::test] -fn assert_has_entry_out_of_range_entry_fails(context: simplex::TestContext) -> anyhow::Result<()> { - let output_index = 0; - let entry_index = u32::MAX; - - case(AssertHasEntry) - .output_index(output_index) - .entry_index(entry_index) - .op_return(DEFAULT_DATA) - .expecting(&context, Expect::AssertFailed) -} diff --git a/tests/stdlib/utxo/op_return/case.rs b/tests/stdlib/utxo/op_return/case.rs new file mode 100644 index 0000000..611ce3a --- /dev/null +++ b/tests/stdlib/utxo/op_return/case.rs @@ -0,0 +1,204 @@ +use simplex::either::Either; +use simplex::simplicityhl::elements::{AssetId, Script}; +use simplex::transaction::PartialOutput; + +use simplicityhl_std::artifacts::tests::utxo::op_return::OpReturnProgram as OpReturnTestProgram; +use simplicityhl_std::artifacts::tests::utxo::op_return::derived_op_return::{ + OpReturnArguments as OpReturnTestArguments, OpReturnWitness as OpReturnTestWitness, +}; + +use crate::common::case::DispatchCase; +use crate::common::core::create_op_return_script; + +pub type OpReturnEntry = Either>; + +pub type PushedData = (u8, [u8; 32]); + +#[derive(Clone, Copy)] +enum FunctionToTest { + GetEntryOption = 0, + PushedDataFromEntry = 1, + GetPushedData = 2, + OpXMinus1FromEntry = 3, + GetOpXMinus1 = 4, + NegateOrReservedFromEntry = 5, + GetNegateOrReserved = 6, + KindOfEntry = 7, + IsOutputOpReturn = 8, + HasEntry = 9, + AssertOutputIsOpReturn = 10, + AssertHasEntry = 11, +} + +pub fn pushed_data(indicator: u8, hash: [u8; 32]) -> OpReturnEntry { + Either::Left((indicator, hash)) +} + +pub fn negate_or_reserved(indicator: u8) -> OpReturnEntry { + Either::Right(Either::Left(indicator)) +} + +pub fn op_x_minus_1(value: u8) -> OpReturnEntry { + Either::Right(Either::Right(value)) +} + +pub fn hash_of_pushed_data(data: &[u8]) -> [u8; 32] { + Script::from(data.to_vec()).wscript_hash().to_byte_array() +} + +pub fn op_return_output(data: &[u8]) -> PartialOutput { + PartialOutput::new(create_op_return_script(data), 0, AssetId::default()) +} + +pub fn get_entry_option( + output_index: u32, + entry_index: u32, + expected: Option, +) -> Case { + Case::new(FunctionToTest::GetEntryOption) + .at(output_index, entry_index) + .expect_entry(expected) +} + +pub fn pushed_data_from_entry(entry: OpReturnEntry, expected: Option) -> Case { + Case::new(FunctionToTest::PushedDataFromEntry) + .entry(entry) + .expect_pushed_data(expected) +} + +pub fn get_pushed_data(output_index: u32, entry_index: u32, expected: Option) -> Case { + Case::new(FunctionToTest::GetPushedData) + .at(output_index, entry_index) + .expect_pushed_data(expected) +} + +pub fn op_x_minus_1_from_entry(entry: OpReturnEntry, expected: Option) -> Case { + Case::new(FunctionToTest::OpXMinus1FromEntry) + .entry(entry) + .expect_value(expected) +} + +pub fn get_op_x_minus_1(output_index: u32, entry_index: u32, expected: Option) -> Case { + Case::new(FunctionToTest::GetOpXMinus1) + .at(output_index, entry_index) + .expect_value(expected) +} + +pub fn negate_or_reserved_from_entry(entry: OpReturnEntry, expected: Option) -> Case { + Case::new(FunctionToTest::NegateOrReservedFromEntry) + .entry(entry) + .expect_value(expected) +} + +pub fn get_negate_or_reserved(output_index: u32, entry_index: u32, expected: Option) -> Case { + Case::new(FunctionToTest::GetNegateOrReserved) + .at(output_index, entry_index) + .expect_value(expected) +} + +pub fn kind_of_entry(entry: OpReturnEntry, expected: u8) -> Case { + Case::new(FunctionToTest::KindOfEntry) + .entry(entry) + .expect_value(Some(expected)) +} + +pub fn is_output_op_return(output_index: u32, expected: bool) -> Case { + Case::new(FunctionToTest::IsOutputOpReturn) + .at(output_index, 0) + .expect_bool(expected) +} + +pub fn has_entry(output_index: u32, entry_index: u32, expected: bool) -> Case { + Case::new(FunctionToTest::HasEntry) + .at(output_index, entry_index) + .expect_bool(expected) +} + +pub fn assert_output_is_op_return(output_index: u32) -> Case { + Case::new(FunctionToTest::AssertOutputIsOpReturn).at(output_index, 0) +} + +pub fn assert_has_entry(output_index: u32, entry_index: u32) -> Case { + Case::new(FunctionToTest::AssertHasEntry).at(output_index, entry_index) +} + +#[derive(Clone, Debug)] +pub struct Case { + witness: OpReturnTestWitness, + data: Option<&'static [u8]>, +} + +impl Case { + fn new(function: FunctionToTest) -> Self { + Self { + witness: OpReturnTestWitness { + function_index: function as u8, + output_index: 0, + entry_index: 0, + entry: op_x_minus_1(0), + expected_entry: None, + expected_value: None, + expected_bool: false, + expected_hash: [0; 32], + }, + data: None, + } + } + + pub fn op_return(mut self, data: &'static [u8]) -> Self { + self.data = Some(data); + self + } + + fn at(mut self, output_index: u32, entry_index: u32) -> Self { + self.witness.output_index = output_index; + self.witness.entry_index = entry_index; + self + } + + fn entry(mut self, entry: OpReturnEntry) -> Self { + self.witness.entry = entry; + self + } + + fn expect_entry(mut self, expected_entry: Option) -> Self { + self.witness.expected_entry = expected_entry; + self + } + + fn expect_value(mut self, expected_value: Option) -> Self { + self.witness.expected_value = expected_value; + self + } + + fn expect_pushed_data(mut self, expected: Option) -> Self { + let (indicator, hash) = expected.unzip(); + + self.witness.expected_value = indicator; + self.witness.expected_hash = hash.unwrap_or_default(); + self + } + + fn expect_bool(mut self, expected_bool: bool) -> Self { + self.witness.expected_bool = expected_bool; + self + } +} + +impl DispatchCase for Case { + type Program = OpReturnTestProgram; + + const PROGRAM_NAME: &'static str = "utxo::op_return"; + + fn arguments() -> OpReturnTestArguments { + OpReturnTestArguments {} + } + + fn into_witness(self) -> OpReturnTestWitness { + self.witness + } + + fn leading_outputs(&self) -> Vec { + self.data.map(op_return_output).into_iter().collect() + } +} diff --git a/tests/stdlib/utxo/op_return/fuzz.rs b/tests/stdlib/utxo/op_return/fuzz.rs new file mode 100644 index 0000000..13d9888 --- /dev/null +++ b/tests/stdlib/utxo/op_return/fuzz.rs @@ -0,0 +1,113 @@ +use std::fmt::Debug; + +use simplex::fuzz::proptest::prelude::{Just, any}; +use simplex::fuzz::proptest::strategy::Strategy; +use simplex::fuzz::{Expect, Scenario}; +use simplex::transaction::{FinalTransaction, PartialInput, PartialOutput, UTXO}; +use simplex::{FuzzMode, TestContext}; + +use crate::common::fuzz::check_scenario; +use crate::utxo::op_return::case; +use crate::utxo::op_return::case::Case; + +const DEFAULT_DATA: &[u8] = &[0, 1]; + +fn scenario(data: Option<&[u8]>) -> Scenario { + let mut tx = FinalTransaction::new(); + tx.add_input(PartialInput::new(UTXO::default()), None); + + match data { + Some(data) => tx.add_output(case::op_return_output(data)), + None => tx.add_output(PartialOutput::default()), + } + + Scenario::new(tx) +} + +fn check( + context: &TestContext, + op_return_data: Option<&[u8]>, + output_indices: impl Strategy + 'static, + build: impl Fn(T) -> Case + 'static, + expect: Expect, +) { + check_scenario( + context, + scenario(op_return_data), + output_indices, + build, + expect, + ); +} + +fn arb_non_zero_u32() -> impl Strategy { + any::().prop_filter("output index should not be zero", |index| *index != 0) +} + +// 8. is_output_op_return +#[simplex::fuzz] +fn is_output_op_return_true(context: TestContext) { + check( + &context, + Some(DEFAULT_DATA), + Just(0_u32), + |output_index| case::is_output_op_return(output_index, true), + Expect::Accept, + ); +} + +#[simplex::fuzz] +fn is_output_op_return_empty_index_false(context: TestContext) { + check( + &context, + Some(DEFAULT_DATA), + arb_non_zero_u32(), + |output_index| case::is_output_op_return(output_index, false), + Expect::Accept, + ); +} + +#[simplex::fuzz] +fn is_output_op_return_false(context: TestContext) { + check( + &context, + None, + Just(0_u32), + |output_index| case::is_output_op_return(output_index, false), + Expect::Accept, + ); +} + +// 10. assert_output_is_op_return +#[simplex::fuzz] +fn assert_output_is_op_return_pass(context: TestContext) { + check( + &context, + Some(DEFAULT_DATA), + Just(0_u32), + case::assert_output_is_op_return, + Expect::Accept, + ); +} + +#[simplex::fuzz] +fn assert_output_is_op_return_empty_index_fail(context: TestContext) { + check( + &context, + Some(DEFAULT_DATA), + arb_non_zero_u32(), + case::assert_output_is_op_return, + Expect::Reject, + ); +} + +#[simplex::fuzz] +fn assert_output_is_op_return_fail(context: TestContext) { + check( + &context, + None, + Just(0_u32), + case::assert_output_is_op_return, + Expect::Reject, + ); +} diff --git a/tests/stdlib/utxo/op_return/mod.rs b/tests/stdlib/utxo/op_return/mod.rs new file mode 100644 index 0000000..3180816 --- /dev/null +++ b/tests/stdlib/utxo/op_return/mod.rs @@ -0,0 +1,3 @@ +mod case; +mod fuzz; +mod unit; diff --git a/tests/stdlib/utxo/op_return/unit.rs b/tests/stdlib/utxo/op_return/unit.rs new file mode 100644 index 0000000..31bdddc --- /dev/null +++ b/tests/stdlib/utxo/op_return/unit.rs @@ -0,0 +1,431 @@ +use crate::common::case::DispatchCase; +use crate::common::core::Expect::AssertFailed; +use crate::utxo::op_return::case; +use crate::utxo::op_return::case::{ + hash_of_pushed_data, negate_or_reserved, op_x_minus_1, pushed_data, +}; + +const DEFAULT_DATA: &[u8] = &[0, 1]; +const OP_PUSHNUM_NEG1: &[u8] = &[0x01, 0x4f]; +const OP_RESERVED: &[u8] = &[0x01, 0x50]; +const OP_1: &[u8] = &[0x01, 0x51]; +const OP_1_TO_16: &[u8] = &[ + 0x01, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f, + 0x60, +]; + +const NONEXISTENT_OUTPUT: u32 = u32::MAX; +const OUT_OF_RANGE_ENTRY: u32 = u32::MAX; + +// 0. get_entry_option +#[test] +fn get_entry_option() { + // 0 = push data (0x12, 0x23), 1 = OP_1, 2 = OP_1NEGATE, 3 = OP_RESERVED. + const MIXED_ENTRIES: &[u8] = &[1, 0x02, 0x12, 0x23, 0x51, 0x4f, 0x50]; + const PUSHED_DATA: &[u8] = &[0x12, 0x23]; + + let entries = [ + pushed_data(0, hash_of_pushed_data(PUSHED_DATA)), + op_x_minus_1(0), + negate_or_reserved(0), + negate_or_reserved(1), + ]; + + for (entry_index, entry) in (0..).zip(entries) { + case::get_entry_option(0, entry_index, Some(entry)) + .op_return(MIXED_ENTRIES) + .accepts(); + } +} + +#[test] +fn get_entry_option_nonexistent_output() { + case::get_entry_option(NONEXISTENT_OUTPUT, 0, None) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn get_entry_option_not_op_return_output() { + case::get_entry_option(0, 0, None).accepts(); + case::get_entry_option(1, 0, None) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn get_entry_option_out_of_range_entry() { + case::get_entry_option(0, 1, None) + .op_return(DEFAULT_DATA) + .accepts(); + case::get_entry_option(0, OUT_OF_RANGE_ENTRY, None) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn get_entry_option_rejects_wrong_expectation() { + case::get_entry_option(0, 0, None) + .op_return(DEFAULT_DATA) + .rejects(AssertFailed); + case::get_entry_option(0, 0, Some(op_x_minus_1(0))) + .op_return(DEFAULT_DATA) + .rejects(AssertFailed); +} + +// 1. pushed_data_from_entry +#[test] +fn pushed_data_from_entry() { + let hash = hash_of_pushed_data(&DEFAULT_DATA[1..]); + + for indicator in 0..4 { + case::pushed_data_from_entry(pushed_data(indicator, hash), Some((indicator, hash))) + .accepts(); + } +} + +#[test] +fn pushed_data_from_entry_other_entries_return_none() { + case::pushed_data_from_entry(op_x_minus_1(0), None).accepts(); + case::pushed_data_from_entry(negate_or_reserved(0), None).accepts(); + case::pushed_data_from_entry(negate_or_reserved(1), None).accepts(); +} + +#[test] +fn pushed_data_from_entry_rejects_wrong_expectation() { + let hash = hash_of_pushed_data(&DEFAULT_DATA[1..]); + + // Wrong indicator, wrong hash, and `None` for pushed data. + case::pushed_data_from_entry(pushed_data(0, hash), Some((1, hash))).rejects(AssertFailed); + case::pushed_data_from_entry(pushed_data(0, hash), Some((0, [0; 32]))).rejects(AssertFailed); + case::pushed_data_from_entry(pushed_data(0, hash), None).rejects(AssertFailed); +} + +// 2. get_pushed_data +#[test] +fn get_pushdata0() { + let hash = hash_of_pushed_data(&DEFAULT_DATA[1..]); + + case::get_pushed_data(0, 0, Some((0, hash))) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn get_pushdata1() { + // A 76-byte push, which needs OP_PUSHDATA1. + const DATA: &[u8] = &[0; 77]; + + let hash = hash_of_pushed_data(&DATA[1..]); + + case::get_pushed_data(0, 0, Some((1, hash))) + .op_return(DATA) + .accepts(); +} + +#[test] +fn get_pushdata2() { + // The last 3 bytes are the pushed data. + const DATA: &[u8] = &[1, 0x4d, 0x03, 0x00, 0xde, 0xad, 0xbe]; + + let hash = hash_of_pushed_data(&DATA[4..]); + + case::get_pushed_data(0, 0, Some((2, hash))) + .op_return(DATA) + .accepts(); +} + +#[test] +fn get_pushdata4() { + // The last 3 bytes are the pushed data. + const DATA: &[u8] = &[1, 0x4e, 0x03, 0x00, 0x00, 0x00, 0xde, 0xad, 0xbe]; + + let hash = hash_of_pushed_data(&DATA[6..]); + + case::get_pushed_data(0, 0, Some((3, hash))) + .op_return(DATA) + .accepts(); +} + +#[test] +fn get_pushed_data_nonexistent_output() { + case::get_pushed_data(NONEXISTENT_OUTPUT, 0, None) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn get_pushed_data_not_op_return_output() { + case::get_pushed_data(0, 0, None).accepts(); +} + +#[test] +fn get_pushed_data_out_of_range_entry() { + case::get_pushed_data(0, OUT_OF_RANGE_ENTRY, None) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn get_pushed_data_neg1_returns_none() { + case::get_pushed_data(0, 0, None) + .op_return(OP_PUSHNUM_NEG1) + .accepts(); +} + +// 3. op_x_minus_1_from_entry +#[test] +fn op_x_minus_1_from_entry() { + for value in 0..16 { + case::op_x_minus_1_from_entry(op_x_minus_1(value), Some(value)).accepts(); + } +} + +#[test] +fn op_x_minus_1_from_entry_returns_none() { + case::op_x_minus_1_from_entry(negate_or_reserved(0), None).accepts(); + case::op_x_minus_1_from_entry(negate_or_reserved(1), None).accepts(); + case::op_x_minus_1_from_entry(pushed_data(0, [0; 32]), None).accepts(); +} + +#[test] +fn op_x_minus_1_from_entry_rejects_wrong_expectation() { + case::op_x_minus_1_from_entry(op_x_minus_1(0), Some(1)).rejects(AssertFailed); + case::op_x_minus_1_from_entry(op_x_minus_1(0), None).rejects(AssertFailed); +} + +// 4. get_op_x_minus_1 +#[test] +fn get_op_x_minus_1() { + for value in 0..16_u8 { + case::get_op_x_minus_1(0, u32::from(value), Some(value)) + .op_return(OP_1_TO_16) + .accepts(); + } +} + +#[test] +fn get_op_x_minus_1_nonexistent_output() { + case::get_op_x_minus_1(NONEXISTENT_OUTPUT, 0, None) + .op_return(OP_1) + .accepts(); +} + +#[test] +fn get_op_x_minus_1_not_op_return_output() { + case::get_op_x_minus_1(0, 0, None).accepts(); +} + +#[test] +fn get_op_x_minus_1_out_of_range_entry() { + case::get_op_x_minus_1(0, OUT_OF_RANGE_ENTRY, None) + .op_return(OP_1) + .accepts(); +} + +#[test] +fn get_op_x_minus_1_returns_none() { + case::get_op_x_minus_1(0, 0, None) + .op_return(OP_RESERVED) + .accepts(); + case::get_op_x_minus_1(0, 0, None) + .op_return(DEFAULT_DATA) + .accepts(); +} + +// 5. negate_or_reserved_from_entry +#[test] +fn negate_or_reserved_from_entry_negate() { + case::negate_or_reserved_from_entry(negate_or_reserved(0), Some(0)).accepts(); +} + +#[test] +fn negate_or_reserved_from_entry_reserved() { + case::negate_or_reserved_from_entry(negate_or_reserved(1), Some(1)).accepts(); +} + +#[test] +fn negate_or_reserved_from_entry_returns_none() { + case::negate_or_reserved_from_entry(pushed_data(0, hash_of_pushed_data(&[1])), None).accepts(); + case::negate_or_reserved_from_entry(op_x_minus_1(0), None).accepts(); +} + +#[test] +fn negate_or_reserved_from_entry_rejects_wrong_expectation() { + case::negate_or_reserved_from_entry(negate_or_reserved(0), Some(1)).rejects(AssertFailed); + case::negate_or_reserved_from_entry(negate_or_reserved(1), None).rejects(AssertFailed); +} + +// 6. get_negate_or_reserved +#[test] +fn get_negate_or_reserved_negate() { + case::get_negate_or_reserved(0, 0, Some(0)) + .op_return(OP_PUSHNUM_NEG1) + .accepts(); +} + +#[test] +fn get_negate_or_reserved_reserved() { + case::get_negate_or_reserved(0, 0, Some(1)) + .op_return(OP_RESERVED) + .accepts(); +} + +#[test] +fn get_negate_or_reserved_nonexistent_output() { + case::get_negate_or_reserved(NONEXISTENT_OUTPUT, 0, None) + .op_return(OP_RESERVED) + .accepts(); +} + +#[test] +fn get_negate_or_reserved_not_op_return_output() { + case::get_negate_or_reserved(0, 0, None).accepts(); +} + +#[test] +fn get_negate_or_reserved_out_of_range_entry() { + case::get_negate_or_reserved(0, OUT_OF_RANGE_ENTRY, None) + .op_return(OP_RESERVED) + .accepts(); +} + +#[test] +fn get_negate_or_reserved_returns_none() { + case::get_negate_or_reserved(0, 0, None) + .op_return(DEFAULT_DATA) + .accepts(); +} + +// 7. kind_of_entry +#[test] +fn kind_of_entry_pushed_data() { + case::kind_of_entry(pushed_data(0, hash_of_pushed_data(&[1])), 0).accepts(); +} + +#[test] +fn kind_of_entry_op_x_minus_1() { + for value in 0..16 { + case::kind_of_entry(op_x_minus_1(value), 1).accepts(); + } +} + +#[test] +fn kind_of_entry_negate_or_reserved() { + // negate + case::kind_of_entry(negate_or_reserved(0), 2).accepts(); + // reserved + case::kind_of_entry(negate_or_reserved(1), 2).accepts(); +} + +#[test] +fn kind_of_entry_rejects_wrong_kind() { + case::kind_of_entry(pushed_data(0, [0; 32]), 1).rejects(AssertFailed); + case::kind_of_entry(op_x_minus_1(0), 2).rejects(AssertFailed); + case::kind_of_entry(negate_or_reserved(0), 0).rejects(AssertFailed); +} + +// 8. is_output_op_return +#[test] +fn is_output_op_return_true() { + case::is_output_op_return(0, true) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn is_output_op_return_nonexistent_output_false() { + case::is_output_op_return(NONEXISTENT_OUTPUT, false) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn is_output_op_return_false() { + case::is_output_op_return(0, false).accepts(); + case::is_output_op_return(1, false) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn is_output_op_return_rejects_wrong_expectation() { + case::is_output_op_return(0, false) + .op_return(DEFAULT_DATA) + .rejects(AssertFailed); + case::is_output_op_return(0, true).rejects(AssertFailed); +} + +// 9. has_entry +#[test] +fn has_entry_true() { + case::has_entry(0, 0, true) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn has_entry_nonexistent_output_false() { + case::has_entry(NONEXISTENT_OUTPUT, 0, false) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn has_entry_out_of_range_entry_false() { + case::has_entry(0, OUT_OF_RANGE_ENTRY, false) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn has_entry_output_not_op_return_false() { + case::has_entry(0, 0, false).accepts(); +} + +// 10. assert_output_is_op_return +#[test] +fn assert_output_is_op_return_pass() { + case::assert_output_is_op_return(0) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn assert_output_is_op_return_nonexistent_output_fails() { + case::assert_output_is_op_return(NONEXISTENT_OUTPUT) + .op_return(DEFAULT_DATA) + .rejects(AssertFailed); +} + +#[test] +fn assert_output_is_op_return_fail() { + case::assert_output_is_op_return(0).rejects(AssertFailed); +} + +// 11. assert_has_entry +#[test] +fn assert_has_entry_pass() { + case::assert_has_entry(0, 0) + .op_return(DEFAULT_DATA) + .accepts(); +} + +#[test] +fn assert_has_entry_nonexistent_output_fails() { + case::assert_has_entry(NONEXISTENT_OUTPUT, 0) + .op_return(DEFAULT_DATA) + .rejects(AssertFailed); +} + +#[test] +fn assert_has_entry_output_not_op_return_fails() { + case::assert_has_entry(0, 0).rejects(AssertFailed); +} + +#[test] +fn assert_has_entry_out_of_range_entry_fails() { + case::assert_has_entry(0, OUT_OF_RANGE_ENTRY) + .op_return(DEFAULT_DATA) + .rejects(AssertFailed); +}