Skip to content

Commit

Permalink
test: expend compile test
Browse files Browse the repository at this point in the history
  • Loading branch information
ArniStarkware committed Jun 30, 2024
1 parent 358bcbd commit 0fc0a7c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/starknet_sierra_compile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ thiserror.workspace = true

[dev-dependencies]
assert_matches.workspace = true
rstest.workspace = true
16 changes: 11 additions & 5 deletions crates/starknet_sierra_compile/src/compile_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ use std::path::Path;

use assert_matches::assert_matches;
use cairo_lang_starknet_classes::allowed_libfuncs::AllowedLibfuncsError;
use cairo_lang_utils::bigint::BigUintAsHex;
use rstest::rstest;

use crate::compile::{compile_sierra_to_casm, CompilationUtilError};
use crate::test_utils::contract_class_from_file;
use crate::test_utils::{contract_class_from_file, flip_bit, trancate_sierra_program};

const FAULTY_ACCOUNT_SIERRA_FILE: &str = "account_faulty.sierra.json";
const TEST_FILES_FOLDER: &str = "./tests/fixtures";
Expand All @@ -22,13 +24,17 @@ fn test_compile_sierra_to_casm() {
}

// TODO(Arni, 1/5/2024): Add a test for panic result test.
#[test]
fn test_negative_flow_compile_sierra_to_casm() {
#[rstest]
fn test_negative_flow_compile_sierra_to_casm_short_program(
#[values(trancate_sierra_program, flip_bit)] modifier: fn(
&mut [BigUintAsHex],
) -> Vec<BigUintAsHex>,
) {
let sierra_path = &Path::new(TEST_FILES_FOLDER).join(FAULTY_ACCOUNT_SIERRA_FILE);

let mut contract_class = contract_class_from_file(sierra_path);
// Truncate the sierra program to trigger an error.
contract_class.sierra_program = contract_class.sierra_program[..100].to_vec();
// Modify the sierra program to trigger an error.
contract_class.sierra_program = modifier(&mut contract_class.sierra_program);

let result = compile_sierra_to_casm(contract_class);
assert_matches!(
Expand Down
17 changes: 17 additions & 0 deletions crates/starknet_sierra_compile/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,20 @@ pub(crate) fn contract_class_from_file(file: &Path) -> ContractClass {
abi: None,
}
}

// Ways to corrupt Sierra programs.
pub fn trancate_sierra_program(sierra_program: &mut [BigUintAsHex]) -> Vec<BigUintAsHex> {
let trancation_ammount = 100_usize;

sierra_program[..trancation_ammount].to_vec()
}

pub fn flip_bit(sierra_program: &mut [BigUintAsHex]) -> Vec<BigUintAsHex> {
let modified_felt = 100_usize;
let fliped_bit = 15;

let mut value = sierra_program[modified_felt].value.clone();
value.set_bit(fliped_bit, !value.bit(fliped_bit));
sierra_program[modified_felt] = BigUintAsHex { value };
sierra_program.to_vec()
}

0 comments on commit 0fc0a7c

Please sign in to comment.