Skip to content

Commit

Permalink
feat: expand tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashutoshvarma committed Sep 21, 2023
1 parent f9014ed commit 97bd0e1
Showing 1 changed file with 82 additions and 45 deletions.
127 changes: 82 additions & 45 deletions pallets/unified-accounts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,83 +184,120 @@ fn account_claim_should_work() {
}

#[test]
fn account_claim_should_not_work() {
fn account_default_claim_works() {
ExtBuilder::default().build().execute_with(|| {
// invald signature
assert_noop!(
UnifiedAccounts::claim_evm_address(
RuntimeOrigin::signed(ALICE),
UnifiedAccounts::eth_address(&bob_secret()),
get_evm_signature(&BOB, &bob_secret())
),
Error::<TestRuntime>::InvalidSignature
let alice_default_evm =
<TestRuntime as Config>::DefaultAccountMapping::into_h160(ALICE.into());

// claim default account
assert_ok!(UnifiedAccounts::claim_default_evm_address(
RuntimeOrigin::signed(ALICE)
));
System::assert_last_event(RuntimeEvent::UnifiedAccounts(
crate::Event::AccountClaimed {
account_id: ALICE.clone(),
evm_address: alice_default_evm.clone(),
},
));

// check UnifiedAddressMapper's mapping works
assert_eq!(
<UnifiedAccounts as UnifiedAddressMapper<_>>::to_h160(&ALICE),
Some(alice_default_evm)
);
assert_eq!(
<UnifiedAccounts as UnifiedAddressMapper<_>>::to_account_id(&alice_default_evm),
Some(ALICE)
);

// should not allow to claim afterwards
assert_noop!(
UnifiedAccounts::claim_evm_address(
RuntimeOrigin::signed(ALICE),
UnifiedAccounts::eth_address(&bob_secret()),
UnifiedAccounts::eth_address(&alice_secret()),
get_evm_signature(&ALICE, &alice_secret())
),
Error::<TestRuntime>::InvalidSignature
Error::<TestRuntime>::AlreadyMapped
);
});
}

#[test]
fn replay_attack_should_not_be_possible() {
ExtBuilder::default().build().execute_with(|| {
let alice_eth = UnifiedAccounts::eth_address(&alice_secret());
let alice_signature = get_evm_signature(&ALICE, &alice_secret());

// alice claim her eth address first
assert_ok!(UnifiedAccounts::claim_evm_address(
RuntimeOrigin::signed(ALICE),
UnifiedAccounts::eth_address(&alice_secret()),
get_evm_signature(&ALICE, &alice_secret())
alice_eth,
alice_signature
));
// AccountId already mapped

// bob intercepted alice signature and tries to perform
// replay attack to claim alice eth address as his own,
// this should fail.
assert_noop!(
UnifiedAccounts::claim_evm_address(
RuntimeOrigin::signed(ALICE),
UnifiedAccounts::eth_address(&alice_secret()),
get_evm_signature(&ALICE, &alice_secret())
RuntimeOrigin::signed(BOB),
alice_eth,
alice_signature
),
Error::<TestRuntime>::AlreadyMapped
);
// eth address already mapped
});
}

#[test]
fn frontrun_attack_should_not_be_possible() {
ExtBuilder::default().build().execute_with(|| {
let alice_eth = UnifiedAccounts::eth_address(&alice_secret());
let alice_signature = get_evm_signature(&ALICE, &alice_secret());

// bob intercepted alice signature and tries to perform
// frontrun attack to claim alice eth address as his own
// this should fail with InvalidSignature.
assert_noop!(
UnifiedAccounts::claim_evm_address(
RuntimeOrigin::signed(BOB),
UnifiedAccounts::eth_address(&alice_secret()),
get_evm_signature(&ALICE, &alice_secret())
alice_eth,
alice_signature
),
Error::<TestRuntime>::AlreadyMapped
Error::<TestRuntime>::InvalidSignature
);

// alice can claim her eth address
assert_ok!(UnifiedAccounts::claim_evm_address(
RuntimeOrigin::signed(ALICE),
alice_eth,
alice_signature
));
});
}

#[test]
fn account_default_claim_works() {
fn connecting_mapped_accounts_should_not_work() {
ExtBuilder::default().build().execute_with(|| {
let alice_default_evm =
<TestRuntime as Config>::DefaultAccountMapping::into_h160(ALICE.into());

// claim default account
assert_ok!(UnifiedAccounts::claim_default_evm_address(
RuntimeOrigin::signed(ALICE)
));
System::assert_last_event(RuntimeEvent::UnifiedAccounts(
crate::Event::AccountClaimed {
account_id: ALICE.clone(),
evm_address: alice_default_evm.clone(),
},
));
// connect ALICE accounts
connect_accounts(&ALICE, &alice_secret());

// check UnifiedAddressMapper's mapping works
assert_eq!(
<UnifiedAccounts as UnifiedAddressMapper<_>>::to_h160(&ALICE),
Some(alice_default_evm)
);
assert_eq!(
<UnifiedAccounts as UnifiedAddressMapper<_>>::to_account_id(&alice_default_evm),
Some(ALICE)
// AccountId already mapped
// ALICE attempts to connect another evm address
assert_noop!(
UnifiedAccounts::claim_evm_address(
RuntimeOrigin::signed(ALICE),
UnifiedAccounts::eth_address(&bob_secret()),
get_evm_signature(&BOB, &bob_secret())
),
Error::<TestRuntime>::AlreadyMapped
);

// should not allow to claim afterwards
// eth address already mapped
// BOB attempts to connect alice_eth that is already mapped
assert_noop!(
UnifiedAccounts::claim_evm_address(
RuntimeOrigin::signed(ALICE),
RuntimeOrigin::signed(BOB),
UnifiedAccounts::eth_address(&alice_secret()),
get_evm_signature(&ALICE, &alice_secret())
),
Expand Down

0 comments on commit 97bd0e1

Please sign in to comment.