From ca681e62333dd66e746d03dc21801ce5cbf28ec8 Mon Sep 17 00:00:00 2001 From: martinfridrich Date: Thu, 19 Oct 2023 12:51:51 +0200 Subject: [PATCH] asset-registry: added test to check name and symblo length --- pallets/asset-registry/src/tests/register.rs | 60 +++++++++++++++++ pallets/asset-registry/src/tests/update.rs | 71 +++++++++++++++++++- 2 files changed, 129 insertions(+), 2 deletions(-) diff --git a/pallets/asset-registry/src/tests/register.rs b/pallets/asset-registry/src/tests/register.rs index d2f271d55..d02629a2c 100644 --- a/pallets/asset-registry/src/tests/register.rs +++ b/pallets/asset-registry/src/tests/register.rs @@ -501,3 +501,63 @@ fn register_external_asset_should_not_work_when_user_cant_pay_storage_fees() { ); }); } + +#[test] +fn register_should_fail_when_name_is_too_long() { + ExtBuilder::default().build().execute_with(|| { + let asset_id = 1; + let name = vec![97u8; ::StringLimit::get() as usize + 1]; + let symbol = b"TKN".to_vec(); + let decimals = 12; + let xcm_rate_limit = 1_000; + let ed = 10_000; + let is_sufficient = true; + + let key = Junction::from(BoundedVec::try_from(asset_id.encode()).unwrap()); + let asset_location = AssetLocation(MultiLocation::new(0, X2(Parachain(200), key))); + + //Act + assert_noop!(Registry::register( + RuntimeOrigin::root(), + Some(asset_id), + Some(name.clone()), + AssetType::Token, + Some(ed), + Some(symbol.clone()), + Some(decimals), + Some(asset_location.clone()), + Some(xcm_rate_limit), + is_sufficient + ), Error::::TooLong); + }); +} + +#[test] +fn register_should_fail_when_symbol_is_too_long() { + ExtBuilder::default().build().execute_with(|| { + let asset_id = 1; + let name = b"Test asset".to_vec(); + let symbol = vec![97u8; ::StringLimit::get() as usize + 1]; + let decimals = 12; + let xcm_rate_limit = 1_000; + let ed = 10_000; + let is_sufficient = true; + + let key = Junction::from(BoundedVec::try_from(asset_id.encode()).unwrap()); + let asset_location = AssetLocation(MultiLocation::new(0, X2(Parachain(200), key))); + + //Act + assert_noop!(Registry::register( + RuntimeOrigin::root(), + Some(asset_id), + Some(name.clone()), + AssetType::Token, + Some(ed), + Some(symbol.clone()), + Some(decimals), + Some(asset_location.clone()), + Some(xcm_rate_limit), + is_sufficient + ), Error::::TooLong); + }); +} diff --git a/pallets/asset-registry/src/tests/update.rs b/pallets/asset-registry/src/tests/update.rs index 0a07c51ab..7e726e1ba 100644 --- a/pallets/asset-registry/src/tests/update.rs +++ b/pallets/asset-registry/src/tests/update.rs @@ -243,7 +243,7 @@ fn update_origin_should_set_decimals_if_its_none() { let details_0 = Registry::assets(asset_id).unwrap(); - //NOTE: update origin is ste to ensure_signed + //NOTE: update origin is set to ensure_signed //Act assert_ok!(Registry::update( RuntimeOrigin::signed(ALICE), @@ -344,7 +344,6 @@ fn create_origin_should_always_set_decimals() { let details_0 = Registry::assets(asset_id).unwrap(); - //NOTE: update origin is ste to ensure_signed //Act assert_ok!(Registry::update( RuntimeOrigin::root(), @@ -615,3 +614,71 @@ fn update_should_not_work_when_name_is_same_as_old() { ); }); } + +#[test] +fn update_should_fail_when_name_is_too_long() { + ExtBuilder::default() + .with_assets(vec![ + (Some(1), Some(b"Tkn1".to_vec()), UNIT, None, None, None, true), + (Some(2), Some(b"Tkn2".to_vec()), UNIT, None, None, None, true), + (Some(3), Some(b"Tkn3".to_vec()), UNIT, None, None, None, true), + ]) + .build() + .execute_with(|| { + let asset_id = 2; + let name = vec![97u8; ::StringLimit::get() as usize + 1]; + let ed = 10_000 * UNIT; + let xcm_rate_limit = 463; + let symbol = b"nTkn2".to_vec(); + let decimals = 23; + let is_sufficient = false; + + //Act + assert_noop!(Registry::update( + RuntimeOrigin::root(), + asset_id, + Some(name.clone()), + Some(AssetType::External), + Some(ed), + Some(xcm_rate_limit), + Some(is_sufficient), + Some(symbol.clone()), + Some(decimals), + None + ), Error::::TooLong); + }); +} + +#[test] +fn update_should_fail_when_symbolis_too_long() { + ExtBuilder::default() + .with_assets(vec![ + (Some(1), Some(b"Tkn1".to_vec()), UNIT, None, None, None, true), + (Some(2), Some(b"Tkn2".to_vec()), UNIT, None, None, None, true), + (Some(3), Some(b"Tkn3".to_vec()), UNIT, None, None, None, true), + ]) + .build() + .execute_with(|| { + let asset_id = 2; + let name = b"New Token Name".to_vec(); + let ed = 10_000 * UNIT; + let xcm_rate_limit = 463; + let symbol = vec![97u8; ::StringLimit::get() as usize + 1]; + let decimals = 23; + let is_sufficient = false; + + //Act + assert_noop!(Registry::update( + RuntimeOrigin::root(), + asset_id, + Some(name.clone()), + Some(AssetType::External), + Some(ed), + Some(xcm_rate_limit), + Some(is_sufficient), + Some(symbol.clone()), + Some(decimals), + None + ), Error::::TooLong); + }); +}