Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some address tests #747

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion sdk/tests/types/address/bech32.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
// Copyright 2020-2023 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use iota_sdk::types::block::address::{Address, Bech32Address, Ed25519Address};
use core::str::FromStr;

use iota_sdk::types::block::{
address::{Address, Bech32Address, Ed25519Address, Hrp},
Error,
};
use packable::PackableExt;

const ED25519_ADDRESS: &str = "0xebe40a263480190dcd7939447ee01aefa73d6f3cc33c90ef7bf905abf8728655";
const ED25519_BECH32: &str = "rms1qr47gz3xxjqpjrwd0yu5glhqrth6w0t08npney8000ust2lcw2r92j5a8rt";

#[test]
fn debug() {
let bech32_address = Bech32Address::from_str(ED25519_BECH32).unwrap();

assert_eq!(
format!("{bech32_address:?}"),
"Bech32Address(rms1qr47gz3xxjqpjrwd0yu5glhqrth6w0t08npney8000ust2lcw2r92j5a8rt)"
);
}

#[test]
fn ctors() {
let ed25519_address = ED25519_ADDRESS.parse::<Ed25519Address>().unwrap();
Expand All @@ -18,3 +34,37 @@ fn ctors() {
assert_eq!(bech32_address, ED25519_BECH32.parse::<Bech32Address>().unwrap());
assert_eq!(bech32_address, Bech32Address::try_from_str(ED25519_BECH32).unwrap());
}

#[test]
fn hrp_from_str() {
Hrp::from_str("rms").unwrap();

assert!(matches!(
Hrp::from_str("中國"),
Err(Error::InvalidBech32Hrp(hrp)) if hrp == "中國"
));
}

#[test]
fn hrp_packed_len() {
let hrp = Hrp::from_str("rms").unwrap();

assert_eq!(hrp.packed_len(), 1 + 3);
assert_eq!(hrp.pack_to_vec().len(), 1 + 3);
}

#[test]
fn hrp_pack_unpack() {
let hrp = Hrp::from_str("rms").unwrap();
let packed_hrp = hrp.pack_to_vec();

assert_eq!(hrp, Hrp::unpack_verified(packed_hrp.as_slice(), &()).unwrap());
}

#[test]
fn bech32_into_inner() {
let address = Address::try_from_bech32(ED25519_BECH32).unwrap();
let bech32_address = Bech32Address::from_str(ED25519_BECH32).unwrap();

assert_eq!(address, bech32_address.into_inner());
}
5 changes: 3 additions & 2 deletions sdk/tests/types/address/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ fn debug() {
"Ed25519Address(0xebe40a263480190dcd7939447ee01aefa73d6f3cc33c90ef7bf905abf8728655)"
);
}

#[test]
fn bech32() {
let address = Address::from(Ed25519Address::from_str(ED25519_ADDRESS).unwrap());
Expand Down Expand Up @@ -151,14 +152,14 @@ fn pack_unpack() {

assert_eq!(
address,
PackableExt::unpack_verified(packed_address.as_slice(), &()).unwrap()
Ed25519Address::unpack_verified(packed_address.as_slice(), &()).unwrap()
);

let address = Address::from(Ed25519Address::from_str(ED25519_ADDRESS).unwrap());
let packed_address = address.pack_to_vec();

assert_eq!(
address,
PackableExt::unpack_verified(packed_address.as_slice(), &()).unwrap()
Address::unpack_verified(packed_address.as_slice(), &()).unwrap()
);
}
45 changes: 44 additions & 1 deletion sdk/tests/types/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ mod bech32;
mod ed25519;
mod nft;

use iota_sdk::types::block::{address::Address, Error};
use core::str::FromStr;

use iota_sdk::types::block::{
address::{Address, AliasAddress, Ed25519Address, NftAddress},
Error,
};

const ED25519_ADDRESS: &str = "0xebe40a263480190dcd7939447ee01aefa73d6f3cc33c90ef7bf905abf8728655";
const ALIAS_ID: &str = "0xe9ba80ad1561e437b663a1f1efbfabd544b0d7da7bb33e0a62e99b20ee450bee";
const NFT_ID: &str = "0xa9ede98a7f0223fa7a49fbc586f7a88bb4f0d152f282b19bcebd05c9e8a02370";
const ED25519_ADDRESS_INVALID: &str = "0x52fdfc072182654f163f5f0f9a621d729566c74d10037c4d7bbb0407d1e2c64x";

#[test]
Expand All @@ -16,3 +24,38 @@ fn invalid_bech32() {

assert!(matches!(address, Err(Error::InvalidAddress)));
}

#[test]
fn debug() {
let address = Address::from(Ed25519Address::from_str(ED25519_ADDRESS).unwrap());

assert_eq!(
format!("{address:?}"),
"Ed25519Address(0xebe40a263480190dcd7939447ee01aefa73d6f3cc33c90ef7bf905abf8728655)"
);

let address = Address::from(AliasAddress::from_str(ALIAS_ID).unwrap());

assert_eq!(
format!("{address:?}"),
"AliasAddress(0xe9ba80ad1561e437b663a1f1efbfabd544b0d7da7bb33e0a62e99b20ee450bee)"
);

let address = Address::from(NftAddress::from_str(NFT_ID).unwrap());

assert_eq!(
format!("{address:?}"),
"NftAddress(0xa9ede98a7f0223fa7a49fbc586f7a88bb4f0d152f282b19bcebd05c9e8a02370)"
);
}

#[test]
fn is_valid_bech32() {
assert!(Address::is_valid_bech32(
"rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy"
));

assert!(!Address::is_valid_bech32(
"rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zY"
));
}
Loading