Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger committed Mar 27, 2024
1 parent 21b6a6c commit 7424472
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 131 deletions.
137 changes: 61 additions & 76 deletions starknet-core/src/types/eth_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> =
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<String> =
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<String> =
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]
Expand Down
55 changes: 0 additions & 55 deletions starknet-core/src/types/test-data/address.json

This file was deleted.

0 comments on commit 7424472

Please sign in to comment.