Skip to content

Commit

Permalink
Added some tests for OmniTypes.
Browse files Browse the repository at this point in the history
  • Loading branch information
kisialiou committed Nov 1, 2024
1 parent 5ddf8f6 commit 7e9aa73
Showing 1 changed file with 167 additions and 0 deletions.
167 changes: 167 additions & 0 deletions near/omni-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,171 @@ mod test {
let res = borsh::to_vec(&PayloadType::ClaimNativeFee).unwrap();
assert_eq!(hex::encode(res), "02");
}

#[test]
fn test_h160_from_str() {
let addr = "5a08feed678c056650b3eb4a5cb1b9bb6f0fe265";
let h160 = H160::from_str(addr).expect("Should parse without 0x prefix");
assert_eq!(h160.to_string(), format!("0x{addr}"));

let addr_with_prefix = format!("0x{addr}");
let h160_with_prefix =
H160::from_str(&addr_with_prefix).expect("Should parse with 0x prefix");
assert_eq!(h160, h160_with_prefix);

let invalid_hex = "0xnot_a_hex_string";
let err = H160::from_str(invalid_hex).expect_err("Should fail with invalid hex");
assert!(err.contains("Invalid character"), "Error was: {err}");

let short_addr = "0x5a08";
let err = H160::from_str(short_addr).expect_err("Should fail with invalid length");
assert!(err.contains("Invalid length:"), "Error was: {err}");
}

#[test]
fn test_eip_55_checksum() {
let test_address = |input: &str, expected: &str, error_message: &str| {
let h160 = H160::from_str(input).expect("Valid address");
assert_eq!(
h160.to_eip_55_checksum(),
expected,
"{error_message} {input} -> {expected}"
);
};

let input = "0x5A08FeED678C056650b3eb4a5cb1b9BB6F0fE265";
let expected = "5A08FeED678C056650b3eb4a5cb1b9BB6F0fE265";
test_address(input, expected, "Original address");
test_address(&input.to_lowercase(), expected, "Lowercase address");
test_address(
&format!("0x{}", expected.to_ascii_uppercase()),
expected,
"Uppercase address",
);

let input = "0x1234567890123456789012345678901234567890";
let expected = "1234567890123456789012345678901234567890";
test_address(input, expected, "No mixed case address");
}

#[test]
fn test_h160_deserialization() {
let json = r#""0x5a08feed678c056650b3eb4a5cb1b9bb6f0fe265""#;
let h160: H160 = serde_json::from_str(json).expect("Should deserialize with 0x prefix");
assert_eq!(
h160.to_string(),
"0x5a08feed678c056650b3eb4a5cb1b9bb6f0fe265",
"Should deserialize with 0x prefix"
);

let json = r#""5a08feed678c056650b3eb4a5cb1b9bb6f0fe265""#;
let h160: H160 = serde_json::from_str(json).expect("Should deserialize without 0x prefix");
assert_eq!(
h160.to_string(),
"0x5a08feed678c056650b3eb4a5cb1b9bb6f0fe265",
"Should deserialize without 0x prefix"
);

let json = r#""0xnot_a_hex_string""#;
let result: Result<H160, _> = serde_json::from_str(json);
assert!(result.is_err(), "Should fail with invalid hex");
let err = result.unwrap_err().to_string();
assert!(
err.contains("Invalid character"),
"Error was: {err} but expected Invalid character"
);

let json = r#""0x5a08""#;
let result: Result<H160, _> = serde_json::from_str(json);
assert!(result.is_err(), "Should fail with invalid length");
let err = result.unwrap_err().to_string();
assert!(
err.contains("Invalid length"),
"Error was: {err} but expected Invalid length"
);

let json = "123";
let result: Result<H160, _> = serde_json::from_str(json);
assert!(result.is_err(), "Should fail with non-string input");
let err = result.unwrap_err().to_string();
assert!(
err.contains("invalid type"),
"Error was: {err} but expected invalid type"
);
}

#[test]
fn test_h160_serialization() {
let addr = "5a08feed678c056650b3eb4a5cb1b9bb6f0fe265";
let h160 = H160::from_str(addr).expect("Valid address");
let serialized = serde_json::to_string(&h160).expect("Should serialize");
assert_eq!(
serialized, r#""0x5a08feed678c056650b3eb4a5cb1b9bb6f0fe265""#,
"Invalid serialization."
);

let deserialized: H160 = serde_json::from_str(&serialized).expect("Should deserialize");
assert_eq!(
h160, deserialized,
"Deserialization is not equal to initial value."
);

assert_eq!(
format!(r#""{}""#, h160.to_string()),
serialized,
"Serialization does not preserve format from to_string()"
);
}

#[test]
fn test_chain_kind_from_omni_address() {
let test_chain_kind = |addr: OmniAddress, expected: ChainKind, chain_name: &str| {
assert_eq!(
ChainKind::from(&addr),
expected,
"Invalid chain kind from {chain_name} address"
);
};

let evm_address =
H160::from_str("0x5a08feed678c056650b3eb4a5cb1b9bb6f0fe265").expect("Valid address");

test_chain_kind(OmniAddress::Eth(evm_address.clone()), ChainKind::Eth, "ETH");
test_chain_kind(
OmniAddress::Near("alice.near".to_string()),
ChainKind::Near,
"NEAR",
);
test_chain_kind(
OmniAddress::Sol("SoLaddr123".to_string()),
ChainKind::Sol,
"SOL",
);
test_chain_kind(OmniAddress::Arb(evm_address.clone()), ChainKind::Arb, "ARB");
test_chain_kind(OmniAddress::Base(evm_address), ChainKind::Base, "BASE");
}

#[test]
fn test_omni_address_from_evm_address() {
let evm_address =
H160::from_str("0x5a08feed678c056650b3eb4a5cb1b9bb6f0fe265").expect("Valid address");

assert_eq!(
OmniAddress::from_evm_address(ChainKind::Eth, evm_address.clone()),
Ok(OmniAddress::Eth(evm_address.clone()))
);

for chain_kind in [
ChainKind::Near,
ChainKind::Sol,
ChainKind::Arb,
ChainKind::Base,
] {
let expected_error = format!("{:?} is not an EVM chain", chain_kind);
assert_eq!(
OmniAddress::from_evm_address(chain_kind, evm_address.clone()),
Err(expected_error)
);
}
}
}

0 comments on commit 7e9aa73

Please sign in to comment.