Skip to content

Commit f31e426

Browse files
authored
test: contract artifact deserliazation integrity (#644)
Adds test cases that asserts contract artifacts are not corrupted whether deserialized directly or via the `ContractArtifact` enum. The test for `LegacyContractClass` - `test_legacy_artifact_deser_from_contract_artifact` - is ignored due to a known issue #392.
1 parent 2774834 commit f31e426

File tree

2 files changed

+49
-6
lines changed

2 files changed

+49
-6
lines changed

starknet-core/src/types/contract/legacy.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ fn should_skip_attributes_for_hinted_hash(value: &Option<Vec<LegacyAttribute>>)
984984

985985
#[cfg(test)]
986986
mod tests {
987+
use super::super::ContractArtifact;
987988
use super::*;
988989

989990
#[derive(serde::Deserialize)]
@@ -1007,6 +1008,32 @@ mod tests {
10071008
}
10081009
}
10091010

1011+
#[test]
1012+
#[ignore = "https://github.com/xJonathanLEI/starknet-rs/issues/392"]
1013+
fn test_legacy_artifact_deser_from_contract_artifact() {
1014+
for raw_artifact in [
1015+
include_str!("../../../test-data/contracts/cairo0/artifacts/oz_account.txt"),
1016+
include_str!("../../../test-data/contracts/cairo0/artifacts/event_example.txt"),
1017+
include_str!("../../../test-data/contracts/cairo0/artifacts/pre-0.11.0/oz_account.txt"),
1018+
include_str!(
1019+
"../../../test-data/contracts/cairo0/artifacts/pre-0.11.0/event_example.txt"
1020+
),
1021+
] {
1022+
let direct_deser = serde_json::from_str::<LegacyContractClass>(raw_artifact).unwrap();
1023+
let deser_via_contract_artifact =
1024+
match serde_json::from_str::<ContractArtifact>(raw_artifact).unwrap() {
1025+
ContractArtifact::LegacyClass(class) => class,
1026+
_ => panic!("unexpected artifact type"),
1027+
};
1028+
1029+
// Class should be identical however it's deserialized
1030+
assert_eq!(
1031+
direct_deser.class_hash().unwrap(),
1032+
deser_via_contract_artifact.class_hash().unwrap()
1033+
);
1034+
}
1035+
}
1036+
10101037
#[test]
10111038
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
10121039
fn test_contract_class_hash() {

starknet-core/src/types/contract/mod.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,10 +1023,18 @@ mod tests {
10231023
include_str!("../../../test-data/contracts/cairo2/artifacts/erc20_sierra.txt"),
10241024
include_str!("../../../test-data/contracts/cairo2.6/artifacts/erc20_sierra.txt"),
10251025
] {
1026-
match serde_json::from_str::<ContractArtifact>(raw_artifact) {
1027-
Ok(ContractArtifact::SierraClass(_)) => {}
1026+
let direct_deser = serde_json::from_str::<SierraClass>(raw_artifact).unwrap();
1027+
let via_contract_artifact = match serde_json::from_str::<ContractArtifact>(raw_artifact)
1028+
{
1029+
Ok(ContractArtifact::SierraClass(class)) => class,
10281030
_ => panic!("Unexpected result"),
1029-
}
1031+
};
1032+
1033+
// Class should be identical however it's deserialized
1034+
assert_eq!(
1035+
direct_deser.class_hash().unwrap(),
1036+
via_contract_artifact.class_hash().unwrap()
1037+
);
10301038
}
10311039
}
10321040

@@ -1040,10 +1048,18 @@ mod tests {
10401048
include_str!("../../../test-data/contracts/cairo2/artifacts/erc20_compiled.txt"),
10411049
include_str!("../../../test-data/contracts/cairo2.6/artifacts/erc20_compiled.txt"),
10421050
] {
1043-
match serde_json::from_str::<ContractArtifact>(raw_artifact) {
1044-
Ok(ContractArtifact::CompiledClass(_)) => {}
1051+
let direct_deser = serde_json::from_str::<CompiledClass>(raw_artifact).unwrap();
1052+
let via_contract_artifact = match serde_json::from_str::<ContractArtifact>(raw_artifact)
1053+
{
1054+
Ok(ContractArtifact::CompiledClass(class)) => class,
10451055
_ => panic!("Unexpected result"),
1046-
}
1056+
};
1057+
1058+
// Class should be identical however it's deserialized
1059+
assert_eq!(
1060+
direct_deser.class_hash().unwrap(),
1061+
via_contract_artifact.class_hash().unwrap()
1062+
);
10471063
}
10481064
}
10491065

0 commit comments

Comments
 (0)