Skip to content

Commit be0f840

Browse files
committed
test(unit): AccountId
Signed-off-by: Skyler Ross <[email protected]>
1 parent 99255f9 commit be0f840

File tree

2 files changed

+147
-3
lines changed

2 files changed

+147
-3
lines changed

src/account/account_id.rs

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ impl From<EntityId> for AccountId {
271271
mod tests {
272272
use std::str::FromStr;
273273

274+
use assert_matches::assert_matches;
274275
use hex_literal::hex;
275276

276277
use crate::ethereum::EvmAddress;
@@ -375,4 +376,137 @@ mod tests {
375376
"0.0.123-esxsf"
376377
);
377378
}
379+
380+
#[tokio::test]
381+
async fn bad_checksum_on_previewnet() {
382+
let client = Client::for_previewnet();
383+
let id = AccountId::from_str("0.0.123-ntjli").unwrap();
384+
385+
assert_matches!(
386+
id.validate_checksum(&client),
387+
Err(crate::Error::BadEntityId {
388+
shard: 0,
389+
realm: 0,
390+
num: 123,
391+
present_checksum: _,
392+
expected_checksum: _
393+
})
394+
);
395+
}
396+
397+
#[test]
398+
fn malformed_id_fails() {
399+
assert_matches!(AccountId::from_str("0.0."), Err(crate::Error::BasicParse(_)));
400+
}
401+
402+
#[test]
403+
fn malformed_checksum() {
404+
assert_matches!(AccountId::from_str("0.0.123-ntjl"), Err(crate::Error::BasicParse(_)));
405+
}
406+
407+
#[test]
408+
fn malformed_checksum_2() {
409+
assert_matches!(AccountId::from_str("0.0.123-ntjl1"), Err(crate::Error::BasicParse(_)));
410+
}
411+
412+
#[test]
413+
fn malformed_alias() {
414+
assert_matches!(AccountId::from_str("0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf777"), Err(crate::Error::KeyParse(_)));
415+
}
416+
#[test]
417+
fn malformed_alias_2() {
418+
assert_matches!(AccountId::from_str("0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf777g"), Err(crate::Error::KeyParse(_)));
419+
}
420+
#[test]
421+
fn malformed_alias_key_3() {
422+
assert_matches!(AccountId::from_str("0.0.303a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777"), Err(crate::Error::KeyParse(_)));
423+
}
424+
425+
#[test]
426+
fn from_string_alias_key() {
427+
expect_test::expect!["0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777"]
428+
.assert_eq(&AccountId::from_str("0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777").unwrap().to_string())
429+
}
430+
431+
#[test]
432+
fn from_string_evm_address() {
433+
expect_test::expect!["0x302a300506032b6570032100114e6abc371b82da"].assert_eq(
434+
&AccountId::from_str("0x302a300506032b6570032100114e6abc371b82da").unwrap().to_string(),
435+
);
436+
}
437+
438+
#[test]
439+
fn from_solidity_address() {
440+
expect_test::expect!["0.0.5005"].assert_eq(
441+
&AccountId::from_solidity_address("000000000000000000000000000000000000138D")
442+
.unwrap()
443+
.to_string(),
444+
);
445+
}
446+
447+
#[test]
448+
fn from_solidity_address_0x() {
449+
expect_test::expect!["0.0.5005"].assert_eq(
450+
&AccountId::from_solidity_address("0x000000000000000000000000000000000000138D")
451+
.unwrap()
452+
.to_string(),
453+
);
454+
}
455+
456+
#[test]
457+
fn from_bytes() {
458+
let bytes = AccountId {
459+
shard: 0,
460+
realm: 0,
461+
num: 5005,
462+
alias: None,
463+
evm_address: None,
464+
checksum: None,
465+
}
466+
.to_bytes();
467+
468+
expect_test::expect!["0.0.5005"]
469+
.assert_eq(&AccountId::from_bytes(&bytes).unwrap().to_string());
470+
}
471+
472+
#[test]
473+
fn from_bytes_alias() {
474+
let bytes = AccountId::from_str("0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777").unwrap().to_bytes();
475+
476+
expect_test::expect!["0.0.302a300506032b6570032100114e6abc371b82dab5c15ea149f02d34a012087b163516dd70f44acafabf7777"].assert_eq(&AccountId::from_bytes(&bytes).unwrap().to_string());
477+
}
478+
479+
#[test]
480+
fn from_bytes_evm_address() {
481+
let bytes =
482+
AccountId::from_str("0x302a300506032b6570032100114e6abc371b82da").unwrap().to_bytes();
483+
expect_test::expect!["0.0.0"]
484+
.assert_eq(&AccountId::from_bytes(&bytes).unwrap().to_string());
485+
}
486+
487+
#[test]
488+
fn to_solidity_address() {
489+
let id = AccountId {
490+
shard: 0,
491+
realm: 0,
492+
num: 5005,
493+
alias: None,
494+
evm_address: None,
495+
checksum: None,
496+
};
497+
498+
expect_test::expect!["000000000000000000000000000000000000138d"]
499+
.assert_eq(&id.to_solidity_address().unwrap());
500+
}
501+
502+
#[test]
503+
fn from_evm_address() {
504+
let evm_address =
505+
EvmAddress::from_str("0x302a300506032b6570032100114e6abc371b82da").unwrap();
506+
507+
let id = AccountId::from_evm_address(&evm_address);
508+
509+
expect_test::expect!["0x302a300506032b6570032100114e6abc371b82da"]
510+
.assert_eq(&id.to_string());
511+
}
378512
}

src/entity_id.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,15 @@ impl FromStr for Checksum {
4848
type Err = Error;
4949

5050
fn from_str(s: &str) -> Result<Self, Self::Err> {
51-
s.parse()
52-
.map(Checksum)
53-
.map_err(|_| Error::basic_parse("Expected checksum to be exactly 5 characters"))
51+
let ascii_str: TinyAsciiStr<5> = s
52+
.parse()
53+
.map_err(|e| Error::basic_parse(format!("Expected checksum to be valid ascii: {e}")))?;
54+
55+
if ascii_str.len() != 5 || !ascii_str.is_ascii_alphabetic_lowercase() {
56+
return Err(Error::basic_parse("Expected checksum to be exactly 5 lowercase letters"));
57+
}
58+
59+
Ok(Self(ascii_str))
5460
}
5561
}
5662

@@ -130,6 +136,10 @@ impl<'a> PartialEntityId<'a> {
130136
Some((shard, rest)) => {
131137
let (realm, last) = rest.split_once('.').ok_or_else(expecting)?;
132138

139+
if last.is_empty() {
140+
return Err(expecting());
141+
}
142+
133143
let shard = shard.parse().map_err(|_| expecting())?;
134144
let realm = realm.parse().map_err(|_| expecting())?;
135145

0 commit comments

Comments
 (0)