From 7424472c233de446b05265ef4af9d0be2aa20e44 Mon Sep 17 00:00:00 2001 From: Thomas Coratger Date: Wed, 27 Mar 2024 17:26:01 +0100 Subject: [PATCH] fix tests --- starknet-core/src/types/eth_address.rs | 137 ++++++++---------- .../src/types/test-data/address.json | 55 ------- 2 files changed, 61 insertions(+), 131 deletions(-) delete mode 100644 starknet-core/src/types/test-data/address.json diff --git a/starknet-core/src/types/eth_address.rs b/starknet-core/src/types/eth_address.rs index 5193595c..ae2fab2a 100644 --- a/starknet-core/src/types/eth_address.rs +++ b/starknet-core/src/types/eth_address.rs @@ -195,97 +195,82 @@ mod tests { use super::EthAddress; use crate::types::eth_address::FromFieldElementError; use alloc::vec::*; + use hex_literal::hex; use starknet_ff::FieldElement; #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] - fn test_eth_address_from_bytes_array() { - // Reading the JSON file - let json_data = include_str!("./test-data/address.json"); - - // Parsing the JSON - let addresses: Vec = - serde_json::from_str(json_data).expect("Unable to parse the JSON"); - - // Iterating over each element in the JSON - for address in addresses.iter() { - // Convert hex string to bytes - let bytes = if let Some(stripped) = address.strip_prefix("0x") { - hex::decode(stripped).expect("Invalid address hex") - } else { - hex::decode(address).expect("Invalid address hex") - }; - - // Convert bytes to a fixed-size array - let mut address_bytes: [u8; 20] = [0; 20]; - address_bytes.copy_from_slice(&bytes[..20]); - - let eth_address: EthAddress = address_bytes.into(); - - // Asserting the conversion from hex string to EthAddress - assert_eq!(EthAddress::from_hex(address).unwrap(), eth_address); - } + fn test_eth_address_from_bytes_array_with_zeros() { + let address = + hex::decode("00000000219ab540356cbb839cbe05303d7705fa").expect("Invalid address hex"); + + // Convert bytes to a fixed-size array + let mut address_bytes: [u8; 20] = [0; 20]; + address_bytes.copy_from_slice(&address[..20]); + + let eth_address: EthAddress = address_bytes.into(); + + // Asserting the conversion from hex string to EthAddress + assert_eq!( + EthAddress::from_hex("0x00000000219ab540356cbb839cbe05303d7705fa").unwrap(), + eth_address + ); + assert_eq!( + EthAddress::from_hex("00000000219ab540356cbb839cbe05303d7705fa").unwrap(), + eth_address + ); + } + + #[test] + #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] + fn test_eth_address_from_bytes_array_zero_address() { + let address = + hex::decode("0000000000000000000000000000000000000000").expect("Invalid address hex"); + + // Convert bytes to a fixed-size array + let mut address_bytes: [u8; 20] = [0; 20]; + address_bytes.copy_from_slice(&address[..20]); + + let eth_address: EthAddress = address_bytes.into(); + + // Asserting the conversion from hex string to EthAddress + assert_eq!( + EthAddress::from_hex("0x0000000000000000000000000000000000000000").unwrap(), + eth_address + ); + assert_eq!( + EthAddress::from_hex("0000000000000000000000000000000000000000").unwrap(), + eth_address + ); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] fn test_eth_address_from_slice() { - // Reading the JSON file containing Ethereum addresses - let json_data = include_str!("./test-data/address.json"); - - // Parsing the JSON into a vector of strings representing Ethereum addresses - let addresses: Vec = - serde_json::from_str(json_data).expect("Unable to parse the JSON"); - - // Iterating over each address in the JSON - for address in addresses.iter() { - // Generate random characters to prepend or append to the address - let chars = "0123456789abcdef"; - - // Combine random characters with the address, removing the "0x" prefix - let address_with_random = format!( - "{}{}{}", - chars, - if let Some(stripped) = address.strip_prefix("0x") { - stripped - } else { - address - }, - chars - ); - - // Convert the modified hex string to bytes - let bytes = hex::decode(&address_with_random[2..]).expect("Invalid address hex"); - - // Convert the byte slice to an Ethereum address - let eth_address: EthAddress = (&bytes[7..27]) - .try_into() - .expect("failed to get EthAddress from slice"); - - // Assert that the conversion from hex string to Ethereum address is correct - assert_eq!(EthAddress::from_hex(address).unwrap(), eth_address); - } + // address: e7f1725e7734ce288f8367e1bb143e90bb3f0512, inside a buffer with more data. + let buffer = hex!("010203e7f1725e7734ce288f8367e1bb143e90bb3f0512"); + + let eth_address: EthAddress = (&buffer[3..23]) + .try_into() + .expect("failed to get EthAddress from slice"); + assert_eq!( + EthAddress::from_hex("0xe7f1725e7734ce288f8367e1bb143e90bb3f0512").unwrap(), + eth_address + ); } #[test] #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] // Define the unit test function fn test_eth_address_from_felt() { - // Reading the JSON file containing addresses - let json_data = include_str!("./test-data/address.json"); - - // Parsing the JSON into a vector of strings - let addresses: Vec = - serde_json::from_str(json_data).expect("Unable to parse the JSON"); - - // Iterating over each address in the JSON - for address in addresses.iter() { - // Asserting the conversion from hex string to EthAddress is equal to Felt conversion - assert_eq!( - EthAddress::from_hex(address).unwrap(), - EthAddress::from_felt(&FieldElement::from_hex_be(address).unwrap()).unwrap() - ); - } + // Asserting the conversion from hex string to EthAddress is equal to Felt conversion + assert_eq!( + EthAddress::from_hex("0xb9fa6e54025b4f0829d8e1b42e8b846914659632").unwrap(), + EthAddress::from_felt( + &FieldElement::from_hex_be("0xb9fa6e54025b4f0829d8e1b42e8b846914659632").unwrap() + ) + .unwrap() + ); } #[test] diff --git a/starknet-core/src/types/test-data/address.json b/starknet-core/src/types/test-data/address.json deleted file mode 100644 index db0f6afb..00000000 --- a/starknet-core/src/types/test-data/address.json +++ /dev/null @@ -1,55 +0,0 @@ -[ - "0x00000000219ab540356cbb839cbe05303d7705fa", - "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", - "0xbe0eb53f46cd790cd13851d5eff43d12404d33e8", - "0x8315177ab297ba92a06054ce80a67ed4dbd7ed3a", - "0x40b38765696e3d5d8d9d834d8aad4bb6e418e489", - "0xda9dfa130df4de4673b89022ee50ff26f6ea73cf", - "0x47ac0fb4f2d84898e4d9e7b4dab3c24507a6d503", - "0xf977814e90da44bfa03b6295a0616a897441acec", - "0xe92d1a43df510f82c66382592a047d288f85226f", - "0xbeb5fc579115071764c7423a4f12edde41f106ed", - "0xafcd96e580138cfa2332c632e66308eacd45c5da", - "0x61edcdf5bb737adffe5043706e7c5bb1f1a56eea", - "0x7d6149ad9a573a6e2ca6ebf7d4897c1b766841b4", - "0xc61b9bb3a7a0767e3179713f3a5c7a9aedce193c", - "0xca8fa8f0b631ecdb18cda619c4fc9d197c8affca", - "0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae", - "0x3bfc20f0b9afcace800d73d2191166ff16540258", - "0x8103683202aa8da10536036edef04cdd865c225e", - "0x28c6c06298d514db089934071355e5743bf21d60", - "0xdf9eb223bafbe5c5271415c75aecd68c21fe3d7f", - "0x8696e84ab5e78983f2456bcb5c199eea9648c8c2", - "0x267be1c1d684f78cb4f6a176c4911b741e4ffdc0", - "0x49048044d57e1c92a77f79988d21fa8faf74e97e", - "0xf3b0073e3a7f747c7a38b36b805247b222c302a3", - "0xbf3aeb96e164ae67e763d9e050ff124e7c3fdd28", - "0x9e927c02c9eadae63f5efb0dd818943c7262fb8e", - "0x8484ef722627bf18ca5ae6bcf031c23e6e922b30", - "0x32400084c286cf3e17e7b677ea9583e60a000324", - "0x5b5b69f4e0add2df5d2176d7dbd20b4897bc7ec4", - "0xe25a329d385f77df5d4ed56265babe2b99a5436e", - "0x7da82c7ab4771ff031b66538d2fb9b0b047f6cf9", - "0x15c22df3e71e7380012668fb837c537d0f8b38a1", - "0x0e58e8993100f1cbe45376c410f97f4893d9bfcd", - "0x2f2d854c1d6d5bb8936bb85bc07c28ebb42c9b10", - "0x1db92e2eebc8e0c075a02bea49a2935bcd2dfcf4", - "0x0c23fc0ef06716d2f8ba19bc4bed56d045581f2d", - "0xfd898a0f677e97a9031654fc79a27cb5e31da34a", - "0xb8cda067fabedd1bb6c11c626862d7255a2414fe", - "0x701c484bfb40ac628afa487b6082f084b14af0bd", - "0x9c2fc4fc75fa2d7eb5ba9147fa7430756654faa9", - "0xb20411c403687d1036e05c8a7310a0f218429503", - "0x9a1ed80ebc9936cee2d3db944ee6bd8d407e7f9f", - "0xba18ded5e0d604a86428282964ae0bb249ceb9d0", - "0xb9fa6e54025b4f0829d8e1b42e8b846914659632", - "0x8d95842b0bca501446683be598e12f1c616770c1", - "0x35aeed3aa9657abf8b847038bb591b51e1e4c69f", - "0xb93d8596ac840816bd366dc0561e8140afd0d1cb", - "0x4ed97d6470f5121a8e02498ea37a50987da0eec0", - "0x0000000000000000000000000000000000000000", - "b20411c403687d1036e05c8a7310a0f218429503", - "9a1ed80ebc9936cee2d3db944ee6bd8d407e7f9f", - "ba18ded5e0d604a86428282964ae0bb249ceb9d0", - "b9fa6e54025b4f0829d8e1b42e8b846914659632" -]