Skip to content

Commit

Permalink
fix(iota-types): Fix address tests (#2128)
Browse files Browse the repository at this point in the history
* fix(iota-types): Fix address tests

* clippy
  • Loading branch information
DaughterOfMars authored Aug 29, 2024
1 parent 99c4069 commit 6c8d0b2
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions crates/iota-types/src/unit_tests/base_types_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ fn test_object_id_conversions() {}

#[test]
fn test_object_id_display() {
let hex = SAMPLE_ADDRESS;
let id = ObjectID::from_str(hex).unwrap();
assert_eq!(format!("{:?}", id), format!("0x{hex}"));
let id = ObjectID::from_str(SAMPLE_ADDRESS).unwrap();
assert_eq!(format!("{:?}", id), SAMPLE_ADDRESS);
}

#[test]
Expand Down Expand Up @@ -187,10 +186,9 @@ fn test_object_id_deserialize_from_json_value() {

#[test]
fn test_object_id_serde_json() {
let hex = format!("0x{}", SAMPLE_ADDRESS);
let json_hex = format!("\"0x{}\"", SAMPLE_ADDRESS);
let json_hex = format!("\"{}\"", SAMPLE_ADDRESS);

let obj_id = ObjectID::from_hex_literal(&hex).unwrap();
let obj_id = ObjectID::from_hex_literal(SAMPLE_ADDRESS).unwrap();

let json = serde_json::to_string(&obj_id).unwrap();
let json_obj_id: ObjectID = serde_json::from_str(&json_hex).unwrap();
Expand All @@ -215,7 +213,7 @@ fn test_object_id_serde_with_expected_value() {
let json_serialized = serde_json::to_string(&object_id).unwrap();
let bcs_serialized = bcs::to_bytes(&object_id).unwrap();

let expected_json_address = format!("\"0x{}\"", SAMPLE_ADDRESS);
let expected_json_address = format!("\"{}\"", SAMPLE_ADDRESS);
assert_eq!(expected_json_address, json_serialized);
assert_eq!(object_id_vec, bcs_serialized);
}
Expand All @@ -241,9 +239,8 @@ fn test_object_id_zero_padding() {

#[test]
fn test_address_display() {
let hex = SAMPLE_ADDRESS;
let id = IotaAddress::from_str(hex).unwrap();
assert_eq!(format!("{:?}", id), format!("0x{hex}"));
let id = IotaAddress::from_str(SAMPLE_ADDRESS).unwrap();
assert_eq!(format!("{:?}", id), SAMPLE_ADDRESS);
}

#[test]
Expand All @@ -262,7 +259,7 @@ fn test_address_serde_not_human_readable() {
fn test_address_serde_human_readable() {
let address = IotaAddress::random_for_testing_only();
let serialized = serde_json::to_string(&address).unwrap();
assert_eq!(format!("\"0x{}\"", Hex::encode(address)), serialized);
assert_eq!(format!("\"{address}\""), serialized);
let deserialized: IotaAddress = serde_json::from_str(&serialized).unwrap();
assert_eq!(deserialized, address);
}
Expand All @@ -273,8 +270,7 @@ fn test_address_serde_with_expected_value() {
let json_serialized = serde_json::to_string(&address).unwrap();
let bcs_serialized = bcs::to_bytes(&address).unwrap();

let expected_json_address = format!("\"0x{}\"", SAMPLE_ADDRESS);
assert_eq!(expected_json_address, json_serialized);
assert_eq!(format!("\"{}\"", SAMPLE_ADDRESS), json_serialized);
assert_eq!(SAMPLE_ADDRESS_VEC.to_vec(), bcs_serialized);
}

Expand Down Expand Up @@ -371,11 +367,10 @@ fn test_move_package_size_for_gas_metering() {

// A sample address in hex generated by the current address derivation
// algorithm.
#[cfg(test)]
const SAMPLE_ADDRESS: &str = "af306e86c74e937552df132b41a6cb3af58559f5342c6e82a98f7d1f7a4a9f30";
const SAMPLE_ADDRESS: &str = "0xee6283a054cab26620a531d18c49ea3560239cfd8ba83a393bbaa7d7eed2082a";
const SAMPLE_ADDRESS_VEC: [u8; 32] = [
175, 48, 110, 134, 199, 78, 147, 117, 82, 223, 19, 43, 65, 166, 203, 58, 245, 133, 89, 245, 52,
44, 110, 130, 169, 143, 125, 31, 122, 74, 159, 48,
238, 98, 131, 160, 84, 202, 178, 102, 32, 165, 49, 209, 140, 73, 234, 53, 96, 35, 156, 253,
139, 168, 58, 57, 59, 186, 167, 215, 238, 210, 8, 42,
];

// Derive a sample address and public key tuple from KeyPair bytes.
Expand All @@ -396,8 +391,8 @@ fn derive_sample_address() -> (IotaAddress, AccountKeyPair) {
fn test_address_backwards_compatibility() {
let (address, _) = derive_sample_address();
assert_eq!(
address.to_vec(),
Hex::decode(SAMPLE_ADDRESS).expect("Decoding failed"),
address.to_string(),
SAMPLE_ADDRESS,
"If this test broke, then the algorithm for deriving addresses from public keys has \
changed. If this was intentional, please compute a new sample address in hex format \
from `derive_sample_address` and update the SAMPLE_ADDRESS const above with the new \
Expand Down

0 comments on commit 6c8d0b2

Please sign in to comment.