Skip to content

Commit

Permalink
[language][diem-framework] rewrite account_limits/basics.move as unit…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
vgao1996 authored and bors-libra committed Dec 17, 2021
1 parent ed21b8e commit 8aad1a1
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 316 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ compiled_package_info:
? address: "00000000000000000000000000000001"
name: XUS
: DiemFramework
source_digest: 1FA91B69C87781EE488E75BE5ECCA46016D8A37733E020BE2E3DEE1F36F29A6A
source_digest: 37FC949B60A8AF65DF113C3B3EF53B7F8A8D8359B4F92FC9EA80DFDE4A038F93
build_flags:
dev_mode: false
test_mode: false
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module DiemFramework::Roles {
friend DiemFramework::ValidatorOperatorConfigTests;
#[test_only]
friend DiemFramework::ValidatorConfigTests;
#[test_only]
friend DiemFramework::AccountLimitsTests;

/// A `RoleId` resource was in an unexpected state
const EROLE_ID: u64 = 0;
Expand Down
2 changes: 2 additions & 0 deletions diem-move/diem-framework/core/sources/Roles.move
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module DiemFramework::Roles {
friend DiemFramework::ValidatorOperatorConfigTests;
#[test_only]
friend DiemFramework::ValidatorConfigTests;
#[test_only]
friend DiemFramework::AccountLimitsTests;

/// A `RoleId` resource was in an unexpected state
const EROLE_ID: u64 = 0;
Expand Down
183 changes: 183 additions & 0 deletions diem-move/diem-framework/core/tests/AccountLimits.move
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
#[test_only]
module DiemFramework::AccountLimitsTests {
use Std::Signer;
use DiemFramework::Genesis;
use DiemFramework::AccountLimits;
use DiemFramework::XUS::XUS;
use DiemFramework::Roles;

struct Hold<T> has key { x: T }
public fun hold<T: store>(account: &signer, x: T) {
move_to(account, Hold<T>{ x })
}

fun setup(dr: &signer, tc: &signer, vasp: &signer) {
Genesis::setup(dr, tc);

Roles::new_parent_vasp_role(tc, vasp);

AccountLimits::publish_unrestricted_limits_for_testing<XUS>(vasp);
AccountLimits::publish_window<XUS>(dr, vasp, Signer::address_of(vasp));
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
#[expected_failure(abort_code = 1)]
fun grant_mutation_capability_after_genesis(dr: signer, tc: signer, vasp: signer) {
Genesis::setup(&dr, &tc);

hold(&vasp, AccountLimits::grant_mutation_capability(&vasp));
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
fun publish_window(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
#[expected_failure(abort_code = 262)]
fun publish_window_twice(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);

AccountLimits::publish_window<XUS>(&dr, &vasp, Signer::address_of(&vasp));
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
#[expected_failure(abort_code = 2)]
fun publish_window_non_diem_root(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
AccountLimits::publish_window<XUS>(&vasp, &vasp, Signer::address_of(&vasp));
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
#[expected_failure(abort_code = 5)]
fun publish_window_non_existent_limit_address(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
AccountLimits::publish_window<XUS>(&dr, &vasp, @0x42 /* non-exsistent */);
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
#[expected_failure(abort_code = 6)]
fun publish_unrestricted_limits_for_testing_twice(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
AccountLimits::publish_unrestricted_limits_for_testing<XUS>(&vasp);
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
fun update_limits_definition_1(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
AccountLimits::update_limits_definition<XUS>(
&tc,
Signer::address_of(&vasp),
100, /* new_max_inflow */
200, /* new_max_outflow */
150, /* new_max_holding_balance */
10000, /* new_time_period */
)
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
fun update_limits_definition_2(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
AccountLimits::update_limits_definition<XUS>(
&tc,
Signer::address_of(&vasp),
0, /* new_max_inflow */
0, /* new_max_outflow */
150, /* new_max_holding_balance */
10000, /* new_time_period */
)
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
fun update_limits_definition_twice(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
AccountLimits::update_limits_definition<XUS>(
&tc,
Signer::address_of(&vasp),
100, /* new_max_inflow */
200, /* new_max_outflow */
150, /* new_max_holding_balance */
10000, /* new_time_period */
);
AccountLimits::update_limits_definition<XUS>(
&tc,
Signer::address_of(&vasp),
0, /* new_max_inflow */
0, /* new_max_outflow */
150, /* new_max_holding_balance */
10000, /* new_time_period */
)
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
#[expected_failure(abort_code = 258)]
fun update_limits_definition_non_tc(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
AccountLimits::update_limits_definition<XUS>(
&dr,
Signer::address_of(&vasp),
100, /* new_max_inflow */
200, /* new_max_outflow */
150, /* new_max_holding_balance */
10000, /* new_time_period */
)
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
#[expected_failure(abort_code = 5)]
fun update_limits_definition_non_exsistent(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
AccountLimits::update_limits_definition<XUS>(
&tc,
@0x42, // non-exsistent
100, /* new_max_inflow */
200, /* new_max_outflow */
150, /* new_max_holding_balance */
10000, /* new_time_period */
)
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
fun update_window_info(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
let vasp_addr = Signer::address_of(&vasp);
AccountLimits::update_window_info<XUS>(
&tc,
vasp_addr,
120,
vasp_addr,
);
AccountLimits::update_window_info<XUS>(
&tc,
vasp_addr,
0,
vasp_addr,
);
AccountLimits::update_window_info<XUS>(
&tc,
vasp_addr,
120,
vasp_addr,
);
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
#[expected_failure(abort_code = 258)]
fun update_window_info_non_tc(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
let vasp_addr = Signer::address_of(&vasp);
AccountLimits::update_window_info<XUS>(
&dr,
vasp_addr,
120,
vasp_addr,
);
}

#[test(dr = @DiemRoot, tc = @TreasuryCompliance, vasp = @0x2)]
fun has_limits_published(dr: signer, tc: signer, vasp: signer) {
setup(&dr, &tc, &vasp);
assert!(AccountLimits::has_limits_published<XUS>(Signer::address_of(&vasp)), 1);
assert!(!AccountLimits::has_limits_published<XUS>(@0x42 /* non-exsistent */), 3);
}
}

This file was deleted.

Loading

0 comments on commit 8aad1a1

Please sign in to comment.