Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: expend compile test #312

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
Loading